Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,63 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
-
from
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
return
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# --- 1. การตั้งค่าโมเดล (The Sandbox Model) ---
|
| 7 |
+
# เลือกโมเดลที่เก่งภาษาไทยและขนาดไม่เกิน RAM ฟรีของ HF
|
| 8 |
+
repo_id = "QuantFactory/Llama-3-8B-Instruct-GGUF"
|
| 9 |
+
filename = "Llama-3-8B-Instruct.Q4_K_M.gguf"
|
| 10 |
+
|
| 11 |
+
print("กำลังดาวน์โหลดโมเดลเข้าสู่ Sandbox... โปรดรอสักครู่")
|
| 12 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 13 |
+
|
| 14 |
+
# โหลดโมเดลเข้าสู่ Memory
|
| 15 |
+
# n_ctx คือจำนวนคำที่จำได้ (ปรับตามความแรงเครื่อง)
|
| 16 |
+
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=2)
|
| 17 |
+
|
| 18 |
+
# --- 2. ขอบเขตคำสั่ง (System Prompt) ---
|
| 19 |
+
SYSTEM_PROMPT = "คุณคือผู้ช่วย AI ที่ฉลาดและสุภาพ ตอบเป็นภาษาไทยอย่างถูกต้อง อยู่ในกรอบของระบบปิด"
|
| 20 |
+
|
| 21 |
+
def respond(message, history):
|
| 22 |
+
# รวมประวัติการแชทเพื่อให้ AI จำบริบทได้
|
| 23 |
+
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{SYSTEM_PROMPT}<|eot_id|>"
|
| 24 |
+
|
| 25 |
+
for user_msg, bot_msg in history:
|
| 26 |
+
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|>"
|
| 27 |
+
|
| 28 |
+
prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 29 |
+
|
| 30 |
+
# รัน AI ในกล่องปิด (Local Inference)
|
| 31 |
+
output = llm(
|
| 32 |
+
prompt,
|
| 33 |
+
max_tokens=512,
|
| 34 |
+
stop=["<|eot_id|>"],
|
| 35 |
+
echo=False
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
return output["choices"][0]["text"].strip()
|
| 39 |
+
|
| 40 |
+
# --- 3. สร้าง UI หน้าแชท ---
|
| 41 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 42 |
+
gr.Markdown("# 🤖 My Local AI Sandbox (Thai Edition)")
|
| 43 |
+
gr.Markdown("โมเดลนี้รันอยู่บน CPU ของ Hugging Face โดยตรง ไม่มีการส่งข้อมูลไปข้างนอก")
|
| 44 |
+
|
| 45 |
+
chatbot = gr.Chatbot(label="ห้องแชทส่วนตัว")
|
| 46 |
+
msg = gr.Textbox(label="พิมพ์คำถามของคุณที่นี่", placeholder="สวัสดีครับ...")
|
| 47 |
+
clear = gr.Button("ล้างการสนทนา")
|
| 48 |
+
|
| 49 |
+
def user(user_message, history):
|
| 50 |
+
return "", history + [[user_message, None]]
|
| 51 |
+
|
| 52 |
+
def bot(history):
|
| 53 |
+
user_message = history[-1][0]
|
| 54 |
+
bot_message = respond(user_message, history[:-1])
|
| 55 |
+
history[-1][1] = bot_message
|
| 56 |
+
return history
|
| 57 |
+
|
| 58 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
| 59 |
+
bot, chatbot, chatbot
|
| 60 |
+
)
|
| 61 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 62 |
+
|
| 63 |
+
demo.launch()
|