Mikecode123 commited on
Commit
4c8f30e
·
verified ·
1 Parent(s): 9aea6fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -18
app.py CHANGED
@@ -4,7 +4,7 @@ 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",
@@ -19,48 +19,62 @@ llm = Llama(
19
  )
20
 
21
  # =========================
22
- # CHAT FUNCTION (GRADIO 4 STYLE)
23
  # =========================
24
- def chat(message, history):
25
- # history is list of dicts in Gradio 4
26
  history = history or []
 
27
 
28
- history.append({"role": "user", "content": message})
 
 
 
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  output = llm.create_chat_completion(
31
- messages=history,
32
  max_tokens=300,
33
  temperature=0.7
34
  )
35
 
36
  response = output["choices"][0]["message"]["content"]
37
 
38
- history.append({"role": "assistant", "content": response})
39
 
40
  return "", history
41
 
42
  # =========================
43
- # GRADIO UI (GRADIO 4)
44
  # =========================
45
  with gr.Blocks() as demo:
46
- gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend Build)")
47
 
48
- chatbot = gr.Chatbot()
49
  msg = gr.Textbox(label="Ask your AI")
50
  clear = gr.Button("Clear")
51
 
52
- # send message
53
  msg.submit(chat, [msg, chatbot], [msg, chatbot])
54
 
55
- # clear chat
56
  clear.click(lambda: [], None, chatbot, queue=False)
57
 
58
  # =========================
59
- # LAUNCH (SPACE SAFE)
60
  # =========================
61
  demo.queue()
62
-
63
- demo.launch(
64
- server_name="0.0.0.0",
65
- server_port=7860
66
- )
 
4
  from huggingface_hub import hf_hub_download
5
 
6
  # =========================
7
+ # LOAD MODEL
8
  # =========================
9
  model_path = hf_hub_download(
10
  repo_id="Mikecode123/ALX",
 
19
  )
20
 
21
  # =========================
22
+ # SAFE CHAT FUNCTION
23
  # =========================
24
+ def chat(prompt, history):
 
25
  history = history or []
26
+ messages = []
27
 
28
+ # Convert Gradio history safely (tuple-based)
29
+ for item in history:
30
+ if isinstance(item, tuple) and len(item) == 2:
31
+ user_msg, bot_msg = item
32
 
33
+ messages.append({
34
+ "role": "user",
35
+ "content": str(user_msg)
36
+ })
37
+
38
+ messages.append({
39
+ "role": "assistant",
40
+ "content": str(bot_msg)
41
+ })
42
+
43
+ # Add current message
44
+ messages.append({
45
+ "role": "user",
46
+ "content": str(prompt)
47
+ })
48
+
49
+ # Call model
50
  output = llm.create_chat_completion(
51
+ messages=messages,
52
  max_tokens=300,
53
  temperature=0.7
54
  )
55
 
56
  response = output["choices"][0]["message"]["content"]
57
 
58
+ history.append((prompt, response))
59
 
60
  return "", history
61
 
62
  # =========================
63
+ # UI (GRADIO SAFE MODE)
64
  # =========================
65
  with gr.Blocks() as demo:
66
+ gr.Markdown("# 🧠 Qwen GGUF AI (Living Legend System)")
67
 
68
+ chatbot = gr.Chatbot() # Gradio 3 safe mode
69
  msg = gr.Textbox(label="Ask your AI")
70
  clear = gr.Button("Clear")
71
 
 
72
  msg.submit(chat, [msg, chatbot], [msg, chatbot])
73
 
 
74
  clear.click(lambda: [], None, chatbot, queue=False)
75
 
76
  # =========================
77
+ # LAUNCH SAFE
78
  # =========================
79
  demo.queue()
80
+ demo.launch(server_name="0.0.0.0", server_port=7860)