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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
# Configuration
|
| 7 |
+
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 8 |
+
MODEL = "llama-3.2-3b-preview" # Fastest free model
|
| 9 |
|
| 10 |
+
def ask_groq(message, history):
|
| 11 |
+
"""Simple function to query GROQ"""
|
| 12 |
+
|
| 13 |
+
if not GROQ_API_KEY:
|
| 14 |
+
return "β **ERROR**: Please add GROQ_API_KEY in Hugging Face Secrets (Settings β Secrets)"
|
| 15 |
+
|
| 16 |
+
# Prepare request
|
| 17 |
+
headers = {
|
| 18 |
+
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 19 |
+
"Content-Type": "application/json"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# Build conversation history
|
| 23 |
+
messages = [
|
| 24 |
+
{"role": "system", "content": "You are a helpful programming tutor. Keep answers concise (2-3 sentences)."}
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
# Add conversation history (last 2 exchanges max for speed)
|
| 28 |
+
for human, assistant in history[-2:]:
|
| 29 |
+
messages.append({"role": "user", "content": human})
|
| 30 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 31 |
+
|
| 32 |
+
# Add current message
|
| 33 |
+
messages.append({"role": "user", "content": message})
|
| 34 |
+
|
| 35 |
+
data = {
|
| 36 |
+
"model": MODEL,
|
| 37 |
+
"messages": messages,
|
| 38 |
+
"temperature": 0.7,
|
| 39 |
+
"max_tokens": 200, # Short for faster responses
|
| 40 |
+
"top_p": 0.9
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
response = requests.post(
|
| 45 |
+
"https://api.groq.com/openai/v1/chat/completions",
|
| 46 |
+
headers=headers,
|
| 47 |
+
json=data,
|
| 48 |
+
timeout=20 # 20 second timeout
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if response.status_code == 200:
|
| 52 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 53 |
+
elif response.status_code == 429:
|
| 54 |
+
return "β° **RATE LIMITED**: Free tier allows ~1 request/minute. Wait 60 seconds."
|
| 55 |
+
elif response.status_code == 404:
|
| 56 |
+
return f"β **MODEL ERROR**: '{MODEL}' not found. Try 'mixtral-8x7b-32768' instead."
|
| 57 |
+
else:
|
| 58 |
+
return f"β **API ERROR {response.status_code}**: {response.text[:100]}..."
|
| 59 |
+
|
| 60 |
+
except requests.exceptions.Timeout:
|
| 61 |
+
return "β±οΈ **TIMEOUT**: GROQ is busy. Try shorter questions."
|
| 62 |
+
except requests.exceptions.ConnectionError:
|
| 63 |
+
return "π **CONNECTION ERROR**: Check your internet or API key."
|
| 64 |
+
except Exception as e:
|
| 65 |
+
return f"β οΈ **ERROR**: {str(e)[:80]}"
|
| 66 |
|
| 67 |
+
# Create the Gradio interface
|
| 68 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="β‘ Programming Tutor - GROQ") as demo:
|
| 69 |
+
|
| 70 |
+
gr.Markdown("""
|
| 71 |
+
# π¨βπ» Programming Tutor Chatbot
|
| 72 |
+
**Powered by GROQ API (Free Tier)**
|
| 73 |
+
|
| 74 |
+
β οΈ **NOTE**: Free tier has rate limits. If stuck, wait 60 seconds between requests.
|
| 75 |
+
""")
|
| 76 |
+
|
| 77 |
+
# Create chatbot interface
|
| 78 |
+
chatbot = gr.Chatbot(
|
| 79 |
+
label="Chat History",
|
| 80 |
+
height=400
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
# Create message input
|
| 84 |
+
msg = gr.Textbox(
|
| 85 |
+
placeholder="Ask a programming question... (Keep it short for faster responses)",
|
| 86 |
+
label="Your Question",
|
| 87 |
+
lines=2
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Create state for chat history
|
| 91 |
+
chat_state = gr.State([])
|
| 92 |
+
|
| 93 |
+
# Function to handle message submission
|
| 94 |
+
def respond(message, history):
|
| 95 |
+
if not message.strip():
|
| 96 |
+
return "", history
|
| 97 |
+
|
| 98 |
+
# Get response from GROQ
|
| 99 |
+
bot_reply = ask_groq(message, history)
|
| 100 |
+
|
| 101 |
+
# Add to history
|
| 102 |
+
history.append((message, bot_reply))
|
| 103 |
+
|
| 104 |
+
return "", history
|
| 105 |
+
|
| 106 |
+
# Clear function
|
| 107 |
+
def clear_chat():
|
| 108 |
+
return [], []
|
| 109 |
+
|
| 110 |
+
# Create buttons in a row
|
| 111 |
+
with gr.Row():
|
| 112 |
+
send_btn = gr.Button("π Send Message", variant="primary")
|
| 113 |
+
clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary")
|
| 114 |
+
|
| 115 |
+
# Create example questions in a separate row
|
| 116 |
+
gr.Markdown("### π‘ Quick Example Questions:")
|
| 117 |
+
|
| 118 |
+
with gr.Row():
|
| 119 |
+
examples = [
|
| 120 |
+
"Hello! Are you working?",
|
| 121 |
+
"Print 'Hello World' in Python",
|
| 122 |
+
"What is a function?",
|
| 123 |
+
"How to fix syntax errors?"
|
| 124 |
+
]
|
| 125 |
+
|
| 126 |
+
for example in examples:
|
| 127 |
+
btn = gr.Button(example, size="sm")
|
| 128 |
+
# When clicked, set the msg textbox to this example
|
| 129 |
+
btn.click(
|
| 130 |
+
fn=lambda x=example: x,
|
| 131 |
+
inputs=[],
|
| 132 |
+
outputs=msg
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
# Set up event handlers
|
| 136 |
+
msg.submit(
|
| 137 |
+
fn=respond,
|
| 138 |
+
inputs=[msg, chat_state],
|
| 139 |
+
outputs=[msg, chatbot]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
send_btn.click(
|
| 143 |
+
fn=respond,
|
| 144 |
+
inputs=[msg, chat_state],
|
| 145 |
+
outputs=[msg, chatbot]
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
clear_btn.click(
|
| 149 |
+
fn=clear_chat,
|
| 150 |
+
inputs=[],
|
| 151 |
+
outputs=[chatbot, chat_state]
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
# Footer with instructions
|
| 155 |
+
gr.Markdown("""
|
| 156 |
+
---
|
| 157 |
+
### π§ **Troubleshooting Guide**
|
| 158 |
+
|
| 159 |
+
**If you see errors or long delays:**
|
| 160 |
+
1. **Wait 60 seconds** between requests (free tier limit)
|
| 161 |
+
2. **Keep questions short** (under 15 words)
|
| 162 |
+
3. **Test with "Hello"** first
|
| 163 |
+
4. **Check API key** in Hugging Face Secrets
|
| 164 |
+
|
| 165 |
+
**Working questions to test:**
|
| 166 |
+
- "Say hello"
|
| 167 |
+
- "What is Python?"
|
| 168 |
+
- "How to declare a variable?"
|
| 169 |
+
|
| 170 |
+
**Model**: `llama-3.2-3b-preview` (Fastest free model)
|
| 171 |
+
**Timeout**: 20 seconds
|
| 172 |
+
""")
|
| 173 |
+
|
| 174 |
+
# Launch the app
|
| 175 |
+
if __name__ == "__main__":
|
| 176 |
+
demo.launch(debug=False, server_name="0.0.0.0", server_port=7860)
|