abcsnoob commited on
Commit
f1fd779
·
verified ·
1 Parent(s): 6d459df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -4
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(model=model, tokenizer=tokenizer, max_new_tokens=200, do_sample=True)
 
 
 
 
 
 
 
9
 
10
  def chat(user_input):
11
- res = pipe(user_input)[0]["generated_text"]
12
- return res[len(user_input):] # chỉ trả lời, không lặp lại câu hỏi
 
 
 
 
 
 
13
 
14
- gr.Interface(fn=chat, inputs="text", outputs="text", title="🤖 TinyLlama Chatbot").launch()
 
 
 
 
 
 
 
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()