Spaces:
Running
Running
Update qtAnswering/main.py
Browse files- qtAnswering/main.py +67 -72
qtAnswering/main.py
CHANGED
@@ -1,72 +1,67 @@
|
|
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.
|
5 |
-
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
app = FastAPI()
|
10 |
-
|
11 |
-
# β
CORS
|
12 |
-
app.add_middleware(
|
13 |
-
CORSMiddleware,
|
14 |
-
allow_origins=["*"],
|
15 |
-
allow_credentials=True,
|
16 |
-
allow_methods=["*"],
|
17 |
-
allow_headers=["*"],
|
18 |
-
)
|
19 |
-
|
20 |
-
# β
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
from
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
return JSONResponse({
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
# β
Serve audio
|
69 |
-
@app.get("/audio/{filename}")
|
70 |
-
async def get_audio(filename: str):
|
71 |
-
filepath = os.path.join(gettempdir(), filename)
|
72 |
-
return FileResponse(filepath, media_type="audio/mpeg")
|
|
|
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.templating import Jinja2Templates
|
5 |
+
import shutil, os
|
6 |
+
from tempfile import gettempdir
|
7 |
+
|
8 |
+
# β
Create app
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# β
CORS middleware
|
12 |
+
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_origins=["*"],
|
15 |
+
allow_credentials=True,
|
16 |
+
allow_methods=["*"],
|
17 |
+
allow_headers=["*"],
|
18 |
+
)
|
19 |
+
|
20 |
+
# β
Templates
|
21 |
+
templates = Jinja2Templates(directory="templates")
|
22 |
+
|
23 |
+
# β
Serve Homepage
|
24 |
+
@app.get("/", response_class=HTMLResponse)
|
25 |
+
async def serve_home(request: Request):
|
26 |
+
return templates.TemplateResponse("home.html", {"request": request})
|
27 |
+
|
28 |
+
# β
Predict endpoint (handles image + document)
|
29 |
+
@app.post("/predict")
|
30 |
+
async def predict(question: str = Form(...), file: UploadFile = Form(...)):
|
31 |
+
try:
|
32 |
+
temp_path = f"temp_{file.filename}"
|
33 |
+
with open(temp_path, "wb") as f:
|
34 |
+
shutil.copyfileobj(file.file, f)
|
35 |
+
|
36 |
+
is_image = file.content_type.startswith("image/")
|
37 |
+
|
38 |
+
if is_image:
|
39 |
+
from .appImage import answer_question_from_image
|
40 |
+
from PIL import Image
|
41 |
+
image = Image.open(temp_path).convert("RGB")
|
42 |
+
answer, audio_path = answer_question_from_image(image, question)
|
43 |
+
else:
|
44 |
+
from .app import answer_question_from_doc
|
45 |
+
class NamedFile:
|
46 |
+
def __init__(self, name): self.filename = name
|
47 |
+
def read(self): return open(self.filename, "rb").read()
|
48 |
+
answer, audio_path = answer_question_from_doc(NamedFile(temp_path), question)
|
49 |
+
|
50 |
+
os.remove(temp_path)
|
51 |
+
|
52 |
+
if audio_path and os.path.exists(audio_path):
|
53 |
+
return JSONResponse({
|
54 |
+
"answer": answer,
|
55 |
+
"audio": f"/qtAnswering/audio/{os.path.basename(audio_path)}"
|
56 |
+
})
|
57 |
+
else:
|
58 |
+
return JSONResponse({"answer": answer})
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
return JSONResponse({"error": str(e)}, status_code=500)
|
62 |
+
|
63 |
+
# β
Serve audio files
|
64 |
+
@app.get("/audio/{filename}")
|
65 |
+
async def get_audio(filename: str):
|
66 |
+
filepath = os.path.join(gettempdir(), filename)
|
67 |
+
return FileResponse(filepath, media_type="audio/mpeg")
|
|
|
|
|
|
|
|
|
|