Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +20 -0
- app.py +46 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python image
|
2 |
+
FROM python:3.9.7
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /code
|
6 |
+
|
7 |
+
# Copy the requirements file into the container
|
8 |
+
COPY ./requirements.txt /code/requirements.txt
|
9 |
+
|
10 |
+
# Install the dependencies
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
12 |
+
|
13 |
+
# Install Tesseract OCR via the package manager
|
14 |
+
RUN apt-get update && apt-get install -y tesseract-ocr
|
15 |
+
|
16 |
+
# Copy the entire project directory into the container
|
17 |
+
COPY . /code
|
18 |
+
|
19 |
+
# Command to run the FastAPI server
|
20 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PIL import Image
|
2 |
+
import pytesseract
|
3 |
+
from fastapi import FastAPI, UploadFile, File
|
4 |
+
from fastapi.middleware.cors import CORSMiddleware
|
5 |
+
from io import BytesIO
|
6 |
+
|
7 |
+
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
|
11 |
+
origins = ["*"]
|
12 |
+
|
13 |
+
app.add_middleware(
|
14 |
+
CORSMiddleware,
|
15 |
+
allow_origins=origins,
|
16 |
+
allow_credentials=True,
|
17 |
+
allow_methods=["*"],
|
18 |
+
allow_headers=["*"],
|
19 |
+
)
|
20 |
+
|
21 |
+
|
22 |
+
@app.get('/')
|
23 |
+
def welcome():
|
24 |
+
return {
|
25 |
+
'success': True,
|
26 |
+
'message': 'server of "image text extractor" is up and running successfully.'
|
27 |
+
}
|
28 |
+
|
29 |
+
@app.post('/extract-text-from-image')
|
30 |
+
async def extract_text_from_img(imageUploadedByUser: UploadFile = File(...)):
|
31 |
+
|
32 |
+
img = await imageUploadedByUser.read()
|
33 |
+
|
34 |
+
img_bytes_io = Image.open(BytesIO(img))
|
35 |
+
|
36 |
+
gray_scale_img = img_bytes_io.convert('L')
|
37 |
+
|
38 |
+
text = pytesseract.image_to_string(gray_scale_img)
|
39 |
+
|
40 |
+
text_cleaned = ' '.join(text.split())
|
41 |
+
|
42 |
+
return {
|
43 |
+
'success': True,
|
44 |
+
'message': 'Text has been successfully extracted from the uploaded image',
|
45 |
+
'extracted_text': text_cleaned
|
46 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.109.2
|
2 |
+
uvicorn==0.27.1
|
3 |
+
python-multipart==0.0.9
|
4 |
+
Pillow==10.2.0
|
5 |
+
pytesseract==0.3.10
|