Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import gradio as gr
|
3 |
+
import pymssql
|
4 |
+
import csv
|
5 |
+
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
from sklearn.metrics.pairwise import cosine_similarity #์ ์ฌ๋ ๊ฒ์ ๊ธฐ๋ฅ์ ์ฌ์ฉํ๊ธฐ ์ํจ
|
8 |
+
import glob
|
9 |
+
|
10 |
+
df = pd.read_csv('./data/real/_UnivSample_20231113.csv')
|
11 |
+
|
12 |
+
encoder = SentenceTransformer('jhgan/ko-sroberta-multitask') # ์ ํ ๋ชจ๋ธ (ko๊ฐ ๋ถ์๊ฑด ํ๊ตญ์ด ์ง์)
|
13 |
+
|
14 |
+
df['embedding'] = pd.Series([[]] * len(df)) # dummy
|
15 |
+
df['embedding'] = df['query'].map(lambda x: list(encoder.encode(x)))
|
16 |
+
|
17 |
+
# ์ฑ๋ด์ ๋ต๋ณ์ ์ฒ๋ฆฌํ๋ ํจ์
|
18 |
+
def respond(message, chat_history):
|
19 |
+
|
20 |
+
embedding = encoder.encode(message)
|
21 |
+
|
22 |
+
df['distance'] = df['embedding'].map(lambda x: cosine_similarity([embedding], [x]).squeeze())
|
23 |
+
answer = df.loc[df['distance'].idxmax()]
|
24 |
+
|
25 |
+
chat_history.append([message, answer['answer']])
|
26 |
+
|
27 |
+
historySave(message=message, answer=str(answer['answer']).replace("'",""))
|
28 |
+
# historySave(message=message, answer="")
|
29 |
+
|
30 |
+
return "", chat_history
|
31 |
+
|
32 |
+
# def historySave(message, answer):
|
33 |
+
|
34 |
+
# conn = pymssql.connect(host=r"(local)", database='Chatbot_Manage', charset='utf8')
|
35 |
+
# conn.autocommit(True) # ์คํ ์ปค๋ฐ ํ์ฑํ
|
36 |
+
# # Connection ์ผ๋ก๋ถํฐ Cursor ์์ฑ
|
37 |
+
# cursor = conn.cursor()
|
38 |
+
|
39 |
+
# SystemType = "SentenceModel"
|
40 |
+
|
41 |
+
# # SQL๋ฌธ ์คํ'
|
42 |
+
# _sql = "EXEC ChatHistory_InsUpd '" + SystemType + "','" + message + "', '" + answer + "'"
|
43 |
+
# cursor.execute(_sql)
|
44 |
+
|
45 |
+
# conn.close() ## ์ฐ๊ฒฐ ๋๊ธฐ
|
46 |
+
|
47 |
+
# ์ฑ๋ด ์ค๋ช
|
48 |
+
title = """
|
49 |
+
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
50 |
+
<div>
|
51 |
+
<h1>Woody's Chatbot V2</h1>
|
52 |
+
</div>
|
53 |
+
<p style="margin-bottom: 10px; font-size: 94%">
|
54 |
+
sentence_transformers๋ฅผ ์ด์ฉํ Chatbot
|
55 |
+
</p>
|
56 |
+
</div>
|
57 |
+
"""
|
58 |
+
|
59 |
+
# ๊พธ๋ฏธ๊ธฐ
|
60 |
+
css="""
|
61 |
+
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
62 |
+
"""
|
63 |
+
with gr.Blocks(css=css) as UnivChatbot:
|
64 |
+
with gr.Column(elem_id="col-container"):
|
65 |
+
gr.HTML(title)
|
66 |
+
|
67 |
+
chatbot = gr.Chatbot(label="๋ํ ์ฑ๋ด์์คํ
(LLM Sentence)", elem_id="chatbot") # ์๋จ ์ข์ธก
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column(scale=9):
|
71 |
+
msg = gr.Textbox(label="์
๋ ฅ", placeholder="๊ถ๊ธํ์ ๋ด์ญ์ ์
๋ ฅํ์ฌ ์ฃผ์ธ์.", elem_id="InputQuery", show_label=False, container=False)
|
72 |
+
|
73 |
+
with gr.Row():
|
74 |
+
with gr.Column(scale=1):
|
75 |
+
submit = gr.Button("์ ์ก", variant="primary")
|
76 |
+
with gr.Column(scale=1):
|
77 |
+
clear = gr.Button("์ด๊ธฐํ", variant="stop")
|
78 |
+
|
79 |
+
|
80 |
+
# ์ฌ์ฉ์์ ์
๋ ฅ์ ์ ์ถ(submit)ํ๋ฉด respond ํจ์๊ฐ ํธ์ถ.
|
81 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
82 |
+
|
83 |
+
submit.click(respond, [msg, chatbot], [msg, chatbot])
|
84 |
+
|
85 |
+
# '์ด๊ธฐํ' ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฑํ
๊ธฐ๋ก์ ์ด๊ธฐํ.
|
86 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
87 |
+
|
88 |
+
UnivChatbot.launch()
|
89 |
+
|