Update App.py Version 6

#17
by abubakaraabi786 - opened
Files changed (1) hide show
  1. app.py +86 -96
app.py CHANGED
@@ -3,174 +3,164 @@ 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)
 
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()