Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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 |
-
|
| 48 |
-
summary_prompt
|
| 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 |
-
|
| 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
|
| 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 |
-
|
| 89 |
-
|
| 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"β
|
| 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
|
| 109 |
if not chat_history:
|
| 110 |
return "β No chat history available"
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
|
| 118 |
def manual_curl_example():
|
| 119 |
-
"""Generate curl command
|
| 120 |
if not API_KEY:
|
| 121 |
-
return "β No API key
|
| 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 |
-
|
| 128 |
-
"messages": [
|
| 129 |
-
{{"role": "user", "content": "Hello, test message"}}
|
| 130 |
-
],
|
| 131 |
-
"max_tokens": 100
|
| 132 |
-
}}'
|
| 133 |
-
'''
|
| 134 |
-
return curl_cmd
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
with gr.Tab("Chat
|
| 141 |
-
gr.
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
groq_with_memory,
|
| 166 |
-
inputs=[chat_input, topic_input, summarize_checkbox],
|
| 167 |
-
outputs=[chat_output]
|
| 168 |
)
|
| 169 |
|
| 170 |
-
with gr.Tab("
|
| 171 |
-
gr.
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
)
|
| 177 |
-
|
| 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
|
| 210 |
gr.Markdown(f"""
|
| 211 |
-
**
|
| 212 |
-
|
| 213 |
-
-
|
| 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)
|