Spaces:
Runtime error
Runtime error
update
Browse files
app.py
CHANGED
|
@@ -1,18 +1,26 @@
|
|
| 1 |
import os
|
| 2 |
-
from transformers import pipeline
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
model_id = "google/gemma-2b-it"
|
| 8 |
-
pipe = pipeline("text-generation", model=model_id, device_map="auto")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|