| import gradio as gr |
| import os |
| import requests |
| import time |
|
|
| |
| GROQ_API_KEY = os.environ.get("GROQ_API_KEY") |
| MODEL = "llama-3.2-3b-preview" |
|
|
| 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)" |
| |
| |
| headers = { |
| "Authorization": f"Bearer {GROQ_API_KEY}", |
| "Content-Type": "application/json" |
| } |
| |
| |
| messages = [ |
| {"role": "system", "content": "You are a helpful programming tutor. Keep answers concise (2-3 sentences)."} |
| ] |
| |
| |
| for human, assistant in history[-2:]: |
| messages.append({"role": "user", "content": human}) |
| messages.append({"role": "assistant", "content": assistant}) |
| |
| |
| messages.append({"role": "user", "content": message}) |
| |
| data = { |
| "model": MODEL, |
| "messages": messages, |
| "temperature": 0.7, |
| "max_tokens": 200, |
| "top_p": 0.9 |
| } |
| |
| try: |
| response = requests.post( |
| "https://api.groq.com/openai/v1/chat/completions", |
| headers=headers, |
| json=data, |
| timeout=20 |
| ) |
| |
| 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]}" |
|
|
| |
| 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. |
| """) |
| |
| |
| chatbot = gr.Chatbot( |
| label="Chat History", |
| height=400 |
| ) |
| |
| |
| msg = gr.Textbox( |
| placeholder="Ask a programming question... (Keep it short for faster responses)", |
| label="Your Question", |
| lines=2 |
| ) |
| |
| |
| chat_state = gr.State([]) |
| |
| |
| def respond(message, history): |
| if not message.strip(): |
| return "", history |
| |
| |
| bot_reply = ask_groq(message, history) |
| |
| |
| history.append((message, bot_reply)) |
| |
| return "", history |
| |
| |
| def clear_chat(): |
| return [], [] |
| |
| |
| with gr.Row(): |
| send_btn = gr.Button("π Send Message", variant="primary") |
| clear_btn = gr.Button("ποΈ Clear Chat", variant="secondary") |
| |
| |
| 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") |
| |
| btn.click( |
| fn=lambda x=example: x, |
| inputs=[], |
| outputs=msg |
| ) |
| |
| |
| 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] |
| ) |
| |
| |
| 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 |
| """) |
|
|
| |
| if __name__ == "__main__": |
| |
| demo.launch(debug=False, server_name="0.0.0.0", server_port=7860) |