| import gradio as gr |
| from llama_cpp import Llama |
| from huggingface_hub import hf_hub_download |
| import os |
|
|
| |
| |
| repo_id = "QuantFactory/Llama-3-8B-Instruct-GGUF" |
| filename = "Llama-3-8B-Instruct.Q2_K.gguf" |
|
|
| print("📥 กำลังดาวน์โหลดโมเดลเข้า Sandbox...") |
| model_path = hf_hub_download(repo_id=repo_id, filename=filename) |
|
|
| |
| |
| llm = Llama( |
| model_path=model_path, |
| n_ctx=512, |
| n_threads=2, |
| n_batch=128 |
| ) |
|
|
| SYSTEM_PROMPT = "คุณคือ AI ผู้ช่วยใน Sandbox ตอบเป็นภาษาไทยอย่างฉลาดและสุภาพ" |
|
|
| def respond(message, history): |
| |
| prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{SYSTEM_PROMPT}<|eot_id|>" |
| |
| for user_msg, bot_msg in history: |
| if user_msg and bot_msg: |
| prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{user_msg}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n{bot_msg}<|eot_id|>" |
| |
| prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n" |
|
|
| output = llm( |
| prompt, |
| max_tokens=512, |
| stop=["<|eot_id|>"], |
| echo=False |
| ) |
| |
| return output["choices"][0]["text"].strip() |
|
|
| |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: |
| gr.Markdown("# 🛡️ Tanny's Secure AI Sandbox") |
| gr.Markdown("สถานะ: กำลังรันภายใน Docker (Isolated Environment)") |
| |
| chatbot = gr.Chatbot(label="Chat History") |
| msg = gr.Textbox(label="พิมพ์คำถามของคุณ", placeholder="ลองถามอะไรดูก็ได้...") |
| clear = gr.Button("ล้างแชท") |
|
|
| def user(user_message, history): |
| return "", history + [[user_message, None]] |
|
|
| def bot(history): |
| user_message = history[-1][0] |
| bot_message = respond(user_message, history[:-1]) |
| history[-1][1] = bot_message |
| return history |
|
|
| msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then( |
| bot, chatbot, chatbot |
| ) |
| clear.click(lambda: None, None, chatbot, queue=False) |
|
|
| |
| if __name__ == "__main__": |
| demo.launch(server_name="0.0.0.0", server_port=7860) |
| |