Update App.py Version 6
#17
by
abubakaraabi786
- opened
app.py
CHANGED
|
@@ -3,174 +3,164 @@ import os
|
|
| 3 |
import requests
|
| 4 |
import time
|
| 5 |
|
| 6 |
-
#
|
| 7 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 8 |
-
|
| 9 |
|
| 10 |
-
def
|
| 11 |
-
"""
|
| 12 |
|
| 13 |
if not GROQ_API_KEY:
|
| 14 |
-
return "β **ERROR**:
|
| 15 |
|
| 16 |
-
# Prepare request
|
| 17 |
headers = {
|
| 18 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 19 |
"Content-Type": "application/json"
|
| 20 |
}
|
| 21 |
|
| 22 |
-
# Build
|
| 23 |
messages = [
|
| 24 |
-
{"role": "system", "content": "You are a helpful programming tutor.
|
| 25 |
]
|
| 26 |
|
| 27 |
-
# Add conversation history
|
| 28 |
-
for
|
| 29 |
-
messages.append({"role": "user", "content":
|
| 30 |
-
messages.append({"role": "assistant", "content":
|
| 31 |
|
| 32 |
# Add current message
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
|
|
|
|
| 35 |
data = {
|
| 36 |
-
"model":
|
| 37 |
"messages": messages,
|
| 38 |
"temperature": 0.7,
|
| 39 |
-
"max_tokens":
|
| 40 |
"top_p": 0.9
|
| 41 |
}
|
| 42 |
|
| 43 |
try:
|
|
|
|
| 44 |
response = requests.post(
|
| 45 |
-
|
| 46 |
headers=headers,
|
| 47 |
json=data,
|
| 48 |
-
timeout=
|
| 49 |
)
|
| 50 |
|
| 51 |
if response.status_code == 200:
|
| 52 |
-
|
|
|
|
| 53 |
elif response.status_code == 429:
|
| 54 |
-
return "β° **RATE LIMITED**:
|
| 55 |
-
elif response.status_code == 404:
|
| 56 |
-
return f"β **MODEL ERROR**: '{MODEL}' not found. Try 'mixtral-8x7b-32768' instead."
|
| 57 |
else:
|
| 58 |
-
return f"β
|
| 59 |
-
|
| 60 |
except requests.exceptions.Timeout:
|
| 61 |
-
return "β±οΈ **TIMEOUT**:
|
| 62 |
-
except requests.exceptions.ConnectionError:
|
| 63 |
-
return "π **CONNECTION ERROR**: Check your internet or API key."
|
| 64 |
except Exception as e:
|
| 65 |
-
return f"β οΈ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
gr.Markdown("""
|
| 71 |
# π¨βπ» Programming Tutor Chatbot
|
| 72 |
-
**Powered by GROQ API
|
| 73 |
-
|
| 74 |
-
β οΈ **NOTE**: Free tier has rate limits. If stuck, wait 60 seconds between requests.
|
| 75 |
""")
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
chatbot = gr.Chatbot(
|
| 79 |
-
label="Chat History",
|
| 80 |
-
height=400
|
| 81 |
-
)
|
| 82 |
|
| 83 |
-
#
|
| 84 |
msg = gr.Textbox(
|
| 85 |
-
placeholder="Ask a programming question...
|
| 86 |
label="Your Question",
|
| 87 |
lines=2
|
| 88 |
)
|
| 89 |
|
| 90 |
-
#
|
| 91 |
chat_state = gr.State([])
|
| 92 |
|
| 93 |
-
#
|
| 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 |
-
|
| 113 |
-
clear_btn = gr.Button("ποΈ Clear
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 128 |
-
# When clicked, set the
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
inputs=
|
| 132 |
outputs=msg
|
| 133 |
)
|
| 134 |
|
| 135 |
# Set up event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
msg.submit(
|
| 137 |
-
|
| 138 |
inputs=[msg, chat_state],
|
| 139 |
outputs=[msg, chatbot]
|
| 140 |
)
|
| 141 |
|
| 142 |
-
|
| 143 |
-
|
| 144 |
inputs=[msg, chat_state],
|
| 145 |
outputs=[msg, chatbot]
|
| 146 |
)
|
| 147 |
|
| 148 |
clear_btn.click(
|
| 149 |
-
|
| 150 |
-
inputs=
|
| 151 |
outputs=[chatbot, chat_state]
|
| 152 |
)
|
| 153 |
|
| 154 |
-
# Footer
|
| 155 |
gr.Markdown("""
|
| 156 |
---
|
| 157 |
-
###
|
| 158 |
-
|
| 159 |
-
**
|
| 160 |
-
|
| 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(
|
|
|
|
| 3 |
import requests
|
| 4 |
import time
|
| 5 |
|
| 6 |
+
# Get API key from environment
|
| 7 |
GROQ_API_KEY = os.environ.get("GROQ_API_KEY")
|
| 8 |
+
GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 9 |
|
| 10 |
+
def query_groq_api(message, chat_history):
|
| 11 |
+
"""Actually call GROQ API"""
|
| 12 |
|
| 13 |
if not GROQ_API_KEY:
|
| 14 |
+
return "β **ERROR**: GROQ_API_KEY not set. Add it in Hugging Face Secrets."
|
| 15 |
|
|
|
|
| 16 |
headers = {
|
| 17 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 18 |
"Content-Type": "application/json"
|
| 19 |
}
|
| 20 |
|
| 21 |
+
# Build messages array
|
| 22 |
messages = [
|
| 23 |
+
{"role": "system", "content": "You are a helpful programming tutor. Answer in 2-3 sentences."}
|
| 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 |
|
| 31 |
# Add current message
|
| 32 |
messages.append({"role": "user", "content": message})
|
| 33 |
|
| 34 |
+
# Request data
|
| 35 |
data = {
|
| 36 |
+
"model": "llama-3.2-3b-preview", # Working model
|
| 37 |
"messages": messages,
|
| 38 |
"temperature": 0.7,
|
| 39 |
+
"max_tokens": 300,
|
| 40 |
"top_p": 0.9
|
| 41 |
}
|
| 42 |
|
| 43 |
try:
|
| 44 |
+
# Make API call
|
| 45 |
response = requests.post(
|
| 46 |
+
GROQ_API_URL,
|
| 47 |
headers=headers,
|
| 48 |
json=data,
|
| 49 |
+
timeout=15
|
| 50 |
)
|
| 51 |
|
| 52 |
if response.status_code == 200:
|
| 53 |
+
result = response.json()
|
| 54 |
+
return result["choices"][0]["message"]["content"]
|
| 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}: {response.text[:100]}"
|
| 59 |
+
|
| 60 |
except requests.exceptions.Timeout:
|
| 61 |
+
return "β±οΈ **TIMEOUT**: Request took too long. Try a shorter question."
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
+
return f"β οΈ Error: {str(e)}"
|
| 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))
|
| 75 |
+
|
| 76 |
+
return "", history
|
| 77 |
|
| 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 |
+
**Powered by GROQ LLM API**
|
|
|
|
|
|
|
| 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 |
+
submit_btn = gr.Button("π Send", variant="primary")
|
| 105 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
|
| 106 |
+
|
| 107 |
+
# Example questions
|
| 108 |
+
gr.Markdown("### π‘ Try these questions:")
|
| 109 |
|
| 110 |
+
examples = [
|
| 111 |
+
"Hello, who are you?",
|
| 112 |
+
"Explain Python variables",
|
| 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 |
# Set up event handlers
|
| 128 |
+
def respond(message, history):
|
| 129 |
+
if not message.strip():
|
| 130 |
+
return "", history
|
| 131 |
+
|
| 132 |
+
# Call GROQ API
|
| 133 |
+
bot_reply = query_groq_api(message, history)
|
| 134 |
+
history.append((message, bot_reply))
|
| 135 |
+
return "", history
|
| 136 |
+
|
| 137 |
+
# Connect inputs and outputs
|
| 138 |
msg.submit(
|
| 139 |
+
respond,
|
| 140 |
inputs=[msg, chat_state],
|
| 141 |
outputs=[msg, chatbot]
|
| 142 |
)
|
| 143 |
|
| 144 |
+
submit_btn.click(
|
| 145 |
+
respond,
|
| 146 |
inputs=[msg, chat_state],
|
| 147 |
outputs=[msg, chatbot]
|
| 148 |
)
|
| 149 |
|
| 150 |
clear_btn.click(
|
| 151 |
+
lambda: ([], []),
|
| 152 |
+
inputs=None,
|
| 153 |
outputs=[chatbot, chat_state]
|
| 154 |
)
|
| 155 |
|
| 156 |
+
# Footer
|
| 157 |
gr.Markdown("""
|
| 158 |
---
|
| 159 |
+
### βΉοΈ About
|
| 160 |
+
- **Model**: llama-3.2-3b-preview
|
| 161 |
+
- **API**: GROQ
|
| 162 |
+
- **Note**: Free tier has rate limits. Wait 1 minute if you get errors.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
""")
|
| 164 |
|
|
|
|
| 165 |
if __name__ == "__main__":
|
| 166 |
+
demo.launch()
|