Miraj74 commited on
Commit
562eb37
Β·
verified Β·
1 Parent(s): cde610d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +267 -0
app.py CHANGED
@@ -0,0 +1,267 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import time
4
+ import gradio as gr
5
+ from datetime import datetime
6
+ from typing import List, Dict, Any, Optional, Union
7
+ import threading
8
+
9
+ # Import Groq
10
+ from groq import Groq
11
+
12
+
13
+ class CreativeAgenticAI:
14
+ """
15
+ Creative Agentic AI Chat Tool using Groq's compound models
16
+ """
17
+ def __init__(self, api_key: str, model: str = "compound-beta"):
18
+ """
19
+ Initialize the Creative Agentic AI system.
20
+ Args:
21
+ api_key: Groq API key
22
+ model: Which Groq model to use ('compound-beta' or 'compound-beta-mini')
23
+ """
24
+ self.api_key = api_key
25
+ if not self.api_key:
26
+ raise ValueError("No API key provided")
27
+ self.client = Groq(api_key=self.api_key)
28
+ self.model = model
29
+ self.conversation_history = []
30
+
31
+ def chat(self, message: str,
32
+ include_domains: List[str] = None,
33
+ exclude_domains: List[str] = None,
34
+ system_prompt: str = None,
35
+ temperature: float = 0.7,
36
+ max_tokens: int = 1024) -> Dict:
37
+ """
38
+ Send a message to the AI and get a response
39
+ """
40
+ if not system_prompt:
41
+ system_prompt = """You are a creative and intelligent AI assistant with agentic capabilities. You can search the web, analyze information, and provide comprehensive responses. Be helpful, creative, and engaging while maintaining accuracy."""
42
+
43
+ messages = [{"role": "system", "content": system_prompt}]
44
+ messages.extend(self.conversation_history[-20:])
45
+ messages.append({"role": "user", "content": message})
46
+
47
+ params = {
48
+ "messages": messages,
49
+ "model": self.model,
50
+ "temperature": temperature,
51
+ "max_tokens": max_tokens
52
+ }
53
+
54
+ if include_domains and include_domains[0].strip():
55
+ params["include_domains"] = [domain.strip() for domain in include_domains if domain.strip()]
56
+ if exclude_domains and exclude_domains[0].strip():
57
+ params["exclude_domains"] = [domain.strip() for domain in exclude_domains if domain.strip()]
58
+
59
+ try:
60
+ response = self.client.chat.completions.create(**params)
61
+ content = response.choices[0].message.content
62
+ tool_info = self._extract_tool_info(response)
63
+
64
+ self.conversation_history.append({"role": "user", "content": message})
65
+ self.conversation_history.append({"role": "assistant", "content": content})
66
+
67
+ return {
68
+ "content": content,
69
+ "timestamp": datetime.now().isoformat(),
70
+ "model": self.model,
71
+ "tool_usage": tool_info,
72
+ "parameters": {
73
+ "temperature": temperature,
74
+ "max_tokens": max_tokens,
75
+ "include_domains": include_domains,
76
+ "exclude_domains": exclude_domains
77
+ }
78
+ }
79
+ except Exception as e:
80
+ error_msg = f"Error: {str(e)}"
81
+ self.conversation_history.append({"role": "user", "content": message})
82
+ self.conversation_history.append({"role": "assistant", "content": error_msg})
83
+ return {
84
+ "content": error_msg,
85
+ "timestamp": datetime.now().isoformat(),
86
+ "model": self.model,
87
+ "tool_usage": None,
88
+ "error": str(e)
89
+ }
90
+
91
+ def _extract_tool_info(self, response) -> Dict:
92
+ tool_info = None
93
+ if hasattr(response.choices[0].message, 'executed_tools'):
94
+ tools = response.choices[0].message.executed_tools
95
+ if tools:
96
+ tool_info = []
97
+ for tool in tools:
98
+ tool_dict = {
99
+ "tool_type": getattr(tool, "type", "unknown"),
100
+ "tool_name": getattr(tool, "name", "unknown"),
101
+ }
102
+ if hasattr(tool, "input"):
103
+ tool_dict["input"] = str(tool.input)
104
+ if hasattr(tool, "output"):
105
+ tool_dict["output"] = str(tool.output)
106
+ tool_info.append(tool_dict)
107
+ return tool_info
108
+
109
+ def clear_history(self):
110
+ self.conversation_history = []
111
+
112
+ def get_history_summary(self) -> str:
113
+ if not self.conversation_history:
114
+ return "No conversation history"
115
+ user_messages = [msg for msg in self.conversation_history if msg["role"] == "user"]
116
+ assistant_messages = [msg for msg in self.conversation_history if msg["role"] == "assistant"]
117
+ return f"Conversation: {len(user_messages)} user messages, {len(assistant_messages)} assistant responses"
118
+
119
+
120
+ # Global variables
121
+ ai_instance = None
122
+ api_key_status = "Not Set"
123
+
124
+ def validate_api_key(api_key: str, model: str) -> str:
125
+ global ai_instance, api_key_status
126
+ if not api_key or len(api_key.strip()) < 10:
127
+ api_key_status = "Invalid ❌"
128
+ return "❌ Please enter a valid API key (should be longer than 10 characters)"
129
+ try:
130
+ client = Groq(api_key=api_key)
131
+ client.chat.completions.create(
132
+ messages=[{"role": "user", "content": "Hello"}],
133
+ model=model,
134
+ max_tokens=10
135
+ )
136
+ ai_instance = CreativeAgenticAI(api_key=api_key, model=model)
137
+ api_key_status = "Valid βœ…"
138
+ return f"βœ… API Key Valid! Creative Agentic AI is ready.\n\n**Model:** {model}\n**Status:** Connected and ready for chat!"
139
+ except Exception as e:
140
+ api_key_status = "Invalid ❌"
141
+ ai_instance = None
142
+ return f"❌ Error validating API key: {str(e)}\n\nPlease check your API key and try again."
143
+
144
+ def update_model(model: str) -> str:
145
+ global ai_instance
146
+ if ai_instance:
147
+ ai_instance.model = model
148
+ return f"βœ… Model updated to: **{model}**"
149
+ else:
150
+ return "⚠ Please set your API key first"
151
+
152
+ def chat_with_ai(message: str,
153
+ include_domains: str,
154
+ exclude_domains: str,
155
+ system_prompt: str,
156
+ temperature: float,
157
+ max_tokens: int,
158
+ history: List) -> tuple:
159
+ global ai_instance
160
+ if not ai_instance:
161
+ error_msg = "⚠ Please set your Groq API key first!"
162
+ history.append([message, error_msg])
163
+ return history, ""
164
+ if not message.strip():
165
+ return history, ""
166
+
167
+ include_list = [d.strip() for d in include_domains.split(",")] if include_domains.strip() else []
168
+ exclude_list = [d.strip() for d in exclude_domains.split(",")] if exclude_domains.strip() else []
169
+
170
+ try:
171
+ response = ai_instance.chat(
172
+ message=message,
173
+ include_domains=include_list if include_list else None,
174
+ exclude_domains=exclude_list if exclude_list else None,
175
+ system_prompt=system_prompt if system_prompt.strip() else None,
176
+ temperature=temperature,
177
+ max_tokens=int(max_tokens)
178
+ )
179
+ ai_response = response["content"]
180
+ if response.get("tool_usage"):
181
+ ai_response += f"\n\n*πŸ”§ Tools used: {len(response['tool_usage'])} tool(s)*"
182
+ history.append([message, ai_response])
183
+ return history, ""
184
+ except Exception as e:
185
+ error_msg = f"❌ Error: {str(e)}"
186
+ history.append([message, error_msg])
187
+ return history, ""
188
+
189
+ def clear_chat_history():
190
+ global ai_instance
191
+ if ai_instance:
192
+ ai_instance.clear_history()
193
+ return []
194
+
195
+ def create_gradio_app():
196
+ css = """
197
+ .container {
198
+ max-width: 1200px;
199
+ margin: 0 auto;
200
+ }
201
+ .header {
202
+ text-align: center;
203
+ background: linear-gradient(to right, #00ff94, #00b4db);
204
+ color: white;
205
+ padding: 20px;
206
+ border-radius: 10px;
207
+ margin-bottom: 20px;
208
+ }
209
+ .status-box {
210
+ background-color: #f8f9fa;
211
+ border: 1px solid #dee2e6;
212
+ border-radius: 8px;
213
+ padding: 15px;
214
+ margin: 10px 0;
215
+ }
216
+ #neuroscope-accordion {
217
+ background: linear-gradient(to right, #00ff94, #00b4db);
218
+ border-radius: 8px;
219
+ }
220
+ """
221
+
222
+ with gr.Blocks(css=css, title="πŸ€– Creative Agentic AI Chat", theme=gr.themes.Ocean()) as app:
223
+ gr.HTML("""
224
+ <div class="header">
225
+ <h1>πŸ€– NeuroScope-AI</h1>
226
+ <p>Powered by Groq's Compound Models with Web Search & Agentic Capabilities</p>
227
+ </div>
228
+ """)
229
+
230
+ with gr.Row():
231
+ api_key = gr.Textbox(label="πŸ”‘ Groq API Key", placeholder="Enter your Groq API key here...", type="password", info="Get your API key from: https://console.groq.com/")
232
+ model_selection = gr.Radio(choices=["compound-beta", "compound-beta-mini"], label="🧠 Model Selection", value="compound-beta", info="compound-beta: More powerful | compound-beta-mini: Faster")
233
+ connect_btn = gr.Button("πŸ”— Connect", variant="primary", size="lg")
234
+
235
+ status_display = gr.Markdown("### πŸ“Š Status: Not connected", elem_classes=["status-box"])
236
+
237
+ connect_btn.click(fn=validate_api_key, inputs=[api_key, model_selection], outputs=[status_display])
238
+ model_selection.change(fn=update_model, inputs=[model_selection], outputs=[status_display])
239
+
240
+ with gr.Tab("πŸ’¬ Chat"):
241
+ chatbot = gr.Chatbot(label="Creative AI Assistant", height=500, show_label=True, bubble_full_width=False, show_copy_button=True)
242
+ with gr.Row():
243
+ msg = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=3)
244
+ with gr.Column():
245
+ send_btn = gr.Button("πŸ“€ Send", variant="primary")
246
+ clear_btn = gr.Button("πŸ—‘ Clear", variant="secondary")
247
+
248
+ with gr.Accordion("βš™ Advanced Settings", open=False, elem_id="neuroscope-accordion"):
249
+ with gr.Row():
250
+ temperature = gr.Slider(minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="🌑 Temperature", info="Higher = more creative, Lower = more focused")
251
+ max_tokens = gr.Slider(minimum=100, maximum=4000, value=1024, step=100, label="πŸ“ Max Tokens", info="Maximum length of response")
252
+ system_prompt = gr.Textbox(label="🎭 Custom System Prompt", placeholder="Override the default system prompt...", lines=2, info="Leave empty to use default creative assistant prompt")
253
+
254
+ with gr.Accordion("🌐 Domain Filtering (for Web Search)", open=False, elem_id="neuroscope-accordion"):
255
+ with gr.Row():
256
+ include_domains = gr.Textbox(label="βœ… Include Domains (comma-separated)", placeholder="arxiv.org, *.edu, github.com, stackoverflow.com", info="Only search these domains")
257
+ exclude_domains = gr.Textbox(label="❌ Exclude Domains (comma-separated)", placeholder="wikipedia.org, reddit.com, twitter.com", info="Never search these domains")
258
+
259
+ send_btn.click(fn=chat_with_ai, inputs=[msg, include_domains, exclude_domains, system_prompt, temperature, max_tokens, chatbot], outputs=[chatbot, msg])
260
+ msg.submit(fn=chat_with_ai, inputs=[msg, include_domains, exclude_domains, system_prompt, temperature, max_tokens, chatbot], outputs=[chatbot, msg])
261
+ clear_btn.click(fn=clear_chat_history, outputs=[chatbot])
262
+
263
+ return app
264
+
265
+ if __name__ == "__main__":
266
+ app = create_gradio_app()
267
+ app.launch(share=True)