adikwok commited on
Commit
ac1d1c0
Β·
verified Β·
1 Parent(s): 95128c5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -22
app.py CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
2
  import requests
3
  import os
4
  import json
 
 
5
 
6
  # Groq API Configuration
7
  API_URL = "https://api.groq.com/openai/v1/chat/completions"
@@ -9,8 +11,11 @@ API_KEY = os.getenv("GROQ_API_KEY")
9
 
10
  print(f"πŸ”‘ API Key Found: {'Yes' if API_KEY else 'No'}")
11
 
12
- def simple_groq_test(message: str) -> str:
13
- """Simple Groq API test without chat history"""
 
 
 
14
 
15
  if not API_KEY:
16
  return "❌ No API Key found"
@@ -24,11 +29,32 @@ def simple_groq_test(message: str) -> str:
24
  "Content-Type": "application/json"
25
  }
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  payload = {
28
  "model": "llama3-8b-8192",
29
- "messages": [
30
- {"role": "user", "content": message.strip()}
31
- ],
32
  "max_tokens": 512,
33
  "temperature": 0.7
34
  }
@@ -50,7 +76,15 @@ def simple_groq_test(message: str) -> str:
50
  if response.status_code == 200:
51
  result = response.json()
52
  if "choices" in result and len(result["choices"]) > 0:
53
- return result["choices"][0]["message"]["content"]
 
 
 
 
 
 
 
 
54
  else:
55
  return f"❌ No choices in response: {json.dumps(result, indent=2)}"
56
  else:
@@ -65,6 +99,22 @@ def simple_groq_test(message: str) -> str:
65
  except Exception as e:
66
  return f"❌ Unexpected error: {str(e)}"
67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  def manual_curl_example():
69
  """Generate curl command for manual testing"""
70
  if not API_KEY:
@@ -83,29 +133,63 @@ def manual_curl_example():
83
  '''
84
  return curl_cmd
85
 
86
- # Create simple interface
87
- with gr.Blocks(title="Groq Debug") as demo:
88
- gr.Markdown("# πŸ” Groq API Debug Tool")
89
 
90
- with gr.Tab("Simple Test"):
91
- gr.Markdown("Test single message without chat history")
92
 
93
- test_input = gr.Textbox(
94
- label="Test Message",
95
- placeholder="Enter a simple test message",
96
- value="Hello, how are you?"
 
 
 
 
 
 
 
 
 
97
  )
98
- test_button = gr.Button("Test API", variant="primary")
99
- test_output = gr.Textbox(
100
  label="Response",
101
  lines=10,
102
  max_lines=20
103
  )
104
 
105
- test_button.click(
106
- simple_groq_test,
107
- inputs=[test_input],
108
- outputs=[test_output]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
  )
110
 
111
  with gr.Tab("Manual Test"):
@@ -138,7 +222,7 @@ with gr.Blocks(title="Groq Debug") as demo:
138
 
139
  **Expected Models:**
140
  - `llama3-8b-8192`
141
- - `llama3-70b-8192`
142
  - `mixtral-8x7b-32768`
143
  - `gemma-7b-it`
144
  """)
 
2
  import requests
3
  import os
4
  import json
5
+ from typing import List, Dict
6
+ from datetime import datetime
7
 
8
  # Groq API Configuration
9
  API_URL = "https://api.groq.com/openai/v1/chat/completions"
 
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"
 
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(),
36
+ "topic": topic,
37
+ "timestamp": datetime.now().isoformat()
38
+ })
39
+
40
+ # Limit history to max_history messages to avoid token overflow
41
+ recent_history = chat_history[-max_history:]
42
+
43
+ # Prepare messages for API call
44
+ messages = recent_history.copy()
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:
51
+ summary_prompt += f"{msg['role'].capitalize()}: {msg['content']}\n"
52
+ summary_prompt += "\nProvide a concise summary of the key points discussed."
53
+ messages.append({"role": "user", "content": summary_prompt})
54
+
55
  payload = {
56
  "model": "llama3-8b-8192",
57
+ "messages": messages,
 
 
58
  "max_tokens": 512,
59
  "temperature": 0.7
60
  }
 
76
  if response.status_code == 200:
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:
 
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"
111
+
112
+ output = "πŸ“œ Chat History:\n\n"
113
+ for msg in chat_history:
114
+ if topic is None or msg["topic"] == topic:
115
+ output += f"[{msg['timestamp']}] {msg['role'].capitalize()} ({msg['topic']}): {msg['content']}\n"
116
+ return output
117
+
118
  def manual_curl_example():
119
  """Generate curl command for manual testing"""
120
  if not API_KEY:
 
133
  '''
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"):
171
+ gr.Markdown("View or clear chat history")
172
+
173
+ history_topic = gr.Textbox(
174
+ label="Filter by Topic (optional)",
175
+ placeholder="Enter topic to filter (leave blank for all)"
176
+ )
177
+ history_button = gr.Button("View History")
178
+ clear_button = gr.Button("Clear History", variant="secondary")
179
+ history_output = gr.Textbox(
180
+ label="Chat History",
181
+ lines=10,
182
+ max_lines=20
183
+ )
184
+
185
+ history_button.click(
186
+ view_history,
187
+ inputs=[history_topic],
188
+ outputs=[history_output]
189
+ )
190
+ clear_button.click(
191
+ clear_history,
192
+ outputs=[history_output]
193
  )
194
 
195
  with gr.Tab("Manual Test"):
 
222
 
223
  **Expected Models:**
224
  - `llama3-8b-8192`
225
+ - `llama3-70b-8192`
226
  - `mixtral-8x7b-32768`
227
  - `gemma-7b-it`
228
  """)