Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,289 +1,147 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
-
|
| 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 |
-
|
| 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 |
-
|
| 16 |
-
"
|
| 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 "
|
| 34 |
-
|
| 35 |
-
|
| 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 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 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 |
-
|
| 77 |
-
|
| 78 |
-
|
| 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 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
else:
|
| 97 |
-
return f"β
|
| 98 |
-
|
| 99 |
except requests.exceptions.Timeout:
|
| 100 |
-
return "β
|
| 101 |
except requests.exceptions.ConnectionError:
|
| 102 |
-
return "β
|
| 103 |
-
except
|
| 104 |
-
return f"β
|
| 105 |
except Exception as e:
|
| 106 |
-
return f"β
|
| 107 |
|
| 108 |
-
def
|
| 109 |
-
"""
|
| 110 |
if not API_KEY:
|
| 111 |
-
return "
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 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 |
-
|
| 133 |
-
|
| 134 |
-
|
| 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.
|
| 148 |
-
|
| 149 |
-
theme=gr.themes.Soft(primary_hue="blue"),
|
| 150 |
-
css=css
|
| 151 |
-
) as demo:
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 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 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
clear_button.click(
|
| 222 |
-
lambda: [],
|
| 223 |
-
outputs=chatbot
|
| 224 |
)
|
| 225 |
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 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 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 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 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
""")
|
| 274 |
|
| 275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 276 |
|
| 277 |
-
# Create and launch the interface
|
| 278 |
if __name__ == "__main__":
|
| 279 |
-
|
| 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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|