File size: 1,697 Bytes
900d27d
fc655be
98d3845
1b29e14
900d27d
 
 
 
 
1b29e14
 
900d27d
1b29e14
900d27d
 
 
 
 
 
1b29e14
900d27d
 
1b29e14
 
 
 
 
 
 
 
 
 
 
900d27d
 
 
 
 
 
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
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()