adikwok commited on
Commit
e480a63
Β·
verified Β·
1 Parent(s): d4cb9b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +201 -71
app.py CHANGED
@@ -7,26 +7,71 @@ from datetime import datetime
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
- print("Environment Variables:", {k: v for k, v in os.environ.items() if k.startswith('GROQ')}) # Debug print
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # In-memory chat history storage
16
  chat_history: List[Dict[str, str]] = []
17
 
18
- def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> List[List[str]]:
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  """Groq API call with chat history and optional summarization, returning chatbot format"""
20
 
21
- if not API_KEY:
22
- return [[None, "❌ No API Key found. Please set GROQ_API_KEY in Hugging Face Spaces Settings > Secrets. Go to https://console.groq.com/keys to get your key."]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  if not message.strip():
25
  return [[None, "❌ Empty message"]]
26
 
27
  try:
28
  headers = {
29
- "Authorization": f"Bearer {API_KEY}",
30
  "Content-Type": "application/json"
31
  }
32
 
@@ -45,7 +90,6 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
45
  messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
46
 
47
  if summarize:
48
- # Create a prompt to summarize the conversation
49
  summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n"
50
  for msg in recent_history:
51
  if msg["topic"] == topic:
@@ -60,32 +104,18 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
60
  "temperature": 0.7
61
  }
62
 
63
- print(f"πŸš€ Sending request to: {API_URL}")
64
- print(f"πŸ“ Payload: {json.dumps(payload, indent=2)}")
65
-
66
- response = requests.post(
67
- API_URL,
68
- headers=headers,
69
- json=payload,
70
- timeout=30
71
- )
72
-
73
- print(f"πŸ“Š Status Code: {response.status_code}")
74
- print(f"πŸ“‹ Response Headers: {dict(response.headers)}")
75
- print(f"πŸ“„ Response Text (first 300 chars): {response.text[:300]}")
76
 
77
  if response.status_code == 200:
78
  result = response.json()
79
  if "choices" in result and len(result["choices"]) > 0:
80
  response_content = result["choices"][0]["message"]["content"]
81
- # Add assistant's response to history
82
  chat_history.append({
83
  "role": "assistant",
84
  "content": response_content,
85
  "topic": topic,
86
  "timestamp": datetime.now().isoformat()
87
  })
88
- # Return in chatbot format: [[user_message, assistant_response]]
89
  return [[message.strip(), response_content]]
90
  else:
91
  error_msg = f"❌ No choices in response: {json.dumps(result, indent=2)}"
@@ -105,7 +135,16 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
105
 
106
  def update_chatbot_display() -> List[List[str]]:
107
  """Update the chatbot display with the entire chat history"""
108
- return [[msg["content"], None] if msg["role"] == "user" else [None, msg["content"]] for msg in chat_history]
 
 
 
 
 
 
 
 
 
109
 
110
  def clear_history():
111
  """Clear the chat history"""
@@ -119,17 +158,19 @@ def view_history(topic: str = None) -> str:
119
 
120
  output = "πŸ“œ Chat History:\n\n"
121
  for msg in chat_history:
122
- if topic is None or msg["topic"] == topic:
123
- output += f"[{msg['timestamp']}] {msg['role'].capitalize()} ({msg['topic']}): {msg['content']}\n"
124
  return output
125
 
126
- def manual_curl_example():
127
  """Generate curl command for manual testing"""
128
- if not API_KEY:
129
- return "❌ No API key to generate curl. Set GROQ_API_KEY in Hugging Face Spaces Settings > Secrets."
 
 
130
 
131
  curl_cmd = f'''curl -X POST "{API_URL}" \\
132
- -H "Authorization: Bearer {API_KEY}" \\
133
  -H "Content-Type: application/json" \\
134
  -d '{{
135
  "model": "llama3-8b-8192",
@@ -141,17 +182,62 @@ def manual_curl_example():
141
  '''
142
  return curl_cmd
143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  # Create Gradio interface
145
- with gr.Blocks(title="Grok Debug with Memory", css="""
146
  .chatbot-container { max-height: 500px; overflow-y: auto; }
147
  .input-container { position: sticky; bottom: 0; background: white; padding: 10px; }
148
- .chatbot .message.user { text-align: left; background: #e6f3ff; padding: 10px; border-radius: 10px; margin: 5px; }
149
- .chatbot .message.assistant { text-align: right; background: #f0f0f0; padding: 10px; border-radius: 10px; margin: 5px; }
150
  """) as demo:
151
- gr.Markdown("# πŸ” Grok API Debug Tool with Memory")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
 
153
  with gr.Tab("Chat with Memory"):
154
- gr.Markdown("Chat with Grok, maintain history, and summarize by topic")
155
 
156
  chatbot = gr.Chatbot(
157
  label="Conversation",
@@ -166,11 +252,12 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
166
  placeholder="Enter your message",
167
  value="Hello, how can you help me today?",
168
  show_label=False,
169
- container=False
 
170
  )
171
  topic_input = gr.Textbox(
172
  label="Topic",
173
- placeholder="Enter topic (e.g., 'coding', 'general')",
174
  value="general",
175
  show_label=False,
176
  container=False,
@@ -178,17 +265,28 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
178
  )
179
  summarize_checkbox = gr.Checkbox(
180
  label="Summarize",
181
- value=False
 
182
  )
183
- chat_button = gr.Button("Send", variant="primary")
 
 
 
 
 
 
184
 
185
  chat_button.click(
186
- groq_with_memory,
187
- inputs=[chat_input, topic_input, summarize_checkbox],
188
- outputs=[chatbot]
189
- ).then(
190
- update_chatbot_display,
191
- outputs=[chatbot]
 
 
 
 
192
  )
193
 
194
  with gr.Tab("View History"):
@@ -198,8 +296,10 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
198
  label="Filter by Topic (optional)",
199
  placeholder="Enter topic to filter (leave blank for all)"
200
  )
201
- history_button = gr.Button("View History")
202
- clear_button = gr.Button("Clear History", variant="secondary")
 
 
203
  history_output = gr.Textbox(
204
  label="Chat History",
205
  lines=10,
@@ -211,8 +311,13 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
211
  inputs=[history_topic],
212
  outputs=[history_output]
213
  )
 
 
 
 
 
214
  clear_button.click(
215
- clear_history,
216
  outputs=[chatbot, history_output]
217
  )
218
 
@@ -222,37 +327,62 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
222
  curl_button = gr.Button("Generate Curl Command")
223
  curl_output = gr.Code(
224
  label="Curl Command",
225
- language="shell"
226
  )
227
 
228
  curl_button.click(
229
  manual_curl_example,
 
230
  outputs=[curl_output]
231
  )
232
 
233
- with gr.Tab("API Info"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
  gr.Markdown(f"""
235
- **Debug Information:**
236
- - API URL: `{API_URL}`
237
- - API Key Status: {'βœ… Found' if API_KEY else '❌ Missing'}
238
- - API Key Preview: `{API_KEY[:10] + '...' if API_KEY else 'None'}`
239
-
240
- **Common Issues:**
241
- 1. **Wrong URL** - Make sure using correct Groq endpoint
242
- 2. **Invalid Model** - Check if model name is correct
243
- 3. **Malformed Request** - Check JSON structure
244
- 4. **Rate Limiting** - Wait between requests
245
- 5. **Network Issues** - Check HF Spaces connectivity
246
-
247
- **Expected Models:**
248
- - `llama3-8b-8192`
249
- - `llama3-70b-8192`
250
- - `mixtral-8x7b-32768`
251
- - `gemma-7b-it`
252
-
253
- **API Key Setup:**
254
- - Ensure GROQ_API_KEY is set in Hugging Face Spaces Settings > Secrets.
255
- - Get your key from https://console.groq.com/keys.
 
 
 
256
  """)
257
 
258
  if __name__ == "__main__":
 
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
 
 
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
  "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)}"
 
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"""
 
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
  '''
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",
 
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,
 
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
  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
  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
 
 
327
  curl_button = gr.Button("Generate Curl Command")
328
  curl_output = gr.Code(
329
  label="Curl Command",
330
+ language="bash"
331
  )
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__":