adikwok commited on
Commit
d1c5d37
Β·
verified Β·
1 Parent(s): 24bf98d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -63
app.py CHANGED
@@ -10,19 +10,18 @@ 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
- print(f"πŸ”‘ API Key Preview: {API_KEY[:10] + '...' if API_KEY else 'None'}")
14
 
15
  # In-memory chat history storage
16
  chat_history: List[Dict[str, str]] = []
17
 
18
- def groq_with_memory(message: str, topic: str = "general", summarize: bool = False, max_history: int = 10) -> tuple[List[List[str]], str]:
19
- """Groq API call with chat history and optional summarization, returning chatbot format and cleared input"""
20
 
21
  if not API_KEY:
22
- return [[None, "❌ No API Key found. Please set GROQ_API_KEY in Hugging Face Spaces Settings > Secrets. Go to https://console.groq.com/keys to get your key."]], ""
23
 
24
  if not message.strip():
25
- return [[None, "❌ Empty message"]], ""
26
 
27
  try:
28
  headers = {
@@ -45,6 +44,7 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
45
  messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
46
 
47
  if summarize:
 
48
  summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n"
49
  for msg in recent_history:
50
  if msg["topic"] == topic:
@@ -77,39 +77,34 @@ def groq_with_memory(message: str, topic: str = "general", summarize: bool = Fal
77
  result = response.json()
78
  if "choices" in result and len(result["choices"]) > 0:
79
  response_content = result["choices"][0]["message"]["content"]
 
80
  chat_history.append({
81
  "role": "assistant",
82
  "content": response_content,
83
  "topic": topic,
84
  "timestamp": datetime.now().isoformat()
85
  })
86
- return [[message.strip(), response_content]], ""
87
  else:
88
- error_msg = f"❌ No choices in response: {json.dumps(result, indent=2)}"
89
- return [[message.strip(), error_msg]], ""
90
  else:
91
- error_msg = f"❌ HTTP {response.status_code}: {response.text}"
92
- return [[message.strip(), error_msg]], ""
93
 
94
  except requests.exceptions.Timeout:
95
- return [[message.strip(), "❌ Request timeout (30s)"]], ""
96
  except requests.exceptions.ConnectionError:
97
- return [[message.strip(), "❌ Connection error - cannot reach Groq API"]], ""
98
  except json.JSONDecodeError:
99
- return [[message.strip(), f"❌ Invalid JSON response: {response.text}"]], ""
100
  except Exception as e:
101
- return [[message.strip(), f"❌ Unexpected error: {str(e)}"]], ""
102
-
103
- def update_chatbot_display() -> List[List[str]]:
104
- """Update the chatbot display with the entire chat history"""
105
- return [[msg["content"], None] if msg["role"] == "user" else [None, msg["content"]] for msg in chat_history]
106
 
107
  def clear_history():
108
  """Clear the chat history"""
109
  chat_history.clear()
110
- return [], "βœ… Chat history cleared"
111
 
112
- def view_history(topic: str = None) -> str:
113
  """View the chat history, optionally filtered by topic"""
114
  if not chat_history:
115
  return "❌ No chat history available"
@@ -123,7 +118,7 @@ def view_history(topic: str = None) -> str:
123
  def manual_curl_example():
124
  """Generate curl command for manual testing"""
125
  if not API_KEY:
126
- return "❌ No API key to generate curl. Set GROQ_API_KEY in Hugging Face Spaces Settings > Secrets."
127
 
128
  curl_cmd = f'''curl -X POST "{API_URL}" \\
129
  -H "Authorization: Bearer {API_KEY}" \\
@@ -139,52 +134,37 @@ def manual_curl_example():
139
  return curl_cmd
140
 
141
  # Create Gradio interface
142
- with gr.Blocks(title="Grok Debug with Memory", css="""
143
- .chatbot-container { max-height: 500px; overflow-y: auto; }
144
- .input-container { position: sticky; bottom: 0; background: white; padding: 10px; }
145
- .chatbot .message.user { text-align: left; background: #e6f3ff; padding: 10px; border-radius: 10px; margin: 5px; }
146
- .chatbot .message.assistant { text-align: right; background: #f0f0f0; padding: 10px; border-radius: 10px; margin: 5px; }
147
- """) as demo:
148
- gr.Markdown("# πŸ” Grok API Debug Tool with Memory")
149
 
150
  with gr.Tab("Chat with Memory"):
151
  gr.Markdown("Chat with Groq, maintain history, and summarize by topic")
152
 
153
- chatbot = gr.Chatbot(
154
- label="Conversation",
155
- container=True,
156
- height=500,
157
- elem_classes="chatbot-container",
158
- bubble_full_width=False
 
 
 
 
 
 
 
 
 
 
 
 
 
159
  )
160
- with gr.Row(elem_classes="input-container"):
161
- chat_input = gr.Textbox(
162
- label="Message",
163
- placeholder="Type your message here...",
164
- show_label=False,
165
- container=False
166
- )
167
- topic_input = gr.Textbox(
168
- label="Topic",
169
- placeholder="Enter topic (e.g., 'coding', 'general')",
170
- value="general",
171
- show_label=False,
172
- container=False,
173
- scale=1
174
- )
175
- summarize_checkbox = gr.Checkbox(
176
- label="Summarize",
177
- value=False
178
- )
179
- chat_button = gr.Button("Send", variant="primary")
180
 
181
  chat_button.click(
182
  groq_with_memory,
183
  inputs=[chat_input, topic_input, summarize_checkbox],
184
- outputs=[chatbot, chat_input]
185
- ).then(
186
- update_chatbot_display,
187
- outputs=[chatbot]
188
  )
189
 
190
  with gr.Tab("View History"):
@@ -209,7 +189,7 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
209
  )
210
  clear_button.click(
211
  clear_history,
212
- outputs=[chatbot, history_output]
213
  )
214
 
215
  with gr.Tab("Manual Test"):
@@ -245,10 +225,6 @@ with gr.Blocks(title="Grok Debug with Memory", css="""
245
  - `llama3-70b-8192`
246
  - `mixtral-8x7b-32768`
247
  - `gemma-7b-it`
248
-
249
- **API Key Setup:**
250
- - Ensure GROQ_API_KEY is set in Hugging Face Spaces Settings > Secrets.
251
- - Get your key from https://console.groq.com/keys.
252
  """)
253
 
254
  if __name__ == "__main__":
 
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]] = []
16
 
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 = {
 
44
  messages = [{"role": msg["role"], "content": msg["content"]} for msg in recent_history]
45
 
46
  if summarize:
47
+ # Create a prompt to summarize the conversation
48
  summary_prompt = f"Summarize the following conversation history for the topic '{topic}':\n\n"
49
  for msg in recent_history:
50
  if msg["topic"] == topic:
 
77
  result = response.json()
78
  if "choices" in result and len(result["choices"]) > 0:
79
  response_content = result["choices"][0]["message"]["content"]
80
+ # Add assistant's response to history
81
  chat_history.append({
82
  "role": "assistant",
83
  "content": response_content,
84
  "topic": topic,
85
  "timestamp": datetime.now().isoformat()
86
  })
87
+ return response_content
88
  else:
89
+ return f"❌ No choices in response: {json.dumps(result, indent=2)}"
 
90
  else:
91
+ return f"❌ HTTP {response.status_code}: {response.text}"
 
92
 
93
  except requests.exceptions.Timeout:
94
+ return "❌ Request timeout (30s)"
95
  except requests.exceptions.ConnectionError:
96
+ return "❌ Connection error - cannot reach Groq API"
97
  except json.JSONDecodeError:
98
+ return f"❌ Invalid JSON response: {response.text}"
99
  except Exception as e:
100
+ return f"❌ Unexpected error: {str(e)}"
 
 
 
 
101
 
102
  def clear_history():
103
  """Clear the chat history"""
104
  chat_history.clear()
105
+ return "βœ… Chat history cleared"
106
 
107
+ def view_history(topic: str = None):
108
  """View the chat history, optionally filtered by topic"""
109
  if not chat_history:
110
  return "❌ No chat history available"
 
118
  def manual_curl_example():
119
  """Generate curl command for manual testing"""
120
  if not API_KEY:
121
+ return "❌ No API key to generate curl"
122
 
123
  curl_cmd = f'''curl -X POST "{API_URL}" \\
124
  -H "Authorization: Bearer {API_KEY}" \\
 
134
  return curl_cmd
135
 
136
  # Create Gradio interface
137
+ with gr.Blocks(title="Groq Debug with Memory") as demo:
138
+ gr.Markdown("# πŸ” Groq API Debug Tool with Memory")
 
 
 
 
 
139
 
140
  with gr.Tab("Chat with Memory"):
141
  gr.Markdown("Chat with Groq, maintain history, and summarize by topic")
142
 
143
+ chat_input = gr.Textbox(
144
+ label="Message",
145
+ placeholder="Enter your message",
146
+ value="Hello, how can you help me today?"
147
+ )
148
+ topic_input = gr.Textbox(
149
+ label="Topic",
150
+ placeholder="Enter topic (e.g., 'coding', 'general')",
151
+ value="general"
152
+ )
153
+ summarize_checkbox = gr.Checkbox(
154
+ label="Summarize conversation for this topic",
155
+ value=False
156
+ )
157
+ chat_button = gr.Button("Send Message", variant="primary")
158
+ chat_output = gr.Textbox(
159
+ label="Response",
160
+ lines=10,
161
+ max_lines=20
162
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
163
 
164
  chat_button.click(
165
  groq_with_memory,
166
  inputs=[chat_input, topic_input, summarize_checkbox],
167
+ outputs=[chat_output]
 
 
 
168
  )
169
 
170
  with gr.Tab("View History"):
 
189
  )
190
  clear_button.click(
191
  clear_history,
192
+ outputs=[history_output]
193
  )
194
 
195
  with gr.Tab("Manual Test"):
 
225
  - `llama3-70b-8192`
226
  - `mixtral-8x7b-32768`
227
  - `gemma-7b-it`
 
 
 
 
228
  """)
229
 
230
  if __name__ == "__main__":