Update app.py Version 6
#18
by
abubakaraabi786
- opened
app.py
CHANGED
|
@@ -7,11 +7,28 @@ import time
|
|
| 7 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 8 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
if not GROQ_API_KEY:
|
| 14 |
-
return "β **ERROR**: GROQ_API_KEY not
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
headers = {
|
| 17 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
@@ -20,11 +37,14 @@ def query_groq_api(message, chat_history):
|
|
| 20 |
|
| 21 |
# Build messages array
|
| 22 |
messages = [
|
| 23 |
-
{
|
|
|
|
|
|
|
|
|
|
| 24 |
]
|
| 25 |
|
| 26 |
-
# Add conversation history
|
| 27 |
-
for user_msg, bot_msg in chat_history:
|
| 28 |
messages.append({"role": "user", "content": user_msg})
|
| 29 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 30 |
|
|
@@ -33,7 +53,7 @@ def query_groq_api(message, chat_history):
|
|
| 33 |
|
| 34 |
# Request data
|
| 35 |
data = {
|
| 36 |
-
"model":
|
| 37 |
"messages": messages,
|
| 38 |
"temperature": 0.7,
|
| 39 |
"max_tokens": 300,
|
|
@@ -51,24 +71,25 @@ def query_groq_api(message, chat_history):
|
|
| 51 |
|
| 52 |
if response.status_code == 200:
|
| 53 |
result = response.json()
|
| 54 |
-
|
|
|
|
| 55 |
elif response.status_code == 429:
|
| 56 |
return "β° **RATE LIMITED**: Please wait 60 seconds before trying again."
|
| 57 |
else:
|
| 58 |
-
return f"β API Error {response.status_code}
|
| 59 |
|
| 60 |
except requests.exceptions.Timeout:
|
| 61 |
-
return "β±οΈ **TIMEOUT**: Request took too long. Try
|
| 62 |
except Exception as e:
|
| 63 |
-
return f"β οΈ Error
|
| 64 |
|
| 65 |
-
def chat_response(message, history):
|
| 66 |
"""Main chat function"""
|
| 67 |
if not message.strip():
|
| 68 |
return "", history
|
| 69 |
|
| 70 |
# Get response from GROQ
|
| 71 |
-
bot_message = query_groq_api(message, history)
|
| 72 |
|
| 73 |
# Add to history
|
| 74 |
history.append((message, bot_message))
|
|
@@ -78,72 +99,98 @@ def chat_response(message, history):
|
|
| 78 |
def clear_chat():
|
| 79 |
return []
|
| 80 |
|
|
|
|
|
|
|
|
|
|
| 81 |
# Create Gradio interface
|
| 82 |
with gr.Blocks(title="Programming Tutor - GROQ", theme=gr.themes.Soft()) as demo:
|
| 83 |
|
| 84 |
gr.Markdown("""
|
| 85 |
# π¨βπ» Programming Tutor Chatbot
|
| 86 |
-
**
|
| 87 |
""")
|
| 88 |
|
| 89 |
# Chatbot display
|
| 90 |
chatbot = gr.Chatbot(height=400, label="Conversation")
|
| 91 |
|
| 92 |
-
# Message input
|
| 93 |
-
msg = gr.Textbox(
|
| 94 |
-
placeholder="Ask a programming question...",
|
| 95 |
-
label="Your Question",
|
| 96 |
-
lines=2
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
# Store chat history
|
| 100 |
chat_state = gr.State([])
|
| 101 |
|
| 102 |
-
# Buttons
|
| 103 |
with gr.Row():
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
"How to write a for loop?",
|
| 114 |
-
"What is a function?"
|
| 115 |
-
]
|
| 116 |
-
|
| 117 |
-
with gr.Row():
|
| 118 |
-
for example in examples:
|
| 119 |
-
example_btn = gr.Button(example, size="sm")
|
| 120 |
-
# When clicked, set the message box to example text
|
| 121 |
-
example_btn.click(
|
| 122 |
-
lambda x=example: x,
|
| 123 |
-
inputs=None,
|
| 124 |
-
outputs=msg
|
| 125 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
#
|
| 128 |
-
def respond(message, history):
|
| 129 |
if not message.strip():
|
| 130 |
return "", history
|
| 131 |
|
| 132 |
-
|
| 133 |
-
bot_reply = query_groq_api(message, history)
|
| 134 |
history.append((message, bot_reply))
|
| 135 |
return "", history
|
| 136 |
|
| 137 |
-
#
|
| 138 |
msg.submit(
|
| 139 |
respond,
|
| 140 |
-
inputs=[msg, chat_state],
|
| 141 |
outputs=[msg, chatbot]
|
| 142 |
)
|
| 143 |
|
| 144 |
-
|
| 145 |
respond,
|
| 146 |
-
inputs=[msg, chat_state],
|
| 147 |
outputs=[msg, chatbot]
|
| 148 |
)
|
| 149 |
|
|
@@ -153,14 +200,29 @@ with gr.Blocks(title="Programming Tutor - GROQ", theme=gr.themes.Soft()) as demo
|
|
| 153 |
outputs=[chatbot, chat_state]
|
| 154 |
)
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
# Footer
|
| 157 |
-
gr.Markdown("""
|
| 158 |
---
|
| 159 |
-
### βΉοΈ About
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
""")
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
| 166 |
-
demo.launch()
|
|
|
|
| 7 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 8 |
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 9 |
|
| 10 |
+
# β
YOUR AVAILABLE MODELS (from your account)
|
| 11 |
+
AVAILABLE_MODELS = {
|
| 12 |
+
"Llama 3.1 (8B) - Fast": "llama-3.1-8b-instant",
|
| 13 |
+
"Llama 3.3 (70B) - Powerful": "llama-3.3-70b-versatile",
|
| 14 |
+
"Qwen 3 (32B)": "qwen/qwen3-32b",
|
| 15 |
+
"Llama 4 Maverick (17B)": "meta-llama/llama-4-maverick-17b-128e-instruct",
|
| 16 |
+
"Llama 4 Scout (17B)": "meta-llama/llama-4-scout-17b-16e-instruct",
|
| 17 |
+
"GPT OSS (20B)": "openai/gpt-oss-20b",
|
| 18 |
+
"GPT OSS (120B)": "openai/gpt-oss-120b"
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Default model that should definitely work
|
| 22 |
+
DEFAULT_MODEL = "llama-3.1-8b-instant"
|
| 23 |
+
|
| 24 |
+
def query_groq_api(message, chat_history, model_name):
|
| 25 |
+
"""Call GROQ API with your available models"""
|
| 26 |
|
| 27 |
if not GROQ_API_KEY:
|
| 28 |
+
return "β **ERROR**: GROQ_API_KEY not found. Add it in Hugging Face Secrets."
|
| 29 |
+
|
| 30 |
+
# Get actual model ID from friendly name
|
| 31 |
+
actual_model = AVAILABLE_MODELS.get(model_name, DEFAULT_MODEL)
|
| 32 |
|
| 33 |
headers = {
|
| 34 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
|
|
|
| 37 |
|
| 38 |
# Build messages array
|
| 39 |
messages = [
|
| 40 |
+
{
|
| 41 |
+
"role": "system",
|
| 42 |
+
"content": "You are CodeMentor, a helpful programming tutor. Keep answers clear and concise (2-3 sentences). Always format code examples in markdown code blocks."
|
| 43 |
+
}
|
| 44 |
]
|
| 45 |
|
| 46 |
+
# Add conversation history (last 3 exchanges for context)
|
| 47 |
+
for user_msg, bot_msg in chat_history[-3:]:
|
| 48 |
messages.append({"role": "user", "content": user_msg})
|
| 49 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 50 |
|
|
|
|
| 53 |
|
| 54 |
# Request data
|
| 55 |
data = {
|
| 56 |
+
"model": actual_model,
|
| 57 |
"messages": messages,
|
| 58 |
"temperature": 0.7,
|
| 59 |
"max_tokens": 300,
|
|
|
|
| 71 |
|
| 72 |
if response.status_code == 200:
|
| 73 |
result = response.json()
|
| 74 |
+
reply = result["choices"][0]["message"]["content"]
|
| 75 |
+
return f"{reply}\n\n**π€ Model**: {model_name}"
|
| 76 |
elif response.status_code == 429:
|
| 77 |
return "β° **RATE LIMITED**: Please wait 60 seconds before trying again."
|
| 78 |
else:
|
| 79 |
+
return f"β **API Error {response.status_code}**: Try a different model from the dropdown."
|
| 80 |
|
| 81 |
except requests.exceptions.Timeout:
|
| 82 |
+
return "β±οΈ **TIMEOUT**: Request took too long. Try the 'Llama 3.1 (8B)' model."
|
| 83 |
except Exception as e:
|
| 84 |
+
return f"β οΈ **Error**: {str(e)[:100]}"
|
| 85 |
|
| 86 |
+
def chat_response(message, history, model):
|
| 87 |
"""Main chat function"""
|
| 88 |
if not message.strip():
|
| 89 |
return "", history
|
| 90 |
|
| 91 |
# Get response from GROQ
|
| 92 |
+
bot_message = query_groq_api(message, history, model)
|
| 93 |
|
| 94 |
# Add to history
|
| 95 |
history.append((message, bot_message))
|
|
|
|
| 99 |
def clear_chat():
|
| 100 |
return []
|
| 101 |
|
| 102 |
+
def reset_settings():
|
| 103 |
+
return ["Llama 3.1 (8B) - Fast", "What is Python?"]
|
| 104 |
+
|
| 105 |
# Create Gradio interface
|
| 106 |
with gr.Blocks(title="Programming Tutor - GROQ", theme=gr.themes.Soft()) as demo:
|
| 107 |
|
| 108 |
gr.Markdown("""
|
| 109 |
# π¨βπ» Programming Tutor Chatbot
|
| 110 |
+
**Using YOUR Available GROQ Models**
|
| 111 |
""")
|
| 112 |
|
| 113 |
# Chatbot display
|
| 114 |
chatbot = gr.Chatbot(height=400, label="Conversation")
|
| 115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
# Store chat history
|
| 117 |
chat_state = gr.State([])
|
| 118 |
|
|
|
|
| 119 |
with gr.Row():
|
| 120 |
+
with gr.Column(scale=1):
|
| 121 |
+
# Model selection dropdown
|
| 122 |
+
gr.Markdown("### βοΈ Settings")
|
| 123 |
+
|
| 124 |
+
model_dropdown = gr.Dropdown(
|
| 125 |
+
choices=list(AVAILABLE_MODELS.keys()),
|
| 126 |
+
value="Llama 3.1 (8B) - Fast",
|
| 127 |
+
label="Select Model",
|
| 128 |
+
info="Choose from your available models"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
+
|
| 131 |
+
# Quick actions
|
| 132 |
+
gr.Markdown("### β‘ Quick Actions")
|
| 133 |
+
with gr.Row():
|
| 134 |
+
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary")
|
| 135 |
+
reset_btn = gr.Button("π Reset", variant="secondary")
|
| 136 |
+
|
| 137 |
+
# Example questions
|
| 138 |
+
gr.Markdown("### π‘ Example Questions")
|
| 139 |
+
|
| 140 |
+
examples = [
|
| 141 |
+
"Hello! Who are you?",
|
| 142 |
+
"Explain variables in programming",
|
| 143 |
+
"Write a Python function to add two numbers",
|
| 144 |
+
"What are data types?"
|
| 145 |
+
]
|
| 146 |
+
|
| 147 |
+
example_output = gr.Textbox(label="Selected Question", interactive=False, visible=False)
|
| 148 |
+
|
| 149 |
+
for example in examples:
|
| 150 |
+
btn = gr.Button(example, size="sm")
|
| 151 |
+
btn.click(
|
| 152 |
+
lambda x=example: x,
|
| 153 |
+
inputs=None,
|
| 154 |
+
outputs=example_output
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
with gr.Column(scale=2):
|
| 158 |
+
# Message input
|
| 159 |
+
msg = gr.Textbox(
|
| 160 |
+
placeholder="Type your programming question here...",
|
| 161 |
+
label="Your Question",
|
| 162 |
+
lines=3
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# Send button
|
| 166 |
+
send_btn = gr.Button("π Send Message", variant="primary", size="lg")
|
| 167 |
+
|
| 168 |
+
# Update message box when example is clicked
|
| 169 |
+
example_output.change(
|
| 170 |
+
lambda x: x,
|
| 171 |
+
inputs=example_output,
|
| 172 |
+
outputs=msg
|
| 173 |
+
)
|
| 174 |
|
| 175 |
+
# Function to handle message submission
|
| 176 |
+
def respond(message, history, model):
|
| 177 |
if not message.strip():
|
| 178 |
return "", history
|
| 179 |
|
| 180 |
+
bot_reply = query_groq_api(message, history, model)
|
|
|
|
| 181 |
history.append((message, bot_reply))
|
| 182 |
return "", history
|
| 183 |
|
| 184 |
+
# Set up event handlers
|
| 185 |
msg.submit(
|
| 186 |
respond,
|
| 187 |
+
inputs=[msg, chat_state, model_dropdown],
|
| 188 |
outputs=[msg, chatbot]
|
| 189 |
)
|
| 190 |
|
| 191 |
+
send_btn.click(
|
| 192 |
respond,
|
| 193 |
+
inputs=[msg, chat_state, model_dropdown],
|
| 194 |
outputs=[msg, chatbot]
|
| 195 |
)
|
| 196 |
|
|
|
|
| 200 |
outputs=[chatbot, chat_state]
|
| 201 |
)
|
| 202 |
|
| 203 |
+
reset_btn.click(
|
| 204 |
+
reset_settings,
|
| 205 |
+
inputs=None,
|
| 206 |
+
outputs=[model_dropdown, msg]
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
# Footer
|
| 210 |
+
gr.Markdown(f"""
|
| 211 |
---
|
| 212 |
+
### βΉοΈ About Your Available Models
|
| 213 |
+
|
| 214 |
+
**Fast Models (Try These First):**
|
| 215 |
+
1. **Llama 3.1 (8B)** - Fastest, good for simple questions
|
| 216 |
+
2. **Qwen 3 (32B)** - Balanced speed and quality
|
| 217 |
+
3. **Llama 4 Maverick (17B)** - Good for code examples
|
| 218 |
+
|
| 219 |
+
**Powerful Models (Slower):**
|
| 220 |
+
1. **Llama 3.3 (70B)** - Most powerful but slower
|
| 221 |
+
2. **GPT OSS (120B)** - Very powerful, may be slow
|
| 222 |
+
|
| 223 |
+
**Rate Limits:** 30 requests/minute for most models
|
| 224 |
+
**Tip:** Start with "Llama 3.1 (8B)" for fastest responses
|
| 225 |
""")
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
| 228 |
+
demo.launch(debug=False, server_name="0.0.0.0", server_port=7860)
|