Spaces:
Runtime error
Runtime error
| from fastapi import FastAPI, File, UploadFile, HTTPException | |
| from fastapi.responses import JSONResponse | |
| from app.ocr import easyocr_ocr, tesseract_ocr,tesseract_ocr_pdf | |
| app = FastAPI() | |
| async def upload_image(file: UploadFile = File(...)): | |
| try: | |
| # Save the uploaded file to a temporary location | |
| with open(f"/tmp/{file.filename}", "wb") as buffer: | |
| buffer.write(await file.read()) | |
| # Perform OCR on the saved image | |
| formatted_output = easyocr_ocr(f"/tmp/{file.filename}") | |
| # Return the formatted output | |
| return JSONResponse(content={"formatted_output": formatted_output}) | |
| except Exception as e: | |
| return JSONResponse(content={"error": str(e)}, status_code=500) | |
| async def upload_image(file: UploadFile = File(...)): | |
| try: | |
| # Save the uploaded file to a temporary location | |
| contents = await file.read() | |
| file_extension = file.filename.lower().split('.')[-1] | |
| file_path = f"temp_{file.filename}" | |
| print(file.filename.lower().endswith(('.pdf'))) | |
| with open(file_path, "wb") as f: | |
| f.write(contents) | |
| if file_extension == 'png' or file_extension == 'jpg' or file_extension == 'jpeg': | |
| ocr_result = await tesseract_ocr(file_path) | |
| elif file_extension == 'pdf': | |
| ocr_result = await tesseract_ocr_pdf(file_path) | |
| else: | |
| raise HTTPException(status_code=400, detail="Unsupported file format") | |
| # ocr_result = await tesseract_ocr(file_path) | |
| return JSONResponse(content={"result": ocr_result}) | |
| return JSONResponse(content={"formatted_output": formatted_output}) | |
| except Exception as e: | |
| return JSONResponse(content={"error": str(e)}, status_code=500) | |