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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -58
app.py CHANGED
@@ -1,59 +1,60 @@
1
- import gradio as gr
2
- from llama_cpp import Llama
3
- from huggingface_hub import hf_hub_download
4
-
5
- # =========================
6
- # LOAD MODEL FROM HF REPO
7
- # =========================
8
- model_path = hf_hub_download(
9
- repo_id="Mikecode123/ALX",
10
- filename="qwen2-1_5b-instruct-q4_0.gguf"
11
- )
12
-
13
- llm = Llama(
14
- model_path=model_path,
15
- n_ctx=1024,
16
- n_threads=2
17
- )
18
-
19
- # =========================
20
- # CHAT FUNCTION
21
- # =========================
22
- def chat(prompt, history):
23
- messages = []
24
-
25
- # convert gradio history to chat format
26
- for user_msg, bot_msg in history:
27
- messages.append({"role": "user", "content": user_msg})
28
- messages.append({"role": "assistant", "content": bot_msg})
29
-
30
- messages.append({"role": "user", "content": prompt})
31
-
32
- output = llm.create_chat_completion(
33
- messages=messages,
34
- max_tokens=500,
35
- temperature=0.7
36
- )
37
-
38
- response = output["choices"][0]["message"]["content"]
39
-
40
- history.append((prompt, response))
41
- return "", history
42
-
43
- # =========================
44
- # GRADIO UI
45
- # =========================
46
- with gr.Blocks() as demo:
47
- gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend Build)")
48
-
49
- chatbot = gr.Chatbot()
50
- msg = gr.Textbox(label="Ask your AI")
51
- clear = gr.Button("Clear")
52
-
53
- msg.submit(chat, [msg, chatbot], [msg, chatbot])
54
- clear.click(lambda: None, None, chatbot)
55
-
56
- # =========================
57
- # LAUNCH
58
- # =========================
 
59
  demo.launch()
 
1
+ import os
2
+ import gradio as gr
3
+ from llama_cpp import Llama
4
+ from huggingface_hub import hf_hub_download
5
+
6
+ # =========================
7
+ # LOAD MODEL FROM HF REPO
8
+ # =========================
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,
35
+ max_tokens=300,
36
+ temperature=0.7
37
+ )
38
+
39
+ response = output["choices"][0]["message"]["content"]
40
+ history.append((prompt, response))
41
+
42
+ return "", history
43
+
44
+ # =========================
45
+ # GRADIO UI
46
+ # =========================
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()