Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,4 +1,3 @@
|
|
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
|
@@ -28,51 +27,27 @@ templates = Jinja2Templates(directory="templates")
|
|
28 |
async def serve_home(request: Request):
|
29 |
return templates.TemplateResponse("HomeS.html", {"request": request})
|
30 |
|
31 |
-
#
|
32 |
@app.post("/summarize/")
|
33 |
async def summarize_api(file: UploadFile = File(...), length: str = Form("medium")):
|
34 |
try:
|
35 |
-
|
36 |
-
|
37 |
-
tmp_file.write(contents)
|
38 |
-
tmp_path = tmp_file.name
|
39 |
-
|
40 |
-
file_ext = tmp_path.split('.')[-1].lower()
|
41 |
-
text, error = extract_text(tmp_path, file_ext)
|
42 |
-
|
43 |
-
if error:
|
44 |
-
return JSONResponse({"detail": error}, status_code=400)
|
45 |
-
|
46 |
-
if not text or len(text.split()) < 30:
|
47 |
-
return JSONResponse({"detail": "Document too short to summarize"}, status_code=400)
|
48 |
-
|
49 |
-
summary = generate_summary(text, length)
|
50 |
-
audio_path = text_to_speech(summary)
|
51 |
-
pdf_path = create_pdf(summary, file.filename)
|
52 |
-
|
53 |
-
response = {"summary": summary}
|
54 |
-
if audio_path:
|
55 |
-
response["audioUrl"] = f"/files/{os.path.basename(audio_path)}"
|
56 |
-
if pdf_path:
|
57 |
-
response["pdfUrl"] = f"/files/{os.path.basename(pdf_path)}"
|
58 |
-
|
59 |
-
return JSONResponse(response)
|
60 |
-
|
61 |
except Exception as e:
|
62 |
-
print(f"Error during summarization: {str(e)}")
|
63 |
return JSONResponse({"detail": f"Internal server error: {str(e)}"}, status_code=500)
|
64 |
|
65 |
-
|
66 |
-
# Image captioning endpoint
|
67 |
@app.post("/imagecaption/")
|
68 |
async def caption(file: UploadFile = File(...)):
|
69 |
try:
|
70 |
result = await caption_image(file)
|
71 |
return JSONResponse(result)
|
72 |
except Exception as e:
|
|
|
73 |
return JSONResponse({"error": f"Image captioning failed: {str(e)}"}, status_code=500)
|
74 |
|
75 |
-
# Serve audio/pdf
|
76 |
@app.get("/files/{filename}")
|
77 |
async def serve_file(filename: str):
|
78 |
filepath = os.path.join(tempfile.gettempdir(), filename)
|
|
|
|
|
1 |
from fastapi import FastAPI, UploadFile, File, Form, Request
|
2 |
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
|
3 |
from fastapi.staticfiles import StaticFiles
|
|
|
27 |
async def serve_home(request: Request):
|
28 |
return templates.TemplateResponse("HomeS.html", {"request": request})
|
29 |
|
30 |
+
# Summarize endpoint (calls summarize_document from app.py)
|
31 |
@app.post("/summarize/")
|
32 |
async def summarize_api(file: UploadFile = File(...), length: str = Form("medium")):
|
33 |
try:
|
34 |
+
result = await summarize_document(file, length)
|
35 |
+
return JSONResponse(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
except Exception as e:
|
37 |
+
print(f"Error during summarization: {str(e)}")
|
38 |
return JSONResponse({"detail": f"Internal server error: {str(e)}"}, status_code=500)
|
39 |
|
40 |
+
# Captioning endpoint
|
|
|
41 |
@app.post("/imagecaption/")
|
42 |
async def caption(file: UploadFile = File(...)):
|
43 |
try:
|
44 |
result = await caption_image(file)
|
45 |
return JSONResponse(result)
|
46 |
except Exception as e:
|
47 |
+
print(f"Error during captioning: {str(e)}")
|
48 |
return JSONResponse({"error": f"Image captioning failed: {str(e)}"}, status_code=500)
|
49 |
|
50 |
+
# Serve audio/pdf files
|
51 |
@app.get("/files/{filename}")
|
52 |
async def serve_file(filename: str):
|
53 |
filepath = os.path.join(tempfile.gettempdir(), filename)
|