abubakaraabi786's picture
Update App.py Version 5
ac13784 verified
raw
history blame
5.25 kB
import gradio as gr
import os
import requests
import time
# Configuration
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
MODEL = "llama-3.2-3b-preview" # Fastest free model
def ask_groq(message, history):
"""Simple function to query GROQ"""
if not GROQ_API_KEY:
return "❌ **ERROR**: Please add GROQ_API_KEY in Hugging Face Secrets (Settings β†’ Secrets)"
# Prepare request
headers = {
"Authorization": f"Bearer {GROQ_API_KEY}",
"Content-Type": "application/json"
}
# Build conversation history
messages = [
{"role": "system", "content": "You are a helpful programming tutor. Keep answers concise (2-3 sentences)."}
]
# Add conversation history (last 2 exchanges max for speed)
for human, assistant in history[-2:]:
messages.append({"role": "user", "content": human})
messages.append({"role": "assistant", "content": assistant})
# Add current message
messages.append({"role": "user", "content": message})
data = {
"model": MODEL,
"messages": messages,
"temperature": 0.7,
"max_tokens": 200, # Short for faster responses
"top_p": 0.9
}
try:
response = requests.post(
"https://api.groq.com/openai/v1/chat/completions",
headers=headers,
json=data,
timeout=20 # 20 second timeout
)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
elif response.status_code == 429:
return "⏰ **RATE LIMITED**: Free tier allows ~1 request/minute. Wait 60 seconds."
elif response.status_code == 404:
return f"❌ **MODEL ERROR**: '{MODEL}' not found. Try 'mixtral-8x7b-32768' instead."
else:
return f"❌ **API ERROR {response.status_code}**: {response.text[:100]}..."
except requests.exceptions.Timeout:
return "⏱️ **TIMEOUT**: GROQ is busy. Try shorter questions."
except requests.exceptions.ConnectionError:
return "🌐 **CONNECTION ERROR**: Check your internet or API key."
except Exception as e:
return f"⚠️ **ERROR**: {str(e)[:80]}"
# Create the Gradio interface
with gr.Blocks(theme=gr.themes.Soft(), title="⚑ Programming Tutor - GROQ") as demo:
gr.Markdown("""
# πŸ‘¨β€πŸ’» Programming Tutor Chatbot
**Powered by GROQ API (Free Tier)**
⚠️ **NOTE**: Free tier has rate limits. If stuck, wait 60 seconds between requests.
""")
# Create chatbot interface
chatbot = gr.Chatbot(
label="Chat History",
height=400
)
# Create message input
msg = gr.Textbox(
placeholder="Ask a programming question... (Keep it short for faster responses)",
label="Your Question",
lines=2
)
# Create state for chat history
chat_state = gr.State([])
# Function to handle message submission
def respond(message, history):
if not message.strip():
return "", history
# Get response from GROQ
bot_reply = ask_groq(message, history)
# Add to history
history.append((message, bot_reply))
return "", history
# Clear function
def clear_chat():
return [], []
# Create buttons in a row
with gr.Row():
send_btn = gr.Button("πŸš€ Send Message", variant="primary")
clear_btn = gr.Button("πŸ—‘οΈ Clear Chat", variant="secondary")
# Create example questions in a separate row
gr.Markdown("### πŸ’‘ Quick Example Questions:")
with gr.Row():
examples = [
"Hello! Are you working?",
"Print 'Hello World' in Python",
"What is a function?",
"How to fix syntax errors?"
]
for example in examples:
btn = gr.Button(example, size="sm")
# When clicked, set the msg textbox to this example
btn.click(
fn=lambda x=example: x,
inputs=[],
outputs=msg
)
# Set up event handlers
msg.submit(
fn=respond,
inputs=[msg, chat_state],
outputs=[msg, chatbot]
)
send_btn.click(
fn=respond,
inputs=[msg, chat_state],
outputs=[msg, chatbot]
)
clear_btn.click(
fn=clear_chat,
inputs=[],
outputs=[chatbot, chat_state]
)
# Footer with instructions
gr.Markdown("""
---
### πŸ”§ **Troubleshooting Guide**
**If you see errors or long delays:**
1. **Wait 60 seconds** between requests (free tier limit)
2. **Keep questions short** (under 15 words)
3. **Test with "Hello"** first
4. **Check API key** in Hugging Face Secrets
**Working questions to test:**
- "Say hello"
- "What is Python?"
- "How to declare a variable?"
**Model**: `llama-3.2-3b-preview` (Fastest free model)
**Timeout**: 20 seconds
""")
# Launch the app
if __name__ == "__main__":
demo.launch(debug=False, server_name="0.0.0.0", server_port=7860)