Spaces:
Runtime error
Runtime error
| import io | |
| from fastapi import File, UploadFile | |
| from src.utils import FileClient | |
| class FileService: | |
| def __init__(self): | |
| self.file_client = FileClient | |
| async def __aenter__(self): | |
| return self | |
| async def __aexit__(self, exc_type, exc_value, traceback): | |
| pass | |
| async def extract_file(self, file: UploadFile = File(...)): | |
| file_content = await file.read() | |
| file_bytes = io.BytesIO(file_content) | |
| file_type = file.content_type | |
| async with self.file_client() as client: | |
| if file_type == "application/pdf": | |
| return await client.extract_from_pdf(file_bytes=file_bytes) | |
| elif file_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document": | |
| return await client.extrcat_from_word(file_bytes=file_bytes) | |
| elif file_type == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": | |
| return await client.extract_from_excel(file_bytes=file_bytes) | |
| elif file_type == "application/json": | |
| return await client.extract_from_json(file_bytes=file_bytes) | |
| elif file_type == "text/markdown": | |
| return await client.extrcat_from_md(file_bytes=file_bytes) | |
| elif file_type == "text/plain": | |
| return await client.extract_from_txt(file_bytes=file_bytes) | |
| elif file_type == "text/csv": | |
| return await client.extract_from_csv(file_bytes=file_bytes) | |
| else: | |
| raise ValueError("Unsupported file type") |