ikraamkb commited on
Commit
84ef14b
Β·
verified Β·
1 Parent(s): c649b34

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +20 -10
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, RedirectResponse
 
 
4
  import shutil, os
5
 
6
  app = FastAPI()
7
 
8
- # CORS
9
  app.add_middleware(
10
  CORSMiddleware,
11
- allow_origins=["*"], allow_credentials=True,
12
- allow_methods=["*"], allow_headers=["*"]
 
 
13
  )
14
 
15
- # βœ… Basic root redirection (optional)
16
- @app.get("/")
17
- def home():
18
- return RedirectResponse(url="/docs") # Or to a frontend URL
19
 
20
- # βœ… File prediction endpoint
 
 
 
 
 
 
 
 
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: