Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,10 +5,29 @@ model_id = "TinyLlama/TinyLlama-1.1B-Chat-v0.3"
|
|
5 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
6 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
7 |
|
8 |
-
pipe = TextGenerationPipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
def chat(user_input):
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
gr.Interface(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
6 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
7 |
|
8 |
+
pipe = TextGenerationPipeline(
|
9 |
+
model=model,
|
10 |
+
tokenizer=tokenizer,
|
11 |
+
max_new_tokens=200,
|
12 |
+
do_sample=True,
|
13 |
+
temperature=0.7,
|
14 |
+
top_p=0.95
|
15 |
+
)
|
16 |
|
17 |
def chat(user_input):
|
18 |
+
prompt = f"<|user|>\n{user_input}\n<|assistant|>\n"
|
19 |
+
res = pipe(prompt)[0]["generated_text"]
|
20 |
+
|
21 |
+
# Cắt bỏ phần prompt để chỉ lấy phần trả lời của assistant
|
22 |
+
if "<|assistant|>" in res:
|
23 |
+
return res.split("<|assistant|>")[-1].strip()
|
24 |
+
else:
|
25 |
+
return res.strip()
|
26 |
|
27 |
+
gr.Interface(
|
28 |
+
fn=chat,
|
29 |
+
inputs="text",
|
30 |
+
outputs="text",
|
31 |
+
title="🤖 TinyLlama Chatbot",
|
32 |
+
description="Một chatbot nhẹ, chạy mô hình TinyLlama 1.1B"
|
33 |
+
).launch()
|