Update app.py
Browse files
app.py
CHANGED
|
@@ -20,11 +20,22 @@ OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
|
| 20 |
|
| 21 |
|
| 22 |
def encode_image_to_base64(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
buffered = io.BytesIO()
|
| 24 |
image.save(buffered, format="PNG")
|
| 25 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 26 |
|
| 27 |
-
|
| 28 |
def analyze_image(image):
|
| 29 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 30 |
base64_image = encode_image_to_base64(image)
|
|
@@ -206,17 +217,20 @@ def process_and_analyze(image):
|
|
| 206 |
if OPENAI_API_KEY is None:
|
| 207 |
return None, "OpenAI API key not found in environment variables."
|
| 208 |
|
| 209 |
-
# Convert numpy array to PIL Image
|
| 210 |
-
if isinstance(image, np.ndarray):
|
| 211 |
-
image = Image.fromarray(image)
|
| 212 |
-
|
| 213 |
try:
|
| 214 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
gpt_response = analyze_image(image)
|
| 216 |
response_data = json.loads(gpt_response)
|
| 217 |
|
| 218 |
if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
|
| 219 |
-
# Process image with detection models
|
| 220 |
result_buf = process_image_detection(image, response_data["element"], response_data["rating"])
|
| 221 |
result_image = Image.open(result_buf)
|
| 222 |
analysis_text = f"Label: {response_data['label']}\nElement: {response_data['element']}\nRating: {response_data['rating']}/5"
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
def encode_image_to_base64(image):
|
| 23 |
+
# If image is a tuple (as sometimes provided by Gradio), take the first element
|
| 24 |
+
if isinstance(image, tuple):
|
| 25 |
+
image = image[0]
|
| 26 |
+
|
| 27 |
+
# If image is a numpy array, convert to PIL Image
|
| 28 |
+
if isinstance(image, np.ndarray):
|
| 29 |
+
image = Image.fromarray(image)
|
| 30 |
+
|
| 31 |
+
# Ensure image is in PIL Image format
|
| 32 |
+
if not isinstance(image, Image.Image):
|
| 33 |
+
raise ValueError("Input must be a PIL Image, numpy array, or tuple containing an image")
|
| 34 |
+
|
| 35 |
buffered = io.BytesIO()
|
| 36 |
image.save(buffered, format="PNG")
|
| 37 |
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 38 |
|
|
|
|
| 39 |
def analyze_image(image):
|
| 40 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 41 |
base64_image = encode_image_to_base64(image)
|
|
|
|
| 217 |
if OPENAI_API_KEY is None:
|
| 218 |
return None, "OpenAI API key not found in environment variables."
|
| 219 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
try:
|
| 221 |
+
# Handle different input types
|
| 222 |
+
if isinstance(image, tuple):
|
| 223 |
+
image = image[0]
|
| 224 |
+
if isinstance(image, np.ndarray):
|
| 225 |
+
image = Image.fromarray(image)
|
| 226 |
+
if not isinstance(image, Image.Image):
|
| 227 |
+
raise ValueError("Invalid image format")
|
| 228 |
+
|
| 229 |
+
# Analyze image
|
| 230 |
gpt_response = analyze_image(image)
|
| 231 |
response_data = json.loads(gpt_response)
|
| 232 |
|
| 233 |
if response_data["label"].lower() == "surprising" and response_data["element"].lower() != "na":
|
|
|
|
| 234 |
result_buf = process_image_detection(image, response_data["element"], response_data["rating"])
|
| 235 |
result_image = Image.open(result_buf)
|
| 236 |
analysis_text = f"Label: {response_data['label']}\nElement: {response_data['element']}\nRating: {response_data['rating']}/5"
|