Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -5,6 +5,7 @@ from langchain_community.document_loaders import WikipediaLoader
|
|
| 5 |
import pandas as pd
|
| 6 |
import whisper
|
| 7 |
import os
|
|
|
|
| 8 |
import tempfile
|
| 9 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 10 |
from PIL import Image
|
|
@@ -64,6 +65,32 @@ def divide(a: int, b: int) -> int:
|
|
| 64 |
raise ValueError("Cannot divide by zero.")
|
| 65 |
return a / b
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
@tool
|
| 69 |
def analyze_image(image_path: str, question: str = "What’s in this image?") -> str:
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
import whisper
|
| 7 |
import os
|
| 8 |
+
import re
|
| 9 |
import tempfile
|
| 10 |
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 11 |
from PIL import Image
|
|
|
|
| 65 |
raise ValueError("Cannot divide by zero.")
|
| 66 |
return a / b
|
| 67 |
|
| 68 |
+
def match_referenced_files(question: str, uploaded_files: list) -> list:
|
| 69 |
+
"""
|
| 70 |
+
Given a question string and a list of uploaded files with metadata,
|
| 71 |
+
return the list of files that are referenced in the question.
|
| 72 |
+
|
| 73 |
+
Parameters:
|
| 74 |
+
- question: str — The user's question.
|
| 75 |
+
- uploaded_files: list of dicts with keys:
|
| 76 |
+
- name (e.g., "data.xlsx")
|
| 77 |
+
- type (e.g., "excel", "image", "audio")
|
| 78 |
+
- path (e.g., "/tmp/data.xlsx")
|
| 79 |
+
|
| 80 |
+
Returns:
|
| 81 |
+
A list of file dicts that were referenced in the question.
|
| 82 |
+
"""
|
| 83 |
+
# Find all mentions of possible filenames in the question (e.g., .xlsx, .png, .mp3)
|
| 84 |
+
referenced_names = set(re.findall(r'[\w\-\s]+\.(xlsx|xls|csv|png|jpg|jpeg|mp3|wav)', question, flags=re.IGNORECASE))
|
| 85 |
+
|
| 86 |
+
matched = []
|
| 87 |
+
for file in uploaded_files:
|
| 88 |
+
base_name = os.path.basename(file["name"]).lower()
|
| 89 |
+
if any(base_name.endswith(name.lower()) for name in referenced_names):
|
| 90 |
+
matched.append(file)
|
| 91 |
+
|
| 92 |
+
return matched
|
| 93 |
+
|
| 94 |
|
| 95 |
@tool
|
| 96 |
def analyze_image(image_path: str, question: str = "What’s in this image?") -> str:
|