Create model.py
Browse files
model.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Importing the requirements
|
2 |
+
from transformers import BlipProcessor, BlipForQuestionAnswering
|
3 |
+
|
4 |
+
# Load the model and processor
|
5 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
6 |
+
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
7 |
+
|
8 |
+
|
9 |
+
# Function to answer the question
|
10 |
+
def answer_question(image, text):
|
11 |
+
"""
|
12 |
+
Generates an answer to a given question based on the provided image and text.
|
13 |
+
|
14 |
+
Args:
|
15 |
+
image (str): The path to the image file.
|
16 |
+
text (str): The question text.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
str: The generated answer to the question.
|
20 |
+
"""
|
21 |
+
|
22 |
+
# Process the inputs and generate the ids
|
23 |
+
inputs = processor(images=image, text=text, return_tensors="pt")
|
24 |
+
generated_ids = model.generate(**inputs, max_length=50)
|
25 |
+
|
26 |
+
# Decode the generated IDs
|
27 |
+
generated_answer = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
28 |
+
|
29 |
+
# Return the generated answer
|
30 |
+
return generated_answer[0]
|