786avinash commited on
Commit
a587508
·
verified ·
1 Parent(s): 2e23250

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -4
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 # For OpenAI API
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 = "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" # 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,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"Exception occurred: {e}")
33
- detailed_answer = "Failed to get response from OpenAI API."
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
+