Commit ·
b96a1eb
1
Parent(s): 4e5a109
add new dependencies: transformers, torch, librosa, openpyxl, and pdfplumber
Browse files- app.py +172 -50
- requirements.txt +6 -1
app.py
CHANGED
|
@@ -47,6 +47,34 @@ except ImportError:
|
|
| 47 |
TavilyClient = None
|
| 48 |
print("WARNING: tavily-python library not found. Tavily search provider will be unavailable. Install: pip install tavily-python")
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
logging.basicConfig(
|
| 51 |
level=logging.INFO,
|
| 52 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
@@ -64,9 +92,13 @@ TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
|
| 64 |
AGENT_DEFAULT_TIMEOUT = 15
|
| 65 |
MAX_CONTEXT_LENGTH_LLM = 15000
|
| 66 |
|
| 67 |
-
MAX_FILE_SIZE = 5 * 1024 * 1024
|
| 68 |
CSV_SAMPLE_ROWS = 3
|
| 69 |
-
MAX_FILE_CONTEXT_LENGTH = 7000
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
DEFAULT_RAG_CONFIG = {
|
| 72 |
'search': {
|
|
@@ -98,6 +130,25 @@ DEFAULT_RAG_CONFIG = {
|
|
| 98 |
}
|
| 99 |
|
| 100 |
class FileProcessor:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
@staticmethod
|
| 102 |
def _get_filename_from_url(url_or_header: Optional[str]) -> str:
|
| 103 |
if not url_or_header: return "unknown_file"
|
|
@@ -112,22 +163,34 @@ class FileProcessor:
|
|
| 112 |
@staticmethod
|
| 113 |
def process(content: bytes, filename: Optional[str] = "unknown_file", content_type: Optional[str] = "") -> str:
|
| 114 |
content_type_str = content_type.lower() if content_type else ""
|
| 115 |
-
filename_str = filename if filename else "unknown_file"
|
| 116 |
|
| 117 |
try:
|
| 118 |
if len(content) > MAX_FILE_SIZE:
|
| 119 |
gaia_logger.warning(f"File '{filename_str}' exceeds max size {MAX_FILE_SIZE} bytes.")
|
| 120 |
return f"Error: File '{filename_str}' exceeds maximum allowed size ({MAX_FILE_SIZE // (1024*1024)}MB)."
|
| 121 |
-
|
| 122 |
-
if 'csv' in content_type_str or filename_str.
|
| 123 |
gaia_logger.info(f"Processing CSV file: {filename_str}")
|
| 124 |
return FileProcessor._process_csv(content, filename_str)
|
| 125 |
-
elif 'json' in content_type_str or filename_str.
|
| 126 |
gaia_logger.info(f"Processing JSON file: {filename_str}")
|
| 127 |
return FileProcessor._process_json(content, filename_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
elif 'text/plain' in content_type_str or \
|
| 129 |
('text/' in content_type_str and not any(sub in content_type_str for sub in ['html', 'xml'])) or \
|
| 130 |
-
filename_str.
|
| 131 |
gaia_logger.info(f"Processing Text-like file: {filename_str} (Content-Type: {content_type_str})")
|
| 132 |
return FileProcessor._process_text(content, filename_str)
|
| 133 |
else:
|
|
@@ -137,6 +200,14 @@ class FileProcessor:
|
|
| 137 |
gaia_logger.error(f"File processing error for '{filename_str}': {str(e)}", exc_info=True)
|
| 138 |
return f"Error processing file '{filename_str}': An unexpected error occurred."
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
@staticmethod
|
| 141 |
def _process_csv(content: bytes, filename: str) -> str:
|
| 142 |
try:
|
|
@@ -145,54 +216,35 @@ class FileProcessor:
|
|
| 145 |
for enc in encodings_to_try:
|
| 146 |
try:
|
| 147 |
df = pd.read_csv(io.BytesIO(content), encoding=enc)
|
| 148 |
-
gaia_logger.info(f"Successfully read CSV '{filename}' with encoding '{enc}'.")
|
| 149 |
break
|
| 150 |
-
except UnicodeDecodeError:
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
gaia_logger.warning(f"Pandas read_csv error for '{filename}' with encoding '{enc}': {read_e}")
|
| 154 |
-
continue
|
| 155 |
-
|
| 156 |
-
if df is None:
|
| 157 |
-
gaia_logger.error(f"Failed to decode CSV '{filename}' with any attempted encoding.")
|
| 158 |
-
return f"Error: Could not decode CSV file '{filename}'. It might be corrupted or use an unsupported encoding."
|
| 159 |
|
| 160 |
num_rows, num_cols = len(df), len(df.columns)
|
| 161 |
cols_str = ', '.join(df.columns)
|
| 162 |
sample_str = df.head(CSV_SAMPLE_ROWS).to_markdown(index=False)
|
| 163 |
-
|
| 164 |
summary = (
|
| 165 |
f"CSV Document Summary: '{filename}' ({num_rows} rows, {num_cols} columns):\n"
|
| 166 |
-
f"Columns: {cols_str}\n"
|
| 167 |
-
f"First {min(CSV_SAMPLE_ROWS, num_rows)} sample rows:\n{sample_str}"
|
| 168 |
)
|
| 169 |
-
|
| 170 |
-
summary = summary[:MAX_FILE_CONTEXT_LENGTH - 20] + "\n... (summary truncated)"
|
| 171 |
-
return summary
|
| 172 |
except Exception as e:
|
| 173 |
-
|
| 174 |
-
return f"Error processing CSV file '{filename}': {str(e)}"
|
| 175 |
|
| 176 |
@staticmethod
|
| 177 |
def _process_json(content: bytes, filename: str) -> str:
|
| 178 |
try:
|
| 179 |
decoded_content = content.decode('utf-8', errors='replace')
|
| 180 |
data = json.loads(decoded_content)
|
| 181 |
-
compact_json = json.dumps(data, separators=(',', ':'))
|
| 182 |
-
if len(compact_json) > MAX_FILE_CONTEXT_LENGTH:
|
| 183 |
-
pretty_truncated = json.dumps(data, indent=2)[:MAX_FILE_CONTEXT_LENGTH - 20] + "\n... (JSON truncated)"
|
| 184 |
-
return f"JSON Document: '{filename}' (Content partially shown due to size):\n{pretty_truncated}"
|
| 185 |
-
|
| 186 |
pretty_json = json.dumps(data, indent=2)
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
except json.JSONDecodeError
|
| 190 |
-
gaia_logger.error(f"JSON decoding error for '{filename}': {str(e)} - trying to return raw text snippet.")
|
| 191 |
text_snippet = content.decode('utf-8', errors='ignore')[:MAX_FILE_CONTEXT_LENGTH - 100]
|
| 192 |
-
return f"Error: Invalid JSON
|
| 193 |
except Exception as e:
|
| 194 |
-
|
| 195 |
-
return f"Error processing JSON file '{filename}': {str(e)}"
|
| 196 |
|
| 197 |
@staticmethod
|
| 198 |
def _process_text(content: bytes, filename: str) -> str:
|
|
@@ -202,24 +254,94 @@ class FileProcessor:
|
|
| 202 |
for enc in encodings_to_try:
|
| 203 |
try:
|
| 204 |
text = content.decode(enc)
|
| 205 |
-
gaia_logger.info(f"Successfully decoded text file '{filename}' with encoding '{enc}'.")
|
| 206 |
break
|
| 207 |
-
except UnicodeDecodeError:
|
| 208 |
-
|
| 209 |
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
text = content.decode('utf-8', errors='ignore')
|
| 213 |
-
return f"Text Document (decoding issues, some characters may be lost): '{filename}':\n{text[:MAX_FILE_CONTEXT_LENGTH]}..." if len(text) > MAX_FILE_CONTEXT_LENGTH else text
|
| 214 |
-
|
| 215 |
-
summary = f"Text Document: '{filename}':\n{text[:MAX_FILE_CONTEXT_LENGTH]}"
|
| 216 |
-
if len(text) > MAX_FILE_CONTEXT_LENGTH:
|
| 217 |
-
summary += "..."
|
| 218 |
-
return summary
|
| 219 |
except Exception as e:
|
| 220 |
-
gaia_logger.error(f"Text processing error for '{filename}': {str(e)}", exc_info=True)
|
| 221 |
return f"Error processing text file '{filename}': {str(e)}"
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
@staticmethod
|
| 224 |
def _handle_unknown_type(content: bytes, filename: str) -> str:
|
| 225 |
gaia_logger.warning(f"Attempting to handle unknown file type for '{filename}' as text snippet.")
|
|
|
|
| 47 |
TavilyClient = None
|
| 48 |
print("WARNING: tavily-python library not found. Tavily search provider will be unavailable. Install: pip install tavily-python")
|
| 49 |
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
from transformers import pipeline as hf_transformers_pipeline
|
| 53 |
+
import torch
|
| 54 |
+
except ImportError:
|
| 55 |
+
hf_transformers_pipeline = None
|
| 56 |
+
torch = None
|
| 57 |
+
print("WARNING: transformers or torch library not found. Audio processing will be unavailable. Install with: pip install transformers torch")
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
import librosa
|
| 61 |
+
except ImportError:
|
| 62 |
+
librosa = None
|
| 63 |
+
print("WARNING: librosa library not found. Audio processing may be impaired. Install with: pip install librosa")
|
| 64 |
+
|
| 65 |
+
try:
|
| 66 |
+
import openpyxl # Engine for pandas to read .xlsx
|
| 67 |
+
except ImportError:
|
| 68 |
+
openpyxl = None
|
| 69 |
+
print("WARNING: openpyxl library not found. .xlsx file processing might fail. Install with: pip install openpyxl")
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
import pdfplumber
|
| 73 |
+
except ImportError:
|
| 74 |
+
pdfplumber = None
|
| 75 |
+
print("WARNING: pdfplumber library not found. PDF file processing will be unavailable. Install with: pip install pdfplumber")
|
| 76 |
+
# --- End of New Imports ---
|
| 77 |
+
|
| 78 |
logging.basicConfig(
|
| 79 |
level=logging.INFO,
|
| 80 |
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
| 92 |
AGENT_DEFAULT_TIMEOUT = 15
|
| 93 |
MAX_CONTEXT_LENGTH_LLM = 15000
|
| 94 |
|
| 95 |
+
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5MB
|
| 96 |
CSV_SAMPLE_ROWS = 3
|
| 97 |
+
MAX_FILE_CONTEXT_LENGTH = 7000 # Max characters for file context summary
|
| 98 |
+
|
| 99 |
+
# Global variable for ASR pipeline (initialized on first use)
|
| 100 |
+
asr_pipeline_instance: Optional[Any] = None
|
| 101 |
+
ASR_MODEL_NAME = "openai/whisper-tiny" # Smaller model for resource efficiency
|
| 102 |
|
| 103 |
DEFAULT_RAG_CONFIG = {
|
| 104 |
'search': {
|
|
|
|
| 130 |
}
|
| 131 |
|
| 132 |
class FileProcessor:
|
| 133 |
+
@staticmethod
|
| 134 |
+
def _get_asr_pipeline():
|
| 135 |
+
global asr_pipeline_instance
|
| 136 |
+
if asr_pipeline_instance is None and hf_transformers_pipeline and torch:
|
| 137 |
+
try:
|
| 138 |
+
# device = 0 if torch.cuda.is_available() else -1 # For GPU if available
|
| 139 |
+
# Simpler for HF Spaces CPU instances:
|
| 140 |
+
device = -1 # CPU
|
| 141 |
+
asr_pipeline_instance = hf_transformers_pipeline(
|
| 142 |
+
"automatic-speech-recognition",
|
| 143 |
+
model=ASR_MODEL_NAME,
|
| 144 |
+
device=device
|
| 145 |
+
)
|
| 146 |
+
gaia_logger.info(f"ASR pipeline initialized: {ASR_MODEL_NAME} on {'cuda' if device==0 else 'cpu'}.")
|
| 147 |
+
except Exception as e:
|
| 148 |
+
gaia_logger.error(f"Failed to initialize ASR pipeline: {e}", exc_info=True)
|
| 149 |
+
return None
|
| 150 |
+
return asr_pipeline_instance
|
| 151 |
+
|
| 152 |
@staticmethod
|
| 153 |
def _get_filename_from_url(url_or_header: Optional[str]) -> str:
|
| 154 |
if not url_or_header: return "unknown_file"
|
|
|
|
| 163 |
@staticmethod
|
| 164 |
def process(content: bytes, filename: Optional[str] = "unknown_file", content_type: Optional[str] = "") -> str:
|
| 165 |
content_type_str = content_type.lower() if content_type else ""
|
| 166 |
+
filename_str = filename.lower() if filename else "unknown_file"
|
| 167 |
|
| 168 |
try:
|
| 169 |
if len(content) > MAX_FILE_SIZE:
|
| 170 |
gaia_logger.warning(f"File '{filename_str}' exceeds max size {MAX_FILE_SIZE} bytes.")
|
| 171 |
return f"Error: File '{filename_str}' exceeds maximum allowed size ({MAX_FILE_SIZE // (1024*1024)}MB)."
|
| 172 |
+
|
| 173 |
+
if 'csv' in content_type_str or filename_str.endswith('.csv'):
|
| 174 |
gaia_logger.info(f"Processing CSV file: {filename_str}")
|
| 175 |
return FileProcessor._process_csv(content, filename_str)
|
| 176 |
+
elif 'json' in content_type_str or filename_str.endswith('.json'):
|
| 177 |
gaia_logger.info(f"Processing JSON file: {filename_str}")
|
| 178 |
return FileProcessor._process_json(content, filename_str)
|
| 179 |
+
elif ('excel' in content_type_str or 'spreadsheetml' in content_type_str or \
|
| 180 |
+
filename_str.endswith(('.xlsx', '.xls'))) and openpyxl: # Check for openpyxl
|
| 181 |
+
gaia_logger.info(f"Processing Excel file: {filename_str}")
|
| 182 |
+
return FileProcessor._process_excel(content, filename_str)
|
| 183 |
+
elif ('pdf' in content_type_str or filename_str.endswith('.pdf')) and pdfplumber: # Check for pdfplumber
|
| 184 |
+
gaia_logger.info(f"Processing PDF file: {filename_str}")
|
| 185 |
+
return FileProcessor._process_pdf(content, filename_str)
|
| 186 |
+
elif ('audio' in content_type_str or \
|
| 187 |
+
filename_str.endswith(('.mp3', '.wav', '.flac', '.ogg', '.m4a'))) and \
|
| 188 |
+
hf_transformers_pipeline and librosa: # Check for ASR libs
|
| 189 |
+
gaia_logger.info(f"Processing Audio file: {filename_str}")
|
| 190 |
+
return FileProcessor._process_audio(content, filename_str)
|
| 191 |
elif 'text/plain' in content_type_str or \
|
| 192 |
('text/' in content_type_str and not any(sub in content_type_str for sub in ['html', 'xml'])) or \
|
| 193 |
+
filename_str.endswith(('.txt', '.md', '.py', '.js', '.c', '.cpp', '.java', '.html', '.xml', '.log')):
|
| 194 |
gaia_logger.info(f"Processing Text-like file: {filename_str} (Content-Type: {content_type_str})")
|
| 195 |
return FileProcessor._process_text(content, filename_str)
|
| 196 |
else:
|
|
|
|
| 200 |
gaia_logger.error(f"File processing error for '{filename_str}': {str(e)}", exc_info=True)
|
| 201 |
return f"Error processing file '{filename_str}': An unexpected error occurred."
|
| 202 |
|
| 203 |
+
@staticmethod
|
| 204 |
+
def _truncate_text(text: str, filename: str, type_name: str) -> str:
|
| 205 |
+
if len(text) > MAX_FILE_CONTEXT_LENGTH:
|
| 206 |
+
truncated_text = text[:MAX_FILE_CONTEXT_LENGTH - 25] + "\n... (content truncated)"
|
| 207 |
+
gaia_logger.info(f"{type_name} '{filename}' content truncated from {len(text)} to {len(truncated_text)} chars.")
|
| 208 |
+
return truncated_text
|
| 209 |
+
return text
|
| 210 |
+
|
| 211 |
@staticmethod
|
| 212 |
def _process_csv(content: bytes, filename: str) -> str:
|
| 213 |
try:
|
|
|
|
| 216 |
for enc in encodings_to_try:
|
| 217 |
try:
|
| 218 |
df = pd.read_csv(io.BytesIO(content), encoding=enc)
|
|
|
|
| 219 |
break
|
| 220 |
+
except UnicodeDecodeError: continue
|
| 221 |
+
except Exception: continue
|
| 222 |
+
if df is None: return f"Error: Could not decode CSV '{filename}'."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
num_rows, num_cols = len(df), len(df.columns)
|
| 225 |
cols_str = ', '.join(df.columns)
|
| 226 |
sample_str = df.head(CSV_SAMPLE_ROWS).to_markdown(index=False)
|
|
|
|
| 227 |
summary = (
|
| 228 |
f"CSV Document Summary: '{filename}' ({num_rows} rows, {num_cols} columns):\n"
|
| 229 |
+
f"Columns: {cols_str}\nFirst {min(CSV_SAMPLE_ROWS, num_rows)} sample rows:\n{sample_str}"
|
|
|
|
| 230 |
)
|
| 231 |
+
return FileProcessor._truncate_text(summary, filename, "CSV")
|
|
|
|
|
|
|
| 232 |
except Exception as e:
|
| 233 |
+
return f"Error processing CSV '{filename}': {str(e)}"
|
|
|
|
| 234 |
|
| 235 |
@staticmethod
|
| 236 |
def _process_json(content: bytes, filename: str) -> str:
|
| 237 |
try:
|
| 238 |
decoded_content = content.decode('utf-8', errors='replace')
|
| 239 |
data = json.loads(decoded_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
pretty_json = json.dumps(data, indent=2)
|
| 241 |
+
summary = f"JSON Document: '{filename}':\n{pretty_json}"
|
| 242 |
+
return FileProcessor._truncate_text(summary, filename, "JSON")
|
| 243 |
+
except json.JSONDecodeError:
|
|
|
|
| 244 |
text_snippet = content.decode('utf-8', errors='ignore')[:MAX_FILE_CONTEXT_LENGTH - 100]
|
| 245 |
+
return f"Error: Invalid JSON in '{filename}'. Snippet:\n{text_snippet}..."
|
| 246 |
except Exception as e:
|
| 247 |
+
return f"Error processing JSON '{filename}': {str(e)}"
|
|
|
|
| 248 |
|
| 249 |
@staticmethod
|
| 250 |
def _process_text(content: bytes, filename: str) -> str:
|
|
|
|
| 254 |
for enc in encodings_to_try:
|
| 255 |
try:
|
| 256 |
text = content.decode(enc)
|
|
|
|
| 257 |
break
|
| 258 |
+
except UnicodeDecodeError: continue
|
| 259 |
+
if text is None: text = content.decode('utf-8', errors='ignore')
|
| 260 |
|
| 261 |
+
summary = f"Text Document: '{filename}':\n{text}"
|
| 262 |
+
return FileProcessor._truncate_text(summary, filename, "Text")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 263 |
except Exception as e:
|
|
|
|
| 264 |
return f"Error processing text file '{filename}': {str(e)}"
|
| 265 |
|
| 266 |
+
@staticmethod
|
| 267 |
+
def _process_excel(content: bytes, filename: str) -> str:
|
| 268 |
+
if not openpyxl: return f"Error: Excel processing skipped for '{filename}', openpyxl library not available."
|
| 269 |
+
try:
|
| 270 |
+
# Reading all sheets and summarizing; can be adjusted for first sheet or specific sheets
|
| 271 |
+
xls = pd.ExcelFile(io.BytesIO(content), engine='openpyxl')
|
| 272 |
+
summary_parts = [f"Excel Document Summary: '{filename}'"]
|
| 273 |
+
for sheet_name in xls.sheet_names:
|
| 274 |
+
df = xls.parse(sheet_name)
|
| 275 |
+
num_rows, num_cols = len(df), len(df.columns)
|
| 276 |
+
cols_str = ', '.join(df.columns)
|
| 277 |
+
sample_str = df.head(CSV_SAMPLE_ROWS).to_markdown(index=False)
|
| 278 |
+
sheet_summary = (
|
| 279 |
+
f"\n---\nSheet: '{sheet_name}' ({num_rows} rows, {num_cols} columns):\n"
|
| 280 |
+
f"Columns: {cols_str}\nFirst {min(CSV_SAMPLE_ROWS, num_rows)} sample rows:\n{sample_str}"
|
| 281 |
+
)
|
| 282 |
+
summary_parts.append(sheet_summary)
|
| 283 |
+
# Check length to avoid overly long summaries from many sheets
|
| 284 |
+
if sum(len(p) for p in summary_parts) > MAX_FILE_CONTEXT_LENGTH * 0.8: # Soft limit before final truncate
|
| 285 |
+
summary_parts.append("\n... (further sheets omitted due to length)")
|
| 286 |
+
break
|
| 287 |
+
full_summary = "".join(summary_parts)
|
| 288 |
+
return FileProcessor._truncate_text(full_summary, filename, "Excel")
|
| 289 |
+
except Exception as e:
|
| 290 |
+
gaia_logger.error(f"Excel processing error for '{filename}': {str(e)}", exc_info=True)
|
| 291 |
+
return f"Error processing Excel file '{filename}': {str(e)}"
|
| 292 |
+
|
| 293 |
+
@staticmethod
|
| 294 |
+
def _process_pdf(content: bytes, filename: str) -> str:
|
| 295 |
+
if not pdfplumber: return f"Error: PDF processing skipped for '{filename}', pdfplumber library not available."
|
| 296 |
+
text_content = ""
|
| 297 |
+
try:
|
| 298 |
+
with io.BytesIO(content) as pdf_buffer:
|
| 299 |
+
with pdfplumber.open(pdf_buffer) as pdf:
|
| 300 |
+
for i, page in enumerate(pdf.pages):
|
| 301 |
+
page_text = page.extract_text()
|
| 302 |
+
if page_text:
|
| 303 |
+
text_content += page_text + "\n"
|
| 304 |
+
if len(text_content) > MAX_FILE_CONTEXT_LENGTH * 1.2: # Allow slight overage before hard truncate
|
| 305 |
+
gaia_logger.info(f"PDF '{filename}' text extraction stopped early due to length at page {i+1}.")
|
| 306 |
+
break
|
| 307 |
+
if not text_content:
|
| 308 |
+
return f"PDF Document: '{filename}'. No text could be extracted or PDF is empty."
|
| 309 |
+
summary = f"PDF Document: '{filename}':\n{text_content}"
|
| 310 |
+
return FileProcessor._truncate_text(summary, filename, "PDF")
|
| 311 |
+
except Exception as e:
|
| 312 |
+
gaia_logger.error(f"PDF processing error for '{filename}': {str(e)}", exc_info=True)
|
| 313 |
+
return f"Error processing PDF file '{filename}': {str(e)}"
|
| 314 |
+
|
| 315 |
+
@staticmethod
|
| 316 |
+
def _process_audio(content: bytes, filename: str) -> str:
|
| 317 |
+
asr_pipeline = FileProcessor._get_asr_pipeline()
|
| 318 |
+
if not asr_pipeline:
|
| 319 |
+
return f"Error: Audio processing skipped for '{filename}', ASR pipeline not available."
|
| 320 |
+
if not librosa:
|
| 321 |
+
return f"Error: Audio processing skipped for '{filename}', librosa library not available."
|
| 322 |
+
try:
|
| 323 |
+
with io.BytesIO(content) as audio_buffer:
|
| 324 |
+
# Load audio, ensure 16kHz mono for Whisper
|
| 325 |
+
y, sr = librosa.load(audio_buffer, sr=16000, mono=True)
|
| 326 |
+
|
| 327 |
+
|
| 328 |
+
gaia_logger.info(f"Transcribing audio file: {filename} ({len(y)/sr:.2f} seconds)")
|
| 329 |
+
start_time = time.time()
|
| 330 |
+
transcription_result = asr_pipeline(y) # Pass numpy array directly
|
| 331 |
+
end_time = time.time()
|
| 332 |
+
gaia_logger.info(f"Audio transcription for '{filename}' took {end_time - start_time:.2f} seconds.")
|
| 333 |
+
|
| 334 |
+
transcribed_text = transcription_result.get("text", "") if isinstance(transcription_result, dict) else str(transcription_result)
|
| 335 |
+
|
| 336 |
+
if not transcribed_text.strip():
|
| 337 |
+
return f"Audio Document: '{filename}'. Transcription result was empty."
|
| 338 |
+
|
| 339 |
+
summary = f"Audio Document (Transcription): '{filename}':\n{transcribed_text}"
|
| 340 |
+
return FileProcessor._truncate_text(summary, filename, "Audio Transcription")
|
| 341 |
+
except Exception as e:
|
| 342 |
+
gaia_logger.error(f"Audio processing/transcription error for '{filename}': {str(e)}", exc_info=True)
|
| 343 |
+
return f"Error processing Audio file '{filename}': {str(e)}"
|
| 344 |
+
|
| 345 |
@staticmethod
|
| 346 |
def _handle_unknown_type(content: bytes, filename: str) -> str:
|
| 347 |
gaia_logger.warning(f"Attempting to handle unknown file type for '{filename}' as text snippet.")
|
requirements.txt
CHANGED
|
@@ -7,4 +7,9 @@ google-generativeai
|
|
| 7 |
pandas
|
| 8 |
beautifulsoup4
|
| 9 |
lxml
|
| 10 |
-
tavily-python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
pandas
|
| 8 |
beautifulsoup4
|
| 9 |
lxml
|
| 10 |
+
tavily-python
|
| 11 |
+
transformers
|
| 12 |
+
torch
|
| 13 |
+
librosa
|
| 14 |
+
openpyxl
|
| 15 |
+
pdfplumber
|