Spaces:
Runtime error
Runtime error
| from fastapi import FastAPI, UploadFile, File, HTTPException | |
| from fastapi.responses import JSONResponse | |
| import uuid | |
| import io | |
| from PIL import Image | |
| from utils.ocr_utils import extract_and_translate_chunk | |
| app = FastAPI(title="Manga Translation API", description="FastAPI implementation for Manga Translation with OCR and Azure") | |
| uploaded_images = {} | |
| async def upload_panel(file: UploadFile = File(...)): | |
| '''Takes an JEPG/PNG manga panel image and returns a image_id for reference''' | |
| if file.content_type not in ["image/jpeg", "image/png"]: | |
| raise HTTPException(status_code=400, detail="Only JPEG and PNG files are supported") | |
| image_id = str(uuid.uuid4()) | |
| content = await file.read() | |
| uploaded_images[image_id] = content | |
| return JSONResponse(status_code=200, content={"image_id": image_id}) | |
| async def translate_panel(image_id: str): | |
| '''Takes an image_id and returns a translated text from OCR in JSON format''' | |
| # Loads the img | |
| if image_id not in uploaded_images: | |
| raise HTTPException(status_code=404, detail="Image not found") | |
| image_content = uploaded_images[image_id] | |
| image = Image.open(io.BytesIO(image_content)).convert("RGB") | |
| # Extract with OCR and translating | |
| translated_text = extract_and_translate_chunk(image) | |
| results = [] | |
| for translation in translated_text: | |
| results.append({ | |
| "polygon": translation['polygon'], | |
| "original_text": translation['original'], | |
| "translated_text": translation['translated'], | |
| "language": "ja", | |
| "confidence": 0.95 # Placeholder confidence value | |
| }) | |
| return JSONResponse(status_code=200, content={"image_id": image_id, "results": results}) | |
| async def root(): | |
| return {"message": "Welcome to the Manga Translation API"} | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) |