Tanxshh commited on
Commit
9a7df04
·
verified ·
1 Parent(s): 54298ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -25
app.py CHANGED
@@ -1,25 +1,31 @@
1
- from flask import Flask, request, jsonify
2
- from pipeline import run_pipeline
3
- import os
4
-
5
- app = Flask(__name__)
6
- PDF_FOLDER = "/app/pdfs"
7
- os.makedirs(PDF_FOLDER, exist_ok=True)
8
-
9
- @app.route("/upload_pdf", methods=["POST"])
10
- def upload_pdf():
11
- if 'pdf' not in request.files:
12
- return jsonify({"error": "No PDF uploaded"}), 400
13
-
14
- files = request.files.getlist('pdf')
15
- for file in files:
16
- path = os.path.join(PDF_FOLDER, file.filename)
17
- file.save(path)
18
-
19
- df = run_pipeline(PDF_FOLDER)
20
-
21
-
22
- return df.to_json(orient="records")
23
-
24
- if __name__ == "__main__":
25
- app.run(host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
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")