Spaces:
Sleeping
Sleeping
Delete tools.py
Browse files
tools.py
DELETED
|
@@ -1,121 +0,0 @@
|
|
| 1 |
-
import tempfile
|
| 2 |
-
import requests
|
| 3 |
-
import os
|
| 4 |
-
from urllib.parse import urlparse
|
| 5 |
-
from typing import Optional, List
|
| 6 |
-
import yt_dlp
|
| 7 |
-
import imageio
|
| 8 |
-
from PIL import Image
|
| 9 |
-
import whisper
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
-
|
| 12 |
-
# Fallback tool decorator if gaia_benchmark.tools is not available
|
| 13 |
-
try:
|
| 14 |
-
from gaia_benchmark.tools import tool
|
| 15 |
-
except ImportError:
|
| 16 |
-
def tool(func):
|
| 17 |
-
return func
|
| 18 |
-
|
| 19 |
-
load_dotenv()
|
| 20 |
-
|
| 21 |
-
@tool
|
| 22 |
-
def use_vision_model(question: str, images: List[Image.Image]) -> str:
|
| 23 |
-
return "Vision model is not available for Mistral. Please integrate a separate endpoint for image analysis."
|
| 24 |
-
|
| 25 |
-
@tool
|
| 26 |
-
def review_youtube_video(url: str, question: str) -> str:
|
| 27 |
-
return "This tool is currently unsupported with Mistral. Please remove or replace."
|
| 28 |
-
|
| 29 |
-
@tool
|
| 30 |
-
def youtube_frames_to_images(url: str, sample_interval_seconds: int = 5) -> List[Image.Image]:
|
| 31 |
-
with tempfile.TemporaryDirectory() as tmpdir:
|
| 32 |
-
ydl_opts = {
|
| 33 |
-
'format': 'bestvideo[height<=1080]+bestaudio/best[height<=1080]/best',
|
| 34 |
-
'outtmpl': os.path.join(tmpdir, 'video.%(ext)s'),
|
| 35 |
-
'quiet': True,
|
| 36 |
-
'noplaylist': True,
|
| 37 |
-
'merge_output_format': 'mp4',
|
| 38 |
-
'force_ipv4': True,
|
| 39 |
-
}
|
| 40 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 41 |
-
ydl.download([url])
|
| 42 |
-
|
| 43 |
-
video_path = next((os.path.join(tmpdir, f) for f in os.listdir(tmpdir) if f.endswith('.mp4')), None)
|
| 44 |
-
reader = imageio.get_reader(video_path)
|
| 45 |
-
fps = reader.get_meta_data().get('fps', 25)
|
| 46 |
-
frame_interval = int(fps * sample_interval_seconds)
|
| 47 |
-
images = [Image.fromarray(frame) for idx, frame in enumerate(reader) if idx % frame_interval == 0]
|
| 48 |
-
reader.close()
|
| 49 |
-
return images
|
| 50 |
-
|
| 51 |
-
@tool
|
| 52 |
-
def read_file(filepath: str) -> str:
|
| 53 |
-
try:
|
| 54 |
-
with open(filepath, 'r', encoding='utf-8') as file:
|
| 55 |
-
return file.read()
|
| 56 |
-
except Exception as e:
|
| 57 |
-
return f"Error reading file: {str(e)}"
|
| 58 |
-
|
| 59 |
-
@tool
|
| 60 |
-
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
| 61 |
-
try:
|
| 62 |
-
if not filename:
|
| 63 |
-
filename = os.path.basename(urlparse(url).path) or f"download_{os.urandom(4).hex()}"
|
| 64 |
-
filepath = os.path.join(tempfile.gettempdir(), filename)
|
| 65 |
-
response = requests.get(url)
|
| 66 |
-
with open(filepath, 'wb') as f:
|
| 67 |
-
f.write(response.content)
|
| 68 |
-
return filepath
|
| 69 |
-
except Exception as e:
|
| 70 |
-
return f"Error downloading file: {str(e)}"
|
| 71 |
-
|
| 72 |
-
@tool
|
| 73 |
-
def extract_text_from_image(image_path: str) -> str:
|
| 74 |
-
try:
|
| 75 |
-
import pytesseract
|
| 76 |
-
return pytesseract.image_to_string(Image.open(image_path))
|
| 77 |
-
except Exception as e:
|
| 78 |
-
return f"Error extracting text: {str(e)}"
|
| 79 |
-
|
| 80 |
-
@tool
|
| 81 |
-
def analyze_csv_file(file_path: str, query: str) -> str:
|
| 82 |
-
try:
|
| 83 |
-
import pandas as pd
|
| 84 |
-
df = pd.read_csv(file_path)
|
| 85 |
-
return f"Loaded CSV with shape {df.shape} and columns: {df.columns.tolist()}"
|
| 86 |
-
except Exception as e:
|
| 87 |
-
return f"CSV error: {str(e)}"
|
| 88 |
-
|
| 89 |
-
@tool
|
| 90 |
-
def analyze_excel_file(file_path: str, query: str) -> str:
|
| 91 |
-
try:
|
| 92 |
-
import pandas as pd
|
| 93 |
-
df = pd.read_excel(file_path)
|
| 94 |
-
return f"Loaded Excel with shape {df.shape} and columns: {df.columns.tolist()}"
|
| 95 |
-
except Exception as e:
|
| 96 |
-
return f"Excel error: {str(e)}"
|
| 97 |
-
|
| 98 |
-
@tool
|
| 99 |
-
def youtube_transcribe(url: str) -> str:
|
| 100 |
-
model = whisper.load_model("small")
|
| 101 |
-
with tempfile.TemporaryDirectory() as tmpdir:
|
| 102 |
-
ydl_opts = {
|
| 103 |
-
'format': 'bestaudio/best',
|
| 104 |
-
'outtmpl': os.path.join(tmpdir, 'audio.%(ext)s'),
|
| 105 |
-
'quiet': True,
|
| 106 |
-
'noplaylist': True,
|
| 107 |
-
'postprocessors': [{
|
| 108 |
-
'key': 'FFmpegExtractAudio',
|
| 109 |
-
'preferredcodec': 'wav',
|
| 110 |
-
}],
|
| 111 |
-
'force_ipv4': True,
|
| 112 |
-
}
|
| 113 |
-
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 114 |
-
ydl.download([url])
|
| 115 |
-
|
| 116 |
-
audio_path = next((os.path.join(tmpdir, f) for f in os.listdir(tmpdir) if f.endswith('.wav')), None)
|
| 117 |
-
return model.transcribe(audio_path)['text']
|
| 118 |
-
|
| 119 |
-
@tool
|
| 120 |
-
def transcribe_audio(audio_file_path: str) -> str:
|
| 121 |
-
return whisper.load_model("small").transcribe(audio_file_path)['text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|