File size: 1,745 Bytes
46018e0
d6a73c4
 
46018e0
5722ab9
d6a73c4
4750d2a
5722ab9
 
 
 
 
 
ed1efd9
5722ab9
 
 
 
d6a73c4
5722ab9
c14fd63
5722ab9
d6a73c4
5722ab9
d6a73c4
5722ab9
fbde2f0
5722ab9
 
 
 
 
 
 
 
 
 
 
 
46018e0
d6a73c4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import gradio as gr
from groq import Groq
import os

# Khởi tạo client Groq (lấy API Key từ Secret của HF Space)
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))

def translate_and_transcribe(text):
    if not text.strip():
        return "Vui lòng nhập văn bản."
        
    prompt = f"""Bạn là một chuyên gia ngôn ngữ tiếng Trung và tiếng Việt. 
Hãy xử lý đoạn văn bản tiếng Trung sau: "{text}"

Yêu cầu xuất ra kết quả theo đúng định dạng sau:
1. Bản dịch tiếng Việt: [Dịch sát nghĩa, văn phong tự nhiên]
2. Phiên âm Pinyin: [Cung cấp pinyin cho toàn bộ câu]
"""
    try:
        response = client.chat.completions.create(
            model="moonshotai/kimi-k2-instruct-0905", # Bạn có thể đổi sang các model khác của Groq nếu muốn
            messages=[{"role": "user", "content": prompt}],
        )
        return response.choices[0].message.content
    except Exception as e:
        return f"Có lỗi xảy ra: {str(e)}"

# Xây dựng giao diện web
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("## 🇨🇳🇻🇳 Công Cụ Dịch Trung - Việt & Phiên Âm (Tốc độ siêu tốc với Groq)")
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(lines=5, label="Nhập văn bản tiếng Trung", placeholder="Ví dụ: 欢迎来到越南!")
            submit_btn = gr.Button("Dịch & Lấy Phiên Âm", variant="primary")
        with gr.Column():
            output_text = gr.Textbox(lines=10, label="Kết quả")
            
    submit_btn.click(fn=translate_and_transcribe, inputs=input_text, outputs=output_text)

if __name__ == "__main__":
    demo.launch()