badman99dev commited on
Commit
1db1a4f
Β·
1 Parent(s): 19dbeb8

πŸš€ Final Hindi OCR API with PDF + Image support

Browse files
Files changed (3) hide show
  1. Dockerfile +4 -5
  2. README.md +3 -4
  3. 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
- COPY . .
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: yellow
5
- colorTo: red
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
- @app.get("/")
10
- def read_root():
11
- return {"message": "βœ… Hindi OCR API is live!"}
12
 
13
  @app.post("/ocr")
14
  async def extract_text(file: UploadFile = File(...)):
15
- content = await file.read()
16
-
17
- if file.filename.endswith(".pdf"):
18
- images = convert_from_bytes(content, dpi=300) # high quality
19
- full_text = ""
20
- for img in images:
21
- text = pytesseract.image_to_string(img, lang='hin+eng')
22
- full_text += text + "\n"
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})