AbuSaleh28 commited on
Commit
20d2ac6
·
verified ·
1 Parent(s): 9700024

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -19
app.py CHANGED
@@ -1,6 +1,6 @@
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
@@ -19,38 +19,36 @@ custom_css = """
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.
27
  """
28
  if not message.strip():
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,
@@ -60,11 +58,12 @@ def chat_stream(message, history, model, system_prompt, temperature, max_tokens)
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
 
66
  except Exception as e:
67
- history[-1][1] = f"⚠️ Error connecting to Groq: {str(e)}"
68
  yield history
69
 
70
  # Build the layout manually
@@ -108,7 +107,8 @@ with gr.Blocks() as demo:
108
 
109
  # --- RIGHT COLUMN: CHAT INTERFACE ---
110
  with gr.Column(scale=3):
111
- chatbot = gr.Chatbot(elem_classes="chat-window")
 
112
 
113
  msg_input = gr.Textbox(
114
  placeholder="Type your message here and press Enter...",
@@ -129,6 +129,7 @@ with gr.Blocks() as demo:
129
  outputs=[msg_input]
130
  )
131
 
 
132
  clear_btn.click(fn=lambda: [], inputs=None, outputs=[chatbot])
133
 
134
  if __name__ == "__main__":
 
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
 
19
  .chat-window { border: 1px solid #1f2937 !important; border-radius: 12px !important; background: #111827 !important; }
20
  """
21
 
 
22
  @spaces.GPU
23
  def chat_stream(message, history, model, system_prompt, temperature, max_tokens):
24
  """
25
+ Handles streaming responses using the modern list-of-dicts (messages) history format.
26
  """
27
  if not message.strip():
28
  yield history
29
  return
30
 
31
+ # 1. Initialize message list with system prompt for the Groq API payload
32
+ api_messages = [{"role": "system", "content": system_prompt}]
33
 
34
+ # 2. Append existing conversation history safely
35
  for turn in history:
36
+ # turn is a dictionary: {"role": "user"|"assistant", "content": "..."}
37
+ api_messages.append({"role": turn["role"], "content": turn["content"]})
 
 
38
 
39
+ # 3. Append the newest user message to the API list
40
+ api_messages.append({"role": "user", "content": message})
41
 
42
+ # 4. Update the Gradio UI history using the dictionary format
43
+ history.append({"role": "user", "content": message})
44
+ history.append({"role": "assistant", "content": ""})
45
  yield history
46
 
47
+ # 5. Stream from Groq
48
  try:
49
  stream = client.chat.completions.create(
50
  model=model,
51
+ messages=api_messages,
52
  temperature=temperature,
53
  max_tokens=max_tokens,
54
  stream=True,
 
58
  for chunk in stream:
59
  if chunk.choices[0].delta.content:
60
  partial_response += chunk.choices[0].delta.content
61
+ # Update the very last item in history (the assistant's content dictionary)
62
+ history[-1]["content"] = partial_response
63
  yield history
64
 
65
  except Exception as e:
66
+ history[-1]["content"] = f"⚠️ Error connecting to Groq: {str(e)}"
67
  yield history
68
 
69
  # Build the layout manually
 
107
 
108
  # --- RIGHT COLUMN: CHAT INTERFACE ---
109
  with gr.Column(scale=3):
110
+ # Explicitly state we are using the modern messages type for the component layout
111
+ chatbot = gr.Chatbot(elem_classes="chat-window", type="messages")
112
 
113
  msg_input = gr.Textbox(
114
  placeholder="Type your message here and press Enter...",
 
129
  outputs=[msg_input]
130
  )
131
 
132
+ # Clear chat utility logic
133
  clear_btn.click(fn=lambda: [], inputs=None, outputs=[chatbot])
134
 
135
  if __name__ == "__main__":