Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,23 +1,33 @@
|
|
1 |
-
from fastapi import FastAPI, UploadFile, Form
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
-
from fastapi.responses import JSONResponse,
|
|
|
|
|
4 |
import shutil, os
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
-
# CORS
|
9 |
app.add_middleware(
|
10 |
CORSMiddleware,
|
11 |
-
allow_origins=["*"],
|
12 |
-
|
|
|
|
|
13 |
)
|
14 |
|
15 |
-
# β
|
16 |
-
|
17 |
-
def home():
|
18 |
-
return RedirectResponse(url="/docs") # Or to a frontend URL
|
19 |
|
20 |
-
# β
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
@app.post("/predict")
|
22 |
async def predict(question: str = Form(...), file: UploadFile = Form(...)):
|
23 |
try:
|
|
|
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 shutil, os
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
# β
CORS to allow frontend to access backend
|
11 |
app.add_middleware(
|
12 |
CORSMiddleware,
|
13 |
+
allow_origins=["*"],
|
14 |
+
allow_credentials=True,
|
15 |
+
allow_methods=["*"],
|
16 |
+
allow_headers=["*"],
|
17 |
)
|
18 |
|
19 |
+
# β
Mount static folder (optional JS/CSS support)
|
20 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
|
21 |
|
22 |
+
# β
Set templates folder
|
23 |
+
templates = Jinja2Templates(directory="templates")
|
24 |
+
|
25 |
+
# β
Serve home.html
|
26 |
+
@app.get("/", response_class=HTMLResponse)
|
27 |
+
async def serve_home(request: Request):
|
28 |
+
return templates.TemplateResponse("home.html", {"request": request})
|
29 |
+
|
30 |
+
# β
Backend prediction API
|
31 |
@app.post("/predict")
|
32 |
async def predict(question: str = Form(...), file: UploadFile = Form(...)):
|
33 |
try:
|