Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
from huggingface_hub import InferenceClient | |
from deep_translator import GoogleTranslator | |
hf_token = os.getenv("HF_AUTH_TOKEN") | |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=hf_token) | |
translator_vi2en = GoogleTranslator(source='vi', target='en') | |
translator_en2vi = GoogleTranslator(source='en', target='vi') | |
def respond( | |
message, | |
history: list[tuple[str, str]], | |
temperature=0.5, | |
top_p=0.9, | |
): | |
history = [] | |
if len(message) > 500: | |
return "⚠️ Câu hỏi quá dài! Vui lòng rút gọn dưới 500 ký tự." | |
if message.count("?") > 10 or message.count("!") > 10: | |
return "⚠️ Tin nhắn có quá nhiều dấu hỏi hoặc dấu chấm than. Vui lòng chỉnh sửa lại." | |
try: | |
message_en = translator_vi2en.translate(message) | |
except Exception: | |
return "⚠️ Không thể dịch câu hỏi sang tiếng Anh." | |
prompt = ( | |
"You are a helpful, professional AI assistant who communicates fluently in Vietnamese.\n" | |
"If the user greets, reply briefly. If the user asks, answer accurately.\n\n" | |
) | |
for user_msg, bot_msg in history: | |
try: | |
user_msg_en = translator_vi2en.translate(user_msg) | |
bot_msg_en = translator_vi2en.translate(bot_msg) | |
except Exception: | |
continue | |
prompt += f"User: {user_msg_en}\nAssistant: {bot_msg_en}\n" | |
prompt += f"User: {message_en}\nAssistant:" | |
resp = client.text_generation( | |
prompt, | |
max_new_tokens=128, | |
temperature=temperature, | |
top_p=top_p, | |
) | |
answer_en = resp.strip().split("Assistant:")[-1].strip() | |
try: | |
answer_vi = translator_en2vi.translate(answer_en) | |
except Exception: | |
answer_vi = "(Không thể dịch câu trả lời về tiếng Việt)" | |
return answer_vi | |
# ⚡ Quan trọng: bật api_open=True ngay lúc tạo | |
demo = gr.ChatInterface( | |
fn=respond, | |
title="🤖 Trợ lý AI Tiếng Việt (Translate-then-Predict)", | |
description="💬 Nhập tiếng Việt ➔ dịch tiếng Anh ➔ hỏi model ➔ dịch lại tiếng Việt.", | |
theme="soft", | |
api_open=True, # <- thêm dòng này để mở API mode | |
) | |
if __name__ == "__main__": | |
demo.launch(share=False) | |