api.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask,request,jsonify,Blueprint,session,render_template,Response
|
2 |
+
import spacy
|
3 |
+
import numpy as np
|
4 |
+
from sentence_transformers import SentenceTransformer,util
|
5 |
+
from elasticsearch import Elasticsearch
|
6 |
+
#bp = Blueprint('bp', __name__)
|
7 |
+
|
8 |
+
app=Flask(__name__)
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
def similarity(model,vertical):
|
13 |
+
vertical_list = ["Mortgage","Real estate","Retail","Insurance","Human_Resource","Pharmaceutical"]
|
14 |
+
word_embeddings = model.encode(vertical_list, convert_to_tensor=True)
|
15 |
+
user_embedding = model.encode(vertical, convert_to_tensor=True)
|
16 |
+
cosine_similarities = util.pytorch_cos_sim(user_embedding, word_embeddings)
|
17 |
+
most_similar_word_index = np.argmax(cosine_similarities)
|
18 |
+
most_similar_word = vertical_list[most_similar_word_index]
|
19 |
+
return most_similar_word
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
def get_entities(text):
|
24 |
+
ner_model=spacy.load("spacy_ner_model/en_pipeline/en_pipeline-0.0.0")
|
25 |
+
sim_model=SentenceTransformer("sentence_transformer_model")
|
26 |
+
|
27 |
+
doc = ner_model(text)
|
28 |
+
|
29 |
+
city = None
|
30 |
+
vertical = None
|
31 |
+
zip_code = None
|
32 |
+
state = None
|
33 |
+
state_code = None
|
34 |
+
|
35 |
+
entity_variables = {
|
36 |
+
'CITY': 'city',
|
37 |
+
'VERTICAL': 'vertical',
|
38 |
+
'ZIPCODE': 'zip_code',
|
39 |
+
'STATE': 'state',
|
40 |
+
'STATE_CODE': 'state_code'
|
41 |
+
}
|
42 |
+
|
43 |
+
entity_values = {var: None for var in entity_variables.values()}
|
44 |
+
|
45 |
+
for ent in doc.ents:
|
46 |
+
label = ent.label_
|
47 |
+
if label in entity_variables:
|
48 |
+
var_name = entity_variables[label]
|
49 |
+
var_value = ent.text
|
50 |
+
if var_name == 'vertical':
|
51 |
+
if entity_values[var_name] is None or len(var_value) < len(entity_values[var_name]):
|
52 |
+
entity_values[var_name] = var_value
|
53 |
+
else:
|
54 |
+
entity_values[var_name] = var_value
|
55 |
+
|
56 |
+
|
57 |
+
city = entity_values['city']
|
58 |
+
if entity_values['vertical']:
|
59 |
+
vertical=similarity(sim_model,entity_values['vertical'])
|
60 |
+
else:
|
61 |
+
vertical = None
|
62 |
+
zip_code = entity_values['zip_code']
|
63 |
+
state = entity_values['state']
|
64 |
+
state_code = entity_values['state_code']
|
65 |
+
|
66 |
+
|
67 |
+
should_clauses = []
|
68 |
+
fields_with_values = []
|
69 |
+
fields = [
|
70 |
+
("city", city),
|
71 |
+
("vertical", vertical),
|
72 |
+
("zip_code", zip_code),
|
73 |
+
("state", state),
|
74 |
+
("state_code", state_code)
|
75 |
+
]
|
76 |
+
|
77 |
+
for field, value in fields:
|
78 |
+
if value is not None:
|
79 |
+
should_clause = {
|
80 |
+
"match": {
|
81 |
+
field: value
|
82 |
+
}
|
83 |
+
}
|
84 |
+
should_clauses.append(should_clause)
|
85 |
+
fields_with_values.append(field)
|
86 |
+
fields_with_values.append("name")
|
87 |
+
|
88 |
+
query = {
|
89 |
+
'query': {
|
90 |
+
"bool": {
|
91 |
+
"must": should_clauses
|
92 |
+
}
|
93 |
+
|
94 |
+
}
|
95 |
+
}
|
96 |
+
|
97 |
+
return query,fields_with_values
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
@app.route('/search',methods=["GET"])
|
102 |
+
def search():
|
103 |
+
text = request.args.get("text")
|
104 |
+
#text=request.get_json()["text"]
|
105 |
+
|
106 |
+
#http://127.0.0.1:5000/search?text=give%20me%20best%20loan%20agents%20in%20Chicago
|
107 |
+
query,fields_with_values=get_entities(text)
|
108 |
+
ELASTIC_PASSWORD = "tILF0omcl27kdZ2G7sFZ3SGf"
|
109 |
+
es = Elasticsearch(["https://elasticsearch.preprod.experience.com:443"], basic_auth=("elastic_app_user", ELASTIC_PASSWORD), verify_certs= True)
|
110 |
+
results = es.search(index='profiles_phase2', body=query, track_total_hits=True, source=fields_with_values)
|
111 |
+
l=[]
|
112 |
+
for hit in results['hits']['hits']:
|
113 |
+
source = hit['_source']
|
114 |
+
l.append(source)
|
115 |
+
print("hai")
|
116 |
+
return jsonify(l)
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
if __name__=="__main__":
|
122 |
+
with app.app_context():
|
123 |
+
#app.register_blueprint(bp)
|
124 |
+
app.run()
|