Vanhwbt commited on
Commit
f0df364
·
1 Parent(s): 88ca24a
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -1,18 +1,26 @@
1
  import os
2
- from transformers import pipeline
3
  import gradio as gr
 
4
 
5
- # Gọi con Gemma ra làm việc
6
- # Lưu ý: Gemma-2b-it là bản đã được train để chat
7
- model_id = "google/gemma-2b-it"
8
- pipe = pipeline("text-generation", model=model_id, device_map="auto")
9
 
10
- def chat_with_gemma(message, history):
11
- # Format tin nhắn theo chuẩn của Gemma
12
- prompt = f"User: {message}\nAI:"
13
- outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7)
14
- return outputs[0]["generated_text"].split("AI:")[1].strip()
 
 
 
15
 
16
- # Tạo giao diện và API tự động
17
- demo = gr.ChatInterface(fn=chat_with_gemma, title="Gemma Backend của Thầy Oáp")
 
 
 
 
 
 
 
18
  demo.launch()
 
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 và 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()