Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -167,6 +167,29 @@ class BasicAgent:
|
|
167 |
except Exception as e:
|
168 |
print(f"Error processing image: {str(e)}")
|
169 |
return f"I encountered an error analyzing the image: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
def process_question(self, question: str) -> str:
|
172 |
try:
|
@@ -175,6 +198,16 @@ class BasicAgent:
|
|
175 |
if image_url_match and self.gemini_model:
|
176 |
image_url = image_url_match.group(0)
|
177 |
return self._process_image_query(question, image_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
# Check if this is a request about a YouTube video
|
180 |
youtube_patterns = ["youtube.com", "youtu.be", "watch youtube", "youtube video"]
|
|
|
167 |
except Exception as e:
|
168 |
print(f"Error processing image: {str(e)}")
|
169 |
return f"I encountered an error analyzing the image: {str(e)}"
|
170 |
+
|
171 |
+
def _calculate(self, expression):
|
172 |
+
try:
|
173 |
+
# Basic calculator using Python's eval with safety restrictions
|
174 |
+
import math, re
|
175 |
+
safe_dict = {k: v for k, v in math.__dict__.items() if not k.startswith('__')}
|
176 |
+
# Only allow safe math operations
|
177 |
+
if re.match(r'^[\d\s\+\-\*\/\(\)\.\,\^\%\w]+$', expression):
|
178 |
+
result = eval(expression, {"__builtins__": {}}, safe_dict)
|
179 |
+
return f"Calculation result: {result}"
|
180 |
+
return "Invalid calculation expression"
|
181 |
+
except Exception as e:
|
182 |
+
return f"Calculation error: {str(e)}"
|
183 |
+
|
184 |
+
def _read_csv(self, file_url):
|
185 |
+
try:
|
186 |
+
response = requests.get(file_url)
|
187 |
+
import io, csv, pandas as pd
|
188 |
+
df = pd.read_csv(io.StringIO(response.text))
|
189 |
+
summary = f"CSV contains {len(df)} rows, {len(df.columns)} columns.\nColumns: {', '.join(df.columns)}\nSample data: {df.head(3).to_string()}"
|
190 |
+
return summary
|
191 |
+
except Exception as e:
|
192 |
+
return f"CSV reading error: {str(e)}"
|
193 |
|
194 |
def process_question(self, question: str) -> str:
|
195 |
try:
|
|
|
198 |
if image_url_match and self.gemini_model:
|
199 |
image_url = image_url_match.group(0)
|
200 |
return self._process_image_query(question, image_url)
|
201 |
+
|
202 |
+
# Read csv
|
203 |
+
if "calculate" in question.lower() and any(c in question for c in "+-*/"):
|
204 |
+
calculation = re.search(r'calculate\s+([\d\s\+\-\*\/\(\)\.\,\^\%]+)', question, re.IGNORECASE)
|
205 |
+
if calculation:
|
206 |
+
return self._calculate(calculation.group(1))
|
207 |
+
|
208 |
+
csv_url_match = re.search(r'https?://\S+\.csv', question, re.IGNORECASE)
|
209 |
+
if csv_url_match:
|
210 |
+
return self._read_csv(csv_url_match.group(0))
|
211 |
|
212 |
# Check if this is a request about a YouTube video
|
213 |
youtube_patterns = ["youtube.com", "youtu.be", "watch youtube", "youtube video"]
|