adikwok commited on
Commit
1c6ad60
Β·
verified Β·
1 Parent(s): d1c5d37

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -140
app.py CHANGED
@@ -37,19 +37,15 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
37
  "timestamp": datetime.now().isoformat()
38
  })
39
 
40
- # Limit history to max_history messages to avoid token overflow
41
  recent_history = chat_history[-max_history:]
42
 
43
- # Prepare messages for API call, excluding timestamp and topic
44
  messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
45
 
46
  if summarize:
47
- # Create a prompt to summarize the conversation
48
- summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n"
49
- for msg in recent_history:
50
- if msg["topic"] == topic:
51
- summary_prompt += f"{msg['role'].capitalize()}: {msg['content']}\n"
52
- summary_prompt += "\nProvide a concise summary of the key points discussed."
53
  messages.append({"role": "user", "content": summary_prompt})
54
 
55
  payload = {
@@ -59,25 +55,12 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
59
  "temperature": 0.7
60
  }
61
 
62
- print(f"πŸš€ Sending request to: {API_URL}")
63
- print(f"πŸ“ Payload: {json.dumps(payload, indent=2)}")
64
-
65
- response = requests.post(
66
- API_URL,
67
- headers=headers,
68
- json=payload,
69
- timeout=30
70
- )
71
-
72
- print(f"πŸ“Š Status Code: {response.status_code}")
73
- print(f"πŸ“‹ Response Headers: {dict(response.headers)}")
74
- print(f"πŸ“„ Response Text (first 300 chars): {response.text[:300]}")
75
 
76
  if response.status_code == 200:
77
  result = response.json()
78
- if "choices" in result and len(result["choices"]) > 0:
79
  response_content = result["choices"][0]["message"]["content"]
80
- # Add assistant's response to history
81
  chat_history.append({
82
  "role": "assistant",
83
  "content": response_content,
@@ -85,19 +68,11 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
85
  "timestamp": datetime.now().isoformat()
86
  })
87
  return response_content
88
- else:
89
- return f"❌ No choices in response: {json.dumps(result, indent=2)}"
90
- else:
91
- return f"❌ HTTP {response.status_code}: {response.text}"
92
 
93
- except requests.exceptions.Timeout:
94
- return "❌ Request timeout (30s)"
95
- except requests.exceptions.ConnectionError:
96
- return "❌ Connection error - cannot reach Groq API"
97
- except json.JSONDecodeError:
98
- return f"❌ Invalid JSON response: {response.text}"
99
  except Exception as e:
100
- return f"❌ Unexpected error: {str(e)}"
101
 
102
  def clear_history():
103
  """Clear the chat history"""
@@ -105,127 +80,81 @@ def clear_history():
105
  return "βœ… Chat history cleared"
106
 
107
  def view_history(topic: str = None):
108
- """View the chat history, optionally filtered by topic"""
109
  if not chat_history:
110
  return "❌ No chat history available"
111
 
112
- output = "πŸ“œ Chat History:\n\n"
113
- for msg in chat_history:
114
- if topic is None or msg["topic"] == topic:
115
- output += f"[{msg['timestamp']}] {msg['role'].capitalize()} ({msg['topic']}): {msg['content']}\n"
116
- return output
117
 
118
  def manual_curl_example():
119
- """Generate curl command for manual testing"""
120
  if not API_KEY:
121
- return "❌ No API key to generate curl"
122
-
123
- curl_cmd = f'''curl -X POST "{API_URL}" \\
124
  -H "Authorization: Bearer {API_KEY}" \\
125
  -H "Content-Type: application/json" \\
126
- -d '{{
127
- "model": "llama3-8b-8192",
128
- "messages": [
129
- {{"role": "user", "content": "Hello, test message"}}
130
- ],
131
- "max_tokens": 100
132
- }}'
133
- '''
134
- return curl_cmd
135
 
136
- # Create Gradio interface
137
- with gr.Blocks(title="Groq Debug with Memory") as demo:
138
- gr.Markdown("# πŸ” Groq API Debug Tool with Memory")
 
 
 
 
 
139
 
140
- with gr.Tab("Chat with Memory"):
141
- gr.Markdown("Chat with Groq, maintain history, and summarize by topic")
142
-
143
- chat_input = gr.Textbox(
144
- label="Message",
145
- placeholder="Enter your message",
146
- value="Hello, how can you help me today?"
147
- )
148
- topic_input = gr.Textbox(
149
- label="Topic",
150
- placeholder="Enter topic (e.g., 'coding', 'general')",
151
- value="general"
152
- )
153
- summarize_checkbox = gr.Checkbox(
154
- label="Summarize conversation for this topic",
155
- value=False
156
- )
157
- chat_button = gr.Button("Send Message", variant="primary")
158
- chat_output = gr.Textbox(
159
- label="Response",
160
- lines=10,
161
- max_lines=20
162
- )
163
-
164
- chat_button.click(
165
- groq_with_memory,
166
- inputs=[chat_input, topic_input, summarize_checkbox],
167
- outputs=[chat_output]
168
  )
169
 
170
- with gr.Tab("View History"):
171
- gr.Markdown("View or clear chat history")
172
-
173
- history_topic = gr.Textbox(
174
- label="Filter by Topic (optional)",
175
- placeholder="Enter topic to filter (leave blank for all)"
176
- )
177
- history_button = gr.Button("View History")
178
- clear_button = gr.Button("Clear History", variant="secondary")
179
- history_output = gr.Textbox(
180
- label="Chat History",
181
- lines=10,
182
- max_lines=20
183
- )
184
-
185
- history_button.click(
186
- view_history,
187
- inputs=[history_topic],
188
- outputs=[history_output]
189
- )
190
- clear_button.click(
191
- clear_history,
192
- outputs=[history_output]
193
- )
194
-
195
- with gr.Tab("Manual Test"):
196
- gr.Markdown("Copy this curl command and run it in terminal")
197
-
198
- curl_button = gr.Button("Generate Curl Command")
199
- curl_output = gr.Code(
200
- label="Curl Command",
201
- language="shell"
202
- )
203
-
204
- curl_button.click(
205
- manual_curl_example,
206
- outputs=[curl_output]
207
- )
208
 
209
- with gr.Tab("API Info"):
210
  gr.Markdown(f"""
211
- **Debug Information:**
212
- - API URL: `{API_URL}`
213
- - API Key Status: {'βœ… Found' if API_KEY else '❌ Missing'}
214
- - API Key Preview: `{API_KEY[:10] + '...' if API_KEY else 'None'}`
215
-
216
- **Common Issues:**
217
- 1. **Wrong URL** - Make sure using correct Groq endpoint
218
- 2. **Invalid Model** - Check if model name is correct
219
- 3. **Malformed Request** - Check JSON structure
220
- 4. **Rate Limiting** - Wait between requests
221
- 5. **Network Issues** - Check HF Spaces connectivity
222
-
223
- **Expected Models:**
224
- - `llama3-8b-8192`
225
- - `llama3-70b-8192`
226
- - `mixtral-8x7b-32768`
227
- - `gemma-7b-it`
228
  """)
 
 
 
 
229
 
230
  if __name__ == "__main__":
231
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
37
  "timestamp": datetime.now().isoformat()
38
  })
39
 
40
+ # Limit history to max_history messages
41
  recent_history = chat_history[-max_history:]
42
 
43
+ # Prepare messages for API call
44
  messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
45
 
46
  if summarize:
47
+ summary_prompt = f"Summarize this conversation about '{topic}':\n\n"
48
+ summary_prompt += "\n".join([f"{msg['role']}: {msg['content']}" for msg in recent_history])
 
 
 
 
49
  messages.append({"role": "user", "content": summary_prompt})
50
 
51
  payload = {
 
55
  "temperature": 0.7
56
  }
57
 
58
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
  if response.status_code == 200:
61
  result = response.json()
62
+ if "choices" in result and result["choices"]:
63
  response_content = result["choices"][0]["message"]["content"]
 
64
  chat_history.append({
65
  "role": "assistant",
66
  "content": response_content,
 
68
  "timestamp": datetime.now().isoformat()
69
  })
70
  return response_content
71
+ return f"❌ No choices in response: {result}"
72
+ return f"❌ HTTP {response.status_code}: {response.text}"
 
 
73
 
 
 
 
 
 
 
74
  except Exception as e:
75
+ return f"❌ Error: {str(e)}"
76
 
77
  def clear_history():
78
  """Clear the chat history"""
 
80
  return "βœ… Chat history cleared"
81
 
82
  def view_history(topic: str = None):
83
+ """View chat history with optional topic filter"""
84
  if not chat_history:
85
  return "❌ No chat history available"
86
 
87
+ filtered = [msg for msg in chat_history if topic is None or msg["topic"] == topic]
88
+ return "\n".join(
89
+ f"[{msg['timestamp']}] {msg['role'].capitalize()}: {msg['content']}"
90
+ for msg in filtered
91
+ )
92
 
93
  def manual_curl_example():
94
+ """Generate example curl command"""
95
  if not API_KEY:
96
+ return "❌ No API key available"
97
+ return f"""curl -X POST "{API_URL}" \\
 
98
  -H "Authorization: Bearer {API_KEY}" \\
99
  -H "Content-Type: application/json" \\
100
+ -d '{{"model": "llama3-8b-8192", "messages": [{{"role": "user", "content": "Hello"}}]}}'
101
+ """
 
 
 
 
 
 
 
102
 
103
+ def process_message(message: str, topic: str, summarize: bool):
104
+ """Process message and clear input"""
105
+ response = groq_with_memory(message, topic, summarize)
106
+ return response, ""
107
+
108
+ # Gradio Interface
109
+ with gr.Blocks(title="Groq Chat Debugger") as demo:
110
+ gr.Markdown("## πŸ’¬ Groq API Chat Interface")
111
 
112
+ with gr.Tab("Chat"):
113
+ with gr.Row():
114
+ with gr.Column():
115
+ msg_input = gr.Textbox(
116
+ label="Your Message",
117
+ value="Hello, how can you help me today?"
118
+ )
119
+ topic_input = gr.Textbox(
120
+ label="Topic",
121
+ value="general"
122
+ )
123
+ summarize_cb = gr.Checkbox(label="Summarize conversation")
124
+ send_btn = gr.Button("Send", variant="primary")
125
+
126
+ with gr.Column():
127
+ response_output = gr.Textbox(
128
+ label="Response",
129
+ lines=10,
130
+ interactive=False
131
+ )
132
+
133
+ send_btn.click(
134
+ process_message,
135
+ inputs=[msg_input, topic_input, summarize_cb],
136
+ outputs=[response_output, msg_input]
 
 
 
137
  )
138
 
139
+ with gr.Tab("History"):
140
+ history_topic = gr.Textbox(label="Filter by Topic")
141
+ view_btn = gr.Button("View History")
142
+ clear_btn = gr.Button("Clear History", variant="stop")
143
+ history_output = gr.Textbox(label="Chat History", lines=10)
144
+
145
+ view_btn.click(view_history, inputs=[history_topic], outputs=[history_output])
146
+ clear_btn.click(clear_history, outputs=[history_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
 
148
+ with gr.Tab("API Tools"):
149
  gr.Markdown(f"""
150
+ **API Status:** {'βœ… Ready' if API_KEY else '❌ No API Key'}
151
+ **Endpoint:** `{API_URL}`
152
+ **Models:** `llama3-8b-8192`, `llama3-70b-8192`, `mixtral-8x7b-32768`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  """)
154
+
155
+ curl_btn = gr.Button("Generate cURL Example")
156
+ curl_output = gr.Code(label="cURL Command", language="bash")
157
+ curl_btn.click(manual_curl_example, outputs=[curl_output])
158
 
159
  if __name__ == "__main__":
160
  demo.launch(server_name="0.0.0.0", server_port=7860)