Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,29 +1,77 @@
|
|
1 |
-
from fastapi import FastAPI, Request
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
-
from fastapi.responses import HTMLResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
from fastapi.templating import Jinja2Templates
|
6 |
import os
|
|
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
-
#
|
11 |
app.add_middleware(
|
12 |
CORSMiddleware,
|
13 |
allow_origins=["*"],
|
14 |
allow_credentials=True,
|
15 |
allow_methods=["*"],
|
16 |
-
allow_headers=["*"]
|
17 |
)
|
18 |
|
19 |
-
#
|
20 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
21 |
app.mount("/resources", StaticFiles(directory="resources"), name="resources")
|
22 |
|
23 |
-
#
|
24 |
templates = Jinja2Templates(directory="templates")
|
25 |
|
26 |
-
# β
Home route
|
27 |
@app.get("/", response_class=HTMLResponse)
|
28 |
async def serve_home(request: Request):
|
29 |
return templates.TemplateResponse("HomeS.html", {"request": request})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, Form, Request
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
from fastapi.templating import Jinja2Templates
|
6 |
import os
|
7 |
+
import tempfile
|
8 |
+
from typing import Optional
|
9 |
|
10 |
app = FastAPI()
|
11 |
|
12 |
+
# CORS Configuration
|
13 |
app.add_middleware(
|
14 |
CORSMiddleware,
|
15 |
allow_origins=["*"],
|
16 |
allow_credentials=True,
|
17 |
allow_methods=["*"],
|
18 |
+
allow_headers=["*"],
|
19 |
)
|
20 |
|
21 |
+
# Static assets
|
22 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
23 |
app.mount("/resources", StaticFiles(directory="resources"), name="resources")
|
24 |
|
25 |
+
# Templates
|
26 |
templates = Jinja2Templates(directory="templates")
|
27 |
|
|
|
28 |
@app.get("/", response_class=HTMLResponse)
|
29 |
async def serve_home(request: Request):
|
30 |
return templates.TemplateResponse("HomeS.html", {"request": request})
|
31 |
+
|
32 |
+
@app.post("/summarize/")
|
33 |
+
async def summarize_document(file: UploadFile = File(...), length: str = Form("medium")):
|
34 |
+
try:
|
35 |
+
from app import summarize_api
|
36 |
+
return await summarize_api(file, length)
|
37 |
+
except ImportError:
|
38 |
+
return JSONResponse(
|
39 |
+
{"error": "Summarization module not available"},
|
40 |
+
status_code=501
|
41 |
+
)
|
42 |
+
|
43 |
+
@app.post("/imagecaption/")
|
44 |
+
async def caption_image(file: UploadFile):
|
45 |
+
try:
|
46 |
+
from appImage import caption_from_frontend
|
47 |
+
return await caption_from_frontend(file)
|
48 |
+
except ImportError:
|
49 |
+
return JSONResponse(
|
50 |
+
{"error": "Image captioning module not available"},
|
51 |
+
status_code=501
|
52 |
+
)
|
53 |
+
|
54 |
+
@app.get("/files/{filename}")
|
55 |
+
async def serve_file(filename: str):
|
56 |
+
file_path = os.path.join(tempfile.gettempdir(), filename)
|
57 |
+
if os.path.exists(file_path):
|
58 |
+
return FileResponse(file_path)
|
59 |
+
return JSONResponse({"error": "File not found"}, status_code=404)
|
60 |
+
|
61 |
+
# Optional: Fallback endpoint if you want a single endpoint
|
62 |
+
@app.post("/predict")
|
63 |
+
async def predict(
|
64 |
+
file: UploadFile,
|
65 |
+
option: str = Form(...), # "Summarize" or "Captioning"
|
66 |
+
length: Optional[str] = Form(None) # Only for summarize
|
67 |
+
):
|
68 |
+
try:
|
69 |
+
if option == "Summarize":
|
70 |
+
return await summarize_document(file, length or "medium")
|
71 |
+
else:
|
72 |
+
return await caption_image(file)
|
73 |
+
except Exception as e:
|
74 |
+
return JSONResponse(
|
75 |
+
{"error": str(e)},
|
76 |
+
status_code=500
|
77 |
+
)
|