Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
),
|
| 26 |
-
outputs=gr.Textbox(
|
| 27 |
-
lines=5,
|
| 28 |
-
label="🇻🇳 Bản dịch tiếng Việt"
|
| 29 |
-
),
|
| 30 |
-
title="🈶 Dịch Trung → Việt",
|
| 31 |
-
description="Sử dụng model Helsinki-NLP/opus-mt-zh-vi để dịch văn bản từ tiếng Trung sang tiếng Việt.",
|
| 32 |
-
examples=[
|
| 33 |
-
["你好,世界!"],
|
| 34 |
-
["今天天气很好。"],
|
| 35 |
-
["我想学习越南语。"],
|
| 36 |
-
],
|
| 37 |
-
flagging_mode="never"
|
| 38 |
)
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Khởi tạo Groq client
|
| 6 |
+
# Hệ thống sẽ tự động lấy GROQ_API_KEY từ mục Secrets của Hugging Face
|
| 7 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 8 |
|
| 9 |
+
def chat_with_qwen(message, history):
|
| 10 |
+
# Xây dựng lịch sử trò chuyện theo định dạng của Groq/OpenAI
|
| 11 |
+
messages = []
|
| 12 |
+
for user_msg, assistant_msg in history:
|
| 13 |
+
messages.append({"role": "user", "content": user_msg})
|
| 14 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 15 |
+
|
| 16 |
+
# Thêm tin nhắn hiện tại của người dùng
|
| 17 |
+
messages.append({"role": "user", "content": message})
|
| 18 |
|
| 19 |
+
try:
|
| 20 |
+
# Gọi API với model qwen/qwen3-32b
|
| 21 |
+
completion = client.chat.completions.create(
|
| 22 |
+
model="qwen/qwen3-32b",
|
| 23 |
+
messages=messages,
|
| 24 |
+
stream=True
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
response = ""
|
| 28 |
+
for chunk in completion:
|
| 29 |
+
# Lấy từng đoạn text trả về để tạo hiệu ứng gõ chữ (streaming)
|
| 30 |
+
if chunk.choices[0].delta.content:
|
| 31 |
+
response += chunk.choices[0].delta.content
|
| 32 |
+
yield response
|
| 33 |
+
|
| 34 |
+
except Exception as e:
|
| 35 |
+
yield f"Đã xảy ra lỗi: {str(e)}"
|
| 36 |
|
| 37 |
+
# Tạo giao diện ChatInterface của Gradio
|
| 38 |
+
demo = gr.ChatInterface(
|
| 39 |
+
fn=chat_with_qwen,
|
| 40 |
+
title="Qwen3 32B Fast Chat (Powered by Groq)",
|
| 41 |
+
description="Chatbot tốc độ cao sử dụng model qwen/qwen3-32b qua API của Groq.",
|
| 42 |
+
fill_height=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|