Spaces:
Sleeping
Sleeping
krishnachoudhary-hclguvi commited on
Sync GitHub commit 440dace robustness fix
Browse files
main.py
CHANGED
|
@@ -6,8 +6,8 @@ import os
|
|
| 6 |
import uuid
|
| 7 |
import time
|
| 8 |
import asyncio
|
| 9 |
-
from typing import Dict, Optional
|
| 10 |
-
from fastapi import FastAPI, UploadFile, File, HTTPException, Depends, Header
|
| 11 |
from fastapi.staticfiles import StaticFiles
|
| 12 |
from fastapi.responses import FileResponse, JSONResponse
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
|
@@ -125,9 +125,6 @@ async def get_api_key(
|
|
| 125 |
authorization: Optional[str] = Header(None, alias="Authorization"),
|
| 126 |
) -> str:
|
| 127 |
"""Validate incoming API key from header or bearer auth."""
|
| 128 |
-
if not config.REQUIRE_API_KEY:
|
| 129 |
-
return "public"
|
| 130 |
-
|
| 131 |
token = x_api_key
|
| 132 |
if authorization:
|
| 133 |
bearer_prefix = "Bearer "
|
|
@@ -293,38 +290,16 @@ async def synchronous_extract(
|
|
| 293 |
file: Optional[UploadFile] = File(None),
|
| 294 |
document: Optional[UploadFile] = File(None),
|
| 295 |
upload: Optional[UploadFile] = File(None),
|
| 296 |
-
files: Optional[List[UploadFile]] = File(None),
|
| 297 |
-
data: Optional[Dict[str, str]] = Body(None),
|
| 298 |
):
|
| 299 |
"""
|
| 300 |
Synchronous extraction endpoint for API testers and bots.
|
| 301 |
-
|
| 302 |
"""
|
| 303 |
-
# 1.
|
| 304 |
selected_file = file or document or upload
|
| 305 |
-
if not selected_file and files:
|
| 306 |
-
selected_file = files[0]
|
| 307 |
-
|
| 308 |
-
# URL payload fallback for bots that send JSON to this endpoint.
|
| 309 |
-
if not selected_file and data and data.get("url"):
|
| 310 |
-
url = data.get("url", "")
|
| 311 |
-
if not url.startswith(("http://", "https://")):
|
| 312 |
-
raise HTTPException(status_code=400, detail="Invalid URL format. Must start with http:// or https://")
|
| 313 |
-
|
| 314 |
-
file_id = f"sync_{str(uuid.uuid4())[:8]}"
|
| 315 |
-
filename = url.split('/')[2] if '//' in url else url.split('/')[0]
|
| 316 |
-
task = ProcessingResult.create_pending(file_id=file_id, filename=filename, file_type="url")
|
| 317 |
-
start_time = time.time()
|
| 318 |
-
await asyncio.get_event_loop().run_in_executor(
|
| 319 |
-
None, _perform_extraction_and_analysis, task, url, "url", start_time
|
| 320 |
-
)
|
| 321 |
-
if task.status == TaskStatus.ERROR:
|
| 322 |
-
raise HTTPException(status_code=500, detail=task.error_message or "Processing failed.")
|
| 323 |
-
return task
|
| 324 |
-
|
| 325 |
if not selected_file:
|
| 326 |
raise HTTPException(
|
| 327 |
-
status_code=400,
|
| 328 |
detail="No input provided. Send multipart file field 'file' (or 'document'/'upload') or JSON with {'url': 'https://...'}"
|
| 329 |
)
|
| 330 |
|
|
@@ -352,10 +327,10 @@ async def synchronous_extract(
|
|
| 352 |
|
| 353 |
# Create the result object
|
| 354 |
task = ProcessingResult.create_pending(file_id=file_id, filename=filename, file_type=file_type)
|
|
|
|
|
|
|
| 355 |
|
| 356 |
-
# Run processing synchronously in the current thread
|
| 357 |
-
# Actually, to be safe with FastAPI's async loop, we should run it in a thread still,
|
| 358 |
-
# but await its completion.
|
| 359 |
await asyncio.get_event_loop().run_in_executor(
|
| 360 |
None, _perform_extraction_and_analysis, task, file_path, file_type, start_time
|
| 361 |
)
|
|
|
|
| 6 |
import uuid
|
| 7 |
import time
|
| 8 |
import asyncio
|
| 9 |
+
from typing import Dict, Optional
|
| 10 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException, Depends, Header
|
| 11 |
from fastapi.staticfiles import StaticFiles
|
| 12 |
from fastapi.responses import FileResponse, JSONResponse
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 125 |
authorization: Optional[str] = Header(None, alias="Authorization"),
|
| 126 |
) -> str:
|
| 127 |
"""Validate incoming API key from header or bearer auth."""
|
|
|
|
|
|
|
|
|
|
| 128 |
token = x_api_key
|
| 129 |
if authorization:
|
| 130 |
bearer_prefix = "Bearer "
|
|
|
|
| 290 |
file: Optional[UploadFile] = File(None),
|
| 291 |
document: Optional[UploadFile] = File(None),
|
| 292 |
upload: Optional[UploadFile] = File(None),
|
|
|
|
|
|
|
| 293 |
):
|
| 294 |
"""
|
| 295 |
Synchronous extraction endpoint for API testers and bots.
|
| 296 |
+
Supports multple field names for maximum compatibility (file, document, upload).
|
| 297 |
"""
|
| 298 |
+
# 1. Selection
|
| 299 |
selected_file = file or document or upload
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
if not selected_file:
|
| 301 |
raise HTTPException(
|
| 302 |
+
status_code=400,
|
| 303 |
detail="No input provided. Send multipart file field 'file' (or 'document'/'upload') or JSON with {'url': 'https://...'}"
|
| 304 |
)
|
| 305 |
|
|
|
|
| 327 |
|
| 328 |
# Create the result object
|
| 329 |
task = ProcessingResult.create_pending(file_id=file_id, filename=filename, file_type=file_type)
|
| 330 |
+
# Explicitly set CamelCase for tester
|
| 331 |
+
task.fileName = filename
|
| 332 |
|
| 333 |
+
# Run processing synchronously in the current thread
|
|
|
|
|
|
|
| 334 |
await asyncio.get_event_loop().run_in_executor(
|
| 335 |
None, _perform_extraction_and_analysis, task, file_path, file_type, start_time
|
| 336 |
)
|