File size: 2,971 Bytes
c319660
59efe71
8888b75
59efe71
 
c0e0602
8888b75
 
59efe71
 
 
8888b75
59efe71
 
 
 
 
8888b75
59efe71
 
8888b75
59efe71
c0e0602
59efe71
8888b75
59efe71
 
 
 
c0e0602
8888b75
 
 
 
 
 
c319660
8888b75
c319660
8888b75
 
c319660
 
 
 
 
8888b75
 
c319660
8888b75
 
 
c319660
8888b75
c319660
8888b75
 
c319660
 
 
 
 
8888b75
 
 
 
 
 
 
 
c319660
8888b75
 
c319660
8888b75
 
 
 
 
 
c319660
8888b75
c319660
 
 
 
 
8888b75
 
c319660
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
83
84
85
86
87
88
89
90
91
92
from fastapi import FastAPI, UploadFile, Form, Request, File
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import os
import tempfile
from typing import Optional

app = FastAPI()

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

# Static assets
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/resources", StaticFiles(directory="resources"), name="resources")

# Templates
templates = Jinja2Templates(directory="templates")

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

@app.post("/summarize/")
async def summarize_document(file: UploadFile = File(...), length: str = Form("medium")):
    try:
        from app import summarize_api
        return await summarize_api(file, length)
    except ImportError as e:
        return JSONResponse(
            {"error": f"Summarization module not available: {str(e)}"}, 
            status_code=501
        )
    except Exception as e:
        return JSONResponse(
            {"error": f"Summarization failed: {str(e)}"},
            status_code=500
        )

@app.post("/imagecaption/")
async def caption_image(file: UploadFile = File(...)):
    try:
        from appImage import caption_from_frontend
        return await caption_from_frontend(file)
    except ImportError as e:
        return JSONResponse(
            {"error": f"Image captioning module not available: {str(e)}"}, 
            status_code=501
        )
    except Exception as e:
        return JSONResponse(
            {"error": f"Image captioning failed: {str(e)}"},
            status_code=500
        )

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

# Optional unified endpoint
@app.post("/predict")
async def predict(
    file: UploadFile = File(...),
    option: str = Form(...),  # "Summarize" or "Captioning"
    length: Optional[str] = Form(None)  # Only for summarize
):
    try:
        if option == "Summarize":
            return await summarize_document(file, length or "medium")
        elif option == "Captioning":
            return await caption_image(file)
        else:
            return JSONResponse(
                {"error": "Invalid option - must be 'Summarize' or 'Captioning'"},
                status_code=400
            )
    except Exception as e:
        return JSONResponse(
            {"error": f"Prediction failed: {str(e)}"},
            status_code=500
        )