Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,66 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
-
|
40 |
-
yield response
|
41 |
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
import json
|
2 |
import gradio as gr
|
3 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
4 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
5 |
|
6 |
+
file_path = "InstructData.jsonl"
|
|
|
|
|
|
|
7 |
|
8 |
+
# 1. ๋ํ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ (๋ชจ๋ human-gpt ์ ์ถ์ถ)
|
9 |
+
def load_conversations(file_path):
|
10 |
+
conversations = []
|
11 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
12 |
+
for line in f:
|
13 |
+
data = json.loads(line)
|
14 |
+
conv = data.get('conversations', [])
|
15 |
+
prev = None
|
16 |
+
for c in conv:
|
17 |
+
if c.get('from') == 'human':
|
18 |
+
prev = c['value']
|
19 |
+
elif c.get('from') == 'gpt' and prev:
|
20 |
+
conversations.append((prev, c['value']))
|
21 |
+
prev = None
|
22 |
+
return conversations
|
23 |
|
24 |
+
# 2. ํ๊ตญ์ด ๋ถ์ฉ์ด ์ค์ + TF-IDF ๋ฒกํฐ๋ผ์ด์ ์ค๋น
|
25 |
+
def build_vectorizer(questions):
|
26 |
+
korean_stopwords = ['๊ทธ๋ฆฌ๊ณ ', '๊ทธ๋ฌ๋', 'ํ์ง๋ง', '๋ํ', '์ด๋ฐ', '์ ๋ฐ', '๊ทธ๋ฐ', '์๋', '์๋', '๊ฒ', '์', '๋๋ฌธ์', 'ํด์']
|
27 |
+
vectorizer = TfidfVectorizer(stop_words=korean_stopwords)
|
28 |
+
tfidf_matrix = vectorizer.fit_transform(questions)
|
29 |
+
return vectorizer, tfidf_matrix
|
|
|
|
|
|
|
30 |
|
31 |
+
# 3. ์ ์ฌ๋ ๊ธฐ๋ฐ Top-k ์๋ต ์ฐพ๊ธฐ
|
32 |
+
def find_top_responses(user_input, conversations, vectorizer, tfidf_matrix, top_k=3):
|
33 |
+
user_vec = vectorizer.transform([user_input])
|
34 |
+
sims = cosine_similarity(user_vec, tfidf_matrix)[0]
|
35 |
+
top_indices = sims.argsort()[-top_k:][::-1]
|
36 |
+
results = ""
|
37 |
+
for i, idx in enumerate(top_indices, 1):
|
38 |
+
answer = conversations[idx][1]
|
39 |
+
score = sims[idx]
|
40 |
+
results += f"{i}. {answer} (์ ์ฌ๋: {score:.2f})\n\n"
|
41 |
+
return results.strip()
|
42 |
|
43 |
+
# ์ด๊ธฐํ
|
44 |
+
convs = load_conversations(file_path)
|
45 |
+
questions = [q for q, _ in convs]
|
46 |
+
vectorizer, tfidf_matrix = build_vectorizer(questions)
|
47 |
|
48 |
+
# Gradio ์ธํฐํ์ด์ค ํจ์
|
49 |
+
def chatbot_interface(user_input):
|
50 |
+
if user_input.strip().lower() == "์ข
๋ฃ":
|
51 |
+
return "ChatBot: ์๋
~ ๋ ๋๋ฌ์!"
|
52 |
+
return find_top_responses(user_input, convs, vectorizer, tfidf_matrix)
|
53 |
|
54 |
+
# Gradio UI ๊ตฌ์ฑ
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown("## ChatBot (TF-IDF ๊ธฐ๋ฐ Top-3 ๋ต๋ณ ์ถ์ฒ)")
|
57 |
+
with gr.Row():
|
58 |
+
with gr.Column():
|
59 |
+
input_box = gr.Textbox(label="์ง๋ฌธ์ ์
๋ ฅํด์ค!", placeholder="์: ์ฑ๋ด ์ด๋ป๊ฒ ๋ง๋ค์ด์?")
|
60 |
+
submit_btn = gr.Button("Toki์๊ฒ ๋ฌผ์ด๋ณด๊ธฐ ๐ง ")
|
61 |
+
with gr.Column():
|
62 |
+
output_box = gr.Textbox(label="Toki์ ์ถ์ฒ ๋ต๋ณ Top-3", lines=10)
|
63 |
|
64 |
+
submit_btn.click(fn=chatbot_interface, inputs=input_box, outputs=output_box)
|
|
|
65 |
|
66 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|