Spaces:
Sleeping
Sleeping
Update Summarization/main.py
Browse files- Summarization/main.py +59 -6
Summarization/main.py
CHANGED
@@ -43,21 +43,74 @@ async def caption(file: UploadFile = File(...)):
|
|
43 |
except Exception as e:
|
44 |
return JSONResponse({"error": str(e)}, status_code=500)
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
@app.get("/files/{filename}")
|
47 |
-
async def serve_file(filename: str):
|
48 |
-
filepath = os.path.join(tempfile.gettempdir(), filename)
|
49 |
-
if filename.endswith('.mp3'):
|
50 |
-
return FileResponse(filepath, media_type="audio/mpeg")
|
51 |
async def serve_file(filename: str):
|
52 |
filepath = os.path.join(tempfile.gettempdir(), filename)
|
53 |
|
54 |
if not os.path.exists(filepath):
|
55 |
return JSONResponse({"error": "File not found"}, status_code=404)
|
56 |
|
57 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
media_type, _ = mimetypes.guess_type(filename)
|
59 |
return FileResponse(
|
60 |
filepath,
|
61 |
media_type=media_type or "application/octet-stream",
|
62 |
-
filename=filename
|
63 |
)
|
|
|
43 |
except Exception as e:
|
44 |
return JSONResponse({"error": str(e)}, status_code=500)
|
45 |
|
46 |
+
from fastapi import FastAPI, UploadFile, File, Form, Request
|
47 |
+
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
|
48 |
+
from fastapi.templating import Jinja2Templates
|
49 |
+
from fastapi.middleware.cors import CORSMiddleware
|
50 |
+
import tempfile
|
51 |
+
import os
|
52 |
+
from pathlib import Path
|
53 |
+
import mimetypes # Add this import
|
54 |
+
|
55 |
+
from .app import summarize_document
|
56 |
+
from .appImage import caption_image
|
57 |
+
|
58 |
+
app = FastAPI()
|
59 |
+
|
60 |
+
app.add_middleware(
|
61 |
+
CORSMiddleware,
|
62 |
+
allow_origins=["*"],
|
63 |
+
allow_credentials=True,
|
64 |
+
allow_methods=["*"],
|
65 |
+
allow_headers=["*"],
|
66 |
+
)
|
67 |
+
|
68 |
+
current_dir = Path(__file__).parent
|
69 |
+
templates = Jinja2Templates(directory=current_dir.parent / "templates")
|
70 |
+
|
71 |
+
@app.get("/", response_class=HTMLResponse)
|
72 |
+
async def serve_home(request: Request):
|
73 |
+
return templates.TemplateResponse("HomeS.html", {"request": request})
|
74 |
+
|
75 |
+
@app.post("/summarize/")
|
76 |
+
async def summarize_api(file: UploadFile = File(...), length: str = Form("medium")):
|
77 |
+
try:
|
78 |
+
result = await summarize_document(file, length)
|
79 |
+
return JSONResponse(result)
|
80 |
+
except Exception as e:
|
81 |
+
return JSONResponse({"detail": str(e)}, status_code=500)
|
82 |
+
|
83 |
+
@app.post("/imagecaption/")
|
84 |
+
async def caption(file: UploadFile = File(...)):
|
85 |
+
try:
|
86 |
+
result = await caption_image(file)
|
87 |
+
return JSONResponse(result)
|
88 |
+
except Exception as e:
|
89 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
90 |
+
|
91 |
@app.get("/files/{filename}")
|
|
|
|
|
|
|
|
|
92 |
async def serve_file(filename: str):
|
93 |
filepath = os.path.join(tempfile.gettempdir(), filename)
|
94 |
|
95 |
if not os.path.exists(filepath):
|
96 |
return JSONResponse({"error": "File not found"}, status_code=404)
|
97 |
|
98 |
+
# Force download for PDF files
|
99 |
+
if filename.endswith('.pdf'):
|
100 |
+
return FileResponse(
|
101 |
+
filepath,
|
102 |
+
media_type="application/pdf",
|
103 |
+
headers={"Content-Disposition": f"attachment; filename={filename}"}
|
104 |
+
)
|
105 |
+
|
106 |
+
# Handle audio files
|
107 |
+
if filename.endswith('.mp3'):
|
108 |
+
return FileResponse(filepath, media_type="audio/mpeg")
|
109 |
+
|
110 |
+
# Default handling for other files
|
111 |
media_type, _ = mimetypes.guess_type(filename)
|
112 |
return FileResponse(
|
113 |
filepath,
|
114 |
media_type=media_type or "application/octet-stream",
|
115 |
+
filename=filename
|
116 |
)
|