Threatthriver commited on
Commit
710e881
·
verified ·
1 Parent(s): 114a365

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -152
app.py CHANGED
@@ -6,15 +6,13 @@ from cerebras.cloud.sdk import Cerebras
6
  from groq import Groq
7
  import requests
8
  from bs4 import BeautifulSoup
9
- from urllib.parse import urljoin, urlparse
10
  import re
11
  import json
12
- import numpy as np
13
- from datetime import datetime
14
  import logging
15
  import aiohttp
16
 
17
- # Enhanced API Setup
18
  CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY")
19
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
20
 
@@ -31,6 +29,7 @@ logging.basicConfig(
31
  filename='agent.log'
32
  )
33
 
 
34
  class EnhancedToolkit:
35
  @staticmethod
36
  async def fetch_webpage_async(url, timeout=10):
@@ -41,18 +40,16 @@ class EnhancedToolkit:
41
  return await response.text()
42
  return f"Error: HTTP {response.status}"
43
  except Exception as e:
 
44
  return f"Error fetching URL: {str(e)}"
45
 
46
  @staticmethod
47
  def extract_text_from_html(html):
48
  soup = BeautifulSoup(html, 'html.parser')
49
- # Remove script and style elements
50
  for script in soup(["script", "style"]):
51
  script.decompose()
52
  text = soup.get_text(separator=' ', strip=True)
53
- # Normalize whitespace
54
- text = ' '.join(text.split())
55
- return text
56
 
57
  @staticmethod
58
  def validate_url(url):
@@ -64,27 +61,17 @@ class EnhancedToolkit:
64
 
65
  @staticmethod
66
  def summarize_text(text, max_length=500):
67
- """Simple text summarization by extracting key sentences"""
68
  sentences = text.split('. ')
69
  if len(sentences) <= 3:
70
  return text
71
-
72
- # Simple importance scoring based on sentence length and position
73
- scores = []
74
- for i, sentence in enumerate(sentences):
75
- score = len(sentence.split()) * (1.0 / (i + 1)) # Length and position weight
76
- scores.append((score, sentence))
77
-
78
- # Get top sentences
79
  scores.sort(reverse=True)
80
- summary = '. '.join(sent for _, sent in scores[:3]) + '.'
81
- return summary
82
 
83
  @staticmethod
84
  def analyze_sentiment(text):
85
- """Simple sentiment analysis"""
86
- positive_words = set(['good', 'great', 'excellent', 'positive', 'amazing', 'wonderful'])
87
- negative_words = set(['bad', 'poor', 'negative', 'terrible', 'awful', 'horrible'])
88
 
89
  words = text.lower().split()
90
  pos_count = sum(1 for word in words if word in positive_words)
@@ -116,102 +103,48 @@ class AgentCore:
116
  async def execute_tool(self, action, parameters):
117
  if self.tool_execution_count >= self.max_tools_per_turn:
118
  return "Tool usage limit reached for this turn."
119
-
120
  self.tool_execution_count += 1
121
 
122
  if action == "scrape":
123
  url = parameters.get("url")
124
  if not self.toolkit.validate_url(url):
125
  return "Invalid URL provided."
126
-
127
  html_content = await self.toolkit.fetch_webpage_async(url)
128
  if html_content.startswith("Error"):
129
  return html_content
130
-
131
  text_content = self.toolkit.extract_text_from_html(html_content)
132
  summary = self.toolkit.summarize_text(text_content)
133
  sentiment = self.toolkit.analyze_sentiment(text_content)
134
-
135
- return {
136
- 'summary': summary,
137
- 'sentiment': sentiment,
138
- 'full_text': text_content[:1000] + '...' if len(text_content) > 1000 else text_content
139
- }
140
-
141
- elif action == "search":
142
- query = parameters.get("query")
143
- return f"Simulated search for: {query}\nThis would connect to a search API in production."
144
-
145
- elif action == "analyze":
146
  text = parameters.get("text")
147
  if not text:
148
  return "No text provided for analysis"
149
-
150
- return {
151
- 'sentiment': self.toolkit.analyze_sentiment(text),
152
- 'summary': self.toolkit.summarize_text(text)
153
- }
154
-
155
  return f"Unknown tool: {action}"
156
 
 
157
  async def chat_with_agent(user_input, chat_history, agent_core):
158
  start_time = time.time()
159
  try:
160
  # Reset tool counter for new turn
161
  agent_core.tool_execution_count = 0
 
 
 
162
 
163
- # Prepare context-aware prompt
164
- system_prompt = """You are OmniAgent, a highly advanced AI assistant with multiple capabilities:
165
-
166
- Core Abilities:
167
- 1. Task Understanding & Planning
168
- 2. Web Information Retrieval & Analysis
169
- 3. Content Summarization & Sentiment Analysis
170
- 4. Context-Aware Problem Solving
171
- 5. Creative Solution Generation
172
-
173
- Available Tools:
174
- - scrape: Extract and analyze web content
175
- - search: Find relevant information
176
- - analyze: Process and understand text
177
-
178
- Use format:
179
- Action: take_action
180
- Parameters: {"action": "tool_name", "parameters": {...}}
181
-
182
- Approach each task with:
183
- 1. Initial analysis
184
- 2. Step-by-step planning
185
- 3. Tool utilization when needed
186
- 4. Result synthesis
187
- 5. Clear explanation
188
-
189
- Remember to maintain a helpful, professional, yet friendly tone."""
190
-
191
- messages = [
192
- {"role": "system", "content": system_prompt},
193
- {"role": "user", "content": user_input}
194
- ]
195
-
196
- # Use both models for different aspects of processing
197
  async def get_cerebras_response():
198
- response = cerebras_client.completions.create(
199
- prompt=f"{system_prompt}\n\nUser: {user_input}",
200
- max_tokens=1000,
201
- temperature=0.7
202
- )
203
  return response.text
204
 
205
  async def get_groq_response():
206
- completion = groq_client.chat.completions.create(
207
- messages=messages,
208
- temperature=0.7,
209
- max_tokens=2048,
210
- stream=True
211
- )
212
  return completion
213
-
214
- # Get responses from both models
215
  cerebras_future = asyncio.create_task(get_cerebras_response())
216
  groq_stream = await get_groq_response()
217
 
@@ -219,12 +152,10 @@ async def chat_with_agent(user_input, chat_history, agent_core):
219
  response = ""
220
  chain_of_thought = ""
221
 
222
- # Process Groq stream
223
  for chunk in groq_stream:
224
  if chunk.choices[0].delta and chunk.choices[0].delta.content:
225
  content = chunk.choices[0].delta.content
226
  response += content
227
-
228
  if "Chain of Thought:" in content:
229
  chain_of_thought += content.split("Chain of Thought:", 1)[-1]
230
 
@@ -235,18 +166,13 @@ async def chat_with_agent(user_input, chat_history, agent_core):
235
  action = action_match.group(1)
236
  try:
237
  parameters = json.loads(action_match.group(2))
238
- tool_result = await agent_core.execute_tool(
239
- parameters.get("action"),
240
- parameters.get("parameters", {})
241
- )
242
  response += f"\nTool Result: {json.dumps(tool_result, indent=2)}\n"
243
  except json.JSONDecodeError:
244
  response += "\nError: Invalid tool parameters\n"
245
 
246
- # Integrate Cerebras response
247
  cerebras_response = await cerebras_future
248
-
249
- # Combine insights from both models
250
  final_response = f"{response}\n\nAdditional Insights:\n{cerebras_response}"
251
 
252
  # Update context
@@ -265,48 +191,28 @@ def create_interface():
265
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
266
  agent_core = AgentCore()
267
 
268
- gr.Markdown("""# 🌟 OmniAgent: Advanced AI Assistant
269
- Powered by dual AI models for enhanced capabilities and deeper understanding.""")
270
 
271
  with gr.Row():
272
  with gr.Column(scale=6):
273
- chat_history = gr.Chatbot(
274
- label="Interaction History",
275
- height=600,
276
- show_label=True
277
- )
278
  with gr.Column(scale=2):
279
  with gr.Accordion("Performance Metrics", open=True):
280
  compute_time = gr.Textbox(label="Processing Time", interactive=False)
281
  token_usage_display = gr.Textbox(label="Resource Usage", interactive=False)
282
  with gr.Accordion("Agent Insights", open=True):
283
- chain_of_thought_display = gr.Textbox(
284
- label="Reasoning Process",
285
- interactive=False,
286
- lines=10
287
- )
288
 
289
- user_input = gr.Textbox(
290
- label="Your Request",
291
- placeholder="How can I assist you today?",
292
- lines=3
293
- )
294
-
295
- with gr.Row():
296
- send_button = gr.Button("Send", variant="primary")
297
- clear_button = gr.Button("Clear History", variant="secondary")
298
- export_button = gr.Button("Export Chat", variant="secondary")
299
 
300
  async def handle_chat(chat_history, user_input):
301
  if not user_input.strip():
302
  return chat_history, "", "", ""
303
 
304
- ai_response, chain_of_thought, compute_info, token_usage = await chat_with_agent(
305
- user_input,
306
- chat_history,
307
- agent_core
308
- )
309
-
310
  chat_history.append((user_input, ai_response))
311
  return chat_history, chain_of_thought, compute_info, token_usage
312
 
@@ -319,35 +225,16 @@ def create_interface():
319
  return "No chat history to export.", ""
320
 
321
  filename = f"omnigent_chat_{int(time.time())}.txt"
322
- chat_text = "\n".join([
323
- f"User: {item[0]}\nAI: {item[1]}\n"
324
- for item in chat_history
325
- ])
326
-
327
  with open(filename, "w") as file:
328
  file.write(chat_text)
329
  return f"Chat exported to {filename}", ""
330
 
331
  # Event handlers
332
- send_button.click(
333
- handle_chat,
334
- inputs=[chat_history, user_input],
335
- outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display]
336
- )
337
- clear_button.click(
338
- clear_chat,
339
- outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display]
340
- )
341
- export_button.click(
342
- export_chat,
343
- inputs=[chat_history],
344
- outputs=[compute_time, chain_of_thought_display]
345
- )
346
- user_input.submit(
347
- handle_chat,
348
- inputs=[chat_history, user_input],
349
- outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display]
350
- )
351
 
352
  gr.Markdown("""### 🚀 Advanced Capabilities:
353
  - Dual AI Model Processing
@@ -364,4 +251,4 @@ def create_interface():
364
 
365
  if __name__ == "__main__":
366
  demo = create_interface()
367
- demo.launch(share=True)
 
6
  from groq import Groq
7
  import requests
8
  from bs4 import BeautifulSoup
9
+ from urllib.parse import urlparse
10
  import re
11
  import json
 
 
12
  import logging
13
  import aiohttp
14
 
15
+ # API Setup
16
  CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY")
17
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
18
 
 
29
  filename='agent.log'
30
  )
31
 
32
+ # Helper Functions
33
  class EnhancedToolkit:
34
  @staticmethod
35
  async def fetch_webpage_async(url, timeout=10):
 
40
  return await response.text()
41
  return f"Error: HTTP {response.status}"
42
  except Exception as e:
43
+ logging.error(f"Error fetching URL: {str(e)}")
44
  return f"Error fetching URL: {str(e)}"
45
 
46
  @staticmethod
47
  def extract_text_from_html(html):
48
  soup = BeautifulSoup(html, 'html.parser')
 
49
  for script in soup(["script", "style"]):
50
  script.decompose()
51
  text = soup.get_text(separator=' ', strip=True)
52
+ return ' '.join(text.split())
 
 
53
 
54
  @staticmethod
55
  def validate_url(url):
 
61
 
62
  @staticmethod
63
  def summarize_text(text, max_length=500):
 
64
  sentences = text.split('. ')
65
  if len(sentences) <= 3:
66
  return text
67
+ scores = [(len(sentence.split()) * (1.0 / (i + 1)), sentence) for i, sentence in enumerate(sentences)]
 
 
 
 
 
 
 
68
  scores.sort(reverse=True)
69
+ return '. '.join([sentence for _, sentence in scores[:3]]) + '.'
 
70
 
71
  @staticmethod
72
  def analyze_sentiment(text):
73
+ positive_words = set(['good', 'great', 'excellent', 'positive', 'amazing'])
74
+ negative_words = set(['bad', 'poor', 'negative', 'terrible', 'horrible'])
 
75
 
76
  words = text.lower().split()
77
  pos_count = sum(1 for word in words if word in positive_words)
 
103
  async def execute_tool(self, action, parameters):
104
  if self.tool_execution_count >= self.max_tools_per_turn:
105
  return "Tool usage limit reached for this turn."
106
+
107
  self.tool_execution_count += 1
108
 
109
  if action == "scrape":
110
  url = parameters.get("url")
111
  if not self.toolkit.validate_url(url):
112
  return "Invalid URL provided."
 
113
  html_content = await self.toolkit.fetch_webpage_async(url)
114
  if html_content.startswith("Error"):
115
  return html_content
 
116
  text_content = self.toolkit.extract_text_from_html(html_content)
117
  summary = self.toolkit.summarize_text(text_content)
118
  sentiment = self.toolkit.analyze_sentiment(text_content)
119
+ return {'summary': summary, 'sentiment': sentiment, 'full_text': text_content[:1000] + '...' if len(text_content) > 1000 else text_content}
120
+
121
+ if action == "analyze":
 
 
 
 
 
 
 
 
 
122
  text = parameters.get("text")
123
  if not text:
124
  return "No text provided for analysis"
125
+ return {'sentiment': self.toolkit.analyze_sentiment(text), 'summary': self.toolkit.summarize_text(text)}
126
+
 
 
 
 
127
  return f"Unknown tool: {action}"
128
 
129
+ # Chat Interaction
130
  async def chat_with_agent(user_input, chat_history, agent_core):
131
  start_time = time.time()
132
  try:
133
  # Reset tool counter for new turn
134
  agent_core.tool_execution_count = 0
135
+ system_prompt = """You are OmniAgent, a highly advanced AI assistant with multiple capabilities."""
136
+
137
+ messages = [{"role": "system", "content": system_prompt}, {"role": "user", "content": user_input}]
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  async def get_cerebras_response():
140
+ response = cerebras_client.completions.create(prompt=f"{system_prompt}\n\nUser: {user_input}", max_tokens=1000, temperature=0.7)
 
 
 
 
141
  return response.text
142
 
143
  async def get_groq_response():
144
+ completion = groq_client.chat.completions.create(messages=messages, temperature=0.7, max_tokens=2048, stream=True)
 
 
 
 
 
145
  return completion
146
+
147
+ # Parallel AI Responses
148
  cerebras_future = asyncio.create_task(get_cerebras_response())
149
  groq_stream = await get_groq_response()
150
 
 
152
  response = ""
153
  chain_of_thought = ""
154
 
 
155
  for chunk in groq_stream:
156
  if chunk.choices[0].delta and chunk.choices[0].delta.content:
157
  content = chunk.choices[0].delta.content
158
  response += content
 
159
  if "Chain of Thought:" in content:
160
  chain_of_thought += content.split("Chain of Thought:", 1)[-1]
161
 
 
166
  action = action_match.group(1)
167
  try:
168
  parameters = json.loads(action_match.group(2))
169
+ tool_result = await agent_core.execute_tool(parameters.get("action"), parameters.get("parameters", {}))
 
 
 
170
  response += f"\nTool Result: {json.dumps(tool_result, indent=2)}\n"
171
  except json.JSONDecodeError:
172
  response += "\nError: Invalid tool parameters\n"
173
 
174
+ # Get Cerebras response and combine
175
  cerebras_response = await cerebras_future
 
 
176
  final_response = f"{response}\n\nAdditional Insights:\n{cerebras_response}"
177
 
178
  # Update context
 
191
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
192
  agent_core = AgentCore()
193
 
194
+ gr.Markdown("""# 🌟 OmniAgent: Advanced AI Assistant""")
 
195
 
196
  with gr.Row():
197
  with gr.Column(scale=6):
198
+ chat_history = gr.Chatbot(label="Interaction History", height=600, show_label=True)
 
 
 
 
199
  with gr.Column(scale=2):
200
  with gr.Accordion("Performance Metrics", open=True):
201
  compute_time = gr.Textbox(label="Processing Time", interactive=False)
202
  token_usage_display = gr.Textbox(label="Resource Usage", interactive=False)
203
  with gr.Accordion("Agent Insights", open=True):
204
+ chain_of_thought_display = gr.Textbox(label="Reasoning Process", interactive=False, lines=10)
 
 
 
 
205
 
206
+ user_input = gr.Textbox(label="Your Request", placeholder="How can I assist you today?", lines=3)
207
+ send_button = gr.Button("Send", variant="primary")
208
+ clear_button = gr.Button("Clear History", variant="secondary")
209
+ export_button = gr.Button("Export Chat", variant="secondary")
 
 
 
 
 
 
210
 
211
  async def handle_chat(chat_history, user_input):
212
  if not user_input.strip():
213
  return chat_history, "", "", ""
214
 
215
+ ai_response, chain_of_thought, compute_info, token_usage = await chat_with_agent(user_input, chat_history, agent_core)
 
 
 
 
 
216
  chat_history.append((user_input, ai_response))
217
  return chat_history, chain_of_thought, compute_info, token_usage
218
 
 
225
  return "No chat history to export.", ""
226
 
227
  filename = f"omnigent_chat_{int(time.time())}.txt"
228
+ chat_text = "\n".join([f"User: {item[0]}\nAI: {item[1]}\n" for item in chat_history])
 
 
 
 
229
  with open(filename, "w") as file:
230
  file.write(chat_text)
231
  return f"Chat exported to {filename}", ""
232
 
233
  # Event handlers
234
+ send_button.click(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
235
+ clear_button.click(clear_chat, outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
236
+ export_button.click(export_chat, inputs=[chat_history], outputs=[compute_time, chain_of_thought_display])
237
+ user_input.submit(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
  gr.Markdown("""### 🚀 Advanced Capabilities:
240
  - Dual AI Model Processing
 
251
 
252
  if __name__ == "__main__":
253
  demo = create_interface()
254
+ demo.launch(share=True)