Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| import json | |
| from typing import List, Dict | |
| from datetime import datetime | |
| # Groq API Configuration | |
| API_URL = "https://api.groq.com/openai/v1/chat/completions" | |
| API_KEY = os.getenv("GROQ_API_KEY") | |
| print(f"π API Key Found: {'Yes' if API_KEY else 'No'}") | |
| # In-memory chat history storage | |
| chat_history: List[Dict[str, str]] = [] | |
| def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> str: | |
| """Groq API call with chat history and optional summarization""" | |
| if not API_KEY: | |
| return "β No API Key found" | |
| if not message.strip(): | |
| return "β Empty message" | |
| try: | |
| headers = { | |
| "Authorization": f"Bearer {API_KEY}", | |
| "Content-Type": "application/json" | |
| } | |
| # Add current message to history with timestamp and topic | |
| chat_history.append({ | |
| "role": "user", | |
| "content": message.strip(), | |
| "topic": topic, | |
| "timestamp": datetime.now().isoformat() | |
| }) | |
| # Limit history to max_history messages to avoid token overflow | |
| recent_history = chat_history[-max_history:] | |
| # Prepare messages for API call, excluding timestamp and topic | |
| messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history] | |
| if summarize: | |
| # Create a prompt to summarize the conversation | |
| summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n" | |
| for msg in recent_history: | |
| if msg["topic"] == topic: | |
| summary_prompt += f"{msg['role'].capitalize()}: {msg['content']}\n" | |
| summary_prompt += "\nProvide a concise summary of the key points discussed." | |
| messages.append({"role": "user", "content": summary_prompt}) | |
| payload = { | |
| "model": "llama3-8b-8192", | |
| "messages": messages, | |
| "max_tokens": 512, | |
| "temperature": 0.7 | |
| } | |
| print(f"π Sending request to: {API_URL}") | |
| print(f"π Payload: {json.dumps(payload, indent=2)}") | |
| response = requests.post( | |
| API_URL, | |
| headers=headers, | |
| json=payload, | |
| timeout=30 | |
| ) | |
| print(f"π Status Code: {response.status_code}") | |
| print(f"π Response Headers: {dict(response.headers)}") | |
| print(f"π Response Text (first 300 chars): {response.text[:300]}") | |
| if response.status_code == 200: | |
| result = response.json() | |
| if "choices" in result and len(result["choices"]) > 0: | |
| response_content = result["choices"][0]["message"]["content"] | |
| # Add assistant's response to history | |
| chat_history.append({ | |
| "role": "assistant", | |
| "content": response_content, | |
| "topic": topic, | |
| "timestamp": datetime.now().isoformat() | |
| }) | |
| return response_content | |
| else: | |
| return f"β No choices in response: {json.dumps(result, indent=2)}" | |
| else: | |
| return f"β HTTP {response.status_code}: {response.text}" | |
| except requests.exceptions.Timeout: | |
| return "β Request timeout (30s)" | |
| except requests.exceptions.ConnectionError: | |
| return "β Connection error - cannot reach Groq API" | |
| except json.JSONDecodeError: | |
| return f"β Invalid JSON response: {response.text}" | |
| except Exception as e: | |
| return f"β Unexpected error: {str(e)}" | |
| def clear_history(): | |
| """Clear the chat history""" | |
| chat_history.clear() | |
| return "β Chat history cleared" | |
| def view_history(topic: str = None): | |
| """View the chat history, optionally filtered by topic""" | |
| if not chat_history: | |
| return "β No chat history available" | |
| output = "π Chat History:\n\n" | |
| for msg in chat_history: | |
| if topic is None or msg["topic"] == topic: | |
| output += f"[{msg['timestamp']}] {msg['role'].capitalize()} ({msg['topic']}): {msg['content']}\n" | |
| return output | |
| def manual_curl_example(): | |
| """Generate curl command for manual testing""" | |
| if not API_KEY: | |
| return "β No API key to generate curl" | |
| curl_cmd = f'''curl -X POST "{API_URL}" \\ | |
| -H "Authorization: Bearer {API_KEY}" \\ | |
| -H "Content-Type: application/json" \\ | |
| -d '{{ | |
| "model": "llama3-8b-8192", | |
| "messages": [ | |
| {{"role": "user", "content": "Hello, test message"}} | |
| ], | |
| "max_tokens": 100 | |
| }}' | |
| ''' | |
| return curl_cmd | |
| # Create Gradio interface | |
| with gr.Blocks(title="Groq Debug with Memory") as demo: | |
| gr.Markdown("# π Groq API Debug Tool with Memory") | |
| with gr.Tab("Chat with Memory"): | |
| gr.Markdown("Chat with Groq, maintain history, and summarize by topic") | |
| chat_input = gr.Textbox( | |
| label="Message", | |
| placeholder="Enter your message", | |
| value="Hello, how can you help me today?" | |
| ) | |
| topic_input = gr.Textbox( | |
| label="Topic", | |
| placeholder="Enter topic (e.g., 'coding', 'general')", | |
| value="general" | |
| ) | |
| summarize_checkbox = gr.Checkbox( | |
| label="Summarize conversation for this topic", | |
| value=False | |
| ) | |
| chat_button = gr.Button("Send Message", variant="primary") | |
| chat_output = gr.Textbox( | |
| label="Response", | |
| lines=10, | |
| max_lines=20 | |
| ) | |
| chat_button.click( | |
| groq_with_memory, | |
| inputs=[chat_input, topic_input, summarize_checkbox], | |
| outputs=[chat_output] | |
| ) | |
| with gr.Tab("View History"): | |
| gr.Markdown("View or clear chat history") | |
| history_topic = gr.Textbox( | |
| label="Filter by Topic (optional)", | |
| placeholder="Enter topic to filter (leave blank for all)" | |
| ) | |
| history_button = gr.Button("View History") | |
| clear_button = gr.Button("Clear History", variant="secondary") | |
| history_output = gr.Textbox( | |
| label="Chat History", | |
| lines=10, | |
| max_lines=20 | |
| ) | |
| history_button.click( | |
| view_history, | |
| inputs=[history_topic], | |
| outputs=[history_output] | |
| ) | |
| clear_button.click( | |
| clear_history, | |
| outputs=[history_output] | |
| ) | |
| with gr.Tab("Manual Test"): | |
| gr.Markdown("Copy this curl command and run it in terminal") | |
| curl_button = gr.Button("Generate Curl Command") | |
| curl_output = gr.Code( | |
| label="Curl Command", | |
| language="shell" | |
| ) | |
| curl_button.click( | |
| manual_curl_example, | |
| outputs=[curl_output] | |
| ) | |
| with gr.Tab("API Info"): | |
| gr.Markdown(f""" | |
| **Debug Information:** | |
| - API URL: `{API_URL}` | |
| - API Key Status: {'β Found' if API_KEY else 'β Missing'} | |
| - API Key Preview: `{API_KEY[:10] + '...' if API_KEY else 'None'}` | |
| **Common Issues:** | |
| 1. **Wrong URL** - Make sure using correct Groq endpoint | |
| 2. **Invalid Model** - Check if model name is correct | |
| 3. **Malformed Request** - Check JSON structure | |
| 4. **Rate Limiting** - Wait between requests | |
| 5. **Network Issues** - Check HF Spaces connectivity | |
| **Expected Models:** | |
| - `llama3-8b-8192` | |
| - `llama3-70b-8192` | |
| - `mixtral-8x7b-32768` | |
| - `gemma-7b-it` | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch(server_name="0.0.0.0", server_port=7860) |