|
|
import gradio as gr |
|
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AI_NAME = "AI Assistant" |
|
|
AI_CREATOR = "AI " |
|
|
AI_DESCRIPTION = ( |
|
|
"Tôi là một trợ lý trí tuệ nhân tạo được tạo ra bởi Trương Quốc Nam. " |
|
|
"Tôi được huấn luyện để hỗ trợ học tập, giải đáp thắc mắc bằng tiếng Việt một cách thân thiện, chính xác và giàu thông tin. " |
|
|
"Tôi luôn cố gắng trả lời ngắn gọn, dễ hiểu và có ví dụ minh họa khi cần." |
|
|
) |
|
|
|
|
|
DEFAULT_SYSTEM_MESSAGE = f""" |
|
|
Bạn là {AI_NAME}, một trợ lý AI thông minh và thân thiện. |
|
|
{AI_DESCRIPTION} |
|
|
|
|
|
Khi được hỏi "bạn là ai", "ai tạo ra bạn", "ai làm ra bạn", hãy trả lời tự nhiên, ví dụ: |
|
|
"Tôi là {AI_NAME}, được tạo ra bởi {AI_CREATOR} để hỗ trợ mọi người học tập và khám phá tri thức!" |
|
|
|
|
|
Luôn trả lời bằng tiếng Việt, lịch sự, rõ ràng và có tính giáo dục. |
|
|
""".strip() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_MODEL = "Qwen/Qwen2.5-72B-Instruct" |
|
|
|
|
|
def respond( |
|
|
message, |
|
|
history: list, |
|
|
system_message, |
|
|
max_tokens, |
|
|
temperature, |
|
|
top_p, |
|
|
model_name, |
|
|
hf_token: gr.OAuthToken, |
|
|
): |
|
|
|
|
|
if hf_token is None or hf_token.token is None: |
|
|
yield "Vui lòng đăng nhập Hugging Face (nút bên trái) để sử dụng chatbot nhé! 😊" |
|
|
return |
|
|
|
|
|
|
|
|
client = InferenceClient(token=hf_token.token) |
|
|
|
|
|
|
|
|
messages = [{"role": "system", "content": system_message or DEFAULT_SYSTEM_MESSAGE}] |
|
|
|
|
|
|
|
|
for user_msg, assistant_msg in history: |
|
|
if user_msg: |
|
|
messages.append({"role": "user", "content": user_msg}) |
|
|
if assistant_msg: |
|
|
messages.append({"role": "assistant", "content": assistant_msg}) |
|
|
|
|
|
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
|
|
|
response = "" |
|
|
try: |
|
|
for chunk in client.chat_completion( |
|
|
messages, |
|
|
model=model_name or DEFAULT_MODEL, |
|
|
max_tokens=max_tokens, |
|
|
temperature=temperature, |
|
|
top_p=top_p, |
|
|
stream=True, |
|
|
): |
|
|
if chunk.choices and chunk.choices[0].delta.content: |
|
|
token = chunk.choices[0].delta.content |
|
|
response += token |
|
|
yield response |
|
|
except Exception as e: |
|
|
yield f"Xin lỗi, có lỗi xảy ra: {str(e)}\nBạn thử lại hoặc chọn model nhẹ hơn nhé!" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks(title=f"{AI_NAME} - Trợ lý AI của HHH", theme=gr.themes.Soft()) as demo: |
|
|
gr.Markdown(f"# 🤖 {AI_NAME}\n{AI_DESCRIPTION}") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
gr.LoginButton() |
|
|
gr.Markdown("##### Cài đặt nâng cao") |
|
|
|
|
|
model_dropdown = gr.Dropdown( |
|
|
label="Chọn mô hình AI", |
|
|
choices=[ |
|
|
"Qwen/Qwen2.5-72B-Instruct", |
|
|
"meta-llama/Meta-Llama-3.1-70B-Instruct", |
|
|
"mistralai/Mistral-7B-Instruct-v0.3", |
|
|
"vinai/PhoGPT-7B5-Instruct", |
|
|
"google/gemma-2-27b-it", |
|
|
], |
|
|
value=DEFAULT_MODEL, |
|
|
allow_custom_value=True, |
|
|
) |
|
|
|
|
|
system_box = gr.Textbox( |
|
|
label="System Prompt (nhận thức của AI)", |
|
|
value=DEFAULT_SYSTEM_MESSAGE, |
|
|
lines=6, |
|
|
) |
|
|
|
|
|
with gr.Accordion("Tham số kỹ thuật", open=False): |
|
|
max_tokens = gr.Slider(1, 4096, value=1024, step=64, label="Max tokens") |
|
|
temperature = gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature") |
|
|
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p") |
|
|
|
|
|
with gr.Column(scale=3): |
|
|
chatbot = gr.ChatInterface( |
|
|
fn=respond, |
|
|
type="messages", |
|
|
autofocus=True, |
|
|
additional_inputs=[ |
|
|
system_box, |
|
|
max_tokens, |
|
|
temperature, |
|
|
top_p, |
|
|
model_dropdown, |
|
|
], |
|
|
) |
|
|
|
|
|
gr.Markdown( |
|
|
"<small></small>" |
|
|
) |
|
|
|
|
|
if __name__ == "app": |
|
|
demo.launch() |