Update app.py
Browse files
app.py
CHANGED
|
@@ -3,31 +3,35 @@ from llama_cpp import Llama
|
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
# --- 1. การตั้งค่าโมเดล (
|
| 7 |
-
# เล
|
| 8 |
repo_id = "QuantFactory/Llama-3-8B-Instruct-GGUF"
|
| 9 |
-
filename = "Llama-3-8B-Instruct.
|
| 10 |
|
| 11 |
-
print("กำลังดาวน์โหลดโมเดลเข้า
|
| 12 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 13 |
|
| 14 |
-
# โหลดโมเดล
|
| 15 |
-
# n_ctx คือ
|
| 16 |
-
llm = Llama(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
SYSTEM_PROMPT = "คุณคือผู้ช่วย AI ที่ฉลาดและสุภาพ ตอบเป็นภาษาไทยอย่างถูกต้อง อยู่ในกรอบของระบบปิด"
|
| 20 |
|
| 21 |
def respond(message, history):
|
| 22 |
-
#
|
| 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 |
-
|
|
|
|
| 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,
|
|
@@ -37,14 +41,14 @@ def respond(message, history):
|
|
| 37 |
|
| 38 |
return output["choices"][0]["text"].strip()
|
| 39 |
|
| 40 |
-
# ---
|
| 41 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 42 |
-
gr.Markdown("#
|
| 43 |
-
gr.Markdown("
|
| 44 |
|
| 45 |
-
chatbot = gr.Chatbot(label="
|
| 46 |
-
msg = gr.Textbox(label="พิมพ์คำถามของคุณ
|
| 47 |
-
clear = gr.Button("ล้าง
|
| 48 |
|
| 49 |
def user(user_message, history):
|
| 50 |
return "", history + [[user_message, None]]
|
|
@@ -60,4 +64,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 60 |
)
|
| 61 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 62 |
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# --- 1. การตั้งค่าโมเดล (เน้นประหยัด RAM เพื่อไม่ให้โดน Kill) ---
|
| 7 |
+
# เปลี่ยนมาใช้ Q2_K ซึ่งเล็กกว่าเดิมมาก แต่ยังคงความฉลาดของ Llama-3 อยู่
|
| 8 |
repo_id = "QuantFactory/Llama-3-8B-Instruct-GGUF"
|
| 9 |
+
filename = "Llama-3-8B-Instruct.Q2_K.gguf"
|
| 10 |
|
| 11 |
+
print("📥 กำลังดาวน์โหลดโมเดลเข้า Sandbox...")
|
| 12 |
model_path = hf_hub_download(repo_id=repo_id, filename=filename)
|
| 13 |
|
| 14 |
+
# โหลดโมเดลแบบจำกัดทรัพยากร
|
| 15 |
+
# n_ctx=512 คือลดความจำระยะสั้นลงนิดหน่อยเพื่อให้รันไหว
|
| 16 |
+
llm = Llama(
|
| 17 |
+
model_path=model_path,
|
| 18 |
+
n_ctx=512,
|
| 19 |
+
n_threads=2,
|
| 20 |
+
n_batch=128
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
SYSTEM_PROMPT = "คุณคือ AI ผู้ช่วยใน Sandbox ตอบเป็นภาษาไทยอย่างฉลาดและสุภาพ"
|
|
|
|
| 24 |
|
| 25 |
def respond(message, history):
|
| 26 |
+
# Format ของ Llama-3
|
| 27 |
prompt = f"<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{SYSTEM_PROMPT}<|eot_id|>"
|
| 28 |
|
| 29 |
for user_msg, bot_msg in history:
|
| 30 |
+
if user_msg and bot_msg:
|
| 31 |
+
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|>"
|
| 32 |
|
| 33 |
prompt += f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
|
| 34 |
|
|
|
|
| 35 |
output = llm(
|
| 36 |
prompt,
|
| 37 |
max_tokens=512,
|
|
|
|
| 41 |
|
| 42 |
return output["choices"][0]["text"].strip()
|
| 43 |
|
| 44 |
+
# --- 2. UI หน้าแชท ---
|
| 45 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 46 |
+
gr.Markdown("# 🛡️ Tanny's Secure AI Sandbox")
|
| 47 |
+
gr.Markdown("สถานะ: กำลังรันภายใน Docker (Isolated Environment)")
|
| 48 |
|
| 49 |
+
chatbot = gr.Chatbot(label="Chat History")
|
| 50 |
+
msg = gr.Textbox(label="พิมพ์คำถามของคุณ", placeholder="ลองถามอะไรดูก็ได้...")
|
| 51 |
+
clear = gr.Button("ล้างแชท")
|
| 52 |
|
| 53 |
def user(user_message, history):
|
| 54 |
return "", history + [[user_message, None]]
|
|
|
|
| 64 |
)
|
| 65 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 66 |
|
| 67 |
+
# สำคัญ: ต้องตั้งพอร์ตเป็น 7860 สำหรับ HF และ host เป็น 0.0.0.0
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 70 |
+
|