que_ans / app.py
786avinash's picture
Update app.py
1b29e14 verified
raw
history blame
1.7 kB
from transformers import BlipForQuestionAnswering, AutoProcessor
from PIL import Image
import gradio as gr
import openai # For OpenAI API
# Load the BLIP model and processor
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
# Set your OpenAI API key
openai.api_key = "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" # Replace with your OpenAI API key
# Function to generate the initial answer with BLIP and expand it with OpenAI API
def qna(image, question):
# Step 1: Get initial short answer from BLIP
inputs = processor(image, question, return_tensors="pt")
out = model.generate(**inputs)
short_answer = processor.decode(out[0], skip_special_tokens=True)
# Step 2: Construct prompt for OpenAI API
prompt = f"Question: {question}\nShort Answer: {short_answer}\nProvide a detailed explanation based on this answer."
# Step 3: Send prompt to OpenAI API for a paragraph-length answer
try:
response = openai.Completion.create(
engine="text-davinci-003", # Specify model
prompt=prompt,
max_tokens=200 # Adjust max_tokens as needed for response length
)
detailed_answer = response.choices[0].text.strip()
except Exception as e:
print(f"Exception occurred: {e}")
detailed_answer = "Failed to get response from OpenAI API."
return detailed_answer
# Create Gradio interface
interf = gr.Interface(fn=qna, inputs=["image", "text"], outputs="text")
interf.launch()