adikwok commited on
Commit
550f98e
Β·
verified Β·
1 Parent(s): b64c343

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -249
app.py CHANGED
@@ -1,289 +1,147 @@
1
  import gradio as gr
2
  import requests
3
  import os
4
- from typing import List, Tuple
5
 
6
  # Groq API Configuration
7
  API_URL = "https://api.groq.com/openai/v1/chat/completions"
8
- API_KEY = os.getenv("GROQ_API_KEY") # This will read from HF Secrets
9
 
10
- # Debug info (will show in HF Spaces logs)
11
- print(f"πŸ”‘ Groq API Key Status: {'βœ… Found' if API_KEY else '❌ Missing'}")
12
- if API_KEY:
13
- print(f"πŸ”‘ API Key Preview: {API_KEY[:8]}...")
14
 
15
- headers = {
16
- "Authorization": f"Bearer {API_KEY}",
17
- "Content-Type": "application/json"
18
- }
19
-
20
- # Groq Models
21
- MODELS = {
22
- "llama3-8b-8192": "Llama 3 8B ⚑ (Fastest)",
23
- "llama3-70b-8192": "Llama 3 70B 🧠 (Smartest)",
24
- "mixtral-8x7b-32768": "Mixtral 8x7B βš–οΈ (Balanced)",
25
- "gemma-7b-it": "Gemma 7B πŸ” (Google)"
26
- }
27
-
28
- def query_groq(message: str, history: List[Tuple[str, str]], model: str) -> str:
29
- """Query Groq API with comprehensive error handling"""
30
 
31
- # Check API key
32
  if not API_KEY:
33
- return """❌ **API Key Not Found**
34
-
35
- Please set up your API key in Hugging Face Spaces:
36
-
37
- 1. Go to your Space's **Settings**
38
- 2. Click **Repository secrets**
39
- 3. Add new secret:
40
- - Name: `GROQ_API_KEY`
41
- - Value: Your Groq API key
42
- 4. Restart the Space
43
-
44
- Get your free API key at: [console.groq.com](https://console.groq.com)"""
45
 
46
  try:
47
- # Build conversation history
48
- messages = [{"role": "system", "content": "You are a helpful AI assistant powered by Groq's lightning-fast inference."}]
 
 
 
 
 
 
 
 
 
 
 
49
 
50
- # Add chat history
51
- for user_msg, bot_msg in history:
52
- if user_msg and bot_msg:
53
- messages.extend([
54
- {"role": "user", "content": user_msg},
55
- {"role": "assistant", "content": bot_msg}
56
- ])
57
 
58
- # Add current message
59
- messages.append({"role": "user", "content": message})
60
-
61
- # Make API request
62
  response = requests.post(
63
  API_URL,
64
  headers=headers,
65
- json={
66
- "model": model,
67
- "messages": messages,
68
- "temperature": 0.7,
69
- "max_tokens": 2048,
70
- "stream": False,
71
- "stop": None
72
- },
73
  timeout=30
74
  )
75
 
76
- # Handle HTTP errors
77
- if response.status_code == 401:
78
- return "❌ **Authentication Failed**\nInvalid API key. Please check your GROQ_API_KEY secret."
79
- elif response.status_code == 403:
80
- return "❌ **Access Forbidden**\nAPI key doesn't have permission."
81
- elif response.status_code == 429:
82
- return "❌ **Rate Limited**\nToo many requests. Groq has generous limits, this should be rare."
83
- elif response.status_code == 500:
84
- return "❌ **Server Error**\nGroq server issue. Please try again."
85
- elif response.status_code == 503:
86
- return "❌ **Service Unavailable**\nGroq is temporarily down. Try again in a moment."
87
-
88
- response.raise_for_status()
89
 
90
- # Parse response
91
- result = response.json()
92
-
93
- if "choices" in result and len(result["choices"]) > 0:
94
- content = result["choices"][0]["message"]["content"]
95
- return content.strip()
96
  else:
97
- return f"❌ **Unexpected Response Format**\n```json\n{result}\n```"
98
-
99
  except requests.exceptions.Timeout:
100
- return "❌ **Timeout**\nRequest took too long (>30s). Try again."
101
  except requests.exceptions.ConnectionError:
102
- return "❌ **Connection Error**\nCannot connect to Groq API. Check internet connection."
103
- except requests.exceptions.RequestException as e:
104
- return f"❌ **Request Error**\n{str(e)}"
105
  except Exception as e:
106
- return f"❌ **Unexpected Error**\n{str(e)}"
107
 
108
- def test_api_connection():
109
- """Test API connection and return status"""
110
  if not API_KEY:
111
- return "πŸ”΄ API Key Missing"
112
 
113
- try:
114
- response = requests.post(
115
- API_URL,
116
- headers=headers,
117
- json={
118
- "model": "llama3-8b-8192",
119
- "messages": [{"role": "user", "content": "test"}],
120
- "max_tokens": 5
121
- },
122
- timeout=10
123
- )
124
-
125
- if response.status_code == 200:
126
- return "🟒 Connected Successfully"
127
- else:
128
- return f"πŸ”΄ HTTP {response.status_code}"
129
- except Exception as e:
130
- return f"πŸ”΄ Connection Failed: {str(e)[:50]}"
131
 
132
- def create_gradio_interface():
133
- # Test connection at startup
134
- connection_status = test_api_connection()
135
-
136
- # Custom CSS for better UI
137
- css = """
138
- .gradio-container {
139
- max-width: 800px !important;
140
- margin: auto !important;
141
- }
142
- .chat-message {
143
- padding: 10px !important;
144
- }
145
- """
146
 
147
- with gr.Blocks(
148
- title="⚑ Groq AI Chat",
149
- theme=gr.themes.Soft(primary_hue="blue"),
150
- css=css
151
- ) as demo:
152
 
153
- # Header
154
- gr.Markdown("""
155
- # ⚑ Groq AI Chat
156
- ### Lightning-fast AI responses powered by Groq's LPUβ„’
157
- """)
158
-
159
- # Status indicator
160
- with gr.Row():
161
- gr.Markdown(f"**Connection Status:** {connection_status}")
162
-
163
- # Model selector
164
- with gr.Row():
165
- model_dropdown = gr.Dropdown(
166
- choices=list(MODELS.keys()),
167
- value="llama3-8b-8192",
168
- label="πŸ€– Select AI Model",
169
- info="Choose the model that best fits your needs"
170
- )
171
-
172
- # Chat interface
173
- chatbot = gr.Chatbot(
174
- label="πŸ’¬ Chat History",
175
- height=500,
176
- bubble_full_width=False,
177
- show_copy_button=True
178
- )
179
-
180
- # Input area
181
- with gr.Row():
182
- msg_textbox = gr.Textbox(
183
- label="✍️ Your Message",
184
- placeholder="Ask me anything... Groq responses are incredibly fast!",
185
- scale=4,
186
- lines=2,
187
- max_lines=5
188
- )
189
- send_button = gr.Button("Send ⚑", variant="primary", scale=1)
190
-
191
- # Control buttons
192
- with gr.Row():
193
- clear_button = gr.Button("πŸ—‘οΈ Clear Chat", variant="secondary")
194
-
195
- # Chat logic
196
- def respond(message: str, chat_history: List[List[str]], model: str):
197
- if not message.strip():
198
- return "", chat_history
199
-
200
- # Convert gradio format to API format
201
- history_tuples = [(h[0], h[1]) for h in chat_history if len(h) >= 2]
202
-
203
- # Get AI response
204
- bot_message = query_groq(message.strip(), history_tuples, model)
205
-
206
- # Update chat history
207
- chat_history.append([message, bot_message])
208
- return "", chat_history
209
-
210
- # Event handlers
211
- msg_textbox.submit(
212
- respond,
213
- inputs=[msg_textbox, chatbot, model_dropdown],
214
- outputs=[msg_textbox, chatbot]
215
  )
216
- send_button.click(
217
- respond,
218
- inputs=[msg_textbox, chatbot, model_dropdown],
219
- outputs=[msg_textbox, chatbot]
220
- )
221
- clear_button.click(
222
- lambda: [],
223
- outputs=chatbot
224
  )
225
 
226
- # Example prompts
227
- gr.Examples(
228
- examples=[
229
- ["Hello! What makes Groq special?"],
230
- ["Explain quantum computing in simple terms"],
231
- ["Write a Python function to find prime numbers"],
232
- ["What's the difference between AI, ML, and Deep Learning?"],
233
- ["Help me debug this error: 'TypeError: 'str' object is not callable'"],
234
- ["Create a simple REST API with FastAPI"],
235
- ["Explain the concept of recursion with examples"],
236
- ["What are the latest trends in web development?"]
237
- ],
238
- inputs=msg_textbox,
239
- label="πŸ’‘ Try these examples:"
240
  )
 
 
 
241
 
242
- # Information accordion
243
- with gr.Accordion("ℹ️ About Groq & Models", open=False):
244
- gr.Markdown(f"""
245
- **πŸš€ Why Groq is Amazing:**
246
- - ⚑ **Fastest AI inference** in the world (500+ tokens/second)
247
- - πŸ†“ **Generous free tier** with high rate limits
248
- - πŸ”„ **Real-time responses** feel like magic
249
- - 🎯 **Consistent quality** across all models
250
-
251
- **πŸ€– Available Models:**
252
- {chr(10).join([f"- **{model}**: {desc}" for model, desc in MODELS.items()])}
253
-
254
- **πŸ”§ Technical Details:**
255
- - Powered by Groq's custom LPUβ„’ (Language Processing Unit)
256
- - Optimized for transformer model inference
257
- - Supports context windows up to 32K tokens
258
- - Enterprise-grade reliability and security
259
- """)
260
-
261
- # Footer
262
- gr.Markdown("""
263
- ---
264
- **πŸ”‘ Setup Instructions:**
265
- 1. Get free API key: [console.groq.com](https://console.groq.com)
266
- 2. Add to HF Spaces: Settings β†’ Repository secrets β†’ `GROQ_API_KEY`
267
- 3. Restart your Space and enjoy lightning-fast AI!
268
 
269
- **πŸ’‘ Tips:**
270
- - Groq excels at coding, explanations, and creative tasks
271
- - Try different models to see which fits your use case
272
- - The speed difference is immediately noticeable!
273
- """)
274
 
275
- return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
 
277
- # Create and launch the interface
278
  if __name__ == "__main__":
279
- print("πŸš€ Initializing Groq Chat Interface...")
280
- print(f"πŸ”‘ API Key Status: {'βœ… Ready' if API_KEY else '❌ Please add GROQ_API_KEY to secrets'}")
281
-
282
- demo = create_gradio_interface()
283
- demo.launch(
284
- server_name="0.0.0.0",
285
- server_port=7860,
286
- show_error=True,
287
- show_api=False, # Hide API docs for cleaner interface
288
- favicon_path=None
289
- )
 
1
  import gradio as gr
2
  import requests
3
  import os
4
+ import json
5
 
6
  # Groq API Configuration
7
  API_URL = "https://api.groq.com/openai/v1/chat/completions"
8
+ API_KEY = os.getenv("GROQ_API_KEY")
9
 
10
+ print(f"πŸ”‘ API Key Found: {'Yes' if API_KEY else 'No'}")
 
 
 
11
 
12
+ def simple_groq_test(message: str) -> str:
13
+ """Simple Groq API test without chat history"""
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
 
15
  if not API_KEY:
16
+ return "❌ No API Key found"
17
+
18
+ if not message.strip():
19
+ return "❌ Empty message"
 
 
 
 
 
 
 
 
20
 
21
  try:
22
+ headers = {
23
+ "Authorization": f"Bearer {API_KEY}",
24
+ "Content-Type": "application/json"
25
+ }
26
+
27
+ payload = {
28
+ "model": "llama3-8b-8192",
29
+ "messages": [
30
+ {"role": "user", "content": message.strip()}
31
+ ],
32
+ "max_tokens": 512,
33
+ "temperature": 0.7
34
+ }
35
 
36
+ print(f"πŸš€ Sending request to: {API_URL}")
37
+ print(f"πŸ“ Payload: {json.dumps(payload, indent=2)}")
 
 
 
 
 
38
 
 
 
 
 
39
  response = requests.post(
40
  API_URL,
41
  headers=headers,
42
+ json=payload,
 
 
 
 
 
 
 
43
  timeout=30
44
  )
45
 
46
+ print(f"πŸ“Š Status Code: {response.status_code}")
47
+ print(f"πŸ“‹ Response Headers: {dict(response.headers)}")
48
+ print(f"πŸ“„ Response Text (first 300 chars): {response.text[:300]}")
 
 
 
 
 
 
 
 
 
 
49
 
50
+ if response.status_code == 200:
51
+ result = response.json()
52
+ if "choices" in result and len(result["choices"]) > 0:
53
+ return result["choices"][0]["message"]["content"]
54
+ else:
55
+ return f"❌ No choices in response: {json.dumps(result, indent=2)}"
56
  else:
57
+ return f"❌ HTTP {response.status_code}: {response.text}"
58
+
59
  except requests.exceptions.Timeout:
60
+ return "❌ Request timeout (30s)"
61
  except requests.exceptions.ConnectionError:
62
+ return "❌ Connection error - cannot reach Groq API"
63
+ except json.JSONDecodeError:
64
+ return f"❌ Invalid JSON response: {response.text}"
65
  except Exception as e:
66
+ return f"❌ Unexpected error: {str(e)}"
67
 
68
+ def manual_curl_example():
69
+ """Generate curl command for manual testing"""
70
  if not API_KEY:
71
+ return "❌ No API key to generate curl"
72
 
73
+ curl_cmd = f'''curl -X POST "{API_URL}" \\
74
+ -H "Authorization: Bearer {API_KEY}" \\
75
+ -H "Content-Type: application/json" \\
76
+ -d '{{
77
+ "model": "llama3-8b-8192",
78
+ "messages": [
79
+ {{"role": "user", "content": "Hello, test message"}}
80
+ ],
81
+ "max_tokens": 100
82
+ }}'
83
+ '''
84
+ return curl_cmd
 
 
 
 
 
 
85
 
86
+ # Create simple interface
87
+ with gr.Blocks(title="Groq Debug") as demo:
88
+ gr.Markdown("# πŸ” Groq API Debug Tool")
 
 
 
 
 
 
 
 
 
 
 
89
 
90
+ with gr.Tab("Simple Test"):
91
+ gr.Markdown("Test single message without chat history")
 
 
 
92
 
93
+ test_input = gr.Textbox(
94
+ label="Test Message",
95
+ placeholder="Enter a simple test message",
96
+ value="Hello, how are you?"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  )
98
+ test_button = gr.Button("Test API", variant="primary")
99
+ test_output = gr.Textbox(
100
+ label="Response",
101
+ lines=10,
102
+ max_lines=20
 
 
 
103
  )
104
 
105
+ test_button.click(
106
+ simple_groq_test,
107
+ inputs=[test_input],
108
+ outputs=[test_output]
 
 
 
 
 
 
 
 
 
 
109
  )
110
+
111
+ with gr.Tab("Manual Test"):
112
+ gr.Markdown("Copy this curl command and run it in terminal")
113
 
114
+ curl_button = gr.Button("Generate Curl Command")
115
+ curl_output = gr.Code(
116
+ label="Curl Command",
117
+ language="bash"
118
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
 
120
+ curl_button.click(
121
+ manual_curl_example,
122
+ outputs=[curl_output]
123
+ )
 
124
 
125
+ with gr.Tab("API Info"):
126
+ gr.Markdown(f"""
127
+ **Debug Information:**
128
+ - API URL: `{API_URL}`
129
+ - API Key Status: {'βœ… Found' if API_KEY else '❌ Missing'}
130
+ - API Key Preview: `{API_KEY[:10] + '...' if API_KEY else 'None'}`
131
+
132
+ **Common Issues:**
133
+ 1. **Wrong URL** - Make sure using correct Groq endpoint
134
+ 2. **Invalid Model** - Check if model name is correct
135
+ 3. **Malformed Request** - Check JSON structure
136
+ 4. **Rate Limiting** - Wait between requests
137
+ 5. **Network Issues** - Check HF Spaces connectivity
138
+
139
+ **Expected Models:**
140
+ - `llama3-8b-8192`
141
+ - `llama3-70b-8192`
142
+ - `mixtral-8x7b-32768`
143
+ - `gemma-7b-it`
144
+ """)
145
 
 
146
  if __name__ == "__main__":
147
+ demo.launch(server_name="0.0.0.0", server_port=7860)