Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
import shutil
|
| 3 |
+
import os
|
| 4 |
+
from OCR import pipeline
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
UPLOAD_FOLDER = "temp"
|
| 9 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 10 |
+
|
| 11 |
+
@app.post("/predict")
|
| 12 |
+
async def predict(file: UploadFile = File(...)):
|
| 13 |
+
file_path = os.path.join(UPLOAD_FOLDER, file.filename)
|
| 14 |
+
|
| 15 |
+
with open(file_path, "wb") as buffer:
|
| 16 |
+
shutil.copyfileobj(file.file, buffer)
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
text = pipeline(file_path, bw=True)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return {"error": str(e)}
|
| 22 |
+
|
| 23 |
+
return {"text": text}
|