-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code for the article: https://medium.com/@enoch3712/unlocking-rapid-d…
- Loading branch information
Showing
6 changed files
with
132 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
API_KEY = 'XXXX' | ||
API_KEY_ANTROPIC = 'XXXX' | ||
API_KEY_OPENAI = 'XXXX' | ||
API_KEY_OPENAI = 'XXXX' | ||
API_KEY_GROQ = 'XXXX' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Use an official Python runtime as a parent image, suitable for TensorFlow | ||
FROM tensorflow/tensorflow:latest | ||
|
||
# Set the working directory in the container | ||
WORKDIR /app | ||
|
||
# Install system dependencies required for OpenCV and WeasyPrint | ||
RUN apt-get update && apt-get install -y \ | ||
libgl1-mesa-glx \ | ||
libpango-1.0-0 \ | ||
libpangocairo-1.0-0 \ | ||
libgdk-pixbuf2.0-0 \ | ||
libffi-dev \ | ||
shared-mime-info | ||
|
||
# Install FastAPI and Uvicorn | ||
RUN pip install fastapi uvicorn python-multipart aiofiles Pillow | ||
|
||
# Copy the local directory contents into the container | ||
COPY . /app | ||
|
||
# Install `doctr` with TensorFlow support | ||
RUN pip install python-doctr[tf] | ||
|
||
# Expose the port FastAPI will run on | ||
EXPOSE 8001 | ||
|
||
# Command to run the FastAPI server on container start | ||
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8001", "--workers", "4"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Simple docTR container to use as an example | ||
|
||
docker build -t myapp . | ||
docker run -p 8001:8001 myapp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from fastapi import FastAPI, File, UploadFile | ||
from fastapi.responses import JSONResponse | ||
from doctr.io import DocumentFile | ||
from doctr.models import ocr_predictor | ||
from PIL import Image | ||
import io | ||
import os | ||
|
||
app = FastAPI(title="OCR Service using docTR") | ||
|
||
@app.post("/ocr/") | ||
async def perform_ocr(file: UploadFile = File(...)): | ||
image_data = await file.read() | ||
|
||
# Attempt to load the image directly from the BytesIO object | ||
doc = DocumentFile.from_images(image_data) | ||
|
||
model = ocr_predictor(pretrained=True) | ||
result = model(doc) | ||
|
||
extracted_texts = [] | ||
for page in result.pages: | ||
for block in page.blocks: | ||
for line in block.lines: | ||
line_text = ' '.join([word.value for word in line.words]) | ||
extracted_texts.append(line_text) | ||
|
||
return JSONResponse(content={"ExtractedText": extracted_texts}) | ||
|
||
if __name__ == "__main__": | ||
import uvicorn | ||
uvicorn.run(app, host="0.0.0.0", port=8001) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.