Mikecode123 commited on
Commit
ed759f7
·
verified ·
1 Parent(s): acbaf2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -9,26 +9,27 @@ from huggingface_hub import hf_hub_download
9
  model_path = hf_hub_download(
10
  repo_id="Mikecode123/ALX",
11
  filename="qwen2-1_5b-instruct-q4_0.gguf",
12
- token=os.getenv("HF_TOKEN") # secure access
13
  )
14
 
15
  llm = Llama(
16
  model_path=model_path,
17
- n_ctx=512, # reduced to avoid OOM (important in 2026 Spaces)
18
- n_threads=1 # safer for low resources
19
  )
20
 
21
  # =========================
22
- # CHAT FUNCTION
23
  # =========================
24
- def chat(prompt, history):
 
25
  messages = []
26
 
27
- for user_msg, bot_msg in history:
28
- messages.append({"role": "user", "content": user_msg})
29
- messages.append({"role": "assistant", "content": bot_msg})
30
 
31
- messages.append({"role": "user", "content": prompt})
32
 
33
  output = llm.create_chat_completion(
34
  messages=messages,
@@ -37,7 +38,9 @@ def chat(prompt, history):
37
  )
38
 
39
  response = output["choices"][0]["message"]["content"]
40
- history.append((prompt, response))
 
 
41
 
42
  return "", history
43
 
@@ -47,14 +50,20 @@ def chat(prompt, history):
47
  with gr.Blocks() as demo:
48
  gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend Build)")
49
 
50
- chatbot = gr.Chatbot()
51
  msg = gr.Textbox(label="Ask your AI")
52
  clear = gr.Button("Clear")
53
 
54
  msg.submit(chat, [msg, chatbot], [msg, chatbot])
55
- clear.click(lambda: [], None, chatbot)
 
56
 
57
  # =========================
58
- # LAUNCH
59
  # =========================
60
- demo.launch()
 
 
 
 
 
 
9
  model_path = hf_hub_download(
10
  repo_id="Mikecode123/ALX",
11
  filename="qwen2-1_5b-instruct-q4_0.gguf",
12
+ token=os.getenv("HF_TOKEN")
13
  )
14
 
15
  llm = Llama(
16
  model_path=model_path,
17
+ n_ctx=512,
18
+ n_threads=1
19
  )
20
 
21
  # =========================
22
+ # CHAT FUNCTION (MODERN FORMAT)
23
  # =========================
24
+ def chat(message, history):
25
+ # convert Gradio "messages" format safely
26
  messages = []
27
 
28
+ for msg in history:
29
+ if isinstance(msg, dict):
30
+ messages.append(msg)
31
 
32
+ messages.append({"role": "user", "content": message})
33
 
34
  output = llm.create_chat_completion(
35
  messages=messages,
 
38
  )
39
 
40
  response = output["choices"][0]["message"]["content"]
41
+
42
+ history.append({"role": "user", "content": message})
43
+ history.append({"role": "assistant", "content": response})
44
 
45
  return "", history
46
 
 
50
  with gr.Blocks() as demo:
51
  gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend Build)")
52
 
53
+ chatbot = gr.Chatbot(type="messages")
54
  msg = gr.Textbox(label="Ask your AI")
55
  clear = gr.Button("Clear")
56
 
57
  msg.submit(chat, [msg, chatbot], [msg, chatbot])
58
+
59
+ clear.click(lambda: [], None, chatbot, queue=False)
60
 
61
  # =========================
62
+ # LAUNCH (SPACE SAFE)
63
  # =========================
64
+ demo.queue()
65
+
66
+ demo.launch(
67
+ server_name="0.0.0.0",
68
+ server_port=7860
69
+ )