Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,8 @@ from datetime import datetime
|
|
| 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 |
# In-memory chat history storage
|
| 14 |
chat_history: List[Dict[str, str]] = []
|
|
@@ -16,18 +17,19 @@ chat_history: List[Dict[str, str]] = []
|
|
| 16 |
def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> str:
|
| 17 |
"""Groq API call with chat history and optional summarization"""
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
if not message.strip():
|
| 20 |
-
return "Empty message"
|
| 21 |
|
| 22 |
try:
|
| 23 |
-
if not API_KEY:
|
| 24 |
-
return "No API Key found"
|
| 25 |
-
|
| 26 |
headers = {
|
| 27 |
"Authorization": f"Bearer {API_KEY}",
|
| 28 |
"Content-Type": "application/json"
|
| 29 |
}
|
| 30 |
|
|
|
|
| 31 |
chat_history.append({
|
| 32 |
"role": "user",
|
| 33 |
"content": message.strip(),
|
|
@@ -35,7 +37,10 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
|
|
| 35 |
"timestamp": datetime.now().isoformat()
|
| 36 |
})
|
| 37 |
|
|
|
|
| 38 |
recent_history = chat_history[-max_history:]
|
|
|
|
|
|
|
| 39 |
messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
|
| 40 |
|
| 41 |
if summarize:
|
|
@@ -44,7 +49,7 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
|
|
| 44 |
messages.append({"role": "user", "content": summary_prompt})
|
| 45 |
|
| 46 |
payload = {
|
| 47 |
-
"model": "
|
| 48 |
"messages": messages,
|
| 49 |
"max_tokens": 5000,
|
| 50 |
"temperature": 0.7
|
|
@@ -63,86 +68,168 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
|
|
| 63 |
"timestamp": datetime.now().isoformat()
|
| 64 |
})
|
| 65 |
return response_content
|
| 66 |
-
return f"No choices in response: {result}"
|
| 67 |
-
return f"HTTP {response.status_code}: {response.text}"
|
| 68 |
-
|
| 69 |
except Exception as e:
|
| 70 |
-
return f"Error: {str(e)}"
|
| 71 |
|
| 72 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
chat_history.clear()
|
| 74 |
-
return [], "Chat history cleared"
|
| 75 |
|
| 76 |
def view_history(topic: str = None):
|
|
|
|
| 77 |
if not chat_history:
|
| 78 |
-
return "No chat history available"
|
| 79 |
|
| 80 |
-
filtered = [msg for msg in chat_history if topic is None or msg
|
| 81 |
return "\n".join(
|
| 82 |
-
f"[{msg
|
| 83 |
for msg in filtered
|
| 84 |
)
|
| 85 |
|
| 86 |
def manual_curl_example():
|
|
|
|
| 87 |
if not API_KEY:
|
| 88 |
-
return "No API key available"
|
| 89 |
return f"""curl -X POST "{API_URL}" \\
|
| 90 |
-H "Authorization: Bearer {API_KEY}" \\
|
| 91 |
-H "Content-Type: application/json" \\
|
| 92 |
-
-d '{{"model": "
|
| 93 |
"""
|
| 94 |
|
| 95 |
-
def process_message(message: str, topic: str, summarize: bool, chatbot):
|
| 96 |
-
if not API_KEY:
|
| 97 |
-
chatbot.append(("user", message))
|
| 98 |
-
chatbot.append(("assistant", "No API Key found."))
|
| 99 |
-
return chatbot, ""
|
| 100 |
-
|
| 101 |
-
response = groq_with_memory(message, topic, summarize)
|
| 102 |
-
chatbot.append(("user", message))
|
| 103 |
-
chatbot.append(("assistant", response))
|
| 104 |
-
return chatbot, ""
|
| 105 |
-
|
| 106 |
# Gradio Interface
|
| 107 |
-
with gr.Blocks(title="Groq Chat
|
| 108 |
-
gr.Markdown("## Groq Chat
|
| 109 |
-
|
| 110 |
-
with gr.Tab("Chat"):
|
| 111 |
-
chatbot = gr.Chatbot(label="Conversation")
|
| 112 |
-
|
| 113 |
-
with gr.Row():
|
| 114 |
-
msg_input = gr.Textbox(placeholder="Tulis pesan di sini...", label="Your Message", lines=2)
|
| 115 |
-
send_btn = gr.Button("Send", variant="primary")
|
| 116 |
-
|
| 117 |
with gr.Row():
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
|
|
|
| 121 |
send_btn.click(
|
| 122 |
-
|
| 123 |
-
inputs=[msg_input, topic_input, summarize_cb
|
| 124 |
outputs=[chatbot, msg_input]
|
| 125 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
with gr.Tab("History"):
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
-
view_btn.click(
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
with gr.Tab("API Tools"):
|
| 137 |
gr.Markdown(f"""
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
""")
|
| 142 |
|
| 143 |
-
curl_btn = gr.Button("Generate cURL Example")
|
| 144 |
-
curl_output = gr.Code(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
curl_btn.click(manual_curl_example, outputs=[curl_output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
print(f"π API Key Found: {'Yes' if API_KEY else 'No'}")
|
| 13 |
|
| 14 |
# In-memory chat history storage
|
| 15 |
chat_history: List[Dict[str, str]] = []
|
|
|
|
| 17 |
def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> str:
|
| 18 |
"""Groq API call with chat history and optional summarization"""
|
| 19 |
|
| 20 |
+
if not API_KEY:
|
| 21 |
+
return "β No API Key found"
|
| 22 |
+
|
| 23 |
if not message.strip():
|
| 24 |
+
return "β Empty message"
|
| 25 |
|
| 26 |
try:
|
|
|
|
|
|
|
|
|
|
| 27 |
headers = {
|
| 28 |
"Authorization": f"Bearer {API_KEY}",
|
| 29 |
"Content-Type": "application/json"
|
| 30 |
}
|
| 31 |
|
| 32 |
+
# Add current message to history with timestamp and topic
|
| 33 |
chat_history.append({
|
| 34 |
"role": "user",
|
| 35 |
"content": message.strip(),
|
|
|
|
| 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:
|
|
|
|
| 49 |
messages.append({"role": "user", "content": summary_prompt})
|
| 50 |
|
| 51 |
payload = {
|
| 52 |
+
"model": "llama-3.3-70b-versatile",
|
| 53 |
"messages": messages,
|
| 54 |
"max_tokens": 5000,
|
| 55 |
"temperature": 0.7
|
|
|
|
| 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 chat_with_groq(message: str, history: List[List[str]], topic: str = "general", summarize: bool = False) -> tuple:
|
| 78 |
+
"""
|
| 79 |
+
Function for Gradio Chatbot
|
| 80 |
+
Returns: (updated_history, empty_string_for_textbox)
|
| 81 |
+
"""
|
| 82 |
+
if not message.strip():
|
| 83 |
+
return history, ""
|
| 84 |
+
|
| 85 |
+
# Get response from Groq
|
| 86 |
+
response = groq_with_memory(message, topic, summarize)
|
| 87 |
+
|
| 88 |
+
# Add to gradio chat history format
|
| 89 |
+
history.append([message, response])
|
| 90 |
+
|
| 91 |
+
return history, ""
|
| 92 |
+
|
| 93 |
+
def clear_chat():
|
| 94 |
+
"""Clear both chat histories"""
|
| 95 |
chat_history.clear()
|
| 96 |
+
return [], "β
Chat history cleared"
|
| 97 |
|
| 98 |
def view_history(topic: str = None):
|
| 99 |
+
"""View chat history with optional topic filter"""
|
| 100 |
if not chat_history:
|
| 101 |
+
return "β No chat history available"
|
| 102 |
|
| 103 |
+
filtered = [msg for msg in chat_history if topic is None or msg.get("topic") == topic]
|
| 104 |
return "\n".join(
|
| 105 |
+
f"[{msg.get('timestamp', 'N/A')}] {msg['role'].capitalize()}: {msg['content']}"
|
| 106 |
for msg in filtered
|
| 107 |
)
|
| 108 |
|
| 109 |
def manual_curl_example():
|
| 110 |
+
"""Generate example curl command"""
|
| 111 |
if not API_KEY:
|
| 112 |
+
return "β No API key available"
|
| 113 |
return f"""curl -X POST "{API_URL}" \\
|
| 114 |
-H "Authorization: Bearer {API_KEY}" \\
|
| 115 |
-H "Content-Type: application/json" \\
|
| 116 |
+
-d '{{"model": "llama-3.3-70b-versatile", "messages": [{{"role": "user", "content": "Hello"}}], "max_tokens": 1000}}'
|
| 117 |
"""
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
# Gradio Interface
|
| 120 |
+
with gr.Blocks(title="Groq Chat Debugger", theme=gr.themes.Soft()) as demo:
|
| 121 |
+
gr.Markdown("## π¬ Groq API Chat Interface")
|
| 122 |
+
|
| 123 |
+
with gr.Tab("π¬ Chat"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
with gr.Row():
|
| 125 |
+
with gr.Column(scale=3):
|
| 126 |
+
# Main chatbot interface
|
| 127 |
+
chatbot = gr.Chatbot(
|
| 128 |
+
label="Chat with Groq",
|
| 129 |
+
height=500,
|
| 130 |
+
show_copy_button=True
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
with gr.Row():
|
| 134 |
+
msg_input = gr.Textbox(
|
| 135 |
+
label="Your Message",
|
| 136 |
+
placeholder="Ketik pesan Anda di sini...",
|
| 137 |
+
scale=4
|
| 138 |
+
)
|
| 139 |
+
send_btn = gr.Button("Send π€", variant="primary", scale=1)
|
| 140 |
+
|
| 141 |
+
with gr.Column(scale=1):
|
| 142 |
+
# Settings panel
|
| 143 |
+
gr.Markdown("### βοΈ Settings")
|
| 144 |
+
topic_input = gr.Textbox(
|
| 145 |
+
label="Topic",
|
| 146 |
+
value="general",
|
| 147 |
+
placeholder="general"
|
| 148 |
+
)
|
| 149 |
+
summarize_cb = gr.Checkbox(
|
| 150 |
+
label="π Summarize conversation",
|
| 151 |
+
value=False
|
| 152 |
+
)
|
| 153 |
+
clear_btn = gr.Button("ποΈ Clear Chat", variant="stop")
|
| 154 |
+
|
| 155 |
+
# Stats
|
| 156 |
+
gr.Markdown("### π Stats")
|
| 157 |
+
stats_output = gr.Textbox(
|
| 158 |
+
label="Chat Stats",
|
| 159 |
+
value=f"API Status: {'β
Ready' if API_KEY else 'β No Key'}",
|
| 160 |
+
interactive=False,
|
| 161 |
+
lines=3
|
| 162 |
+
)
|
| 163 |
|
| 164 |
+
# Event handlers
|
| 165 |
send_btn.click(
|
| 166 |
+
chat_with_groq,
|
| 167 |
+
inputs=[msg_input, chatbot, topic_input, summarize_cb],
|
| 168 |
outputs=[chatbot, msg_input]
|
| 169 |
)
|
| 170 |
+
|
| 171 |
+
msg_input.submit( # Enter key support
|
| 172 |
+
chat_with_groq,
|
| 173 |
+
inputs=[msg_input, chatbot, topic_input, summarize_cb],
|
| 174 |
+
outputs=[chatbot, msg_input]
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
clear_btn.click(
|
| 178 |
+
clear_chat,
|
| 179 |
+
outputs=[chatbot, stats_output]
|
| 180 |
+
)
|
| 181 |
|
| 182 |
+
with gr.Tab("π History"):
|
| 183 |
+
gr.Markdown("### View and manage your chat history")
|
| 184 |
+
with gr.Row():
|
| 185 |
+
history_topic = gr.Textbox(
|
| 186 |
+
label="Filter by Topic",
|
| 187 |
+
placeholder="Leave empty to see all"
|
| 188 |
+
)
|
| 189 |
+
view_btn = gr.Button("π View History", variant="primary")
|
| 190 |
+
|
| 191 |
+
history_output = gr.Textbox(
|
| 192 |
+
label="Chat History",
|
| 193 |
+
lines=15,
|
| 194 |
+
interactive=False
|
| 195 |
+
)
|
| 196 |
|
| 197 |
+
view_btn.click(
|
| 198 |
+
view_history,
|
| 199 |
+
inputs=[history_topic],
|
| 200 |
+
outputs=[history_output]
|
| 201 |
+
)
|
| 202 |
|
| 203 |
+
with gr.Tab("π§ API Tools"):
|
| 204 |
gr.Markdown(f"""
|
| 205 |
+
### API Configuration
|
| 206 |
+
**Status:** {'β
Connected' if API_KEY else 'β No API Key Set'}
|
| 207 |
+
**Endpoint:** `{API_URL}`
|
| 208 |
+
**Available Models:**
|
| 209 |
+
- `llama-3.3-70b-versatile` (Default)
|
| 210 |
+
- `llama3-8b-8192`
|
| 211 |
+
- `mixtral-8x7b-32768`
|
| 212 |
""")
|
| 213 |
|
| 214 |
+
curl_btn = gr.Button("π Generate cURL Example")
|
| 215 |
+
curl_output = gr.Code(
|
| 216 |
+
label="cURL Command",
|
| 217 |
+
language="bash",
|
| 218 |
+
lines=8
|
| 219 |
+
)
|
| 220 |
curl_btn.click(manual_curl_example, outputs=[curl_output])
|
| 221 |
+
|
| 222 |
+
if not API_KEY:
|
| 223 |
+
gr.Markdown("""
|
| 224 |
+
### β οΈ Setup Instructions
|
| 225 |
+
1. Get your API key from [Groq Console](https://console.groq.com/)
|
| 226 |
+
2. Set environment variable: `export GROQ_API_KEY="your-key-here"`
|
| 227 |
+
3. Restart the application
|
| 228 |
+
""")
|
| 229 |
|
| 230 |
if __name__ == "__main__":
|
| 231 |
+
demo.launch(
|
| 232 |
+
server_name="0.0.0.0",
|
| 233 |
+
server_port=7860,
|
| 234 |
+
share=False # Set True if you want public link
|
| 235 |
+
)
|