Spaces:
Runtime error
Runtime error
Upload new_app.py
#2
by mHewDai - opened
- new_app.py +62 -0
new_app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
import uuid
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from utils.ocr_utils import extract_and_translate_chunk
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="Manga Translation API", description="FastAPI implementation for Manga Translation with OCR and Azure")
|
| 9 |
+
|
| 10 |
+
uploaded_images = {}
|
| 11 |
+
|
| 12 |
+
@app.post("/upload_panel/")
|
| 13 |
+
|
| 14 |
+
async def upload_panel(file: UploadFile = File(...)):
|
| 15 |
+
'''Takes an JEPG/PNG manga panel image and returns a image_id for reference'''
|
| 16 |
+
|
| 17 |
+
if file.content_type not in ["image/jpeg", "image/png"]:
|
| 18 |
+
raise HTTPException(status_code=400, detail="Only JPEG and PNG files are supported")
|
| 19 |
+
|
| 20 |
+
image_id = str(uuid.uuid4())
|
| 21 |
+
|
| 22 |
+
content = await file.read()
|
| 23 |
+
uploaded_images[image_id] = content
|
| 24 |
+
|
| 25 |
+
return JSONResponse(status_code=200, content={"image_id": image_id})
|
| 26 |
+
|
| 27 |
+
@app.get("/translate_panel")
|
| 28 |
+
|
| 29 |
+
async def translate_panel(image_id: str):
|
| 30 |
+
'''Takes an image_id and returns a translated text from OCR in JSON format'''
|
| 31 |
+
|
| 32 |
+
# Loads the img
|
| 33 |
+
if image_id not in uploaded_images:
|
| 34 |
+
raise HTTPException(status_code=404, detail="Image not found")
|
| 35 |
+
|
| 36 |
+
image_content = uploaded_images[image_id]
|
| 37 |
+
|
| 38 |
+
image = Image.open(io.BytesIO(image_content)).convert("RGB")
|
| 39 |
+
|
| 40 |
+
# Extract with OCR and translating
|
| 41 |
+
translated_text = extract_and_translate_chunk(image)
|
| 42 |
+
|
| 43 |
+
results = []
|
| 44 |
+
|
| 45 |
+
for translation in translated_text:
|
| 46 |
+
results.append({
|
| 47 |
+
"polygon": translation['polygon'],
|
| 48 |
+
"original_text": translation['original'],
|
| 49 |
+
"translated_text": translation['translated'],
|
| 50 |
+
"language": "ja",
|
| 51 |
+
"confidence": 0.95 # Placeholder confidence value
|
| 52 |
+
})
|
| 53 |
+
|
| 54 |
+
return JSONResponse(status_code=200, content={"image_id": image_id, "results": results})
|
| 55 |
+
|
| 56 |
+
@app.get("/")
|
| 57 |
+
async def root():
|
| 58 |
+
return {"message": "Welcome to the Manga Translation API"}
|
| 59 |
+
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
import uvicorn
|
| 62 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|