Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,31 @@
|
|
| 1 |
-
from
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import shutil
|
| 4 |
+
import os
|
| 5 |
+
import tempfile
|
| 6 |
+
from pipeline import run_pipeline # Make sure pipeline.py is in the same folder
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="SC API")
|
| 9 |
+
|
| 10 |
+
# Allow CORS so your website or n8n can POST
|
| 11 |
+
app.add_middleware(
|
| 12 |
+
CORSMiddleware,
|
| 13 |
+
allow_origins=["*"], # You can restrict to your website
|
| 14 |
+
allow_credentials=True,
|
| 15 |
+
allow_methods=["*"],
|
| 16 |
+
allow_headers=["*"],
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
@app.post("/upload_pdf")
|
| 20 |
+
async def upload_pdf(files: list[UploadFile] = File(...)):
|
| 21 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 22 |
+
pdf_paths = []
|
| 23 |
+
for file in files:
|
| 24 |
+
dest = os.path.join(tmpdirname, file.filename)
|
| 25 |
+
with open(dest, "wb") as f:
|
| 26 |
+
shutil.copyfileobj(file.file, f)
|
| 27 |
+
pdf_paths.append(dest)
|
| 28 |
+
|
| 29 |
+
# Run the SC pipeline
|
| 30 |
+
results = run_pipeline(tmpdirname)
|
| 31 |
+
return results.to_dict(orient="records")
|