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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -196
app.py CHANGED
@@ -1,254 +1,213 @@
1
  import gradio as gr
2
  import os
3
  import time
4
- import asyncio
5
  from cerebras.cloud.sdk import Cerebras
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
 
19
- if not CEREBRAS_API_KEY or not GROQ_API_KEY:
20
- raise ValueError("Both CEREBRAS_API_KEY and GROQ_API_KEY environment variables must be set.")
21
-
22
- cerebras_client = Cerebras(api_key=CEREBRAS_API_KEY)
23
- groq_client = Groq(api_key=GROQ_API_KEY)
24
-
25
- # Configure logging
26
- logging.basicConfig(
27
- level=logging.INFO,
28
- format='%(asctime)s - %(levelname)s - %(message)s',
29
- filename='agent.log'
30
- )
31
-
32
- # Helper Functions
33
- class EnhancedToolkit:
34
- @staticmethod
35
- async def fetch_webpage_async(url, timeout=10):
36
- try:
37
- async with aiohttp.ClientSession() as session:
38
- async with session.get(url, timeout=timeout) as response:
39
- if response.status == 200:
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):
56
- try:
57
- result = urlparse(url)
58
- return all([result.scheme, result.netloc])
59
- except ValueError:
60
- return False
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)
78
- neg_count = sum(1 for word in words if word in negative_words)
79
-
80
- if pos_count > neg_count:
81
- return 'positive'
82
- elif neg_count > pos_count:
83
- return 'negative'
84
- return 'neutral'
85
-
86
- class AgentCore:
87
- def __init__(self):
88
- self.toolkit = EnhancedToolkit()
89
- self.tool_execution_count = 0
90
- self.max_tools_per_turn = 5
91
- self.context_window = []
92
- self.max_context_items = 10
93
-
94
- def update_context(self, user_input, ai_response):
95
- self.context_window.append({
96
- 'user_input': user_input,
97
- 'ai_response': ai_response,
98
- 'timestamp': datetime.now().isoformat()
99
- })
100
- if len(self.context_window) > self.max_context_items:
101
- self.context_window.pop(0)
102
-
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
-
151
- # Process responses
 
 
 
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
 
162
- # Tool execution handling
163
  if "Action:" in content:
164
  action_match = re.search(r"Action: (\w+), Parameters: (\{.*\})", content)
165
- if action_match:
 
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
179
- agent_core.update_context(user_input, final_response)
 
 
 
 
 
180
 
181
  compute_time = time.time() - start_time
182
- token_usage = len(user_input.split()) + len(final_response.split())
183
-
184
- return final_response, chain_of_thought, f"Compute Time: {compute_time:.2f}s", f"Tokens: {token_usage}"
185
 
186
  except Exception as e:
187
- logging.error(f"Error in chat_with_agent: {str(e)}", exc_info=True)
188
- return f"Error: {str(e)}", "", "Error occurred", ""
189
 
190
- def create_interface():
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
 
219
  def clear_chat():
220
- agent_core.context_window.clear()
221
  return [], "", "", ""
222
 
223
  def export_chat(chat_history):
224
  if not chat_history:
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
241
- - Advanced Web Content Analysis
242
- - Sentiment Understanding
243
- - Intelligent Text Summarization
244
- - Context-Aware Responses
245
- - Enhanced Error Handling
246
- - Detailed Performance Tracking
247
- - Comprehensive Logging
248
- """)
249
 
250
  return demo
251
 
252
- if __name__ == "__main__":
253
- demo = create_interface()
254
- demo.launch(share=True)
 
1
  import gradio as gr
2
  import os
3
  import time
 
4
  from cerebras.cloud.sdk import Cerebras
 
5
  import requests
6
  from bs4 import BeautifulSoup
7
+ from urllib.parse import urljoin, urlparse
8
+ from groq import Groq
9
  import re
10
  import json
 
 
11
 
12
+ # --- Constants and API Setup ---
13
  CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY")
14
+ if not CEREBRAS_API_KEY:
15
+ raise ValueError("CEREBRAS_API_KEY environment variable is not set.")
16
+
17
  GROQ_API_KEY = os.getenv("GROQ_API_KEY")
18
+ if not GROQ_API_KEY:
19
+ raise ValueError("GROQ_API_KEY environment variable is not set.")
20
+
21
+ client_cerebras = Cerebras(api_key=CEREBRAS_API_KEY)
22
+ client_groq = Groq(api_key=GROQ_API_KEY)
23
+
24
+ # --- Model Rate Limit Info ---
25
+ CHAT_COMPLETION_MODELS_INFO = """
26
+ Chat Completion
27
+ ID Requests per Minute Requests per Day Tokens per Minute Tokens per Day
28
+ gemma-7b-it 30 14,400 15,000 500,000
29
+ gemma2-9b-it 30 14,400 15,000 500,000
30
+ llama-3.1-70b-versatile 30 14,400 6,000 200,000
31
+ llama-3.1-8b-instant 30 14,400 20,000 500,000
32
+ llama-3.2-11b-text-preview 30 7,000 7,000 500,000
33
+ llama-3.2-11b-vision-preview 30 7,000 7,000 500,000
34
+ llama-3.2-1b-preview 30 7,000 7,000 500,000
35
+ llama-3.2-3b-preview 30 7,000 7,000 500,000
36
+ llama-3.2-90b-text-preview 30 7,000 7,000 500,000
37
+ llama-3.2-90b-vision-preview 15 3,500 7,000 250,000
38
+ llama-3.3-70b-specdec 30 1,000 6,000 100,000
39
+ llama-3.3-70b-versatile 30 1,000 6,000 100,000
40
+ llama-guard-3-8b 30 14,400 15,000 500,000
41
+ llama3-70b-8192 30 14,400 6,000 500,000
42
+ llama3-8b-8192 30 14,400 30,000 500,000
43
+ llama3-groq-70b-8192-tool-use-preview 30 14,400 15,000 500,000
44
+ llama3-groq-8b-8192-tool-use-preview 30 14,400 15,000 500,000
45
+ llava-v1.5-7b-4096-preview 30 14,400 30,000 (No limit)
46
+ mixtral-8x7b-32768 30 14,400 5,000 500,000
47
+ """
48
+
49
+ SPEECH_TO_TEXT_MODELS_INFO = """
50
+ Speech To Text
51
+ ID Requests per Minute Requests per Day Audio Seconds per Hour Audio Seconds per Day
52
+ distil-whisper-large-v3-en 20 2,000 7,200 28,800
53
+ whisper-large-v3 20 2,000 7,200 28,800
54
+ whisper-large-v3-turbo 20 2,000 7,200 28,800
55
+ """
56
+
57
+ def get_model_info():
58
+ return f"""
59
+ {CHAT_COMPLETION_MODELS_INFO}
60
+
61
+ {SPEECH_TO_TEXT_MODELS_INFO}
62
+ """
63
+
64
+
65
+ # --- Helper Functions ---
66
+
67
+ def is_valid_url(url):
68
+ try:
69
+ result = urlparse(url)
70
+ return all([result.scheme, result.netloc])
71
+ except ValueError:
72
+ return False
73
+
74
+
75
+ def fetch_webpage(url):
76
+ try:
77
+ response = requests.get(url, timeout=10)
78
+ response.raise_for_status() # Raise an exception for bad status codes
79
+ return response.text
80
+ except requests.exceptions.RequestException as e:
81
+ return f"Error fetching URL: {e}"
82
+
83
+
84
+ def extract_text_from_html(html):
85
+ soup = BeautifulSoup(html, 'html.parser')
86
+ text = soup.get_text(separator=' ', strip=True)
87
+ return text
88
+
89
 
90
+ # --- Chat Logic with Groq ---
91
+ async def chat_with_groq(user_input, chat_history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  start_time = time.time()
93
  try:
94
+ # Prepare chat history for the prompt
95
+ formatted_history = "\n".join([f"User: {msg[0]}\nAI: {msg[1]}" for msg in chat_history[-10:]])
96
+
97
+ messages = [
98
+ {"role": "system", "content": f"""You are IntellijMind, a highly advanced and proactive AI agent. You are designed to assist users in achieving their goals through detailed insights, creative problem-solving, and the use of various tools. Your objective is to understand the user's intentions, break them into logical steps, and use available tools when needed to achieve the best outcome. Available tools: scrape with a URL, and search_internet with a query. Be creative and inject humor when appropriate. You have access to multiple tools to help the user with their requests. Available actions: take_action: 'scrape', parameters: url, take_action: 'search_internet', parameters: query. Example action: Action: take_action, Parameters: {{"action":"scrape", "url":"https://example.com"}} or Action: take_action, Parameters: {{"action":"search_internet", "query":"latest news on AI"}} . Current conversation: {formatted_history}"""},
99
+ {"role": "user", "content": user_input}
100
+ ]
101
+
102
+ if user_input.lower() == "model info":
103
+ response = get_model_info()
104
+ return response, "", f"Compute Time: {time.time() - start_time:.2f} seconds", f"Tokens used: {len(user_input.split()) + len(response.split())}"
105
+
106
+ completion = client_groq.chat.completions.create(
107
+ model="llama3-groq-70b-8192-tool-use-preview",
108
+ messages=messages,
109
+ temperature=1,
110
+ max_tokens=2048,
111
+ top_p=1,
112
+ stream=True,
113
+ stop=None,
114
+ )
115
+
116
  response = ""
117
  chain_of_thought = ""
118
+ tool_execution_count = 0
119
+ for chunk in completion:
120
  if chunk.choices[0].delta and chunk.choices[0].delta.content:
121
  content = chunk.choices[0].delta.content
122
  response += content
123
  if "Chain of Thought:" in content:
124
  chain_of_thought += content.split("Chain of Thought:", 1)[-1]
125
 
 
126
  if "Action:" in content:
127
  action_match = re.search(r"Action: (\w+), Parameters: (\{.*\})", content)
128
+ if action_match and tool_execution_count < 3: # Limit tool use to avoid infinite loops
129
+ tool_execution_count +=1
130
  action = action_match.group(1)
131
+ parameters = json.loads(action_match.group(2))
132
+ if action == "take_action":
133
+ if parameters.get("action") == "scrape":
134
+ url = parameters.get("url")
135
+ if is_valid_url(url):
136
+ html_content = fetch_webpage(url)
137
+ if not html_content.startswith("Error"):
138
+ webpage_text = extract_text_from_html(html_content)
139
+ response += f"\nWebpage Content: {webpage_text}\n"
140
+ else:
141
+ response += f"\nError scraping webpage: {html_content}\n"
142
+ else:
143
+ response += "\nInvalid URL provided.\n"
144
+ elif parameters.get("action") == "search_internet":
145
+ query = parameters.get("query")
146
+ response += f"\n Search query: {query}. Note: Search is simulated in this environment. Results may vary. \n"
147
+ # Replace the line with a real internet search if you have a search api
148
+ response += f"\n Search Results: Mock Results for query: {query} \n"
149
 
150
  compute_time = time.time() - start_time
151
+ token_usage = len(user_input.split()) + len(response.split())
152
+ return response, chain_of_thought, f"Compute Time: {compute_time:.2f} seconds", f"Tokens used: {token_usage}"
 
153
 
154
  except Exception as e:
155
+ return "Error: Unable to process your request.", "", str(e), ""
 
156
 
 
 
 
157
 
158
+ # --- Gradio Interface ---
159
+ def gradio_ui():
160
+ with gr.Blocks() as demo:
161
+ gr.Markdown("""# 🚀 IntellijMind: The Autonomous AI Agent\nExperience the forefront of AI capabilities, where the agent proactively achieves your goals!""")
162
 
163
  with gr.Row():
164
  with gr.Column(scale=6):
165
+ chat_history = gr.Chatbot(label="Chat History")
166
  with gr.Column(scale=2):
167
+ compute_time = gr.Textbox(label="Compute Time", interactive=False)
168
+ chain_of_thought_display = gr.Textbox(label="Chain of Thought", interactive=False, lines=10)
169
+ token_usage_display = gr.Textbox(label="Token Usage", interactive=False)
170
+
171
+ user_input = gr.Textbox(label="Type your message", placeholder="Ask me anything...", lines=2)
172
+
173
 
174
+ with gr.Row():
175
+ send_button = gr.Button("Send", variant="primary")
176
+ clear_button = gr.Button("Clear Chat")
177
+ export_button = gr.Button("Export Chat History")
178
 
179
  async def handle_chat(chat_history, user_input):
180
  if not user_input.strip():
181
+ return chat_history, "", "", "", "Please enter a valid message."
182
+
183
+ ai_response, chain_of_thought, compute_info, token_usage = await chat_with_groq(user_input, chat_history)
184
+
185
+
186
  chat_history.append((user_input, ai_response))
187
  return chat_history, chain_of_thought, compute_info, token_usage
188
 
189
  def clear_chat():
 
190
  return [], "", "", ""
191
 
192
  def export_chat(chat_history):
193
  if not chat_history:
194
+ return "", "No chat history to export."
195
+ chat_text = "\n".join([f"User: {item[0]}\nAI: {item[1]}" for item in chat_history])
196
+ filename = f"chat_history_{int(time.time())}.txt"
 
197
  with open(filename, "w") as file:
198
  file.write(chat_text)
199
+ return f"Chat history exported to {filename}.", ""
200
 
 
201
  send_button.click(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
202
  clear_button.click(clear_chat, outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
203
  export_button.click(export_chat, inputs=[chat_history], outputs=[compute_time, chain_of_thought_display])
204
+
205
  user_input.submit(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
206
 
207
+ gr.Markdown("""---\n### 🌟 Features:\n- **Autonomous Agent**: Proactively pursues your goals.\n- **Advanced Tool Use**: Utilizes multiple tools like web scraping and search.\n- **Dynamic and Creative**: Engages with humor and creative responses.\n- **Enhanced Chat History**: Maintains better context of the conversation.\n- **Real-Time Performance Metrics**: Measure response compute time instantly.\n- **Token Usage Tracking**: Monitor token usage per response for transparency.\n- **Export Chat History**: Save your conversation as a text file for future reference.\n- **User-Friendly Design**: Intuitive chatbot interface with powerful features.\n- **Insightful Chain of Thought**: See the reasoning process behind AI decisions.\n- **Submit on Enter**: Seamless interaction with keyboard support.\n""")
 
 
 
 
 
 
 
 
 
208
 
209
  return demo
210
 
211
+ # Run the Gradio app
212
+ demo = gradio_ui()
213
+ demo.launch()