ikraamkb commited on
Commit
9f09ae7
Β·
verified Β·
1 Parent(s): e540abd

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -44
main.py CHANGED
@@ -1,58 +1,38 @@
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
-
11
-
12
- # βœ… CORS to allow frontend to access backend
13
- app.add_middleware(
14
- CORSMiddleware,
15
- allow_origins=["*"],
16
- allow_credentials=True,
17
- allow_methods=["*"],
18
- allow_headers=["*"],
19
- )
20
- app.mount("/resources", StaticFiles(directory="resources"), name="resources")
21
-
22
- # βœ… Mount static folder (optional JS/CSS support)
23
  app.mount("/static", StaticFiles(directory="static"), name="static")
 
24
 
25
- # βœ… Set templates folder
26
  templates = Jinja2Templates(directory="templates")
27
 
28
- # βœ… Serve home.html
29
  @app.get("/", response_class=HTMLResponse)
30
  async def serve_home(request: Request):
31
  return templates.TemplateResponse("home.html", {"request": request})
32
 
33
- # βœ… Backend prediction API
34
  @app.post("/predict")
35
- async def predict(question: str = Form(...), file: UploadFile = Form(...)):
36
- try:
37
- temp_path = f"temp_{file.filename}"
38
- with open(temp_path, "wb") as f:
39
- shutil.copyfileobj(file.file, f)
40
-
41
- if file.content_type.startswith("image/"):
42
- from appImage import answer_question_from_image
43
- from PIL import Image
44
- image = Image.open(temp_path)
45
- answer = answer_question_from_image(image, question)
46
- else:
47
- from app import answer_question_from_doc
48
- class NamedFile:
49
- def __init__(self, name): self.name = name
50
- answer = answer_question_from_doc(NamedFile(temp_path), question)
51
-
52
- os.remove(temp_path)
53
- return JSONResponse(content={"answer": answer})
54
-
55
- except Exception as e:
56
- return JSONResponse(content={"error": str(e)}, status_code=500)
57
-
58
-
 
1
+ # main.py
2
+ from fastapi import FastAPI, Request, UploadFile, Form
3
+ from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
4
  from fastapi.staticfiles import StaticFiles
5
  from fastapi.templating import Jinja2Templates
6
+ import shutil
7
+ import os
8
+ import app
9
+ import appImage
10
 
11
  app = FastAPI()
12
 
13
+ # Mount static and templates
 
 
 
 
 
 
 
 
 
 
 
 
14
  app.mount("/static", StaticFiles(directory="static"), name="static")
15
+ app.mount("/resources", StaticFiles(directory="resources"), name="resources")
16
 
 
17
  templates = Jinja2Templates(directory="templates")
18
 
19
+ # Serve your main home page
20
  @app.get("/", response_class=HTMLResponse)
21
  async def serve_home(request: Request):
22
  return templates.TemplateResponse("home.html", {"request": request})
23
 
24
+ # Route to handle prediction requests (image or doc)
25
  @app.post("/predict")
26
+ async def predict(file: UploadFile = Form(...), question: str = Form(...)):
27
+ ext = file.filename.split(".")[-1].lower()
28
+ with open(file.filename, "wb") as buffer:
29
+ shutil.copyfileobj(file.file, buffer)
30
+
31
+ if ext in ["pdf", "docx", "pptx", "xlsx"]:
32
+ answer, audio = app.answer_question_from_doc(file=buffer, question=question)
33
+ else:
34
+ image = appImage.Image.open(file.filename)
35
+ answer, audio = appImage.answer_question_from_image(image, question)
36
+
37
+ os.remove(file.filename)
38
+ return JSONResponse({"answer": answer, "audio": audio})