Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,58 +1,38 @@
|
|
1 |
-
|
2 |
-
from fastapi
|
3 |
-
from fastapi.responses import HTMLResponse, JSONResponse
|
4 |
from fastapi.staticfiles import StaticFiles
|
5 |
from fastapi.templating import Jinja2Templates
|
6 |
-
import shutil
|
|
|
|
|
|
|
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 |
-
#
|
29 |
@app.get("/", response_class=HTMLResponse)
|
30 |
async def serve_home(request: Request):
|
31 |
return templates.TemplateResponse("home.html", {"request": request})
|
32 |
|
33 |
-
#
|
34 |
@app.post("/predict")
|
35 |
-
async def predict(
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
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})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|