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()