badman99dev
commited on
Commit
ยท
1db1a4f
1
Parent(s):
19dbeb8
๐ Final Hindi OCR API with PDF + Image support
Browse files- Dockerfile +4 -5
- README.md +3 -4
- app.py +12 -20
Dockerfile
CHANGED
|
@@ -7,12 +7,11 @@ RUN apt-get update && apt-get install -y \
|
|
| 7 |
libglib2.0-0 \
|
| 8 |
libsm6 \
|
| 9 |
libxrender1 \
|
| 10 |
-
libxext6
|
|
|
|
| 11 |
|
| 12 |
WORKDIR /app
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
RUN pip install --no-cache-dir -r requirements.txt
|
| 17 |
|
| 18 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 7 |
libglib2.0-0 \
|
| 8 |
libsm6 \
|
| 9 |
libxrender1 \
|
| 10 |
+
libxext6 \
|
| 11 |
+
&& apt-get clean
|
| 12 |
|
| 13 |
WORKDIR /app
|
| 14 |
+
COPY . /app
|
| 15 |
+
RUN pip install -r requirements.txt
|
|
|
|
|
|
|
| 16 |
|
| 17 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,9 @@
|
|
| 1 |
---
|
| 2 |
title: Hindi OCR API
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
-
sdk_version: "1.0"
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
| 1 |
---
|
| 2 |
title: Hindi OCR API
|
| 3 |
+
emoji: ๐
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
app_file: app.py
|
| 8 |
pinned: false
|
| 9 |
---
|
app.py
CHANGED
|
@@ -1,30 +1,22 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
|
|
|
| 2 |
from pdf2image import convert_from_bytes
|
| 3 |
-
import pytesseract
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
import io
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
return {"message": "โ
Hindi OCR API is live!"}
|
| 12 |
|
| 13 |
@app.post("/ocr")
|
| 14 |
async def extract_text(file: UploadFile = File(...)):
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return {"text": full_text.strip()}
|
| 24 |
-
|
| 25 |
-
elif file.filename.endswith((".jpg", ".jpeg", ".png")):
|
| 26 |
-
image = Image.open(io.BytesIO(content))
|
| 27 |
-
text = pytesseract.image_to_string(image, lang='hin+eng')
|
| 28 |
-
return {"text": text.strip()}
|
| 29 |
-
|
| 30 |
-
return {"error": "โ Unsupported file format"}
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
from pdf2image import convert_from_bytes
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
+
import pytesseract
|
| 6 |
import io
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
def ocr_image(image: Image.Image) -> str:
|
| 11 |
+
return pytesseract.image_to_string(image, lang='hin+eng')
|
|
|
|
| 12 |
|
| 13 |
@app.post("/ocr")
|
| 14 |
async def extract_text(file: UploadFile = File(...)):
|
| 15 |
+
contents = await file.read()
|
| 16 |
+
if file.filename.lower().endswith(".pdf"):
|
| 17 |
+
images = convert_from_bytes(contents)
|
| 18 |
+
text = "\n".join([ocr_image(img) for img in images])
|
| 19 |
+
else:
|
| 20 |
+
image = Image.open(io.BytesIO(contents))
|
| 21 |
+
text = ocr_image(image)
|
| 22 |
+
return JSONResponse(content={"text": text})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|