adikwok commited on
Commit
663780e
·
verified ·
1 Parent(s): a7739e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -243
app.py CHANGED
@@ -7,71 +7,25 @@ from datetime import datetime
7
 
8
  # Groq API Configuration
9
  API_URL = "https://api.groq.com/openai/v1/chat/completions"
 
10
 
11
- # Try multiple ways to get API key
12
- API_KEY = None
13
- api_key_sources = []
14
-
15
- # Method 1: Environment variable
16
- env_key = os.getenv("GROQ_API_KEY")
17
- if env_key:
18
- API_KEY = env_key
19
- api_key_sources.append("Environment variable GROQ_API_KEY")
20
-
21
- # Method 2: Alternative environment variable names
22
- alt_names = ["GROQ_KEY", "GROQ_API", "GROQ_TOKEN"]
23
- for name in alt_names:
24
- alt_key = os.getenv(name)
25
- if alt_key and not API_KEY:
26
- API_KEY = alt_key
27
- api_key_sources.append(f"Environment variable {name}")
28
-
29
- # Method 3: Check if running in HF Spaces
30
- hf_token = os.getenv("HF_TOKEN") # HF Spaces sets this automatically
31
- is_hf_spaces = hf_token is not None
32
 
33
  # In-memory chat history storage
34
  chat_history: List[Dict[str, str]] = []
35
 
36
- def get_api_key_status():
37
- """Get detailed API key status for debugging"""
38
- status = {
39
- "found": API_KEY is not None,
40
- "preview": API_KEY[:10] + "..." if API_KEY else "None",
41
- "sources": api_key_sources,
42
- "is_hf_spaces": is_hf_spaces,
43
- "env_vars": {k: v[:10] + "..." if v and len(v) > 10 else v
44
- for k, v in os.environ.items()
45
- if 'GROQ' in k.upper() or 'API' in k.upper()}
46
- }
47
- return status
48
-
49
- def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10, user_api_key: str = "") -> List[List[str]]:
50
- """Groq API call with chat history and optional summarization, returning chatbot format"""
51
 
52
- # Use user-provided API key if available
53
- current_api_key = user_api_key.strip() if user_api_key.strip() else API_KEY
54
-
55
- if not current_api_key:
56
- status = get_api_key_status()
57
- error_msg = f"""❌ No API Key found.
58
-
59
- Debug Info:
60
- - Environment check: {status}
61
- - Running in HF Spaces: {is_hf_spaces}
62
-
63
- Solutions:
64
- 1. If in HF Spaces: Go to Settings > Secrets and add GROQ_API_KEY
65
- 2. If local: Set environment variable or enter API key in the input field below
66
- 3. Get your key from: https://console.groq.com/keys"""
67
- return [[message, error_msg]]
68
 
69
  if not message.strip():
70
- return [[None, "❌ Empty message"]]
71
 
72
  try:
73
  headers = {
74
- "Authorization": f"Bearer {current_api_key}",
75
  "Content-Type": "application/json"
76
  }
77
 
@@ -86,10 +40,11 @@ Solutions:
86
  # Limit history to max_history messages to avoid token overflow
87
  recent_history = chat_history[-max_history:]
88
 
89
- # Prepare messages for API call, excluding timestamp and topic
90
- messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
91
 
92
  if summarize:
 
93
  summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n"
94
  for msg in recent_history:
95
  if msg["topic"] == topic:
@@ -104,73 +59,69 @@ Solutions:
104
  "temperature": 0.7
105
  }
106
 
107
- response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  if response.status_code == 200:
110
  result = response.json()
111
  if "choices" in result and len(result["choices"]) > 0:
112
  response_content = result["choices"][0]["message"]["content"]
 
113
  chat_history.append({
114
  "role": "assistant",
115
  "content": response_content,
116
  "topic": topic,
117
  "timestamp": datetime.now().isoformat()
118
  })
119
- return [[message.strip(), response_content]]
120
  else:
121
- error_msg = f"❌ No choices in response: {json.dumps(result, indent=2)}"
122
- return [[message.strip(), error_msg]]
123
  else:
124
- error_msg = f"❌ HTTP {response.status_code}: {response.text}"
125
- return [[message.strip(), error_msg]]
126
 
127
  except requests.exceptions.Timeout:
128
- return [[message.strip(), "❌ Request timeout (30s)"]]
129
  except requests.exceptions.ConnectionError:
130
- return [[message.strip(), "❌ Connection error - cannot reach Groq API"]]
131
  except json.JSONDecodeError:
132
- return [[message.strip(), f"❌ Invalid JSON response: {response.text}"]]
133
  except Exception as e:
134
- return [[message.strip(), f"❌ Unexpected error: {str(e)}"]]
135
-
136
- def update_chatbot_display() -> List[List[str]]:
137
- """Update the chatbot display with the entire chat history"""
138
- display_history = []
139
- for msg in chat_history:
140
- if msg["role"] == "user":
141
- display_history.append([msg["content"], None])
142
- else:
143
- if display_history: # If there's a previous user message
144
- display_history[-1][1] = msg["content"]
145
- else: # If assistant message without user message
146
- display_history.append([None, msg["content"]])
147
- return display_history
148
 
149
  def clear_history():
150
  """Clear the chat history"""
151
  chat_history.clear()
152
- return [], "✅ Chat history cleared"
153
 
154
- def view_history(topic: str = None) -> str:
155
  """View the chat history, optionally filtered by topic"""
156
  if not chat_history:
157
  return "❌ No chat history available"
158
 
159
  output = "📜 Chat History:\n\n"
160
  for msg in chat_history:
161
- if topic is None or msg.get("topic") == topic:
162
- output += f"[{msg['timestamp']}] {msg['role'].capitalize()} ({msg.get('topic', 'N/A')}): {msg['content']}\n\n"
163
  return output
164
 
165
- def manual_curl_example(user_api_key: str = ""):
166
  """Generate curl command for manual testing"""
167
- current_api_key = user_api_key.strip() if user_api_key.strip() else API_KEY
168
-
169
- if not current_api_key:
170
- return "❌ No API key to generate curl. Set GROQ_API_KEY in environment or enter it below."
171
 
172
  curl_cmd = f'''curl -X POST "{API_URL}" \\
173
- -H "Authorization: Bearer {current_api_key}" \\
174
  -H "Content-Type: application/json" \\
175
  -d '{{
176
  "model": "llama3-8b-8192",
@@ -182,111 +133,38 @@ def manual_curl_example(user_api_key: str = ""):
182
  '''
183
  return curl_cmd
184
 
185
- def test_api_connection(user_api_key: str = ""):
186
- """Test API connection"""
187
- current_api_key = user_api_key.strip() if user_api_key.strip() else API_KEY
188
-
189
- if not current_api_key:
190
- return "❌ No API key provided"
191
-
192
- try:
193
- headers = {
194
- "Authorization": f"Bearer {current_api_key}",
195
- "Content-Type": "application/json"
196
- }
197
-
198
- payload = {
199
- "model": "llama3-8b-8192",
200
- "messages": [{"role": "user", "content": "Test connection"}],
201
- "max_tokens": 10
202
- }
203
-
204
- response = requests.post(API_URL, headers=headers, json=payload, timeout=10)
205
-
206
- if response.status_code == 200:
207
- return f"✅ API connection successful! Status: {response.status_code}"
208
- else:
209
- return f"❌ API error: {response.status_code} - {response.text}"
210
-
211
- except Exception as e:
212
- return f"❌ Connection test failed: {str(e)}"
213
-
214
  # Create Gradio interface
215
- with gr.Blocks(title="Groq Debug with Memory", css="""
216
- .chatbot-container { max-height: 500px; overflow-y: auto; }
217
- .input-container { position: sticky; bottom: 0; background: white; padding: 10px; }
218
- """) as demo:
219
  gr.Markdown("# 🔍 Groq API Debug Tool with Memory")
220
 
221
- # Global API key input
222
- with gr.Row():
223
- global_api_key = gr.Textbox(
224
- label="API Key (Optional - if not set in environment)",
225
- placeholder="Enter your Groq API key here...",
226
- type="password",
227
- scale=3
228
- )
229
- test_button = gr.Button("Test Connection", scale=1)
230
-
231
- connection_status = gr.Textbox(label="Connection Status", interactive=False)
232
-
233
- test_button.click(
234
- test_api_connection,
235
- inputs=[global_api_key],
236
- outputs=[connection_status]
237
- )
238
-
239
  with gr.Tab("Chat with Memory"):
240
  gr.Markdown("Chat with Groq, maintain history, and summarize by topic")
241
 
242
- chatbot = gr.Chatbot(
243
- label="Conversation",
244
- container=True,
245
- height=500,
246
- elem_classes="chatbot-container",
247
- bubble_full_width=False
248
  )
249
- with gr.Row(elem_classes="input-container"):
250
- chat_input = gr.Textbox(
251
- label="Message",
252
- placeholder="Enter your message",
253
- value="Hello, how can you help me today?",
254
- show_label=False,
255
- container=False,
256
- scale=3
257
- )
258
- topic_input = gr.Textbox(
259
- label="Topic",
260
- placeholder="Topic (e.g., 'coding')",
261
- value="general",
262
- show_label=False,
263
- container=False,
264
- scale=1
265
- )
266
- summarize_checkbox = gr.Checkbox(
267
- label="Summarize",
268
- value=False,
269
- scale=1
270
- )
271
- chat_button = gr.Button("Send", variant="primary", scale=1)
272
-
273
- def chat_and_update(message, topic, summarize, api_key):
274
- # Get the new chat entry
275
- new_chat = groq_with_memory(message, topic, summarize, user_api_key=api_key)
276
- # Return updated chatbot display and clear input
277
- return update_chatbot_display(), ""
278
-
279
- chat_button.click(
280
- chat_and_update,
281
- inputs=[chat_input, topic_input, summarize_checkbox, global_api_key],
282
- outputs=[chatbot, chat_input]
283
  )
284
 
285
- # Enable Enter key
286
- chat_input.submit(
287
- chat_and_update,
288
- inputs=[chat_input, topic_input, summarize_checkbox, global_api_key],
289
- outputs=[chatbot, chat_input]
290
  )
291
 
292
  with gr.Tab("View History"):
@@ -296,10 +174,8 @@ with gr.Blocks(title="Groq Debug with Memory", css="""
296
  label="Filter by Topic (optional)",
297
  placeholder="Enter topic to filter (leave blank for all)"
298
  )
299
- with gr.Row():
300
- history_button = gr.Button("View History")
301
- clear_button = gr.Button("Clear History", variant="secondary")
302
-
303
  history_output = gr.Textbox(
304
  label="Chat History",
305
  lines=10,
@@ -311,14 +187,9 @@ with gr.Blocks(title="Groq Debug with Memory", css="""
311
  inputs=[history_topic],
312
  outputs=[history_output]
313
  )
314
-
315
- def clear_and_update():
316
- clear_history()
317
- return [], "✅ Chat history cleared"
318
-
319
  clear_button.click(
320
- clear_and_update,
321
- outputs=[chatbot, history_output]
322
  )
323
 
324
  with gr.Tab("Manual Test"):
@@ -332,57 +203,28 @@ with gr.Blocks(title="Groq Debug with Memory", css="""
332
 
333
  curl_button.click(
334
  manual_curl_example,
335
- inputs=[global_api_key],
336
  outputs=[curl_output]
337
  )
338
 
339
- with gr.Tab("Debug Info"):
340
- gr.Markdown("**System Information & Troubleshooting**")
341
-
342
- debug_button = gr.Button("Show Debug Info")
343
- debug_output = gr.JSON(label="Debug Information")
344
-
345
- def show_debug_info():
346
- status = get_api_key_status()
347
- debug_info = {
348
- "api_key_status": status,
349
- "api_url": API_URL,
350
- "available_env_vars": list(os.environ.keys()),
351
- "python_version": os.sys.version,
352
- "current_directory": os.getcwd()
353
- }
354
- return debug_info
355
-
356
- debug_button.click(
357
- show_debug_info,
358
- outputs=[debug_output]
359
- )
360
-
361
  gr.Markdown(f"""
362
- **Setup Instructions:**
363
-
364
- **For Hugging Face Spaces:**
365
- 1. Go to your Space Settings
366
- 2. Click on "Secrets" tab
367
- 3. Add new secret: `GROQ_API_KEY` = your_api_key_here
368
- 4. Restart the space
369
-
370
- **For Local Development:**
371
- ```bash
372
- export GROQ_API_KEY="your_api_key_here"
373
- python app.py
374
- ```
375
-
376
- **Get API Key:**
377
- - Visit: https://console.groq.com/keys
378
- - Create account and generate API key
379
-
380
- **Troubleshooting:**
381
- - ✅ API Key Status: {'Found' if API_KEY else 'Missing'}
382
- - ✅ Running in HF Spaces: {is_hf_spaces}
383
- - ✅ API URL: `{API_URL}`
384
-
385
- **Alternative:** Enter your API key in the field above for temporary testing.
386
  """)
387
 
388
  if __name__ == "__main__":
 
7
 
8
  # Groq API Configuration
9
  API_URL = "https://api.groq.com/openai/v1/chat/completions"
10
+ API_KEY = os.getenv("GROQ_API_KEY")
11
 
12
+ print(f"🔑 API Key Found: {'Yes' if API_KEY else 'No'}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # In-memory chat history storage
15
  chat_history: List[Dict[str, str]] = []
16
 
17
+ def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> str:
18
+ """Groq API call with chat history and optional summarization"""
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ if not API_KEY:
21
+ return "❌ No API Key found"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
  if not message.strip():
24
+ return "❌ Empty message"
25
 
26
  try:
27
  headers = {
28
+ "Authorization": f"Bearer {API_KEY}",
29
  "Content-Type": "application/json"
30
  }
31
 
 
40
  # Limit history to max_history messages to avoid token overflow
41
  recent_history = chat_history[-max_history:]
42
 
43
+ # Prepare messages for API call
44
+ messages = recent_history.copy()
45
 
46
  if summarize:
47
+ # Create a prompt to summarize the conversation
48
  summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n"
49
  for msg in recent_history:
50
  if msg["topic"] == topic:
 
59
  "temperature": 0.7
60
  }
61
 
62
+ print(f"🚀 Sending request to: {API_URL}")
63
+ print(f"📝 Payload: {json.dumps(payload, indent=2)}")
64
+
65
+ response = requests.post(
66
+ API_URL,
67
+ headers=headers,
68
+ json=payload,
69
+ timeout=30
70
+ )
71
+
72
+ print(f"📊 Status Code: {response.status_code}")
73
+ print(f"📋 Response Headers: {dict(response.headers)}")
74
+ print(f"📄 Response Text (first 300 chars): {response.text[:300]}")
75
 
76
  if response.status_code == 200:
77
  result = response.json()
78
  if "choices" in result and len(result["choices"]) > 0:
79
  response_content = result["choices"][0]["message"]["content"]
80
+ # Add assistant's response to history
81
  chat_history.append({
82
  "role": "assistant",
83
  "content": response_content,
84
  "topic": topic,
85
  "timestamp": datetime.now().isoformat()
86
  })
87
+ return response_content
88
  else:
89
+ return f"❌ No choices in response: {json.dumps(result, indent=2)}"
 
90
  else:
91
+ return f"❌ HTTP {response.status_code}: {response.text}"
 
92
 
93
  except requests.exceptions.Timeout:
94
+ return "❌ Request timeout (30s)"
95
  except requests.exceptions.ConnectionError:
96
+ return "❌ Connection error - cannot reach Groq API"
97
  except json.JSONDecodeError:
98
+ return f"❌ Invalid JSON response: {response.text}"
99
  except Exception as e:
100
+ return f"❌ Unexpected error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
  def clear_history():
103
  """Clear the chat history"""
104
  chat_history.clear()
105
+ return "✅ Chat history cleared"
106
 
107
+ def view_history(topic: str = None):
108
  """View the chat history, optionally filtered by topic"""
109
  if not chat_history:
110
  return "❌ No chat history available"
111
 
112
  output = "📜 Chat History:\n\n"
113
  for msg in chat_history:
114
+ if topic is None or msg["topic"] == topic:
115
+ output += f"[{msg['timestamp']}] {msg['role'].capitalize()} ({msg['topic']}): {msg['content']}\n"
116
  return output
117
 
118
+ def manual_curl_example():
119
  """Generate curl command for manual testing"""
120
+ if not API_KEY:
121
+ return "❌ No API key to generate curl"
 
 
122
 
123
  curl_cmd = f'''curl -X POST "{API_URL}" \\
124
+ -H "Authorization: Bearer {API_KEY}" \\
125
  -H "Content-Type: application/json" \\
126
  -d '{{
127
  "model": "llama3-8b-8192",
 
133
  '''
134
  return curl_cmd
135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
  # Create Gradio interface
137
+ with gr.Blocks(title="Groq Debug with Memory") as demo:
 
 
 
138
  gr.Markdown("# 🔍 Groq API Debug Tool with Memory")
139
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  with gr.Tab("Chat with Memory"):
141
  gr.Markdown("Chat with Groq, maintain history, and summarize by topic")
142
 
143
+ chat_input = gr.Textbox(
144
+ label="Message",
145
+ placeholder="Enter your message",
146
+ value="Hello, how can you help me today?"
 
 
147
  )
148
+ topic_input = gr.Textbox(
149
+ label="Topic",
150
+ placeholder="Enter topic (e.g., 'coding', 'general')",
151
+ value="general"
152
+ )
153
+ summarize_checkbox = gr.Checkbox(
154
+ label="Summarize conversation for this topic",
155
+ value=False
156
+ )
157
+ chat_button = gr.Button("Send Message", variant="primary")
158
+ chat_output = gr.Textbox(
159
+ label="Response",
160
+ lines=10,
161
+ max_lines=20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  )
163
 
164
+ chat_button.click(
165
+ groq_with_memory,
166
+ inputs=[chat_input, topic_input, summarize_checkbox],
167
+ outputs=[chat_output]
 
168
  )
169
 
170
  with gr.Tab("View History"):
 
174
  label="Filter by Topic (optional)",
175
  placeholder="Enter topic to filter (leave blank for all)"
176
  )
177
+ history_button = gr.Button("View History")
178
+ clear_button = gr.Button("Clear History", variant="secondary")
 
 
179
  history_output = gr.Textbox(
180
  label="Chat History",
181
  lines=10,
 
187
  inputs=[history_topic],
188
  outputs=[history_output]
189
  )
 
 
 
 
 
190
  clear_button.click(
191
+ clear_history,
192
+ outputs=[history_output]
193
  )
194
 
195
  with gr.Tab("Manual Test"):
 
203
 
204
  curl_button.click(
205
  manual_curl_example,
 
206
  outputs=[curl_output]
207
  )
208
 
209
+ with gr.Tab("API Info"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  gr.Markdown(f"""
211
+ **Debug Information:**
212
+ - API URL: `{API_URL}`
213
+ - API Key Status: {'✅ Found' if API_KEY else '❌ Missing'}
214
+ - API Key Preview: `{API_KEY[:10] + '...' if API_KEY else 'None'}`
215
+
216
+ **Common Issues:**
217
+ 1. **Wrong URL** - Make sure using correct Groq endpoint
218
+ 2. **Invalid Model** - Check if model name is correct
219
+ 3. **Malformed Request** - Check JSON structure
220
+ 4. **Rate Limiting** - Wait between requests
221
+ 5. **Network Issues** - Check HF Spaces connectivity
222
+
223
+ **Expected Models:**
224
+ - `llama3-8b-8192`
225
+ - `llama3-70b-8192`
226
+ - `mixtral-8x7b-32768`
227
+ - `gemma-7b-it`
 
 
 
 
 
 
 
228
  """)
229
 
230
  if __name__ == "__main__":