ibsocr1 commited on
Commit
b2096b4
·
verified ·
1 Parent(s): 385605e

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile.txt +11 -0
  2. app.py +48 -0
  3. requirements.txt +4 -0
Dockerfile.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+
7
+ RUN pip install -r requirements.txt
8
+
9
+ COPY . .
10
+
11
+ CMD uvicorn app:app --host 0.0.0.0 --port 7860
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ from google.cloud import vision
4
+ import base64
5
+ import json
6
+ import os
7
+
8
+ app = FastAPI()
9
+
10
+ class OCRRequest(BaseModel):
11
+ image_base64: str
12
+
13
+ # Arabic digits -> English digits
14
+ translation = str.maketrans(
15
+ "٠١٢٣٤٥٦٧٨٩",
16
+ "0123456789"
17
+ )
18
+
19
+ # Load Google credentials from HF Secret
20
+ creds_json = os.environ["GOOGLE_CREDS_JSON"]
21
+
22
+ with open("google_creds.json", "w", encoding="utf-8") as f:
23
+ f.write(creds_json)
24
+
25
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "google_creds.json"
26
+
27
+ client = vision.ImageAnnotatorClient()
28
+
29
+ @app.post("/ocr")
30
+ async def ocr(req: OCRRequest):
31
+
32
+ image = vision.Image(
33
+ content=base64.b64decode(req.image_base64)
34
+ )
35
+
36
+ response = client.text_detection(image=image)
37
+
38
+ text = ""
39
+
40
+ if response.text_annotations:
41
+ text = response.text_annotations[0].description
42
+
43
+ text = text.translate(translation)
44
+
45
+ return {
46
+ "success": True,
47
+ "text": text
48
+ }
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ google-cloud-vision
4
+ pydantic