File size: 792 Bytes
2c5cb17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from models.blip_model import blip_answer
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
from config import DEVICE

tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-base")
model = AutoModelForSeq2SeqLM.from_pretrained(
    "google/flan-t5-base"
).to(DEVICE)

model.eval()

def reasoning_answer(image, question):

    caption, base_answer = blip_answer(image, question)

    prompt = f"""
Scene:
{caption}

Question:
{question}

Base Answer:
{base_answer}

Provide:
Final Answer:
Explanation:
"""

    inputs = tokenizer(prompt, return_tensors="pt").to(DEVICE)

    with torch.no_grad():
        outputs = model.generate(**inputs, max_new_tokens=100)

    text = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return caption, base_answer, text