1234ty commited on
Commit
2100a4c
·
verified ·
1 Parent(s): 7aee22b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
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. การตั้งค่าโมเดล (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,
@@ -37,14 +41,14 @@ def respond(message, history):
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]]
@@ -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
- demo.launch()
 
 
 
 
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
+