adikwok commited on
Commit
3a01f98
Β·
verified Β·
1 Parent(s): 5768297

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -126
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
- 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
- if not API_KEY:
20
- return "❌ No API Key found"
21
 
22
  if not message.strip():
23
- return "❌ Empty message"
24
 
25
  try:
26
- headers = {
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.strip(),
35
  "topic": topic,
36
  "timestamp": datetime.now().isoformat()
37
  })
38
 
39
- # Prepare messages for API call
40
  messages = [{"role": msg["role"], "content": msg["content"]}
41
- for msg in chat_history[-max_history:]]
42
 
43
  if summarize:
44
- summary_prompt = f"Summarize this conversation about '{topic}':\n\n"
45
- summary_prompt += "\n".join([f"{msg['role']}: {msg['content']}"
46
- for msg in chat_history if msg["topic"] == topic])
47
- messages.append({"role": "user", "content": summary_prompt})
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(API_URL, headers=headers, json=payload, timeout=30)
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  if response.status_code == 200:
59
- result = response.json()
60
- if "choices" in result and result["choices"]:
61
- response_content = result["choices"][0]["message"]["content"]
62
- chat_history.append({
63
- "role": "assistant",
64
- "content": response_content,
65
- "topic": topic,
66
- "timestamp": datetime.now().isoformat()
67
- })
68
- return response_content
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"❌ Error: {str(e)}"
74
 
75
- def clear_history():
76
- """Clear the chat history"""
77
- chat_history.clear()
78
- return "βœ… Chat history cleared"
 
 
79
 
80
- def view_history(topic: str = None):
81
- """View chat history with optional topic filter"""
82
  if not chat_history:
83
- return "❌ No chat history available"
84
-
85
- filtered = [msg for msg in chat_history if topic is None or msg["topic"] == topic]
86
- return "\n".join(
87
- f"[{msg['timestamp']}] {msg['role'].capitalize()}: {msg['content']}"
88
- for msg in filtered
89
- )
90
 
91
- def manual_curl_example():
92
- """Generate example curl command"""
93
- if not API_KEY:
94
- return "❌ No API key available"
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 with Chatbox Style
108
- with gr.Blocks(title="Groq Chat Interface") as demo:
109
  with gr.Tabs():
110
- with gr.TabItem("πŸ’¬ Chat"):
111
- chatbot = gr.Chatbot(height=500)
 
112
  with gr.Row():
113
- msg_input = gr.Textbox(
114
- placeholder="Type your message here...",
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("βš™οΈ Settings", open=False):
124
- topic_input = gr.Textbox(
125
- label="Conversation Topic",
126
- value="general"
127
- )
128
- summarize_cb = gr.Checkbox(
129
- label="Summarize conversation",
130
- value=False
131
- )
132
 
133
- # Chat interaction
134
- msg_input.submit(
135
- process_chat,
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
- with gr.TabItem("πŸ“œ History"):
147
- history_topic = gr.Textbox(label="Filter by Topic")
 
148
  view_btn = gr.Button("View History")
149
- clear_history_btn = gr.Button("Clear All History", variant="stop")
150
- history_output = gr.Textbox(label="Raw History", lines=15)
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
- with gr.TabItem("πŸ”§ API Tools"):
 
163
  gr.Markdown(f"""
164
- **API Status:** {'βœ… Ready' if API_KEY else '❌ No API Key'}
165
  **Endpoint:** `{API_URL}`
166
- **Models:** `llama3-8b-8192`, `llama3-70b-8192`, `mixtral-8x7b-32768`
167
- """)
 
 
168
 
169
- curl_btn = gr.Button("Generate cURL Example")
170
- curl_output = gr.Code(label="cURL Command", language="shell")
171
- curl_btn.click(manual_curl_example, outputs=[curl_output])
 
 
 
 
 
 
 
 
 
 
172
 
173
  if __name__ == "__main__":
174
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
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")