Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 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. Mom AI: {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. Mom AI",
|
| 20 |
+
theme="default")
|
| 21 |
+
|
| 22 |
+
demo.launch()
|