api-deepseek / app.py
adikwok's picture
Update app.py
f213ab0 verified
raw
history blame
8.89 kB
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 clear_history():
"""Clear the chat history"""
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['timestamp']}] {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}}'
"""
def process_message(message: str, topic: str, summarize: bool):
"""Process message and clear input"""
if not message.strip():
return "❌ Please enter a message", ""
response = groq_with_memory(message, topic, summarize)
return response, ""
# Gradio Interface
with gr.Blocks(title="Groq Chat Interface", theme=gr.themes.Soft()) as demo:
gr.Markdown("## πŸ’¬ Groq API Chat Interface")
with gr.Tab("πŸ’¬ Chat"):
# Response area at top
response_output = gr.Textbox(
label="πŸ€– AI Response",
lines=15,
interactive=False,
placeholder="AI responses will appear here...",
show_copy_button=True
)
gr.Markdown("---")
# Input area at bottom
with gr.Row():
with gr.Column(scale=3):
msg_input = gr.Textbox(
label="✍️ Your Message",
placeholder="Type your message here...",
lines=3,
max_lines=10
)
with gr.Column(scale=1):
topic_input = gr.Textbox(
label="🏷️ Topic",
value="general",
placeholder="conversation topic"
)
summarize_cb = gr.Checkbox(
label="πŸ“‹ Summarize conversation",
value=False
)
with gr.Row():
send_btn = gr.Button("πŸ“€ Send Message", variant="primary", scale=2)
clear_btn = gr.Button("πŸ—‘οΈ Clear Response", variant="secondary", scale=1)
# Event handlers
send_btn.click(
process_message,
inputs=[msg_input, topic_input, summarize_cb],
outputs=[response_output, msg_input]
)
# Enter key support (Shift+Enter for new line, Enter to send)
msg_input.submit(
process_message,
inputs=[msg_input, topic_input, summarize_cb],
outputs=[response_output, msg_input]
)
clear_btn.click(
lambda: ("", ""),
outputs=[response_output, msg_input]
)
with gr.Tab("πŸ“œ History"):
gr.Markdown("### Chat History Management")
with gr.Row():
history_topic = gr.Textbox(
label="πŸ” Filter by Topic",
placeholder="Leave empty to see all conversations"
)
view_btn = gr.Button("πŸ‘€ View History", variant="primary")
clear_history_btn = gr.Button("πŸ—‘οΈ Clear All History", variant="stop")
history_output = gr.Textbox(
label="πŸ“š Chat History",
lines=20,
interactive=False,
show_copy_button=True
)
view_btn.click(view_history, inputs=[history_topic], outputs=[history_output])
clear_history_btn.click(clear_history, outputs=[history_output, response_output])
with gr.Tab("πŸ”§ API Tools"):
gr.Markdown("### API Configuration & Testing")
# API Status
api_status = gr.Markdown(f"""
**πŸ”Œ API Status:** {'βœ… Ready' if API_KEY else '❌ No API Key Set'}
**🌐 Endpoint:** `{API_URL}`
**πŸ€– Available Models:**
- `llama-3.3-70b-versatile` (Default - Most Capable)
- `llama3-8b-8192` (Fast)
- `llama3-70b-8192` (Balanced)
- `mixtral-8x7b-32768` (Long Context)
""")
# cURL Generator
gr.Markdown("### πŸ“‹ Generate cURL Command")
curl_btn = gr.Button("πŸ“‹ Generate cURL Example", variant="primary")
curl_output = gr.Code(
label="cURL Command",
language="bash",
lines=8,
show_copy_button=True
)
curl_btn.click(manual_curl_example, outputs=[curl_output])
# Setup Instructions
if not API_KEY:
gr.Markdown("""
### ⚠️ Setup Required
**To use this interface:**
1. πŸ”‘ Get your API key from [Groq Console](https://console.groq.com/)
2. πŸ’» Set environment variable:
```bash
export GROQ_API_KEY="your-groq-api-key-here"
```
3. πŸ”„ Restart the application
**Alternative:** Set in your system environment or `.env` file
""")
else:
gr.Markdown("""
### βœ… Ready to Chat!
**Tips for better conversations:**
- 🎯 Use specific topics to organize conversations
- πŸ“‹ Enable summarization for long conversations
- πŸ”„ Check history to review past interactions
- πŸ’‘ Try different models in the API for various needs
""")
# Launch message
print("πŸš€ Starting Groq Chat Interface...")
print(f"🌐 Access at: http://localhost:7860")
if API_KEY:
print("βœ… API Key detected - Ready to chat!")
else:
print("⚠️ No API Key found - Please set GROQ_API_KEY environment variable")
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
show_error=True
)