Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -30,12 +30,38 @@ async def serve_home(request: Request):
|
|
30 |
|
31 |
# Document summarization endpoint
|
32 |
@app.post("/summarize/")
|
33 |
-
async def
|
34 |
try:
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
except Exception as e:
|
38 |
-
|
|
|
|
|
39 |
|
40 |
# Image captioning endpoint
|
41 |
@app.post("/imagecaption/")
|
|
|
30 |
|
31 |
# Document summarization endpoint
|
32 |
@app.post("/summarize/")
|
33 |
+
async def summarize_api(file: UploadFile = File(...), length: str = Form("medium")):
|
34 |
try:
|
35 |
+
contents = await file.read()
|
36 |
+
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
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)}") # << SUPER IMPORTANT FOR DEBUG
|
63 |
+
return JSONResponse({"detail": f"Internal server error: {str(e)}"}, status_code=500)
|
64 |
+
|
65 |
|
66 |
# Image captioning endpoint
|
67 |
@app.post("/imagecaption/")
|