File size: 2,411 Bytes
8888b75
59efe71
8888b75
59efe71
 
c0e0602
8888b75
 
59efe71
 
 
8888b75
59efe71
 
 
 
 
8888b75
59efe71
 
8888b75
59efe71
c0e0602
59efe71
8888b75
59efe71
 
 
 
c0e0602
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
from fastapi import FastAPI, UploadFile, Form, Request
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:
        return JSONResponse(
            {"error": "Summarization module not available"}, 
            status_code=501
        )

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

@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: Fallback endpoint if you want a single endpoint
@app.post("/predict")
async def predict(
    file: UploadFile,
    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")
        else:
            return await caption_image(file)
    except Exception as e:
        return JSONResponse(
            {"error": str(e)},
            status_code=500
        )