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 | |
| recent_history = chat_history[-max_history:] | |
| # Prepare messages for API call | |
| messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history] | |
| if summarize: | |
| summary_prompt = f"Summarize this conversation about '{topic}':\n\n" | |
| summary_prompt += "\n".join([f"{msg['role']}: {msg['content']}" for msg in recent_history]) | |
| messages.append({"role": "user", "content": summary_prompt}) | |
| payload = { | |
| "model": "llama-3.3-70b-versatile", | |
| "messages": messages, | |
| "max_tokens": 5000, | |
| "temperature": 0.7 | |
| } | |
| response = requests.post(API_URL, headers=headers, json=payload, timeout=30) | |
| if response.status_code == 200: | |
| result = response.json() | |
| if "choices" in result and result["choices"]: | |
| response_content = result["choices"][0]["message"]["content"] | |
| chat_history.append({ | |
| "role": "assistant", | |
| "content": response_content, | |
| "topic": topic, | |
| "timestamp": datetime.now().isoformat() | |
| }) | |
| return response_content | |
| return f"β No choices in response: {result}" | |
| return f"β HTTP {response.status_code}: {response.text}" | |
| except Exception as e: | |
| return f"β Error: {str(e)}" | |
| def chat_with_groq(message: str, history: List[List[str]], topic: str = "general", summarize: bool = False) -> tuple: | |
| """ | |
| Function for Gradio Chatbot | |
| Returns: (updated_history, empty_string_for_textbox) | |
| """ | |
| if not message.strip(): | |
| return history, "" | |
| # Get response from Groq | |
| response = groq_with_memory(message, topic, summarize) | |
| # Add to gradio chat history format | |
| history.append([message, response]) | |
| return history, "" | |
| def clear_chat(): | |
| """Clear both chat histories""" | |
| chat_history.clear() | |
| return [], "β Chat history cleared" | |
| def view_history(topic: str = None): | |
| """View chat history with optional topic filter""" | |
| if not chat_history: | |
| return "β No chat history available" | |
| filtered = [msg for msg in chat_history if topic is None or msg.get("topic") == topic] | |
| return "\n".join( | |
| f"[{msg.get('timestamp', 'N/A')}] {msg['role'].capitalize()}: {msg['content']}" | |
| for msg in filtered | |
| ) | |
| def manual_curl_example(): | |
| """Generate example curl command""" | |
| if not API_KEY: | |
| return "β No API key available" | |
| return f"""curl -X POST "{API_URL}" \\ | |
| -H "Authorization: Bearer {API_KEY}" \\ | |
| -H "Content-Type: application/json" \\ | |
| -d '{{"model": "llama-3.3-70b-versatile", "messages": [{{"role": "user", "content": "Hello"}}], "max_tokens": 1000}}' | |
| """ | |
| # Gradio Interface | |
| with gr.Blocks(title="Groq Chat Debugger", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("## π¬ Groq API Chat Interface") | |
| with gr.Tab("π¬ Chat"): | |
| with gr.Row(): | |
| with gr.Column(scale=3): | |
| # Main chatbot interface | |
| chatbot = gr.Chatbot( | |
| label="Chat with Groq", | |
| height=500, | |
| show_copy_button=True | |
| ) | |
| with gr.Row(): | |
| msg_input = gr.Textbox( | |
| label="Your Message", | |
| placeholder="Ketik pesan Anda di sini...", | |
| scale=4 | |
| ) | |
| send_btn = gr.Button("Send π€", variant="primary", scale=1) | |
| with gr.Column(scale=1): | |
| # Settings panel | |
| gr.Markdown("### βοΈ Settings") | |
| topic_input = gr.Textbox( | |
| label="Topic", | |
| value="general", | |
| placeholder="general" | |
| ) | |
| summarize_cb = gr.Checkbox( | |
| label="π Summarize conversation", | |
| value=False | |
| ) | |
| clear_btn = gr.Button("ποΈ Clear Chat", variant="stop") | |
| # Stats | |
| gr.Markdown("### π Stats") | |
| stats_output = gr.Textbox( | |
| label="Chat Stats", | |
| value=f"API Status: {'β Ready' if API_KEY else 'β No Key'}", | |
| interactive=False, | |
| lines=3 | |
| ) | |
| # Event handlers | |
| send_btn.click( | |
| chat_with_groq, | |
| inputs=[msg_input, chatbot, topic_input, summarize_cb], | |
| outputs=[chatbot, msg_input] | |
| ) | |
| msg_input.submit( # Enter key support | |
| chat_with_groq, | |
| inputs=[msg_input, chatbot, topic_input, summarize_cb], | |
| outputs=[chatbot, msg_input] | |
| ) | |
| clear_btn.click( | |
| clear_chat, | |
| outputs=[chatbot, stats_output] | |
| ) | |
| with gr.Tab("π History"): | |
| gr.Markdown("### View and manage your chat history") | |
| with gr.Row(): | |
| history_topic = gr.Textbox( | |
| label="Filter by Topic", | |
| placeholder="Leave empty to see all" | |
| ) | |
| view_btn = gr.Button("π View History", variant="primary") | |
| history_output = gr.Textbox( | |
| label="Chat History", | |
| lines=15, | |
| interactive=False | |
| ) | |
| view_btn.click( | |
| view_history, | |
| inputs=[history_topic], | |
| outputs=[history_output] | |
| ) | |
| with gr.Tab("π§ API Tools"): | |
| gr.Markdown(f""" | |
| ### API Configuration | |
| **Status:** {'β Connected' if API_KEY else 'β No API Key Set'} | |
| **Endpoint:** `{API_URL}` | |
| **Available Models:** | |
| - `llama-3.3-70b-versatile` (Default) | |
| - `llama3-8b-8192` | |
| - `mixtral-8x7b-32768` | |
| """) | |
| curl_btn = gr.Button("π Generate cURL Example") | |
| curl_output = gr.Code( | |
| label="cURL Command", | |
| language="shell", | |
| lines=8 | |
| ) | |
| curl_btn.click(manual_curl_example, outputs=[curl_output]) | |
| if not API_KEY: | |
| gr.Markdown(""" | |
| ### β οΈ Setup Instructions | |
| 1. Get your API key from [Groq Console](https://console.groq.com/) | |
| 2. Set environment variable: `export GROQ_API_KEY="your-key-here"` | |
| 3. Restart the application | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False # Set True if you want public link | |
| ) |