Vanhwbt commited on
Commit
5557ff6
·
1 Parent(s): f0df364
Files changed (2) hide show
  1. app.py +33 -15
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,26 +1,44 @@
1
  import os
2
  import gradio as gr
3
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
 
4
 
5
- # Model này cực nhẹ, chạy trên CPU HF Space rất mượt
6
- model_id = "Qwen/Qwen2.5-1.5B-Instruct"
7
 
8
- # Tải tokenizer model
9
- tokenizer = AutoTokenizer.from_pretrained(model_id)
10
  pipe = pipeline(
11
- "text-generation",
12
- model=model_id,
13
- torch_dtype="auto",
14
- device_map="auto"
15
  )
16
 
17
  def chat(message, history):
18
- messages = [{"role": "user", "content": message}]
19
- # Format chuẩn cho Qwen
20
- prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
 
 
 
21
 
22
- outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
23
- return outputs[0]["generated_text"].split("<|im_start|>assistant\n")[-1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- demo = gr.ChatInterface(fn=chat, title="Gemma thì chậm, Qwen thì đậm chất chơi!")
26
  demo.launch()
 
1
  import os
2
  import gradio as gr
3
+ import torch
4
+ from transformers import pipeline
5
 
6
+ # "Chiến thần" siêu nhẹ của nhà Meta
7
+ model_id = "meta-llama/Llama-3.2-1B-Instruct"
8
 
9
+ # Khởi tạo pipeline chat
10
+ # Lưu ý: Llama 3.2 cần transformers bản mới nhất
11
  pipe = pipeline(
12
+ "text-generation",
13
+ model=model_id,
14
+ torch_dtype=torch.bfloat16, # Tối ưu cho CPU/GPU đời mới
15
+ device_map="auto",
16
  )
17
 
18
  def chat(message, history):
19
+ # Tạo cấu trúc hội thoại đúng chuẩn Llama 3
20
+ messages = []
21
+ for h in history:
22
+ messages.append({"role": "user", "content": h[0]})
23
+ messages.append({"role": "assistant", "content": h[1]})
24
+ messages.append({"role": "user", "content": message})
25
 
26
+ # Gọi Llama trả lời
27
+ outputs = pipe(
28
+ messages,
29
+ max_new_tokens=256,
30
+ do_sample=True,
31
+ temperature=0.7,
32
+ top_p=0.9,
33
+ )
34
+
35
+ return outputs[0]["generated_text"][-1]["content"]
36
+
37
+ # Tạo giao diện API
38
+ demo = gr.ChatInterface(
39
+ fn=chat,
40
+ title="Llama-3.2-1B: Nhỏ mà có võ!",
41
+ description="Backend siêu tốc cho Website của bạn."
42
+ )
43
 
 
44
  demo.launch()
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- transformers
2
  torch
3
  accelerate
4
  gradio
 
1
+ transformers>=4.45.0
2
  torch
3
  accelerate
4
  gradio