AbuSaleh28 commited on
Commit
9700024
·
verified ·
1 Parent(s): 34612d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -38
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import os
3
- import spaces
4
  from groq import Groq
5
 
6
  # Initialize the Groq client via Hugging Face Secrets
@@ -18,16 +18,9 @@ custom_css = """
18
  .sidebar-panel { background: #111827 !important; border: 1px solid #1f2937 !important; border-radius: 12px !important; }
19
  .chat-window { border: 1px solid #1f2937 !important; border-radius: 12px !important; background: #111827 !important; }
20
  """
21
- @spaces.GPU
22
- def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
23
- """
24
- Handles streaming responses using the standard history list layout.
25
- """
26
- if not message.strip():
27
- yield history
28
- return
29
-
30
 
 
 
31
  def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
32
  """
33
  Handles streaming responses using the standard history list layout.
@@ -36,46 +29,37 @@ def chat_stream(message, history, model, system_prompt, temperature, max_tokens)
36
  yield history
37
  return
38
 
39
- # 1. Initialize message list with system prompt
40
  messages = [{"role": "system", "content": system_prompt}]
41
 
42
- # 2. Add conversation history
43
  for turn in history:
44
- # turn is typically [user_message, assistant_message]
45
  if turn[0]:
46
  messages.append({"role": "user", "content": turn[0]})
47
  if turn[1]:
48
  messages.append({"role": "assistant", "content": turn[1]})
49
 
50
- # 3. Add the current user message
51
  messages.append({"role": "user", "content": message})
52
 
53
- # 4. Append user message to history visually before generating
54
- history.append(
55
- {
56
- "role": "assistant",
57
- "content": response
58
- }
59
- )
60
-
61
- return history
62
-
63
-
64
- # 5. Stream from Groq
65
- try:
66
- stream = client.chat.completions.create(
67
- model=model,
68
- messages=messages,
69
- temperature=temperature,
70
- max_tokens=max_tokens,
71
- stream=True,
72
  )
73
 
74
  partial_response = ""
75
  for chunk in stream:
76
  if chunk.choices[0].delta.content:
77
  partial_response += chunk.choices[0].delta.content
78
- # Update the last assistant turn in history
79
  history[-1][1] = partial_response
80
  yield history
81
 
@@ -83,9 +67,8 @@ try:
83
  history[-1][1] = f"⚠️ Error connecting to Groq: {str(e)}"
84
  yield history
85
 
86
- # Build the layout manually without gr.ChatInterface macros
87
  with gr.Blocks() as demo:
88
-
89
  gr.Markdown("# 🚀 Personal AI ChatBot", elem_id="title-header")
90
  gr.Markdown("A fully customizable, hyper-fast LLM workspace.")
91
 
@@ -121,7 +104,7 @@ with gr.Blocks() as demo:
121
  )
122
 
123
  gr.Markdown("---")
124
- gr.Markdown("**Status:** 🟢 Connected to Groq API")
125
 
126
  # --- RIGHT COLUMN: CHAT INTERFACE ---
127
  with gr.Column(scale=3):
@@ -146,7 +129,6 @@ with gr.Blocks() as demo:
146
  outputs=[msg_input]
147
  )
148
 
149
- # Clear chat utility logic
150
  clear_btn.click(fn=lambda: [], inputs=None, outputs=[chatbot])
151
 
152
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import os
3
+ import spaces # <--- 1. Import Hugging Face ZeroGPU utilities
4
  from groq import Groq
5
 
6
  # Initialize the Groq client via Hugging Face Secrets
 
18
  .sidebar-panel { background: #111827 !important; border: 1px solid #1f2937 !important; border-radius: 12px !important; }
19
  .chat-window { border: 1px solid #1f2937 !important; border-radius: 12px !important; background: #111827 !important; }
20
  """
 
 
 
 
 
 
 
 
 
21
 
22
+ # 2. Add the ZeroGPU decorator here to make Hugging Face happy
23
+ @spaces.GPU
24
  def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
25
  """
26
  Handles streaming responses using the standard history list layout.
 
29
  yield history
30
  return
31
 
32
+ # Initialize message list with system prompt
33
  messages = [{"role": "system", "content": system_prompt}]
34
 
35
+ # Add conversation history
36
  for turn in history:
 
37
  if turn[0]:
38
  messages.append({"role": "user", "content": turn[0]})
39
  if turn[1]:
40
  messages.append({"role": "assistant", "content": turn[1]})
41
 
42
+ # Add the current user message
43
  messages.append({"role": "user", "content": message})
44
 
45
+ # Append user message to history visually before generating
46
+ history.append([message, ""])
47
+ yield history
48
+
49
+ # Stream from Groq
50
+ try:
51
+ stream = client.chat.completions.create(
52
+ model=model,
53
+ messages=messages,
54
+ temperature=temperature,
55
+ max_tokens=max_tokens,
56
+ stream=True,
 
 
 
 
 
 
 
57
  )
58
 
59
  partial_response = ""
60
  for chunk in stream:
61
  if chunk.choices[0].delta.content:
62
  partial_response += chunk.choices[0].delta.content
 
63
  history[-1][1] = partial_response
64
  yield history
65
 
 
67
  history[-1][1] = f"⚠️ Error connecting to Groq: {str(e)}"
68
  yield history
69
 
70
+ # Build the layout manually
71
  with gr.Blocks() as demo:
 
72
  gr.Markdown("# 🚀 Personal AI ChatBot", elem_id="title-header")
73
  gr.Markdown("A fully customizable, hyper-fast LLM workspace.")
74
 
 
104
  )
105
 
106
  gr.Markdown("---")
107
+ gr.Markdown("**Status:** 🟢 Connected via ZeroGPU to Groq")
108
 
109
  # --- RIGHT COLUMN: CHAT INTERFACE ---
110
  with gr.Column(scale=3):
 
129
  outputs=[msg_input]
130
  )
131
 
 
132
  clear_btn.click(fn=lambda: [], inputs=None, outputs=[chatbot])
133
 
134
  if __name__ == "__main__":