Spaces:
Sleeping
Sleeping
File size: 8,273 Bytes
58a3678 ae2d3da 77b5b16 58a3678 77b5b16 58a3678 77b5b16 58a3678 77b5b16 58a3678 77b5b16 58a3678 7be1746 58a3678 7be1746 | 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 | import base64
import os
import tempfile
import uuid
from io import BytesIO
from pathlib import Path
import pandas as pd
import requests
from langchain_core.messages import HumanMessage
from langchain_core.tools import tool
from langchain_google_genai import ChatGoogleGenerativeAI
from PIL import Image
@tool("file-download-tool", parse_docstring=True)
def download_file_from_url(url: str, filename: str = None) -> str:
"""Downloads a file from a URL and saves it to a temporary location.
This function downloads a file from a given URL and saves it in a local
temporary location as `filename`. If the filename is not provided, a
random name is generated. If download succeds, the function returns the
local path to the file. If an error occurs, it returns the exception
as a string.
Args:
url (str): The URL of the file to download.
filename (str, optional): The name to save the file as. If not set,
a random name will be generated for the file.
Returns:
str: The local path to the file, or the raised exception.
"""
try:
# Generate a filename if not provided
if not filename:
filename = f"downloaded_{uuid.uuid4()}"
# Create temporary file
temp_dir = tempfile.gettempdir()
filepath = Path(temp_dir) / filename
# Download the file
response = requests.get(url, stream=True)
response.raise_for_status()
# Save the file
with open(filepath, "wb") as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
return filepath
except Exception as e:
return str(e)
@tool("file-csv-read-tool", parse_docstring=True)
def read_csv(filepath: str) -> str:
"""Reads a csv file from filepath.
This function reads a csv file from `filepath` and returns its contents as
a markdown table. If an error occurs, it returns the exception as a string.
Args:
filepath (str): The local filepath to the file.
Returns:
str: The contents of the csv file in markdown table format.
"""
# Read the csv file
df = pd.read_csv(filepath)
# Convert to markdown table string
text_table = df.to_markdown(index=False)
return text_table
@tool("file-excel-read-tool", parse_docstring=True)
def read_excel(filepath: str) -> str:
"""Reads an excel file from filepath.
This function reads an xlsx file from `filepath` and returns its contents
as a markdown table. If an error occurs, it returns the exception as a
string.
Args:
filepath (str): The local filepath to the file.
Returns:
str: The contents of the excel file in markdown table format.
"""
# Read the csv file
df = pd.read_excel(filepath)
# Convert to markdown table string
text_table = df.to_markdown(index=False)
return text_table
@tool("file-code-read-tool", parse_docstring=True)
def read_code(filepath: str) -> str:
"""Reads a code file from filepath.
This function reads a code file from `filepath` and returns its contents
as a string. If an error occurs, it returns the exception as a string.
Args:
filepath (str): The local filepath to the file.
Returns:
str: The contents of the code file.
"""
try:
with open(filepath, "r") as f:
code_content = f.read()
return code_content
except Exception as e:
return str(e)
@tool("analyze-image-tool", parse_docstring=True)
async def analyze_image(img_path: str, query: str) -> str:
"""Analyzes the image at img_path and responds to the input query.
This function reads an image from `img_path`, converts it to its Base64
encoding as a string, and responds to the input query based on the image
analysis. If an error occurs, it returns the exception as a string.
Args:
img_path (str): The local filepath to the image.
query (str): The query about the image that needs to be answered.
Returns:
str: Answer to the query about the image.
"""
try:
# Open the image file
with Image.open(img_path) as img:
buffered = BytesIO()
# Save the image to the buffer in its original format
img.save(buffered, format=img.format)
img_bytes = buffered.getvalue()
b64_image = base64.b64encode(img_bytes).decode('utf-8')
# Initialize LLM
llm = ChatGoogleGenerativeAI(
google_api_key=os.getenv("GOOGLE_API_KEY"),
model=os.getenv("GOOGLE_AGENT_MODEL_NAME"),
temperature=0.7,
max_retries=5
)
# Call the LLM
message = HumanMessage(
content=[
{
"type": "text",
"text": query
},
{
"type": "image",
"source_type": "base64",
"mime_type": "image/png",
"data": b64_image
}
],
)
response = await llm.ainvoke([message])
return response.content
except Exception as e:
return str(e)
@tool("analyze-video-tool", parse_docstring=True)
async def analyze_video(video_url: str, query: str) -> str:
"""Analyzes the video at video_url and responds to the input query.
This function analyzes the video at the provided video_url and responds
to the input query based on the video analysis. If an error occurs, it
returns the exception as a string.
Args:
video_url (str): URL where the video is located.
query (str): The query about the video that needs to be answered.
Returns:
str: Answer to the query about the video url.
"""
try:
# Initialize LLM
llm = ChatGoogleGenerativeAI(
google_api_key=os.getenv("GOOGLE_API_KEY"),
model=os.getenv("GOOGLE_AGENT_MODEL_NAME"),
temperature=0.7,
max_retries=5
)
# Call the LLM
message = HumanMessage(
content=[
{
"type": "text",
"text": query
},
{
"type": "media",
"mime_type": "video/mp4",
"file_uri": video_url
}
],
)
response = await llm.ainvoke([message])
return response.content
except Exception as e:
return str(e)
@tool("analyze-audio-tool", parse_docstring=True)
async def analyze_audio(audio_path: str, query: str) -> str:
"""Analyzes the audio at audio_path and responds to the input query.
This function reads an audio from `audio_path`, converts it to its Base64
encoding as a string, and responds to the input query based on the audio
analysis. If an error occurs, it returns the exception as a string.
Args:
audio_path: String path where the audio file is located on disk.
query: The query about the audio file that needs to be answered.
Returns:
str: Answer to the query about the audio file.
"""
audio_mime_type = "audio/mpeg"
try:
with open(audio_path, "rb") as rb:
encoded_audio = base64.b64encode(rb.read()).decode('utf-8')
# Initialize LLM
llm = ChatGoogleGenerativeAI(
google_api_key=os.getenv("GOOGLE_API_KEY"),
model=os.getenv("GOOGLE_AGENT_MODEL_NAME"),
temperature=0.7,
max_retries=5
)
# Call the LLM
message = HumanMessage(
content=[
{
"type": "text",
"text": query
},
{
"type": "media",
"data": encoded_audio,
"mime_type": audio_mime_type
}
],
)
response = await llm.ainvoke([message])
return response.content
except Exception as e:
return str(e)
|