Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,21 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
|
|
|
| 5 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
|
| 6 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
demo = gr.Interface(fn=chatbot,
|
| 15 |
-
inputs=gr.Textbox(label="Bạn
|
| 16 |
-
outputs=gr.Textbox(label="
|
| 17 |
-
title="🐱 Dr. Ask
|
| 18 |
-
|
| 19 |
|
| 20 |
demo.launch()
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# Tải mô hình chỉ 1 lần duy nhất
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
|
| 7 |
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
|
| 8 |
|
| 9 |
+
# Hàm chat đơn giản
|
| 10 |
+
def chatbot(msg):
|
| 11 |
+
input_ids = tokenizer.encode(msg + tokenizer.eos_token, return_tensors='pt')
|
| 12 |
+
output_ids = model.generate(input_ids, max_length=100, pad_token_id=tokenizer.eos_token_id)
|
| 13 |
+
reply = tokenizer.decode(output_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 14 |
+
return f"🐱 Dr. Ask: {reply}"
|
| 15 |
|
| 16 |
demo = gr.Interface(fn=chatbot,
|
| 17 |
+
inputs=gr.Textbox(label="Bạn hỏi gì nè?"),
|
| 18 |
+
outputs=gr.Textbox(label="Dr. Ask trả lời"),
|
| 19 |
+
title="🐱 Dr. Ask AI",
|
| 20 |
+
theme="default")
|
| 21 |
|
| 22 |
demo.launch()
|