Sanket17 commited on
Commit
c43b93e
·
verified ·
1 Parent(s): ce25499

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, Form
2
+ from fastapi.responses import JSONResponse
3
+ from transformers import AutoProcessor, AutoModelForVisualQuestionAnswering
4
+ from PIL import Image
5
+ import io
6
+
7
+ # Initialize FastAPI app
8
+ app = FastAPI()
9
+
10
+ # Load the model and processor
11
+ processor = AutoProcessor.from_pretrained("microsoft/OmniParser")
12
+ model = AutoModelForVisualQuestionAnswering.from_pretrained("microsoft/OmniParser")
13
+
14
+ @app.post("/predict")
15
+ async def predict(image: UploadFile, question: str = Form(...)):
16
+ try:
17
+ # Read image file
18
+ image_data = await image.read()
19
+ pil_image = Image.open(io.BytesIO(image_data))
20
+
21
+ # Process the input
22
+ inputs = processor(images=pil_image, text=question, return_tensors="pt")
23
+
24
+ # Get model predictions
25
+ outputs = model(**inputs)
26
+ answer = processor.decode(outputs.logits.argmax(-1).squeeze().tolist())
27
+
28
+ return JSONResponse(content={"answer": answer})
29
+ except Exception as e:
30
+ return JSONResponse(content={"error": str(e)}, status_code=500)
31
+
32
+ @app.get("/")
33
+ def root():
34
+ return {"message": "Welcome to the OmniParser Visual Question Answering API!"}