Update app.py
Browse files
app.py
CHANGED
|
@@ -3,73 +3,82 @@ import uvicorn
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 6 |
-
from fastapi.responses import JSONResponse
|
| 7 |
from bson import ObjectId
|
| 8 |
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
|
| 9 |
-
from PIL import Image
|
| 10 |
from rapidocr_onnxruntime import RapidOCR
|
| 11 |
|
| 12 |
-
#
|
| 13 |
# CONFIG
|
| 14 |
-
#
|
| 15 |
-
MONGO_URI =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
DB_NAME = "ocr_fastapi"
|
| 17 |
BUCKET_NAME = "ocr_images"
|
| 18 |
|
| 19 |
-
app = FastAPI(
|
| 20 |
|
| 21 |
# DB INIT
|
| 22 |
_client = AsyncIOMotorClient(MONGO_URI)
|
| 23 |
_db = _client[DB_NAME]
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
ocr_engine = RapidOCR()
|
| 26 |
OCR_RESULTS = {}
|
| 27 |
|
| 28 |
-
|
| 29 |
@app.get("/health")
|
| 30 |
async def health():
|
| 31 |
return {"status": "ok"}
|
| 32 |
|
| 33 |
-
|
| 34 |
-
# -------------------------------------------------------------------
|
| 35 |
-
# UPLOAD
|
| 36 |
-
# -------------------------------------------------------------------
|
| 37 |
@app.post("/upload")
|
| 38 |
async def upload_image(file: UploadFile = File(...)):
|
| 39 |
try:
|
| 40 |
data = await file.read()
|
| 41 |
|
| 42 |
-
fs =
|
| 43 |
|
| 44 |
oid = await fs.upload_from_stream(
|
| 45 |
file.filename,
|
| 46 |
data,
|
| 47 |
metadata={"contentType": file.content_type},
|
| 48 |
)
|
|
|
|
|
|
|
|
|
|
| 49 |
return {"image_id": str(oid)}
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
raise HTTPException(status_code=500, detail=str(e))
|
| 52 |
|
| 53 |
-
|
| 54 |
-
# -------------------------------------------------------------------
|
| 55 |
-
# LOAD IMAGE
|
| 56 |
-
# -------------------------------------------------------------------
|
| 57 |
async def load_image_from_gridfs(image_id: str):
|
| 58 |
try:
|
|
|
|
|
|
|
| 59 |
oid = ObjectId(image_id)
|
| 60 |
-
fs =
|
| 61 |
|
| 62 |
stream = await fs.open_download_stream(oid)
|
| 63 |
data = await stream.read()
|
| 64 |
await stream.close()
|
|
|
|
|
|
|
|
|
|
| 65 |
return data
|
|
|
|
| 66 |
except Exception as e:
|
|
|
|
| 67 |
raise HTTPException(status_code=404, detail="Image not found")
|
| 68 |
|
| 69 |
|
| 70 |
-
#
|
| 71 |
-
# GENERATE OCR
|
| 72 |
-
# -------------------------------------------------------------------
|
| 73 |
@app.post("/generate/{image_id}")
|
| 74 |
async def generate(image_id: str):
|
| 75 |
raw_bytes = await load_image_from_gridfs(image_id)
|
|
@@ -96,14 +105,13 @@ async def generate(image_id: str):
|
|
| 96 |
|
| 97 |
return {"image_id": image_id, "text": full_text}
|
| 98 |
|
| 99 |
-
|
| 100 |
@app.get("/view/{image_id}")
|
| 101 |
async def view_details(image_id: str):
|
| 102 |
if image_id not in OCR_RESULTS:
|
| 103 |
raise HTTPException(status_code=404, detail="No OCR result found")
|
| 104 |
-
|
| 105 |
return OCR_RESULTS[image_id]
|
| 106 |
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
-
uvicorn.run("app:app", host="0.0.0.0", port=8000
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
|
|
|
| 6 |
from bson import ObjectId
|
| 7 |
from motor.motor_asyncio import AsyncIOMotorClient, AsyncIOMotorGridFSBucket
|
|
|
|
| 8 |
from rapidocr_onnxruntime import RapidOCR
|
| 9 |
|
| 10 |
+
# ------------------------------
|
| 11 |
# CONFIG
|
| 12 |
+
# ------------------------------
|
| 13 |
+
MONGO_URI = (
|
| 14 |
+
"mongodb+srv://harilogicgo_db_user:jFhyDM4oA4dklUsp"
|
| 15 |
+
"@api-logs.i7rqf9p.mongodb.net/ocr_fastapi"
|
| 16 |
+
"?retryWrites=true&w=majority&appName=API-LOGS"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
DB_NAME = "ocr_fastapi"
|
| 20 |
BUCKET_NAME = "ocr_images"
|
| 21 |
|
| 22 |
+
app = FastAPI()
|
| 23 |
|
| 24 |
# DB INIT
|
| 25 |
_client = AsyncIOMotorClient(MONGO_URI)
|
| 26 |
_db = _client[DB_NAME]
|
| 27 |
|
| 28 |
+
def gridfs():
|
| 29 |
+
return AsyncIOMotorGridFSBucket(_db, bucket_name=BUCKET_NAME)
|
| 30 |
+
|
| 31 |
ocr_engine = RapidOCR()
|
| 32 |
OCR_RESULTS = {}
|
| 33 |
|
| 34 |
+
# ------------------------------
|
| 35 |
@app.get("/health")
|
| 36 |
async def health():
|
| 37 |
return {"status": "ok"}
|
| 38 |
|
| 39 |
+
# ------------------------------
|
|
|
|
|
|
|
|
|
|
| 40 |
@app.post("/upload")
|
| 41 |
async def upload_image(file: UploadFile = File(...)):
|
| 42 |
try:
|
| 43 |
data = await file.read()
|
| 44 |
|
| 45 |
+
fs = gridfs()
|
| 46 |
|
| 47 |
oid = await fs.upload_from_stream(
|
| 48 |
file.filename,
|
| 49 |
data,
|
| 50 |
metadata={"contentType": file.content_type},
|
| 51 |
)
|
| 52 |
+
|
| 53 |
+
print("📌 Uploaded ID:", oid)
|
| 54 |
+
|
| 55 |
return {"image_id": str(oid)}
|
| 56 |
+
|
| 57 |
except Exception as e:
|
| 58 |
raise HTTPException(status_code=500, detail=str(e))
|
| 59 |
|
| 60 |
+
# ------------------------------
|
|
|
|
|
|
|
|
|
|
| 61 |
async def load_image_from_gridfs(image_id: str):
|
| 62 |
try:
|
| 63 |
+
print("🔍 Fetching ID:", image_id)
|
| 64 |
+
|
| 65 |
oid = ObjectId(image_id)
|
| 66 |
+
fs = gridfs()
|
| 67 |
|
| 68 |
stream = await fs.open_download_stream(oid)
|
| 69 |
data = await stream.read()
|
| 70 |
await stream.close()
|
| 71 |
+
|
| 72 |
+
print("✅ Found image in GridFS")
|
| 73 |
+
|
| 74 |
return data
|
| 75 |
+
|
| 76 |
except Exception as e:
|
| 77 |
+
print("❌ GridFS error:", e)
|
| 78 |
raise HTTPException(status_code=404, detail="Image not found")
|
| 79 |
|
| 80 |
|
| 81 |
+
# ------------------------------
|
|
|
|
|
|
|
| 82 |
@app.post("/generate/{image_id}")
|
| 83 |
async def generate(image_id: str):
|
| 84 |
raw_bytes = await load_image_from_gridfs(image_id)
|
|
|
|
| 105 |
|
| 106 |
return {"image_id": image_id, "text": full_text}
|
| 107 |
|
| 108 |
+
# ------------------------------
|
| 109 |
@app.get("/view/{image_id}")
|
| 110 |
async def view_details(image_id: str):
|
| 111 |
if image_id not in OCR_RESULTS:
|
| 112 |
raise HTTPException(status_code=404, detail="No OCR result found")
|
|
|
|
| 113 |
return OCR_RESULTS[image_id]
|
| 114 |
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
uvicorn.run("app:app", host="0.0.0.0", port=8000)
|