Tanxshh commited on
Commit
9b81991
·
verified ·
1 Parent(s): 72d628f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -11
app.py CHANGED
@@ -1,17 +1,33 @@
1
  from fastapi import FastAPI, UploadFile, File
 
2
  import shutil
3
  import os
4
- import tempfile
5
  from pipeline import run_pipeline
 
6
 
7
- app = FastAPI()
8
 
9
- @app.post("/upload_pdf")
10
- async def upload_pdf(files: list[UploadFile] = File(...)):
11
- with tempfile.TemporaryDirectory() as tmpdirname:
12
- for file in files:
13
- dest = os.path.join(tmpdirname, file.filename)
14
- with open(dest, "wb") as f:
15
- shutil.copyfileobj(file.file, f)
16
- results = run_pipeline(tmpdirname)
17
- return results.to_dict(orient="records")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.middleware.cors import CORSMiddleware
3
  import shutil
4
  import os
 
5
  from pipeline import run_pipeline
6
+ import pandas as pd
7
 
8
+ app = FastAPI(title="SC API", description="PDF ESG Analysis API")
9
 
10
+ # Enable CORS for all origins (you can restrict later)
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_methods=["*"],
15
+ allow_headers=["*"],
16
+ )
17
+
18
+ UPLOAD_FOLDER = "/app/uploads"
19
+ os.makedirs(UPLOAD_FOLDER, exist_ok=True)
20
+
21
+ @app.post("/pdf-upload")
22
+ async def pdf_upload(file: UploadFile = File(...)):
23
+ file_path = os.path.join(UPLOAD_FOLDER, file.filename)
24
+ with open(file_path, "wb") as buffer:
25
+ shutil.copyfileobj(file.file, buffer)
26
+
27
+ # Run pipeline
28
+ df = run_pipeline(UPLOAD_FOLDER)
29
+
30
+ # Optional: delete uploaded file after processing
31
+ # os.remove(file_path)
32
+
33
+ return {"status": "success", "data": df.to_dict(orient="records")}