HM_trans / app.py
vannguyencb's picture
Update app.py
cbc031d verified
import gradio as gr
from huggingface_hub import InferenceClient
import os
# SỬ DỤNG MÔ HÌNH LLM ĐỈNH CAO: Qwen2.5-72B-Instruct (Thông minh ngang ngửa ChatGPT/Gemini)
# Nó sẽ đọc hiểu toàn bộ "Bộ Luật Dịch Thuật" và "Bối Cảnh" mà tool của bạn gửi lên.
model_id = "Qwen/Qwen2.5-72B-Instruct"
# Lấy Token từ cài đặt bảo mật của Space (Nếu có) để tăng tốc độ và không bị giới hạn
token = os.environ.get("HF_TOKEN")
client = InferenceClient(model_id, token=token)
def ask_llm(text, prompt=""):
try:
messages = []
# Nếu tool Python có gửi kèm Prompt (Luật dịch/Bối cảnh), nhét nó vào đầu để AI học thuộc
if prompt.strip():
messages.append({"role": "system", "content": prompt})
# Nội dung cần xử lý (Các câu sub hoặc đoạn văn bản)
messages.append({"role": "user", "content": text})
# Gọi AI xử lý
response = client.chat_completion(
messages=messages,
max_tokens=4000, # Cho phép trả về văn bản dài
temperature=0.3 # Giữ ở mức 0.3 để AI dịch chính xác, không sáng tạo lung tung
)
return response.choices[0].message.content
except Exception as e:
return f"[LỖI TỪ AI ĐÁM MÂY]: {str(e)}"
# Giao diện siêu tối giản, chủ yếu mở cổng API ("/ask") cho Tool Portable gọi
demo = gr.Interface(
fn=ask_llm,
inputs=[
gr.Textbox(lines=5, label="Dữ liệu đầu vào (Text)"),
gr.Textbox(lines=5, label="Chỉ thị (Prompt/Luật dịch)")
],
outputs=gr.Textbox(label="Kết quả trả về"),
title="Hải Minh A.I - Cloud LLM Engine",
description=f"Đang chạy Model: {model_id}. Chuyên xử lý Prompt phức tạp từ Tool Portable.",
api_name="ask"
)
if __name__ == "__main__":
demo.launch()