Spaces:
Running
Running
add file handling from remote api calls
Browse files
app.py
CHANGED
|
@@ -161,8 +161,39 @@ def cleanup_temp_video(file_path):
|
|
| 161 |
def process_input(input_data):
|
| 162 |
"""Process input data to extract text for ASL conversion"""
|
| 163 |
if isinstance(input_data, str):
|
| 164 |
-
#
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
elif hasattr(input_data, 'name'):
|
| 167 |
# File input - extract text from document
|
| 168 |
try:
|
|
@@ -361,8 +392,21 @@ def predict(text, file):
|
|
| 361 |
# Use text input
|
| 362 |
input_data = text.strip()
|
| 363 |
elif file is not None:
|
| 364 |
-
#
|
| 365 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
else:
|
| 367 |
# No input provided
|
| 368 |
return {
|
|
@@ -370,6 +414,7 @@ def predict(text, file):
|
|
| 370 |
"message": "Please provide either text or upload a file"
|
| 371 |
}, None
|
| 372 |
|
|
|
|
| 373 |
# Process using the unified function
|
| 374 |
return predict_unified(input_data)
|
| 375 |
|
|
|
|
| 161 |
def process_input(input_data):
|
| 162 |
"""Process input data to extract text for ASL conversion"""
|
| 163 |
if isinstance(input_data, str):
|
| 164 |
+
# Check if it's a file path (contains file extension)
|
| 165 |
+
if any(ext in input_data.lower() for ext in ['.pdf', '.txt', '.docx', '.doc', '.epub']):
|
| 166 |
+
# It's a file path - extract text directly
|
| 167 |
+
try:
|
| 168 |
+
print(f"Processing file path: {input_data}")
|
| 169 |
+
from document_parsing import DocumentParser
|
| 170 |
+
parser = DocumentParser()
|
| 171 |
+
extracted_text = parser.extract_text(input_data)
|
| 172 |
+
if extracted_text:
|
| 173 |
+
print(f"Extracted {len(extracted_text)} characters from file")
|
| 174 |
+
# Convert the extracted text to ASL gloss
|
| 175 |
+
gloss = asl_converter.asl_converter.convert_text(extracted_text)
|
| 176 |
+
print(f"Converted gloss: {gloss[:100]}...")
|
| 177 |
+
return gloss
|
| 178 |
+
else:
|
| 179 |
+
print("No text extracted from file")
|
| 180 |
+
return None
|
| 181 |
+
except Exception as e:
|
| 182 |
+
print(f"Error processing file path: {e}")
|
| 183 |
+
return None
|
| 184 |
+
else:
|
| 185 |
+
# Direct text input
|
| 186 |
+
return input_data.strip()
|
| 187 |
+
elif isinstance(input_data, dict) and 'path' in input_data:
|
| 188 |
+
# This is a gradio.FileData object from API calls
|
| 189 |
+
try:
|
| 190 |
+
print(f"Processing API file: {input_data['path']}")
|
| 191 |
+
gloss = asl_converter.convert_document(input_data['path'])
|
| 192 |
+
print(f"Converted gloss: {gloss[:100]}...") # Show first 100 chars
|
| 193 |
+
return gloss
|
| 194 |
+
except Exception as e:
|
| 195 |
+
print(f"Error processing API file: {e}")
|
| 196 |
+
return None
|
| 197 |
elif hasattr(input_data, 'name'):
|
| 198 |
# File input - extract text from document
|
| 199 |
try:
|
|
|
|
| 392 |
# Use text input
|
| 393 |
input_data = text.strip()
|
| 394 |
elif file is not None:
|
| 395 |
+
# Handle different file input types
|
| 396 |
+
if isinstance(file, dict) and 'path' in file:
|
| 397 |
+
# This is a gradio.FileData object from API calls
|
| 398 |
+
print(f"Processing API file: {file}")
|
| 399 |
+
input_data = file
|
| 400 |
+
elif hasattr(file, 'name'):
|
| 401 |
+
# This is a regular file object
|
| 402 |
+
print(f"Processing regular file: {file.name}")
|
| 403 |
+
input_data = file
|
| 404 |
+
else:
|
| 405 |
+
print(f"Unknown file type: {type(file)}")
|
| 406 |
+
return {
|
| 407 |
+
"status": "error",
|
| 408 |
+
"message": "Unsupported file format"
|
| 409 |
+
}, None
|
| 410 |
else:
|
| 411 |
# No input provided
|
| 412 |
return {
|
|
|
|
| 414 |
"message": "Please provide either text or upload a file"
|
| 415 |
}, None
|
| 416 |
|
| 417 |
+
print("Input", input_data)
|
| 418 |
# Process using the unified function
|
| 419 |
return predict_unified(input_data)
|
| 420 |
|