Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,4 +1,4 @@
|
|
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
|
@@ -34,22 +34,32 @@ async def summarize_document(file: UploadFile = File(...), length: str = Form("m
|
|
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):
|
@@ -58,20 +68,25 @@ async def serve_file(filename: str):
|
|
58 |
return FileResponse(file_path)
|
59 |
return JSONResponse({"error": "File not found"}, status_code=404)
|
60 |
|
61 |
-
# Optional
|
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 |
-
|
72 |
return await caption_image(file)
|
|
|
|
|
|
|
|
|
|
|
73 |
except Exception as e:
|
74 |
return JSONResponse(
|
75 |
-
{"error": str(e)},
|
76 |
status_code=500
|
77 |
)
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, Form, Request, File
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.responses import HTMLResponse, JSONResponse, FileResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
|
|
34 |
try:
|
35 |
from app import summarize_api
|
36 |
return await summarize_api(file, length)
|
37 |
+
except ImportError as e:
|
38 |
return JSONResponse(
|
39 |
+
{"error": f"Summarization module not available: {str(e)}"},
|
40 |
status_code=501
|
41 |
)
|
42 |
+
except Exception as e:
|
43 |
+
return JSONResponse(
|
44 |
+
{"error": f"Summarization failed: {str(e)}"},
|
45 |
+
status_code=500
|
46 |
+
)
|
47 |
|
48 |
@app.post("/imagecaption/")
|
49 |
+
async def caption_image(file: UploadFile = File(...)):
|
50 |
try:
|
51 |
from appImage import caption_from_frontend
|
52 |
return await caption_from_frontend(file)
|
53 |
+
except ImportError as e:
|
54 |
return JSONResponse(
|
55 |
+
{"error": f"Image captioning module not available: {str(e)}"},
|
56 |
status_code=501
|
57 |
)
|
58 |
+
except Exception as e:
|
59 |
+
return JSONResponse(
|
60 |
+
{"error": f"Image captioning failed: {str(e)}"},
|
61 |
+
status_code=500
|
62 |
+
)
|
63 |
|
64 |
@app.get("/files/{filename}")
|
65 |
async def serve_file(filename: str):
|
|
|
68 |
return FileResponse(file_path)
|
69 |
return JSONResponse({"error": "File not found"}, status_code=404)
|
70 |
|
71 |
+
# Optional unified endpoint
|
72 |
@app.post("/predict")
|
73 |
async def predict(
|
74 |
+
file: UploadFile = File(...),
|
75 |
option: str = Form(...), # "Summarize" or "Captioning"
|
76 |
length: Optional[str] = Form(None) # Only for summarize
|
77 |
):
|
78 |
try:
|
79 |
if option == "Summarize":
|
80 |
return await summarize_document(file, length or "medium")
|
81 |
+
elif option == "Captioning":
|
82 |
return await caption_image(file)
|
83 |
+
else:
|
84 |
+
return JSONResponse(
|
85 |
+
{"error": "Invalid option - must be 'Summarize' or 'Captioning'"},
|
86 |
+
status_code=400
|
87 |
+
)
|
88 |
except Exception as e:
|
89 |
return JSONResponse(
|
90 |
+
{"error": f"Prediction failed: {str(e)}"},
|
91 |
status_code=500
|
92 |
)
|