Spaces:
Runtime error
Runtime error
update
Browse files- app.py +33 -15
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,26 +1,44 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
model_id = "
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
pipe = pipeline(
|
| 11 |
-
"text-generation",
|
| 12 |
-
model=model_id,
|
| 13 |
-
torch_dtype=
|
| 14 |
-
device_map="auto"
|
| 15 |
)
|
| 16 |
|
| 17 |
def chat(message, history):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|