Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,16 +12,16 @@ 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):
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
try:
|
25 |
response = openai.Completion.create(
|
26 |
engine="text-davinci-003", # Specify model
|
27 |
prompt=prompt,
|
@@ -29,18 +29,20 @@ def qna(image, question):
|
|
29 |
)
|
30 |
detailed_answer = response.choices[0].text.strip()
|
31 |
|
|
|
|
|
32 |
except openai.error.OpenAIError as e:
|
33 |
-
#
|
34 |
print(f"OpenAI API error: {e}")
|
35 |
-
|
|
|
36 |
|
37 |
except Exception as e:
|
|
|
38 |
print(f"General exception: {e}")
|
39 |
-
|
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 |
-
|
|
|
12 |
|
13 |
# Function to generate the initial answer with BLIP and expand it with OpenAI API
|
14 |
def qna(image, question):
|
15 |
+
try:
|
16 |
+
# Step 1: Get initial short answer from BLIP
|
17 |
+
inputs = processor(image, question, return_tensors="pt")
|
18 |
+
out = model.generate(**inputs)
|
19 |
+
short_answer = processor.decode(out[0], skip_special_tokens=True)
|
20 |
|
21 |
+
# Step 2: Construct prompt for OpenAI API
|
22 |
+
prompt = f"Question: {question}\nShort Answer: {short_answer}\nProvide a detailed explanation based on this answer."
|
23 |
|
24 |
+
# Step 3: Send prompt to OpenAI API for a paragraph-length answer
|
|
|
25 |
response = openai.Completion.create(
|
26 |
engine="text-davinci-003", # Specify model
|
27 |
prompt=prompt,
|
|
|
29 |
)
|
30 |
detailed_answer = response.choices[0].text.strip()
|
31 |
|
32 |
+
return detailed_answer
|
33 |
+
|
34 |
except openai.error.OpenAIError as e:
|
35 |
+
# Log full error details in the backend
|
36 |
print(f"OpenAI API error: {e}")
|
37 |
+
# Return a user-friendly error message to the Gradio interface
|
38 |
+
return "An error occurred while generating the response. Please try again later."
|
39 |
|
40 |
except Exception as e:
|
41 |
+
# Log any other exceptions in the backend
|
42 |
print(f"General exception: {e}")
|
43 |
+
# Return a general error message to the Gradio interface
|
44 |
+
return "A technical issue occurred. Please try again later."
|
|
|
45 |
|
46 |
# Create Gradio interface
|
47 |
interf = gr.Interface(fn=qna, inputs=["image", "text"], outputs="text")
|
48 |
interf.launch()
|
|