File size: 1,693 Bytes
07a7f47
 
 
8678eca
 
07a7f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
45
46
47
from fastapi import FastAPI, UploadFile, Form
from fastapi.responses import JSONResponse
from transformers import AutoProcessor, AutoModelForVisualQuestionAnswering
from PIL import Image
import torch
import uvicorn

# Initialize FastAPI app
app = FastAPI()

# Load model and processor with trust_remote_code=True
processor = AutoProcessor.from_pretrained("Sanket17/hello", trust_remote_code=True)
model = AutoModelForVisualQuestionAnswering.from_pretrained("Sanket17/hello", trust_remote_code=True)

@app.post("/vqa/")
async def visual_question_answer(file: UploadFile, question: str = Form(...)):
    """
    Endpoint for visual question answering.
    - file: Upload an image file
    - question: Textual question about the image
    """
    try:
        # Load image
        image = Image.open(file.file).convert("RGB")
        
        # Preprocess inputs
        inputs = processor(images=image, text=question, return_tensors="pt")
        
        # Get model predictions
        outputs = model(**inputs)
        
        # Decode the answer (check model output for correct handling)
        answer = outputs.logits.argmax(dim=-1).item()  # Example way to get the answer index
        
        # If the output logits contain a mapping, we can return the answer string
        answer_str = processor.decode([answer])  # Assuming you get the answer index from logits

        # Return JSON response
        return JSONResponse(content={"question": question, "answer": answer_str})
    
    except Exception as e:
        return JSONResponse(content={"error": str(e)}, status_code=500)

# Start the FastAPI server
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)