ranbac commited on
Commit
d6a73c4
·
verified ·
1 Parent(s): af99142

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -33
app.py CHANGED
@@ -1,40 +1,46 @@
1
  import gradio as gr
2
- from transformers import MarianMTModel, MarianTokenizer
 
3
 
4
- MODEL_NAME = "Helsinki-NLP/opus-mt-zh-vi"
 
 
5
 
6
- print("Đang tải model...")
7
- tokenizer = MarianTokenizer.from_pretrained(MODEL_NAME)
8
- model = MarianMTModel.from_pretrained(MODEL_NAME)
9
- print("Tải model thành công!")
 
 
 
 
 
10
 
11
- def translate(text: str) -> str:
12
- if not text.strip():
13
- return ""
14
- inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True, max_length=512)
15
- translated = model.generate(**inputs)
16
- result = tokenizer.decode(translated[0], skip_special_tokens=True)
17
- return result
 
 
 
 
 
 
 
 
 
 
18
 
19
- demo = gr.Interface(
20
- fn=translate,
21
- inputs=gr.Textbox(
22
- lines=5,
23
- placeholder="Nhập văn bản tiếng Trung...",
24
- label="📝 Văn bản tiếng Trung (中文)"
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
- demo.launch()
 
 
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()