Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,54 +1,62 @@
|
|
|
|
1 |
from fastapi import FastAPI, UploadFile, File, Form, Request
|
2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
|
|
5 |
from fastapi.templating import Jinja2Templates
|
6 |
-
import os
|
7 |
-
import
|
8 |
-
from
|
9 |
|
10 |
-
# Initialize FastAPI
|
11 |
app = FastAPI()
|
12 |
|
13 |
-
|
14 |
-
app.add_middleware(
|
15 |
-
CORSMiddleware,
|
16 |
-
allow_origins=["*"],
|
17 |
-
allow_credentials=True,
|
18 |
-
allow_methods=["*"],
|
19 |
-
allow_headers=["*"],
|
20 |
-
)
|
21 |
|
22 |
-
# Static files and templates
|
23 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
24 |
app.mount("/resources", StaticFiles(directory="resources"), name="resources")
|
25 |
templates = Jinja2Templates(directory="templates")
|
26 |
|
27 |
-
# --- Serve Frontend ---
|
28 |
-
|
29 |
@app.get("/", response_class=HTMLResponse)
|
30 |
async def serve_home(request: Request):
|
31 |
return templates.TemplateResponse("HomeS.html", {"request": request})
|
32 |
|
33 |
-
# --- API Endpoints that frontend needs ---
|
34 |
-
|
35 |
@app.post("/summarize/")
|
36 |
async def summarize_document_endpoint(file: UploadFile = File(...), length: str = Form("medium")):
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
@app.post("/imagecaption/")
|
44 |
async def caption_image_endpoint(file: UploadFile = File(...)):
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
return JSONResponse({"error": f"Image captioning failed: {str(e)}"}, status_code=500)
|
50 |
|
51 |
-
|
|
|
52 |
|
53 |
@app.get("/files/{filename}")
|
54 |
async def serve_file(filename: str):
|
@@ -56,20 +64,3 @@ async def serve_file(filename: str):
|
|
56 |
if os.path.exists(path):
|
57 |
return FileResponse(path)
|
58 |
return JSONResponse({"error": "File not found"}, status_code=404)
|
59 |
-
|
60 |
-
# (Optional) Unified prediction endpoint — Only if you want
|
61 |
-
@app.post("/predict")
|
62 |
-
async def predict(
|
63 |
-
file: UploadFile = File(...),
|
64 |
-
option: str = Form(...), # "Summarize" or "Captioning"
|
65 |
-
length: Optional[str] = Form(None) # Only for Summarize
|
66 |
-
):
|
67 |
-
try:
|
68 |
-
if option == "Summarize":
|
69 |
-
return await summarize_document_endpoint(file, length or "medium")
|
70 |
-
elif option == "Captioning":
|
71 |
-
return await caption_image_endpoint(file)
|
72 |
-
else:
|
73 |
-
return JSONResponse({"error": "Invalid option"}, status_code=400)
|
74 |
-
except Exception as e:
|
75 |
-
return JSONResponse({"error": f"Prediction failed: {str(e)}"}, status_code=500)
|
|
|
1 |
+
# main.py
|
2 |
from fastapi import FastAPI, UploadFile, File, Form, Request
|
|
|
3 |
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
6 |
from fastapi.templating import Jinja2Templates
|
7 |
+
import tempfile, os
|
8 |
+
from app_logic import extract_text, generate_summary, text_to_speech, create_pdf
|
9 |
+
from app_image_logic import generate_caption
|
10 |
|
|
|
11 |
app = FastAPI()
|
12 |
|
13 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
15 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
16 |
app.mount("/resources", StaticFiles(directory="resources"), name="resources")
|
17 |
templates = Jinja2Templates(directory="templates")
|
18 |
|
|
|
|
|
19 |
@app.get("/", response_class=HTMLResponse)
|
20 |
async def serve_home(request: Request):
|
21 |
return templates.TemplateResponse("HomeS.html", {"request": request})
|
22 |
|
|
|
|
|
23 |
@app.post("/summarize/")
|
24 |
async def summarize_document_endpoint(file: UploadFile = File(...), length: str = Form("medium")):
|
25 |
+
contents = await file.read()
|
26 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp:
|
27 |
+
tmp.write(contents)
|
28 |
+
tmp_path = tmp.name
|
29 |
+
|
30 |
+
file_ext = file.filename.split('.')[-1].lower()
|
31 |
+
text, error = extract_text(tmp_path, file_ext)
|
32 |
+
|
33 |
+
if error:
|
34 |
+
return JSONResponse({"detail": error}, status_code=400)
|
35 |
+
|
36 |
+
if not text or len(text.split()) < 30:
|
37 |
+
return JSONResponse({"detail": "Document too short to summarize"}, status_code=400)
|
38 |
+
|
39 |
+
summary = generate_summary(text, length)
|
40 |
+
audio_path = text_to_speech(summary)
|
41 |
+
pdf_path = create_pdf(summary, file.filename)
|
42 |
+
|
43 |
+
response = {"summary": summary}
|
44 |
+
if audio_path:
|
45 |
+
response["audioUrl"] = f"/files/{os.path.basename(audio_path)}"
|
46 |
+
if pdf_path:
|
47 |
+
response["pdfUrl"] = f"/files/{os.path.basename(pdf_path)}"
|
48 |
+
|
49 |
+
return JSONResponse(response)
|
50 |
|
51 |
@app.post("/imagecaption/")
|
52 |
async def caption_image_endpoint(file: UploadFile = File(...)):
|
53 |
+
contents = await file.read()
|
54 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
55 |
+
tmp.write(contents)
|
56 |
+
image_path = tmp.name
|
|
|
57 |
|
58 |
+
caption = generate_caption(image_path)
|
59 |
+
return JSONResponse({"caption": caption})
|
60 |
|
61 |
@app.get("/files/{filename}")
|
62 |
async def serve_file(filename: str):
|
|
|
64 |
if os.path.exists(path):
|
65 |
return FileResponse(path)
|
66 |
return JSONResponse({"error": "File not found"}, status_code=404)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|