Update App.py Version 3
#7
by
abubakaraabi786
- opened
app.py
CHANGED
|
@@ -3,16 +3,20 @@ import os
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
from typing import List, Tuple
|
|
|
|
| 6 |
|
| 7 |
# Load GROQ API key from environment (set it in Hugging Face secrets)
|
| 8 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 9 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
MODELS = {
|
| 13 |
-
"Llama 3 (
|
| 14 |
-
"Llama 3 (
|
| 15 |
-
"
|
|
|
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
# π― Customize this system prompt based on your bot's role
|
|
@@ -53,9 +57,12 @@ def query_groq_api(message: str, chat_history: List[Tuple[str, str]], model: str
|
|
| 53 |
# Add current message
|
| 54 |
messages.append({"role": "user", "content": message})
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
# Prepare payload
|
| 57 |
payload = {
|
| 58 |
-
"model":
|
| 59 |
"messages": messages,
|
| 60 |
"temperature": temperature,
|
| 61 |
"max_tokens": max_tokens,
|
|
@@ -64,14 +71,19 @@ def query_groq_api(message: str, chat_history: List[Tuple[str, str]], model: str
|
|
| 64 |
}
|
| 65 |
|
| 66 |
try:
|
| 67 |
-
response = requests.post(GROQ_API_URL, headers=headers, json=payload)
|
| 68 |
|
| 69 |
if response.status_code == 200:
|
| 70 |
data = response.json()
|
| 71 |
return data["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
| 72 |
else:
|
| 73 |
return f"β Error {response.status_code}: {response.text}"
|
| 74 |
|
|
|
|
|
|
|
| 75 |
except requests.exceptions.RequestException as e:
|
| 76 |
return f"π« Connection error: {str(e)}"
|
| 77 |
except Exception as e:
|
|
@@ -83,11 +95,15 @@ def respond(message: str, chat_history: List[Tuple[str, str]], model: str, tempe
|
|
| 83 |
if not message.strip():
|
| 84 |
return "", chat_history
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# Get bot response
|
| 87 |
-
bot_reply = query_groq_api(message, chat_history, model, temperature, max_tokens)
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
chat_history
|
| 91 |
|
| 92 |
return "", chat_history
|
| 93 |
|
|
@@ -143,19 +159,21 @@ with gr.Blocks(theme=gr.themes.Soft(), title="CodeMentor - Programming Tutor") a
|
|
| 143 |
- Best practices and design patterns
|
| 144 |
|
| 145 |
Select your preferences below and start asking questions!
|
|
|
|
|
|
|
| 146 |
""")
|
| 147 |
|
| 148 |
with gr.Row():
|
| 149 |
with gr.Column(scale=1):
|
| 150 |
-
# UI Improvements
|
| 151 |
gr.Markdown("### βοΈ Settings")
|
| 152 |
|
| 153 |
# Model selection dropdown
|
| 154 |
model_dropdown = gr.Dropdown(
|
| 155 |
choices=list(MODELS.keys()),
|
| 156 |
-
value="Llama 3 (
|
| 157 |
label="Select AI Model",
|
| 158 |
-
info="
|
| 159 |
)
|
| 160 |
|
| 161 |
# Programming language selection
|
|
@@ -186,7 +204,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="CodeMentor - Programming Tutor") a
|
|
| 186 |
info="Maximum length of responses"
|
| 187 |
)
|
| 188 |
|
| 189 |
-
# Example questions dropdown
|
| 190 |
gr.Markdown("### π‘ Example Questions")
|
| 191 |
example_dropdown = gr.Dropdown(
|
| 192 |
choices=[
|
|
@@ -223,7 +241,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="CodeMentor - Programming Tutor") a
|
|
| 223 |
)
|
| 224 |
|
| 225 |
with gr.Row():
|
| 226 |
-
# Send button
|
| 227 |
send_btn = gr.Button("π Send", variant="primary")
|
| 228 |
stop_btn = gr.Button("βΉοΈ Stop", variant="stop")
|
| 229 |
|
|
@@ -264,7 +281,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="CodeMentor - Programming Tutor") a
|
|
| 264 |
# Handle reset button
|
| 265 |
def reset_settings():
|
| 266 |
return [
|
| 267 |
-
"Llama 3 (
|
| 268 |
"Python", # language_dropdown
|
| 269 |
0.7, # temperature_slider
|
| 270 |
500, # max_tokens_slider
|
|
@@ -277,25 +294,27 @@ with gr.Blocks(theme=gr.themes.Soft(), title="CodeMentor - Programming Tutor") a
|
|
| 277 |
outputs=[model_dropdown, language_dropdown, temperature_slider, max_tokens_slider, example_dropdown]
|
| 278 |
)
|
| 279 |
|
| 280 |
-
#
|
| 281 |
-
stop_btn.click(
|
| 282 |
-
fn=None,
|
| 283 |
-
inputs=None,
|
| 284 |
-
outputs=None,
|
| 285 |
-
cancels=[]
|
| 286 |
-
)
|
| 287 |
-
|
| 288 |
-
# Footer
|
| 289 |
gr.Markdown("""
|
| 290 |
---
|
| 291 |
-
### βΉοΈ About
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
-
|
| 298 |
""")
|
| 299 |
|
| 300 |
if __name__ == "__main__":
|
| 301 |
-
demo.launch(debug=False)
|
|
|
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
from typing import List, Tuple
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
# Load GROQ API key from environment (set it in Hugging Face secrets)
|
| 9 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 10 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 11 |
|
| 12 |
+
# β
UPDATED: Correct GROQ model names (as of Dec 2024)
|
| 13 |
MODELS = {
|
| 14 |
+
"Llama 3.2 (3B) - Fast": "llama-3.2-3b-preview",
|
| 15 |
+
"Llama 3.2 (1B) - Light": "llama-3.2-1b-preview",
|
| 16 |
+
"Llama 3.2 (90B Text) - Powerful": "llama-3.2-90b-text-preview",
|
| 17 |
+
"Llama 3.2 (11B Text) - Balanced": "llama-3.2-11b-text-preview",
|
| 18 |
+
"Mixtral (8x7B)": "mixtral-8x7b-32768",
|
| 19 |
+
"Gemma 2 (9B)": "gemma2-9b-it"
|
| 20 |
}
|
| 21 |
|
| 22 |
# π― Customize this system prompt based on your bot's role
|
|
|
|
| 57 |
# Add current message
|
| 58 |
messages.append({"role": "user", "content": message})
|
| 59 |
|
| 60 |
+
# β
Get the actual model name from the dictionary
|
| 61 |
+
actual_model = MODELS.get(model, "llama-3.2-3b-preview")
|
| 62 |
+
|
| 63 |
# Prepare payload
|
| 64 |
payload = {
|
| 65 |
+
"model": actual_model,
|
| 66 |
"messages": messages,
|
| 67 |
"temperature": temperature,
|
| 68 |
"max_tokens": max_tokens,
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
try:
|
| 74 |
+
response = requests.post(GROQ_API_URL, headers=headers, json=payload, timeout=30)
|
| 75 |
|
| 76 |
if response.status_code == 200:
|
| 77 |
data = response.json()
|
| 78 |
return data["choices"][0]["message"]["content"]
|
| 79 |
+
elif response.status_code == 404:
|
| 80 |
+
# β
Specific error for model not found
|
| 81 |
+
return f"β Error: Model '{actual_model}' not found. Available models are: {', '.join(MODELS.values())}"
|
| 82 |
else:
|
| 83 |
return f"β Error {response.status_code}: {response.text}"
|
| 84 |
|
| 85 |
+
except requests.exceptions.Timeout:
|
| 86 |
+
return "β° Request timeout. Please try again."
|
| 87 |
except requests.exceptions.RequestException as e:
|
| 88 |
return f"π« Connection error: {str(e)}"
|
| 89 |
except Exception as e:
|
|
|
|
| 95 |
if not message.strip():
|
| 96 |
return "", chat_history
|
| 97 |
|
| 98 |
+
# Show typing indicator
|
| 99 |
+
chat_history.append((message, "π€ Thinking..."))
|
| 100 |
+
yield "", chat_history
|
| 101 |
+
|
| 102 |
# Get bot response
|
| 103 |
+
bot_reply = query_groq_api(message, chat_history[:-1], model, temperature, max_tokens)
|
| 104 |
|
| 105 |
+
# Replace typing indicator with actual response
|
| 106 |
+
chat_history[-1] = (message, bot_reply)
|
| 107 |
|
| 108 |
return "", chat_history
|
| 109 |
|
|
|
|
| 159 |
- Best practices and design patterns
|
| 160 |
|
| 161 |
Select your preferences below and start asking questions!
|
| 162 |
+
|
| 163 |
+
β οΈ **Note**: Using GROQ API with free tier (limited requests per minute)
|
| 164 |
""")
|
| 165 |
|
| 166 |
with gr.Row():
|
| 167 |
with gr.Column(scale=1):
|
| 168 |
+
# UI Improvements
|
| 169 |
gr.Markdown("### βοΈ Settings")
|
| 170 |
|
| 171 |
# Model selection dropdown
|
| 172 |
model_dropdown = gr.Dropdown(
|
| 173 |
choices=list(MODELS.keys()),
|
| 174 |
+
value="Llama 3.2 (3B) - Fast",
|
| 175 |
label="Select AI Model",
|
| 176 |
+
info="β
Updated with correct GROQ model names"
|
| 177 |
)
|
| 178 |
|
| 179 |
# Programming language selection
|
|
|
|
| 204 |
info="Maximum length of responses"
|
| 205 |
)
|
| 206 |
|
| 207 |
+
# Example questions dropdown
|
| 208 |
gr.Markdown("### π‘ Example Questions")
|
| 209 |
example_dropdown = gr.Dropdown(
|
| 210 |
choices=[
|
|
|
|
| 241 |
)
|
| 242 |
|
| 243 |
with gr.Row():
|
|
|
|
| 244 |
send_btn = gr.Button("π Send", variant="primary")
|
| 245 |
stop_btn = gr.Button("βΉοΈ Stop", variant="stop")
|
| 246 |
|
|
|
|
| 281 |
# Handle reset button
|
| 282 |
def reset_settings():
|
| 283 |
return [
|
| 284 |
+
"Llama 3.2 (3B) - Fast", # model_dropdown
|
| 285 |
"Python", # language_dropdown
|
| 286 |
0.7, # temperature_slider
|
| 287 |
500, # max_tokens_slider
|
|
|
|
| 294 |
outputs=[model_dropdown, language_dropdown, temperature_slider, max_tokens_slider, example_dropdown]
|
| 295 |
)
|
| 296 |
|
| 297 |
+
# Footer with troubleshooting info
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
gr.Markdown("""
|
| 299 |
---
|
| 300 |
+
### βΉοΈ About & Troubleshooting
|
| 301 |
+
|
| 302 |
+
**Powered by**: GROQ API
|
| 303 |
+
**Current Models Available**:
|
| 304 |
+
- `llama-3.2-3b-preview` (Fast, 3B parameters)
|
| 305 |
+
- `llama-3.2-1b-preview` (Lightweight, 1B)
|
| 306 |
+
- `llama-3.2-90b-text-preview` (Most powerful, 90B)
|
| 307 |
+
- `llama-3.2-11b-text-preview` (Balanced, 11B)
|
| 308 |
+
- `mixtral-8x7b-32768` (Mixture of experts)
|
| 309 |
+
- `gemma2-9b-it` (Google's model)
|
| 310 |
+
|
| 311 |
+
**If you see "model not found" error**:
|
| 312 |
+
1. Check GROQ Console for available models
|
| 313 |
+
2. Ensure your API key has access to the selected model
|
| 314 |
+
3. Try a different model from the dropdown
|
| 315 |
|
| 316 |
+
**Note**: Free tier has rate limits. If requests fail, wait 1 minute and try again.
|
| 317 |
""")
|
| 318 |
|
| 319 |
if __name__ == "__main__":
|
| 320 |
+
demo.launch(debug=False, server_name="0.0.0.0", server_port=7860)
|