-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (52 loc) Β· 1.93 KB
/
app.py
File metadata and controls
67 lines (52 loc) Β· 1.93 KB
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
from flask import Flask
from flask import request
import pandas as pd
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import json
import sys
app = Flask(__name__)
@app.route('/')
def hello():
return "HelloWorld"
@app.route("/model",methods=['POST'])
def decision():
json_data = request.get_json()
text = json_data['userRequest']['utterance']
model = SentenceTransformer('jhgan/ko-sroberta-multitask')
# df = pd.read_csv('wellness_dataset.csv')
df = pd.read_csv('answer.csv')
df['embedding'] = df['embedding'].apply(json.loads)
embedding = model.encode(text)
#미리 μλ² λ©ν λ°μ΄ν°μ
μμ μ¬μ©μκ° μ
λ ₯ν λ¬Έμ₯μ μλ² λ©κ³Ό λΉκ΅λ₯Ό νμ¬ κ°μ₯ μ μ¬νκ²μ μ°Ύμ
df['distance'] = df['embedding'].map(lambda x: cosine_similarity([embedding], [x]).squeeze())
df.head()
answer = df.loc[df['distance'].idxmax()]
print('[μ§λ¬Έ]', text)
print('[ꡬλΆ]', answer['ꡬλΆ'])
print('[μ μ¬ν μ§λ¬Έ]', answer['μ μ '])
print('[μ μ¬λ]', answer['distance'])
print('\n[μ±λ΄ λ΅λ³]', answer['μ±λ΄'])
# μ¬μ©μ λ°νμ λ΅λ³μ μ μ¬λκ° 0.6 λ―Έλ§μ΄λ©΄ μμΈ μ²λ¦¬ λ΅λ³μ λ°νν©λλ€.
if(answer['distance'] < 0.6):
useranswer = "μ¦μμ μ‘°κΈ λ ꡬ체μ μΌλ‘ λ§μν΄μ£ΌμΈμ. (μμ: λν΅μ΄ μ¬νλ° μ΄λ λ³μμ κ°μΌν κΉμ?)"
else:
useranswer = str(answer['μ±λ΄'])
responseBody = {
"version": "2.0",
"template": {
"outputs": [
{
"simpleText": {
"text": useranswer
}
}
]
}
}
return responseBody
@app.route('/model', methods=['GET'])
def connect():
return "Backend Server Connect"
if __name__ == '__main__':
app.run(host='0.0.0.0' ,port = 5002, debug=True)