Threatthriver commited on
Commit
0627e44
β€’
1 Parent(s): 30e3c7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -59
app.py CHANGED
@@ -9,56 +9,43 @@ import re
9
  import json
10
 
11
  # --- Constants and API Setup ---
12
- CEREBRAS_API_KEY = os.getenv("CEREBRAS_API_KEY")
13
- if not CEREBRAS_API_KEY:
14
- raise ValueError("CEREBRAS_API_KEY environment variable is not set.")
 
 
15
 
16
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
17
- if not GROQ_API_KEY:
18
- raise ValueError("GROQ_API_KEY environment variable is not set.")
19
 
20
  client_cerebras = Cerebras(api_key=CEREBRAS_API_KEY)
21
  client_groq = Groq(api_key=GROQ_API_KEY)
22
 
23
  # --- Model Rate Limit Info ---
24
- CHAT_COMPLETION_MODELS_INFO = """
25
- Chat Completion
26
- ID Requests per Minute Requests per Day Tokens per Minute Tokens per Day
27
- gemma-7b-it 30 14,400 15,000 500,000
28
- gemma2-9b-it 30 14,400 15,000 500,000
29
- llama-3.1-70b-versatile 30 14,400 6,000 200,000
30
- llama-3.1-8b-instant 30 14,400 20,000 500,000
31
- llama-3.2-11b-text-preview 30 7,000 7,000 500,000
32
- llama-3.2-11b-vision-preview) 30 7,000 7,000 500,000
33
- llama-3.2-1b-preview) 30 7,000 7,000 500,000
34
- llama-3.2-90b-text-preview 30 7,000 7,000 500,000
35
- llama-3.2-90b-vision-preview 15 3,500 7,000 250,000
36
- llama-3.3-70b-specdec 30 1,000 6,000 100,000
37
- llama-3.3-70b-versatile 30 1,000 6,000 100,000
38
- llama-guard-3-8b 30 14,400 15,000 500,000
39
- llama3-70b-8192 30 14,400 6,000 500,000
40
- llama3-8b-8192 30 14,400 30,000 500,000
41
- llama3-groq-70b-8192-tool-use-preview 30 14,400 15,000 500,000
42
- llama3-groq-8b-8192-tool-use-preview 30 14,400 15,000 500,000
43
- llava-v1.5-7b-4096-preview 30 14,400 30,000 (No limit)
44
- mixtral-8x7b-32768 30 14,400 5,000 500,000
45
- """
46
-
47
- SPEECH ToText
48
- ID Requests per Minute Requests per Day Audio Seconds per Hour Audio Seconds per Day
49
- distil-whisper-large-v3-en 20 2,000 7,200 28,800
50
- whisper-large-v3 20 2,000 7,200 28,800
51
- whisper-large-v3-turbo 20 2,000 7,200 28,800
52
 
53
  def get_model_info():
54
- return f"""
55
- {CHAT_COMPLETION_MODELS_INFO)
56
-
57
- {SPEECH_TO_TEXT_MODELS_INFO}
58
- """
 
 
59
 
60
  # --- Helper Functions ---
61
  def is_valid_url(url):
 
62
  try:
63
  result = urlparse(url)
64
  return all([result.scheme, result.netloc])
@@ -66,6 +53,7 @@ def is_valid_url(url):
66
  return False
67
 
68
  def fetch_webpage(url):
 
69
  try:
70
  response = requests.get(url, timeout=10)
71
  response.raise_for_status()
@@ -74,20 +62,25 @@ def fetch_webpage(url):
74
  return f"Error fetching URL: {e}"
75
 
76
  def extract_text_from_html(html):
77
- soup = BeautifulSoup(html, 'html.parser)
 
78
  text = soup.get_text(separator=' ', strip=True)
79
  return text
80
 
81
  # --- Chat Logic with Groq ---
82
  async def chat_with_groq(user_input, chat_history):
 
83
  start_time = time.time()
84
  try:
85
- formatted_history = "\n".join([f"User: {msg[0]}\nAI: {msg[1]}" for msg in chat_history[-10:]])
 
86
 
87
  messages = [
88
  {"role": "system", "content": f"""
89
- 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}
90
- }},
 
 
91
  {"role": "user", "content": user_input}
92
  ]
93
 
@@ -112,31 +105,35 @@ async def chat_with_groq(user_input, chat_history):
112
  if chunk.choices[0].delta and chunk.choices[0].delta.content:
113
  content = chunk.choices[0].delta.content
114
  response += content
 
115
  if "Chain of Thought:" in content:
116
- chain_of_thought += content.split("Chain of Thought:", 1)[-1]
117
 
 
118
  if "Action:" in content:
119
  action_match = re.search(r"Action: (\w+), Parameters: (\{.*\})", content)
120
- if action_match and tool_execution_count < 3: # Limit tool use to avoid infinite loops
121
  tool_execution_count += 1
122
- action = action_match.group(1)
123
- parameters = json.loads(action_match.group(2))
124
  if action == "take_action":
125
  if parameters.get("action") == "scrape":
 
126
  url = parameters.get("url")
127
  if is_valid_url(url):
128
  html_content = fetch_webpage(url)
129
  if not html_content.startswith("Error"):
130
  webpage_text = extract_text_from_html(html_content)
131
- response += f"\nWebpage Content: {webpage_text}\n")
132
  else:
133
- response += f"\nError scraping webpage: {html_content}\n"
134
  else:
135
- response += "\nInvalid URL provided.\n"
136
  elif parameters.get("action") == "search_internet":
 
137
  query = parameters.get("query")
138
- response += f"\n Search query: {query}. Note: Search is simulated in this environment. Results may vary. \n"
139
- response += f"\nSearch Results: Mock Results for query: {query} \n"
140
 
141
  compute_time = time.time() - start_time
142
  token_usage = len(user_input.split()) + len(response.split())
@@ -167,7 +164,7 @@ def gradio_ui():
167
 
168
  async def handle_chat(chat_history, user_input):
169
  if not user_input.strip():
170
- return chat_history, "", "", "", "Please enter a valid message.")
171
 
172
  ai_response, chain_of_thought, compute_info, token_usage = await chat_with_groq(user_input, chat_history)
173
 
@@ -187,15 +184,16 @@ def gradio_ui():
187
  return f"Chat history exported to {filename}.", ""
188
 
189
  send_button.click(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
190
- clear_button.click(clear_chat, outputs=[chat_history, chain_of(thought_display, compute_time, token_usage_display])
191
- export_button.click(export_chat, inputs=[chat_history], outputs=[compute_time, chain_of(thought_display])
192
 
193
- user_input.submit(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of(thought_display, compute_time, token_usage_display])
194
 
195
- 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""")
196
 
197
  return demo
198
 
199
  # Run the Gradio app
200
- demo = gradio_ui()
201
- demo.launch()
 
 
9
  import json
10
 
11
  # --- Constants and API Setup ---
12
+ # **Environment Variable Validation**
13
+ def validate_env_var(var_name, env_var):
14
+ if not env_var:
15
+ raise ValueError(f"{var_name} environment variable is not set.")
16
+ return env_var
17
 
18
+ CEREBRAS_API_KEY = validate_env_var("CEREBRAS_API_KEY", os.getenv("CEREBRAS_API_KEY"))
19
+ GROQ_API_KEY = validate_env_var("GROQ_API_KEY", os.getenv("GROQ_API_KEY"))
 
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
+ # **Formatted as a Dictionary for Easy Access**
26
+ MODEL_INFO = {
27
+ "Chat Completion": {
28
+ "gemma-7b-it": {"requests_per_minute": 30, "tokens_per_minute": 15000},
29
+ # Add more models here...
30
+ },
31
+ "Speech to Text": {
32
+ "distil-whisper-large-v3-en": {"requests_per_minute": 20, "audio_seconds_per_hour": 7200},
33
+ # Add more models here...
34
+ }
35
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  def get_model_info():
38
+ """Returns formatted model info as a string"""
39
+ output = ""
40
+ for category, models in MODEL_INFO.items():
41
+ output += f"**{category}**\n"
42
+ for model, limits in models.items():
43
+ output += f"* {model}: {limits}\n"
44
+ return output
45
 
46
  # --- Helper Functions ---
47
  def is_valid_url(url):
48
+ """Checks if a URL is valid"""
49
  try:
50
  result = urlparse(url)
51
  return all([result.scheme, result.netloc])
 
53
  return False
54
 
55
  def fetch_webpage(url):
56
+ """Fetches a webpage with a 10-second timeout"""
57
  try:
58
  response = requests.get(url, timeout=10)
59
  response.raise_for_status()
 
62
  return f"Error fetching URL: {e}"
63
 
64
  def extract_text_from_html(html):
65
+ """Extracts text from HTML using BeautifulSoup"""
66
+ soup = BeautifulSoup(html, 'html.parser')
67
  text = soup.get_text(separator=' ', strip=True)
68
  return text
69
 
70
  # --- Chat Logic with Groq ---
71
  async def chat_with_groq(user_input, chat_history):
72
+ """Handles user input and returns AI response, chain of thought, compute time, and token usage"""
73
  start_time = time.time()
74
  try:
75
+ # **Simplified History Formatting**
76
+ formatted_history = "\n\n".join([f"User: {msg[0]}\nAI: {msg[1]}" for msg in chat_history[-10:]])
77
 
78
  messages = [
79
  {"role": "system", "content": f"""
80
+ You are IntellijMind, a highly advanced and proactive AI agent.
81
+ Available tools: scrape with a URL, and search_internet with a query.
82
+ Current conversation: {formatted_history}
83
+ """},
84
  {"role": "user", "content": user_input}
85
  ]
86
 
 
105
  if chunk.choices[0].delta and chunk.choices[0].delta.content:
106
  content = chunk.choices[0].delta.content
107
  response += content
108
+ # **Simplified Chain of Thought Extraction**
109
  if "Chain of Thought:" in content:
110
+ chain_of_thought += content.split("Chain of Thought:", 1)[-1].strip()
111
 
112
+ # **Simplified Tool Execution**
113
  if "Action:" in content:
114
  action_match = re.search(r"Action: (\w+), Parameters: (\{.*\})", content)
115
+ if action_match and tool_execution_count < 3:
116
  tool_execution_count += 1
117
+ action, parameters = action_match.groups()
118
+ parameters = json.loads(parameters)
119
  if action == "take_action":
120
  if parameters.get("action") == "scrape":
121
+ # **Simplified Scrape Action**
122
  url = parameters.get("url")
123
  if is_valid_url(url):
124
  html_content = fetch_webpage(url)
125
  if not html_content.startswith("Error"):
126
  webpage_text = extract_text_from_html(html_content)
127
+ response += f"\nWebpage Content: {webpage_text}\n"
128
  else:
129
+ response += f"\nError scraping webpage: {html_content}\n"
130
  else:
131
+ response += "\nInvalid URL provided.\n"
132
  elif parameters.get("action") == "search_internet":
133
+ # **Simplified Search Action**
134
  query = parameters.get("query")
135
+ response += f"\nSearch query: {query}. Note: Search is simulated in this environment. Results may vary.\n"
136
+ response += f"\nSearch Results: Mock Results for query: {query}\n"
137
 
138
  compute_time = time.time() - start_time
139
  token_usage = len(user_input.split()) + len(response.split())
 
164
 
165
  async def handle_chat(chat_history, user_input):
166
  if not user_input.strip():
167
+ return chat_history, "", "", "", "Please enter a valid message."
168
 
169
  ai_response, chain_of_thought, compute_info, token_usage = await chat_with_groq(user_input, chat_history)
170
 
 
184
  return f"Chat history exported to {filename}.", ""
185
 
186
  send_button.click(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
187
+ clear_button.click(clear_chat, outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
188
+ export_button.click(export_chat, inputs=[chat_history], outputs=[compute_time, chain_of_thought_display])
189
 
190
+ user_input.submit(handle_chat, inputs=[chat_history, user_input], outputs=[chat_history, chain_of_thought_display, compute_time, token_usage_display])
191
 
192
+ 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""")
193
 
194
  return demo
195
 
196
  # Run the Gradio app
197
+ if __name__ == "__main__":
198
+ demo = gradio_ui()
199
+ demo.launch()