Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
from transformers import BlipForQuestionAnswering, AutoProcessor
|
| 2 |
from PIL import Image
|
| 3 |
import gradio as gr
|
| 4 |
-
import openai
|
| 5 |
|
| 6 |
# Load the BLIP model and processor
|
| 7 |
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
| 8 |
processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
| 9 |
|
| 10 |
# Set your OpenAI API key
|
| 11 |
-
openai.api_key = "
|
| 12 |
|
| 13 |
# Function to generate the initial answer with BLIP and expand it with OpenAI API
|
| 14 |
def qna(image, question):
|
|
@@ -28,12 +28,19 @@ def qna(image, question):
|
|
| 28 |
max_tokens=200 # Adjust max_tokens as needed for response length
|
| 29 |
)
|
| 30 |
detailed_answer = response.choices[0].text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
-
print(f"
|
| 33 |
-
detailed_answer = "Failed to
|
| 34 |
|
| 35 |
return detailed_answer
|
| 36 |
|
| 37 |
# Create Gradio interface
|
| 38 |
interf = gr.Interface(fn=qna, inputs=["image", "text"], outputs="text")
|
| 39 |
interf.launch()
|
|
|
|
|
|
| 1 |
from transformers import BlipForQuestionAnswering, AutoProcessor
|
| 2 |
from PIL import Image
|
| 3 |
import gradio as gr
|
| 4 |
+
import openai
|
| 5 |
|
| 6 |
# Load the BLIP model and processor
|
| 7 |
model = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
| 8 |
processor = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
| 9 |
|
| 10 |
# Set your OpenAI API key
|
| 11 |
+
openai.api_key = "your_openai_api_key_here" # Replace with your OpenAI API key
|
| 12 |
|
| 13 |
# Function to generate the initial answer with BLIP and expand it with OpenAI API
|
| 14 |
def qna(image, question):
|
|
|
|
| 28 |
max_tokens=200 # Adjust max_tokens as needed for response length
|
| 29 |
)
|
| 30 |
detailed_answer = response.choices[0].text.strip()
|
| 31 |
+
|
| 32 |
+
except openai.error.OpenAIError as e:
|
| 33 |
+
# Print detailed error information
|
| 34 |
+
print(f"OpenAI API error: {e}")
|
| 35 |
+
detailed_answer = f"Failed to get response from OpenAI API: {e}"
|
| 36 |
+
|
| 37 |
except Exception as e:
|
| 38 |
+
print(f"General exception: {e}")
|
| 39 |
+
detailed_answer = "Failed to connect to OpenAI API."
|
| 40 |
|
| 41 |
return detailed_answer
|
| 42 |
|
| 43 |
# Create Gradio interface
|
| 44 |
interf = gr.Interface(fn=qna, inputs=["image", "text"], outputs="text")
|
| 45 |
interf.launch()
|
| 46 |
+
|