Spaces:
Sleeping
Sleeping
File size: 16,164 Bytes
ff23105 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 | import requests
import base64
import logging
from playwright.sync_api import sync_playwright
import threading
import speech_recognition as sr
from pydub import AudioSegment
import io
import tempfile
import sys
import os
import pandas as pd
import numpy as np
import speech_recognition as sr
from bs4 import BeautifulSoup
import pydub
from pydub import AudioSegment
import pypdf
import zipfile
import duckdb
from PIL import Image
import json
logger = logging.getLogger(__name__)
def run_python_code(code: str) -> str:
"""
Executes Python code and returns the output.
"""
try:
logger.info("Executing Python code...")
# Robustly extract code from markdown blocks if present
if "```python" in code:
code = code.split("```python")[1].split("```")[0].strip()
elif "```" in code:
code = code.split("```")[1].split("```")[0].strip()
logger.info(f"Code:\n{code}")
# Create a buffer to capture stdout
old_stdout = sys.stdout
redirected_output = io.StringIO()
sys.stdout = redirected_output
# Execution context
local_scope = {
"pd": pd,
"np": np,
"requests": requests,
"io": io,
"sr": sr,
"pydub": pydub,
"sys": sys,
"os": os,
"BeautifulSoup": BeautifulSoup,
"pypdf": pypdf,
"zipfile": zipfile,
"duckdb": duckdb,
"Image": Image
}
try:
exec(code, {}, local_scope)
except Exception as exec_error:
return f"Error executing code: {exec_error}"
finally:
sys.stdout = old_stdout
output = redirected_output.getvalue()
logger.info(f"Code Output:\n{output}")
return output if output.strip() else "Code executed successfully but produced no output. Did you forget to print the result?"
except Exception as e:
logger.error(f"System error during code execution: {e}")
return f"System error: {e}"
def execute_python(code: str) -> str:
"""
Executes Python code and returns the output.
Use this for math, data analysis (pandas, numpy, duckdb), and file processing.
"""
return run_python_code(code)
def read_pdf(url: str) -> str:
"""
Downloads a PDF from a URL and extracts its text content.
"""
try:
logger.info(f"Reading PDF from: {url}")
if not url.startswith("http"):
return f"Error: URL must be absolute. Received: {url}"
response = requests.get(url)
response.raise_for_status()
with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
temp_pdf.write(response.content)
temp_pdf_path = temp_pdf.name
text = ""
try:
reader = pypdf.PdfReader(temp_pdf_path)
for page in reader.pages:
text += page.extract_text() + "\n"
except Exception as e:
return f"Error reading PDF: {e}"
finally:
os.remove(temp_pdf_path)
return text[:5000] # Truncate if too long to avoid context overflow
except Exception as e:
logger.error(f"Error downloading PDF: {e}")
return f"Error downloading PDF: {e}"
def read_zip(url: str) -> str:
"""
Downloads a ZIP file from a URL, lists its contents, and extracts text from small files.
"""
try:
logger.info(f"Reading ZIP from: {url}")
if not url.startswith("http"):
return f"Error: URL must be absolute. Received: {url}"
response = requests.get(url)
response.raise_for_status()
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as temp_zip:
temp_zip.write(response.content)
temp_zip_path = temp_zip.name
result = "ZIP Contents:\n"
try:
with zipfile.ZipFile(temp_zip_path, 'r') as zip_ref:
for file_info in zip_ref.infolist():
result += f"- {file_info.filename} ({file_info.file_size} bytes)\n"
# If it's a small text file, try to read it
if file_info.file_size < 10000 and not file_info.filename.endswith(('.png', '.jpg', '.jpeg', '.gif')):
try:
with zip_ref.open(file_info) as f:
content = f.read().decode('utf-8', errors='ignore')
result += f" Content: {content[:500]}\n"
except:
pass
except Exception as e:
return f"Error reading ZIP: {e}"
finally:
os.remove(temp_zip_path)
return result
except Exception as e:
logger.error(f"Error downloading ZIP: {e}")
return f"Error downloading ZIP: {e}"
def search_history(query: str) -> str:
"""
Searches the history of solved quizzes for a given query (e.g., a previous URL).
Returns the answer if found.
"""
try:
history_file = "history.jsonl"
if not os.path.exists(history_file):
return "No history found."
results = []
with open(history_file, "r") as f:
for line in f:
try:
entry = json.loads(line)
if query in str(entry):
results.append(str(entry))
except:
pass
if results:
return "\n".join(results)
return "No matching history found."
except Exception as e:
return f"Error searching history: {e}"
def transcribe_audio(url: str) -> str:
"""
Downloads an audio file from a URL and transcribes it using Google Speech Recognition.
Supports MP3, WAV, etc.
"""
try:
logger.info(f"Transcribing audio from: {url}")
if not url.startswith("http"):
return f"Error: URL must be absolute. Received: {url}"
response = requests.get(url)
response.raise_for_status()
# Create a temporary file to save the audio
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_wav:
temp_wav_path = temp_wav.name
# Convert to WAV if needed (using pydub)
try:
audio_content = io.BytesIO(response.content)
audio = AudioSegment.from_file(audio_content)
audio.export(temp_wav_path, format="wav")
except Exception as e:
logger.error(f"Error converting audio: {e}")
return f"Error converting audio: {e}"
# Transcribe using local Whisper (tiny model)
# This runs entirely on-device (CPU/GPU) and does not make external API calls.
import whisper
# Load model (downloads once if not cached)
model = whisper.load_model("tiny")
result = model.transcribe(temp_wav_path)
text = result["text"]
logger.info(f"WHISPER OUTPUT: {text}")
# Clean up
os.remove(temp_wav_path)
return text
except Exception as e:
logger.error(f"Error transcribing audio: {e}")
return f"Error transcribing audio: {e}"
def understand_image(url: str, prompt: str = "Describe this image in detail") -> str:
"""
Analyzes an image from a URL using the agent's vision capabilities (via API).
Returns a description of the image.
"""
try:
logger.info(f"Analyzing image from: {url}")
if os.path.exists(url):
# It's a local file, convert to data URI
try:
with open(url, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8')
# Determine mime type based on extension, default to jpeg
mime_type = "image/jpeg"
if url.lower().endswith(".png"):
mime_type = "image/png"
elif url.lower().endswith(".gif"):
mime_type = "image/gif"
elif url.lower().endswith(".webp"):
mime_type = "image/webp"
url = f"data:{mime_type};base64,{encoded_string}"
except Exception as e:
return f"Error reading local image file: {e}"
if not url.startswith("http") and not url.startswith("data:"):
return f"Error: URL must be absolute (http/https), a data URI, or a valid local file path. Received: {url[:50]}..."
api_key = os.getenv("AI_TOKEN") or os.getenv("OPENROUTER_API_KEY")
if not api_key:
return "Error: AI_TOKEN not found."
# Use OpenRouter API to analyze the image
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"model": "google/gemini-2.0-flash-lite-001",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": url}}
]
}
]
}
response = requests.post("https://aipipe.org/openrouter/v1/chat/completions", headers=headers, json=payload)
response.raise_for_status()
result = response.json()
description = result['choices'][0]['message']['content']
logger.info(f"IMAGE ANALYSIS OUTPUT: {description}")
return description
except Exception as e:
logger.error(f"Error analyzing image: {e}")
return f"Error analyzing image: {e}"
def call_api(url: str, method: str = "GET", headers: dict = None, json_data: dict = None) -> str:
"""
Makes an HTTP request to an external API.
Useful for sourcing data from APIs as required by the quiz.
"""
try:
logger.info(f"Calling API: {method} {url}")
if not url.startswith("http"):
return f"Error: URL must be absolute. Received: {url}"
response = requests.request(method, url, headers=headers, json=json_data)
try:
return json.dumps(response.json(), indent=2)
except:
return response.text
except Exception as e:
logger.error(f"Error calling API: {e}")
return f"Error calling API: {e}"
def fetch_page_text(url: str) -> dict:
"""
Fetches the text content of a web page using Playwright.
Also extracts links, audio sources, and takes a screenshot if visual elements are present.
Args:
url (str): The URL of the page to fetch.
Returns:
dict: A dictionary containing the 'content' (text + links/media) or 'error'.
"""
result = {}
try:
logger.info(f"Fetching page text from: {url}")
if not url.startswith("http"):
result["error"] = f"Error: URL must be absolute. Received: {url}"
return result
def run_playwright():
try:
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto(url)
page.wait_for_load_state("networkidle")
html_content = page.content()
# Screenshot Logic
images = []
try:
has_visuals = page.evaluate("() => document.querySelectorAll('canvas, img').length > 0")
if has_visuals:
import uuid
unique_id = str(uuid.uuid4())[:8]
screenshot_path = f"/tmp/screenshot_{unique_id}.jpg"
page.screenshot(path=screenshot_path, full_page=True, type='jpeg', quality=50)
images.append(f"Image: [Page Screenshot]({screenshot_path})")
logger.info(f"Visual elements detected. Saved screenshot to {screenshot_path}")
except Exception as e:
logger.warning(f"Error handling screenshot in fetch_page_text: {e}")
browser.close()
# Parse with BS4
soup = BeautifulSoup(html_content, 'html.parser')
text_content = soup.get_text(separator='\n', strip=True)
links = []
for a in soup.find_all('a', href=True):
href = a['href']
links.append(f"Link: [{a.get_text(strip=True)}]({href})")
audio_sources = []
for audio in soup.find_all('audio'):
if audio.get('src'):
audio_sources.append(f"Audio: {audio['src']}")
for source in audio.find_all('source', src=True):
audio_sources.append(f"Audio: {source['src']}")
result["content"] = text_content + "\n\n--- Extracted Links & Media ---\n" + "\n".join(links + audio_sources + images)
except Exception as e:
result["error"] = str(e)
thread = threading.Thread(target=run_playwright)
thread.start()
thread.join()
if "error" in result:
logger.error(f"Error fetching page: {result['error']}")
return f"Error fetching page: {result['error']}"
content = result.get("content", "")
logger.info(f"Fetched content (first 100 chars): {content[:100]}")
return content
except Exception as e:
logger.error(f"Error fetching page: {e}")
return f"Error fetching page: {e}"
def fetch_page_scripts(url: str) -> str:
"""
Fetches only the scripts (inline and src) from a web page.
Useful when the page mentions embedded logic or hidden code.
"""
result = {}
try:
logger.info(f"Fetching page scripts from: {url}")
if not url.startswith("http"):
return f"Error: URL must be absolute. Received: {url}"
def run_playwright():
try:
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
page = browser.new_page()
page.goto(url)
page.wait_for_load_state("networkidle")
html_content = page.content()
browser.close()
soup = BeautifulSoup(html_content, 'html.parser')
scripts = []
for script in soup.find_all('script'):
if script.get('src'):
scripts.append(f"Script Source: {script['src']}")
elif script.string and script.string.strip():
scripts.append(f"Inline Script: {script.string.strip()[:2000]}")
result["content"] = "--- Extracted Scripts ---\n" + "\n".join(scripts)
except Exception as e:
result["error"] = str(e)
thread = threading.Thread(target=run_playwright)
thread.start()
thread.join()
if "error" in result:
return f"Error fetching scripts: {result['error']}"
return result.get("content", "No scripts found.")
except Exception as e:
return f"Error fetching scripts: {e}"
|