Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,10 @@ from google.genai.types import HarmBlockThreshold
|
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def generate_image(input_image, partner_type):
|
| 12 |
"""
|
|
@@ -23,9 +27,12 @@ def generate_image(input_image, partner_type):
|
|
| 23 |
if input_image is None:
|
| 24 |
return None, "Please upload an image first."
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# Save the uploaded image to a temporary file
|
| 31 |
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
|
@@ -39,7 +46,7 @@ def generate_image(input_image, partner_type):
|
|
| 39 |
]
|
| 40 |
|
| 41 |
# Create the prompt based on the partner type selection
|
| 42 |
-
prompt = f"add a {partner_type.lower()}
|
| 43 |
|
| 44 |
contents = [
|
| 45 |
types.Content(
|
|
@@ -82,7 +89,7 @@ def generate_image(input_image, partner_type):
|
|
| 82 |
),
|
| 83 |
types.SafetySetting(
|
| 84 |
category="HARM_CATEGORY_CIVIC_INTEGRITY",
|
| 85 |
-
threshold=
|
| 86 |
),
|
| 87 |
],
|
| 88 |
response_mime_type="text/plain",
|
|
@@ -106,9 +113,33 @@ def generate_image(input_image, partner_type):
|
|
| 106 |
if part.text is not None:
|
| 107 |
output_text += part.text + "\n"
|
| 108 |
elif part.inline_data is not None:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
# Check if there are finish details to display
|
| 114 |
if hasattr(candidate, 'finish_details') and candidate.finish_details:
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
from io import BytesIO
|
| 9 |
import tempfile
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
# Load environment variables from .env file
|
| 13 |
+
load_dotenv()
|
| 14 |
|
| 15 |
def generate_image(input_image, partner_type):
|
| 16 |
"""
|
|
|
|
| 27 |
if input_image is None:
|
| 28 |
return None, "Please upload an image first."
|
| 29 |
|
| 30 |
+
# Get API key from environment variables
|
| 31 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
| 32 |
+
if not api_key:
|
| 33 |
+
return None, "GEMINI_API_KEY not found in environment variables."
|
| 34 |
+
|
| 35 |
+
client = genai.Client(api_key=api_key)
|
| 36 |
|
| 37 |
# Save the uploaded image to a temporary file
|
| 38 |
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
|
|
|
| 46 |
]
|
| 47 |
|
| 48 |
# Create the prompt based on the partner type selection
|
| 49 |
+
prompt = f"add a {partner_type.lower()} beside the person in the picture. Do not change the environment or background."
|
| 50 |
|
| 51 |
contents = [
|
| 52 |
types.Content(
|
|
|
|
| 89 |
),
|
| 90 |
types.SafetySetting(
|
| 91 |
category="HARM_CATEGORY_CIVIC_INTEGRITY",
|
| 92 |
+
threshold="OFF",
|
| 93 |
),
|
| 94 |
],
|
| 95 |
response_mime_type="text/plain",
|
|
|
|
| 113 |
if part.text is not None:
|
| 114 |
output_text += part.text + "\n"
|
| 115 |
elif part.inline_data is not None:
|
| 116 |
+
try:
|
| 117 |
+
# Add debug information
|
| 118 |
+
# output_text += f"Received image data of type: {type(part.inline_data.data)}\n"
|
| 119 |
+
# output_text += f"MIME type: {part.inline_data.mime_type}\n"
|
| 120 |
+
|
| 121 |
+
# Try different approaches to handle the image data
|
| 122 |
+
if isinstance(part.inline_data.data, bytes):
|
| 123 |
+
# If it's already bytes, use it directly
|
| 124 |
+
image_data = part.inline_data.data
|
| 125 |
+
else:
|
| 126 |
+
# Otherwise, try to decode it from base64
|
| 127 |
+
image_data = base64.b64decode(part.inline_data.data)
|
| 128 |
+
|
| 129 |
+
# Save to a temporary file first
|
| 130 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
| 131 |
+
temp_file.write(image_data)
|
| 132 |
+
temp_file_path = temp_file.name
|
| 133 |
+
|
| 134 |
+
# Open the image from the temporary file
|
| 135 |
+
output_image = Image.open(temp_file_path)
|
| 136 |
+
|
| 137 |
+
# Clean up the temporary file
|
| 138 |
+
os.unlink(temp_file_path)
|
| 139 |
+
|
| 140 |
+
except Exception as img_error:
|
| 141 |
+
output_text += f"Error processing image: {str(img_error)}\n"
|
| 142 |
+
output_text += f"Error type: {type(img_error).__name__}\n"
|
| 143 |
|
| 144 |
# Check if there are finish details to display
|
| 145 |
if hasattr(candidate, 'finish_details') and candidate.finish_details:
|