Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -1,10 +1,13 @@
|
|
1 |
-
from fastapi import UploadFile, Form
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.responses import JSONResponse
|
4 |
import shutil
|
5 |
import os
|
6 |
|
7 |
-
#
|
|
|
|
|
|
|
8 |
app.add_middleware(
|
9 |
CORSMiddleware,
|
10 |
allow_origins=["*"],
|
@@ -13,14 +16,16 @@ app.add_middleware(
|
|
13 |
allow_headers=["*"],
|
14 |
)
|
15 |
|
|
|
16 |
@app.post("/predict")
|
17 |
async def predict(question: str = Form(...), file: UploadFile = Form(...)):
|
18 |
try:
|
19 |
-
# Save uploaded file
|
20 |
temp_path = f"temp_{file.filename}"
|
21 |
with open(temp_path, "wb") as f:
|
22 |
shutil.copyfileobj(file.file, f)
|
23 |
|
|
|
24 |
if file.content_type.startswith("image/"):
|
25 |
from appImage import answer_question_from_image
|
26 |
from PIL import Image
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, Form
|
2 |
from fastapi.middleware.cors import CORSMiddleware
|
3 |
from fastapi.responses import JSONResponse
|
4 |
import shutil
|
5 |
import os
|
6 |
|
7 |
+
# β
Create FastAPI app
|
8 |
+
app = FastAPI()
|
9 |
+
|
10 |
+
# β
Enable CORS so frontend can talk to backend
|
11 |
app.add_middleware(
|
12 |
CORSMiddleware,
|
13 |
allow_origins=["*"],
|
|
|
16 |
allow_headers=["*"],
|
17 |
)
|
18 |
|
19 |
+
# β
Prediction endpoint
|
20 |
@app.post("/predict")
|
21 |
async def predict(question: str = Form(...), file: UploadFile = Form(...)):
|
22 |
try:
|
23 |
+
# Save uploaded file temporarily
|
24 |
temp_path = f"temp_{file.filename}"
|
25 |
with open(temp_path, "wb") as f:
|
26 |
shutil.copyfileobj(file.file, f)
|
27 |
|
28 |
+
# β
Decide handler based on file type
|
29 |
if file.content_type.startswith("image/"):
|
30 |
from appImage import answer_question_from_image
|
31 |
from PIL import Image
|