Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
|
| 10 |
torch_dtype=torch.float16,
|
| 11 |
device_map="auto"
|
| 12 |
)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
-
#
|
| 20 |
-
if
|
| 21 |
-
response = response
|
| 22 |
return response
|
| 23 |
-
|
| 24 |
-
# Tạo giao diện + API endpoint
|
| 25 |
-
demo = gr.Interface(
|
| 26 |
fn=chat,
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
+
# Load model
|
| 5 |
+
model_name = "HOANGHUAN/dulich"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
model_name,
|
| 9 |
torch_dtype=torch.float16,
|
| 10 |
device_map="auto"
|
| 11 |
)
|
| 12 |
+
def chat(message, history):
|
| 13 |
+
messages = [
|
| 14 |
+
{"role": "system", "content": "Bạn là Vistours Assistant."},
|
| 15 |
+
{"role": "user", "content": message}
|
| 16 |
+
]
|
| 17 |
+
|
| 18 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False)
|
| 19 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 20 |
+
|
| 21 |
+
outputs = model.generate(
|
| 22 |
+
**inputs,
|
| 23 |
+
max_new_tokens=256,
|
| 24 |
+
temperature=0.7,
|
| 25 |
+
top_p=0.9,
|
| 26 |
+
do_sample=True
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 30 |
+
# Extract assistant response
|
| 31 |
+
if "assistant" in response:
|
| 32 |
+
response = response.split("assistant")[-1].strip()
|
| 33 |
return response
|
| 34 |
+
gr.ChatInterface(
|
|
|
|
|
|
|
| 35 |
fn=chat,
|
| 36 |
+
title="Vistours Chatbot",
|
| 37 |
+
description="AI Assistant cho công ty du lịch Vistours"
|
| 38 |
+
).launch()
|
|
|
|
|
|
|
|
|