Summarization / app.py
ikraamkb's picture
Update app.py
094c949 verified
raw
history blame
885 Bytes
@app.post("/summarize/")
async def summarize_api(file: UploadFile = File(...), length: str = Form("medium")):
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as temp:
shutil.copyfileobj(file.file, temp)
temp.flush()
class FileObj: name = temp.name
text, error = extract_text(FileObj.name, os.path.splitext(file.filename)[1][1:].lower())
if error:
return JSONResponse({"error": error}, status_code=400)
summary = generate_summary(text, length)
audio_path = text_to_speech(summary)
pdf_path = create_pdf(summary, file.filename)
return JSONResponse({
"summary": summary,
"audio_url": f"/files/{os.path.basename(audio_path)}" if audio_path else None,
"pdf_url": f"/files/{os.path.basename(pdf_path)}" if pdf_path else None
})