This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.py
138 lines (100 loc) · 3.71 KB
/
api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from flask import Flask, render_template, request
import xml.dom.minidom as minidom
from flask_cors import CORS
import requests, json
import sys
import io
import summarize as anthony
import wikipedia_processing as komila
import wolfpack as aaron
from PIL import Image
import re
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
app = Flask(__name__)
CORS(app)
# CONSTANTS
animal = 'a'
person = 'h'
planet = 'p'
cities = 'c'
max_query_lengh = 100
with open("configs.json") as conf:
config = json.load(conf)
wolfram_app_id = config["wolfram_id"]
azure_cv_key = config["cv_id"]
#print(azure_cv_key)
@app.route("/")
def main():
return render_template("index.html")
def string_from_image(url_input):
#print("here")
predictor = CustomVisionPredictionClient(config["cv_id"], endpoint="https://eastus.api.cognitive.microsoft.com/")
results = predictor.classify_image_url(config["project_id"], "version0.2", url_input)
prediction = results.predictions[0]
print(prediction)
return prediction.tag_name.replace(' ', '+')
def categorize_string(s):
url = aaron.build_request_url(s, ["Result"])
#print(url)
req = requests.get(url)
#print(req.text)
categories = {"Species" : animal, "Planet" : planet, "City" : 'c', "Person" : 'p'}
dt = minidom.parseString(req.text)
collection = dt.documentElement
assumptions = collection.getElementsByTagName("assumptions")
if not assumptions:
return person
assumption = assumptions[0].getElementsByTagName("assumption")[0]
values = assumption.getElementsByTagName("value")
for v in values:
name = v.getAttribute("name")
#print(name)
if name in categories:
return categories[name]
if "::" in name:
#print(name)
return person
return None
@app.route("/search_query", methods=["POST"])
def post_search_query():
input_string = "https://upload.wikimedia.org/wikipedia/commons/7/71/2010-kodiak-bear-1.jpg"
# check if string is url
if re.search("(?:http\:|https\:)?\/\/.*\.(?:png|jpg)", input_string):
input_string = string_from_image(input_string)
# validate input
if not input_string or len(input_string) > max_query_lengh:
return(json.encoder({"success" : False, "message" : "query length too long"}))
# get category
category = categorize_string(input_string)
print(category)
# get wiki data
wiki_data = komila.create_maps(input_string)
if category == animal:
wolfram_data = aaron.process_animal(input_string)
summary = anthony.summarize_animals(wiki_data, wolfram_data)
elif category == person:
wolfram_data = aaron.process_person(input_string)
summary = anthony.summarize_person(wiki_data, wolfram_data)
elif category == planet:
wolfram_data = aaron.process_planet(input_string)
summary = anthony.summarize_planets(wiki_data, wolfram_data)
elif category == cities:
wolfram_data = aaron.process_cities(input_string)
summary = anthony.summarize_cities(wiki_data, wolfram_data)
else:
return json.dumps({"success" : False, "message" : "could not find category"})
if not summary["success"]:
return json.dumps({"success" : False, "message" : "failure in summarize"} )
del summary["success"]
return json.dumps(summary)
@app.route("/sample", methods=["POST"])
def get_sample_query():
# For testing
#{"query" : "user_search_term"}
sample = request.json["query"]
with open("scripts/data/mock_summary_zebra.json", 'r', encoding='UTF8') as f:
sample = json.load(f.read())
return sample
post_search_query()
if __name__ == "__main__":
app.run()