-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
51 lines (42 loc) · 1.32 KB
/
Copy pathapi.py
File metadata and controls
51 lines (42 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
import os
import json
import shutil
from fastapi import FastAPI, UploadFile, File, Form
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from typing import List
import subprocess
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173"],
allow_methods=["*"],
allow_headers=["*"],
)
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
@app.post("/generate")
async def generate(
topic: str = Form(...),
file_type: str = Form(...),
sources: str = Form("[]"),
pdfs: List[UploadFile] = File(default=[])
):
url_list = json.loads(sources)
# save uploaded pdfs temporarily
pdf_paths = []
for pdf in pdfs:
path = os.path.join(BASE_DIR, pdf.filename)
with open(path, "wb") as f:
shutil.copyfileobj(pdf.file, f)
pdf_paths.append(path)
# combine urls and pdf paths into sources list
all_sources = url_list + pdf_paths
cmd = [sys.executable, "generate.py", topic, file_type] + all_sources
subprocess.run(cmd, cwd=BASE_DIR)
# cleanup uploaded pdfs
for path in pdf_paths:
os.remove(path)
filename = topic.replace(" ", "_") + "." + file_type
filepath = os.path.join(BASE_DIR, filename)
return FileResponse(filepath, filename=filename)