Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,45 @@ import pyclip
|
|
11 |
import pytesseract
|
12 |
from nltk.tokenize import sent_tokenize
|
13 |
from transformers import MarianMTModel, MarianTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
nltk.download('punkt')
|
16 |
|
|
|
11 |
import pytesseract
|
12 |
from nltk.tokenize import sent_tokenize
|
13 |
from transformers import MarianMTModel, MarianTokenizer
|
14 |
+
# Added below code
|
15 |
+
from fastapi import FastAPI, File, Request, UploadFile, Body, Depends, HTTPException
|
16 |
+
from fastapi.security.api_key import APIKeyHeader
|
17 |
+
from typing import Optional, Annotated
|
18 |
+
from fastapi.encoders import jsonable_encoder
|
19 |
+
from PIL import Image
|
20 |
+
from io import BytesIO
|
21 |
+
|
22 |
+
API_KEY = os.environ.get("API_KEY")
|
23 |
+
|
24 |
+
app = FastAPI()
|
25 |
+
api_key_header = APIKeyHeader(name="api_key", auto_error=False)
|
26 |
+
|
27 |
+
def get_api_key(api_key: Optional[str] = Depends(api_key_header)):
|
28 |
+
if api_key is None or api_key != API_KEY:
|
29 |
+
raise HTTPException(status_code=401, detail="Unauthorized access")
|
30 |
+
return api_key
|
31 |
+
|
32 |
+
@app.post("/api/ocr", response_model=dict)
|
33 |
+
async def ocr(
|
34 |
+
api_key: str = Depends(get_api_key),
|
35 |
+
image: UploadFile = File(...),
|
36 |
+
# languages: list = Body(["eng"])
|
37 |
+
):
|
38 |
+
try:
|
39 |
+
content = await image.read()
|
40 |
+
image = Image.open(BytesIO(content))
|
41 |
+
print("[image]",image)
|
42 |
+
if hasattr(pytesseract, "image_to_string"):
|
43 |
+
print("Image to string function is available")
|
44 |
+
# print(pytesseract.image_to_string(image, lang = 'eng'))
|
45 |
+
text = ocr_tesseract(image, ['eng'])
|
46 |
+
else:
|
47 |
+
print("Image to string function is not available")
|
48 |
+
# text = pytesseract.image_to_string(image, lang="+".join(languages))
|
49 |
+
except Exception as e:
|
50 |
+
return {"error": str(e)}, 500
|
51 |
+
|
52 |
+
return {"ImageText": "text"}
|
53 |
|
54 |
nltk.download('punkt')
|
55 |
|