Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,168 +7,143 @@ from datetime import datetime
|
|
| 7 |
|
| 8 |
# Groq API Configuration
|
| 9 |
API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 10 |
-
API_KEY = os.getenv("GROQ_API_KEY")
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# In-memory chat history storage
|
| 15 |
chat_history: List[Dict[str, str]] = []
|
| 16 |
|
| 17 |
-
def groq_with_memory(message: str, topic: str = "general", summarize: bool = False
|
| 18 |
-
"""Groq API call with chat history
|
| 19 |
-
if not
|
| 20 |
-
return "β
|
| 21 |
|
| 22 |
if not message.strip():
|
| 23 |
-
return "β
|
| 24 |
|
| 25 |
try:
|
| 26 |
-
|
| 27 |
-
"Authorization": f"Bearer {API_KEY}",
|
| 28 |
-
"Content-Type": "application/json"
|
| 29 |
-
}
|
| 30 |
-
|
| 31 |
-
# Add current message to history
|
| 32 |
chat_history.append({
|
| 33 |
"role": "user",
|
| 34 |
-
"content": message
|
| 35 |
"topic": topic,
|
| 36 |
"timestamp": datetime.now().isoformat()
|
| 37 |
})
|
| 38 |
|
| 39 |
-
# Prepare messages
|
| 40 |
messages = [{"role": msg["role"], "content": msg["content"]}
|
| 41 |
-
for msg in chat_history[-
|
| 42 |
|
| 43 |
if summarize:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
payload = {
|
| 50 |
-
"model": "llama3-8b-8192",
|
| 51 |
-
"messages": messages,
|
| 52 |
-
"max_tokens": 512,
|
| 53 |
-
"temperature": 0.7
|
| 54 |
-
}
|
| 55 |
|
| 56 |
-
response = requests.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
if response.status_code == 200:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
return f"β No choices in response: {result}"
|
| 70 |
-
return f"β HTTP {response.status_code}: {response.text}"
|
| 71 |
-
|
| 72 |
except Exception as e:
|
| 73 |
-
return f"β
|
| 74 |
|
| 75 |
-
def
|
| 76 |
-
"""
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
def view_history(topic: str = None):
|
| 81 |
-
"""View chat history
|
| 82 |
if not chat_history:
|
| 83 |
-
return "
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
return "\n".join(
|
| 87 |
-
f"[{msg['timestamp']}] {msg['role'].capitalize()}: {msg['content']}"
|
| 88 |
-
for msg in filtered
|
| 89 |
-
)
|
| 90 |
|
| 91 |
-
def
|
| 92 |
-
"""
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
return f"""curl -X POST "{API_URL}" \\
|
| 96 |
-
-H "Authorization: Bearer {API_KEY}" \\
|
| 97 |
-
-H "Content-Type: application/json" \\
|
| 98 |
-
-d '{{"model": "llama3-8b-8192", "messages": [{{"role": "user", "content": "Hello"}}]}}'
|
| 99 |
-
"""
|
| 100 |
-
|
| 101 |
-
def process_chat(message: str, chat_history: list, topic: str, summarize: bool):
|
| 102 |
-
"""Process chat message and update history"""
|
| 103 |
-
response = groq_with_memory(message, topic, summarize)
|
| 104 |
-
chat_history.append((message, response))
|
| 105 |
-
return "", chat_history
|
| 106 |
|
| 107 |
-
# Gradio Interface
|
| 108 |
-
with gr.Blocks(title="Groq Chat
|
| 109 |
with gr.Tabs():
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
with gr.Row():
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
show_label=False,
|
| 116 |
-
container=False,
|
| 117 |
-
scale=7
|
| 118 |
-
)
|
| 119 |
-
with gr.Column(scale=1):
|
| 120 |
-
send_btn = gr.Button("Send", variant="primary")
|
| 121 |
-
clear_btn = gr.Button("Clear", variant="secondary")
|
| 122 |
|
| 123 |
-
with gr.Accordion("
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
value="general"
|
| 127 |
-
)
|
| 128 |
-
summarize_cb = gr.Checkbox(
|
| 129 |
-
label="Summarize conversation",
|
| 130 |
-
value=False
|
| 131 |
-
)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
[msg_input, chatbot, topic_input, summarize_cb],
|
| 137 |
-
[msg_input, chatbot]
|
| 138 |
-
)
|
| 139 |
-
send_btn.click(
|
| 140 |
-
process_chat,
|
| 141 |
-
[msg_input, chatbot, topic_input, summarize_cb],
|
| 142 |
-
[msg_input, chatbot]
|
| 143 |
-
)
|
| 144 |
-
clear_btn.click(lambda: [], None, chatbot)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
|
|
|
| 148 |
view_btn = gr.Button("View History")
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
view_btn.click(
|
| 153 |
-
view_history,
|
| 154 |
-
inputs=[history_topic],
|
| 155 |
-
outputs=[history_output]
|
| 156 |
-
)
|
| 157 |
-
clear_history_btn.click(
|
| 158 |
-
clear_history,
|
| 159 |
-
outputs=[history_output]
|
| 160 |
-
)
|
| 161 |
|
| 162 |
-
|
|
|
|
| 163 |
gr.Markdown(f"""
|
| 164 |
-
**API Status:** {'β
Ready' if
|
| 165 |
**Endpoint:** `{API_URL}`
|
| 166 |
-
**Models:**
|
| 167 |
-
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
if __name__ == "__main__":
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Groq API Configuration
|
| 9 |
API_URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 10 |
+
API_KEY = os.getenv("GROQ_API_KEY") or "your-api-key-here" # Fallback for testing
|
| 11 |
|
| 12 |
+
def verify_api_key():
|
| 13 |
+
if not API_KEY or API_KEY == "your-api-key-here":
|
| 14 |
+
print("β No valid API key found")
|
| 15 |
+
print("Please set GROQ_API_KEY environment variable")
|
| 16 |
+
return False
|
| 17 |
+
return True
|
| 18 |
|
| 19 |
# In-memory chat history storage
|
| 20 |
chat_history: List[Dict[str, str]] = []
|
| 21 |
|
| 22 |
+
def groq_with_memory(message: str, topic: str = "general", summarize: bool = False) -> str:
|
| 23 |
+
"""Groq API call with chat history"""
|
| 24 |
+
if not verify_api_key():
|
| 25 |
+
return "β API key not configured - please set GROQ_API_KEY environment variable"
|
| 26 |
|
| 27 |
if not message.strip():
|
| 28 |
+
return "β Please enter a message"
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
# Add to history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
chat_history.append({
|
| 33 |
"role": "user",
|
| 34 |
+
"content": message,
|
| 35 |
"topic": topic,
|
| 36 |
"timestamp": datetime.now().isoformat()
|
| 37 |
})
|
| 38 |
|
| 39 |
+
# Prepare messages (last 10)
|
| 40 |
messages = [{"role": msg["role"], "content": msg["content"]}
|
| 41 |
+
for msg in chat_history[-10:]]
|
| 42 |
|
| 43 |
if summarize:
|
| 44 |
+
messages.append({
|
| 45 |
+
"role": "user",
|
| 46 |
+
"content": f"Summarize our conversation about {topic}"
|
| 47 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
response = requests.post(
|
| 50 |
+
API_URL,
|
| 51 |
+
headers={
|
| 52 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 53 |
+
"Content-Type": "application/json"
|
| 54 |
+
},
|
| 55 |
+
json={
|
| 56 |
+
"model": "llama3-8b-8192",
|
| 57 |
+
"messages": messages,
|
| 58 |
+
"temperature": 0.7
|
| 59 |
+
},
|
| 60 |
+
timeout=30
|
| 61 |
+
)
|
| 62 |
|
| 63 |
if response.status_code == 200:
|
| 64 |
+
reply = response.json()["choices"][0]["message"]["content"]
|
| 65 |
+
chat_history.append({
|
| 66 |
+
"role": "assistant",
|
| 67 |
+
"content": reply,
|
| 68 |
+
"topic": topic,
|
| 69 |
+
"timestamp": datetime.now().isoformat()
|
| 70 |
+
})
|
| 71 |
+
return reply
|
| 72 |
+
return f"β Error {response.status_code}: {response.text}"
|
| 73 |
+
|
|
|
|
|
|
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
+
return f"β Connection error: {str(e)}"
|
| 76 |
|
| 77 |
+
def process_chat(message: str, chat_ui: list, topic: str) -> tuple:
|
| 78 |
+
"""Process chat message and update UI"""
|
| 79 |
+
if not message.strip():
|
| 80 |
+
return "", chat_ui
|
| 81 |
+
reply = groq_with_memory(message, topic)
|
| 82 |
+
return "", chat_ui + [(message, reply)]
|
| 83 |
|
| 84 |
+
def view_history(topic: str = None) -> str:
|
| 85 |
+
"""View raw chat history"""
|
| 86 |
if not chat_history:
|
| 87 |
+
return "No history yet"
|
| 88 |
+
filtered = [msg for msg in chat_history if not topic or msg["topic"] == topic]
|
| 89 |
+
return "\n".join(f"{msg['timestamp']} - {msg['role']}: {msg['content']}" for msg in filtered)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
def clear_all_history():
|
| 92 |
+
"""Clear all chat history"""
|
| 93 |
+
chat_history.clear()
|
| 94 |
+
return "β
History cleared", []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
+
# Gradio Interface
|
| 97 |
+
with gr.Blocks(title="Groq Chat", theme=gr.themes.Soft()) as demo:
|
| 98 |
with gr.Tabs():
|
| 99 |
+
# Chat Tab
|
| 100 |
+
with gr.Tab("π¬ Chat"):
|
| 101 |
+
chatbot = gr.Chatbot(height=400)
|
| 102 |
with gr.Row():
|
| 103 |
+
message = gr.Textbox(placeholder="Type your message...", scale=7)
|
| 104 |
+
send = gr.Button("Send", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
with gr.Accordion("Settings", open=False):
|
| 107 |
+
topic = gr.Textbox(label="Conversation Topic", value="general")
|
| 108 |
+
gr.Markdown("**Note:** History is preserved until you clear it")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
clear_chat = gr.Button("Clear Chat UI")
|
| 111 |
+
clear_history = gr.Button("β οΈ Clear ALL History", variant="stop")
|
| 112 |
+
status = gr.Textbox(label="Status")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
+
# History Tab
|
| 115 |
+
with gr.Tab("π History"):
|
| 116 |
+
history_topic = gr.Textbox(label="Filter by topic")
|
| 117 |
view_btn = gr.Button("View History")
|
| 118 |
+
raw_history = gr.Textbox(label="Raw History", lines=15)
|
| 119 |
+
view_btn.click(view_history, history_topic, raw_history)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
# API Info Tab
|
| 122 |
+
with gr.Tab("βΉοΈ API Info"):
|
| 123 |
gr.Markdown(f"""
|
| 124 |
+
**API Status:** {'β
Ready' if verify_api_key() else 'β Not Configured'}
|
| 125 |
**Endpoint:** `{API_URL}`
|
| 126 |
+
**Models Available:**
|
| 127 |
+
- `llama3-8b-8192`
|
| 128 |
+
- `llama3-70b-8192`
|
| 129 |
+
- `mixtral-8x7b-32768`
|
| 130 |
|
| 131 |
+
**How to set up:**
|
| 132 |
+
1. Get API key from [Groq Cloud](https://console.groq.com/)
|
| 133 |
+
2. Set environment variable:
|
| 134 |
+
```bash
|
| 135 |
+
export GROQ_API_KEY="your-api-key-here"
|
| 136 |
+
```
|
| 137 |
+
""")
|
| 138 |
+
|
| 139 |
+
# Event handlers
|
| 140 |
+
message.submit(process_chat, [message, chatbot, topic], [message, chatbot])
|
| 141 |
+
send.click(process_chat, [message, chatbot, topic], [message, chatbot])
|
| 142 |
+
clear_chat.click(lambda: [], None, chatbot)
|
| 143 |
+
clear_history.click(clear_all_history, outputs=[status, chatbot])
|
| 144 |
|
| 145 |
if __name__ == "__main__":
|
| 146 |
+
if verify_api_key():
|
| 147 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 148 |
+
else:
|
| 149 |
+
print("Cannot start app - no valid API key found")
|