Ai01 / app.py
1234ty's picture
Update app.py
2100a4c verified
import gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
import os
# --- 1. การตั้งค่าโมเดล (เน้นประหยัด RAM เพื่อไม่ให้โดน Kill) ---
# เปลี่ยนมาใช้ Q2_K ซึ่งเล็กกว่าเดิมมาก แต่ยังคงความฉลาดของ Llama-3 อยู่
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)
# โหลดโมเดลแบบจำกัดทรัพยากร
# n_ctx=512 คือลดความจำระยะสั้นลงนิดหน่อยเพื่อให้รันไหว
llm = Llama(
model_path=model_path,
n_ctx=512,
n_threads=2,
n_batch=128
)
SYSTEM_PROMPT = "คุณคือ AI ผู้ช่วยใน Sandbox ตอบเป็นภาษาไทยอย่างฉลาดและสุภาพ"
def respond(message, history):
# Format ของ Llama-3
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()
# --- 2. UI หน้าแชท ---
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)
# สำคัญ: ต้องตั้งพอร์ตเป็น 7860 สำหรับ HF และ host เป็น 0.0.0.0
if __name__ == "__main__":
demo.launch(server_name="0.0.0.0", server_port=7860)