File size: 2,826 Bytes
895bf16
a377214
8888b75
59efe71
 
b5f584b
895bf16
b5f584b
 
 
59efe71
 
 
b5f584b
 
 
 
 
 
 
59efe71
 
c0e0602
59efe71
 
b5f584b
59efe71
 
c0e0602
8888b75
b5f584b
8888b75
734e13c
b5f584b
734e13c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5f584b
734e13c
 
 
b5f584b
 
8888b75
b5f584b
 
 
 
 
 
 
 
8888b75
 
b5f584b
 
 
8888b75
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# main.py
from fastapi import FastAPI, UploadFile, File, Form, Request
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
import tempfile, os

from app import summarize_document
from appImage import caption_image

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/resources", StaticFiles(directory="resources"), name="resources")
templates = Jinja2Templates(directory="templates")

# Serve homepage
@app.get("/", response_class=HTMLResponse)
async def serve_home(request: Request):
    return templates.TemplateResponse("HomeS.html", {"request": request})

# Document summarization endpoint
@app.post("/summarize/")
async def summarize_api(file: UploadFile = File(...), length: str = Form("medium")):
    try:
        contents = await file.read()
        with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
            tmp_file.write(contents)
            tmp_path = tmp_file.name

        file_ext = tmp_path.split('.')[-1].lower()
        text, error = extract_text(tmp_path, file_ext)

        if error:
            return JSONResponse({"detail": error}, status_code=400)

        if not text or len(text.split()) < 30:
            return JSONResponse({"detail": "Document too short to summarize"}, status_code=400)

        summary = generate_summary(text, length)
        audio_path = text_to_speech(summary)
        pdf_path = create_pdf(summary, file.filename)

        response = {"summary": summary}
        if audio_path:
            response["audioUrl"] = f"/files/{os.path.basename(audio_path)}"
        if pdf_path:
            response["pdfUrl"] = f"/files/{os.path.basename(pdf_path)}"

        return JSONResponse(response)

    except Exception as e:
        print(f"Error during summarization: {str(e)}")  # << SUPER IMPORTANT FOR DEBUG
        return JSONResponse({"detail": f"Internal server error: {str(e)}"}, status_code=500)


# Image captioning endpoint
@app.post("/imagecaption/")
async def caption(file: UploadFile = File(...)):
    try:
        result = await caption_image(file)
        return JSONResponse(result)
    except Exception as e:
        return JSONResponse({"error": f"Image captioning failed: {str(e)}"}, status_code=500)

# Serve audio/pdf generated
@app.get("/files/{filename}")
async def serve_file(filename: str):
    filepath = os.path.join(tempfile.gettempdir(), filename)
    if os.path.exists(filepath):
        return FileResponse(filepath)
    return JSONResponse({"error": "File not found"}, status_code=404)