Spaces:
No application file
No application file
Create aa.py
Browse files
aa.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import io
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="VQA Backend")
|
| 8 |
+
app.add_middleware(
|
| 9 |
+
CORSMiddleware,
|
| 10 |
+
allow_origins=["*"],
|
| 11 |
+
allow_methods=["*"],
|
| 12 |
+
allow_headers=["*"],
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
@app.get("/")
|
| 17 |
+
def health_check():
|
| 18 |
+
return {"status": "ok"}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
@app.post("/predict")
|
| 22 |
+
async def predict(image: UploadFile = File(...), question: str = "What is this?"):
|
| 23 |
+
contents = await image.read()
|
| 24 |
+
img = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 25 |
+
|
| 26 |
+
# Just for test; later you’ll load your model inside Docker
|
| 27 |
+
answer = f"Model would answer: {question!r} on {img.size}"
|
| 28 |
+
|
| 29 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg") as f:
|
| 30 |
+
img.save(f.name)
|
| 31 |
+
# Here you’ll plug in your own `final_pipeline(...)`
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
return {"answer": answer}
|