Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,47 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
|
|
3 |
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
+
# مدل مورد نظر: میتونی اینو با هر مدل دیگه مثل HooshvareLab/gpt2-fa عوض کنی
|
6 |
+
model_name = "microsoft/DialoGPT-medium"
|
7 |
|
8 |
+
# بارگذاری مدل و توکنایزر
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
11 |
|
12 |
+
# تاریخچه چت را نگه میداریم
|
13 |
+
chat_history_ids = None
|
14 |
+
|
15 |
+
def chat_with_bot(user_input, history=[]):
|
16 |
+
global chat_history_ids
|
17 |
+
|
18 |
+
# توکنایز ورودی
|
19 |
+
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
20 |
+
|
21 |
+
# ترکیب با تاریخچه قبلی (context-aware)
|
22 |
+
bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1) if chat_history_ids is not None else new_input_ids
|
23 |
+
|
24 |
+
# تولید پاسخ
|
25 |
+
chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
26 |
+
|
27 |
+
# گرفتن پاسخ آخر فقط
|
28 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
29 |
+
|
30 |
+
# به تاریخچه اضافه کن
|
31 |
+
history.append((user_input, response))
|
32 |
+
return history, history
|
33 |
+
|
34 |
+
# رابط Gradio
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
gr.Markdown("## 🤖 Chat with DialoGPT")
|
37 |
+
|
38 |
+
chatbot = gr.Chatbot()
|
39 |
+
msg = gr.Textbox(label="Type your message")
|
40 |
+
clear = gr.Button("Clear")
|
41 |
+
|
42 |
+
state = gr.State([])
|
43 |
+
|
44 |
+
msg.submit(chat_with_bot, [msg, state], [chatbot, state])
|
45 |
+
clear.click(lambda: ([], []), None, [chatbot, state])
|
46 |
+
|
47 |
+
demo.launch()
|