-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
executable file
·186 lines (154 loc) · 5.34 KB
/
app.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
import tensorflow_hub as hub
import sys
import io
import pytesseract
from PIL import Image
import requests
from flask import Flask, request, render_template, redirect, url_for, session
import tensorflow as tf
import flask
import json
# Connecting to ElasticSearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
if es.ping():
print('Connected to ES!')
else:
print('Could not connect!')
sys.exit()
# Loading the Universal Encoder
embed = hub.load('universal_encoder')
def make_vector(query):
embeddings = embed([query])
vector = []
for i in embeddings[0]:
vector.append(float(i))
return vector
# Function to Normalize the scores of each document
def search(query):
def norm_list(lis):
scores = [x[0] for x in lis]
try:
ma = max(scores)
mi = min(scores)
except:
ma=mi=0
for i in range(len(lis)):
lis[i][0] = (lis[i][0] - mi)/(ma - mi + 0.0001)
return lis
request={
'query':{ 'match':{"question":query } }
}
res= es.search(index='ie-4',body=request)
l1 = []
for hit in res['hits']['hits']:
l1.append([hit['_score'] , hit['_id']])
# Using Cosine Similarity to calculate scores of each document
query_vector = make_vector(query)
request ={
"query" : {
"script_score" : {
"query" : {
"match_all": {}
},
"script" : {
"source": "cosineSimilarity(params.query_vector, 'total_vectors') + 1.0",
"params": {"query_vector": query_vector}
}
}
}
}
res= es.search(index='ie-4',body=request)
l2 = []
for hit in res['hits']['hits']:
l2.append([hit['_score'] , hit['_id']])
l1 = norm_list(l1)
l2 = norm_list(l2)
# Calculating the weighted average score for Text and Semantic Search
temp_doc = {}
for i in l1:
temp_doc[i[1]] = i[0]*2
for i in l2:
temp_doc[i[1]] = temp_doc.get(i[1] , 0) + i[0]*5
inverse_temp_doc = [(i[1] , i[0]) for i in temp_doc.items()]
inverse_temp_doc = sorted(inverse_temp_doc , reverse = True)
return inverse_temp_doc[:10]
# Getting the search results.
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
#Home Page
@app.route('/')
def index():
return flask.render_template('index.html')
#Image Search
@app.route('/image_search',methods=['GET'])
def image_search():
return flask.render_template('image_search.html')
#Search Results
@app.route('/return_searches', methods=['POST'])
def return_searches():
result_sup = []
for i in search(request.form.to_dict()['query']):
result = es.search(index="ie-4",body={"query": {
"terms": {
"_id": ['{}'.format(i[1])]
}
}})
for x in result['hits']['hits']:
question = x['_source']['question']
details = x['_source']['details']
answer = x['_source']['answers']
upvotes =x['_source']['upvotes']
tags = x['_source']['tags']
result_sup.append([question,details,answer,upvotes,tags])
return flask.render_template('search.html',result=result_sup)
#Converting image into textual data
@app.route('/scanner', methods=['POST'])
def scan_file():
image_data = request.files['file'].read()
query = pytesseract.image_to_string(Image.open(io.BytesIO(image_data)))
print(query)
result_sup = []
for i in search(query):
result = es.search(index="ie-4",body={"query": {
"terms": {
"_id": ['{}'.format(i[1])]
}
}})
for x in result['hits']['hits']:
question = x['_source']['question']
details = x['_source']['details']
answer = x['_source']['answers']
upvotes =x['_source']['upvotes']
tags = x['_source']['tags']
result_sup.append([question,details,answer,upvotes,tags])
return flask.render_template('search.html',result=result_sup)
#Testing the image to text conversion
# @app.route('/result')
# def result():
# if "data" in session:
# data = session['data']
# return render_template(
# "result.html",
# title="Result",
# time=data["time"],
# text=data["text"],
# words=len(data["text"].split(" "))
# )
# else:
# return "Wrong request method."
#Autocomplete the search query
@app.route('/pipe', methods=["GET", "POST"])
def pipe():
data = request.form.get("data")
payload = {}
headers= {}
url = "http://127.0.0.1:4000/autocomplete?query="+str(data)
print(url)
response = requests.request("GET", url, headers=headers, data = payload)
return response.json()
if __name__ == '__main__':
#pytesseract.pytesseract.tesseract_cmd = r'D:\Pytesseract\tesseract'
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\\Tesseract-OCR\\tesseract.exe'
app.run( port=8080)