Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,8 @@ 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()
|
@@ -23,101 +25,115 @@ def swap_clothing(person_image, clothing_image):
|
|
23 |
Returns:
|
24 |
The generated image with the clothing swapped and any relevant messages
|
25 |
"""
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
#
|
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 both uploaded images to temporary files
|
38 |
temp_files = []
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
),
|
68 |
-
types.
|
69 |
-
|
70 |
-
|
71 |
-
mime_type=files[1].mime_type,
|
72 |
),
|
73 |
-
types.
|
74 |
-
|
75 |
-
|
76 |
-
mime_type=files[0].mime_type,
|
77 |
),
|
78 |
],
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
generate_content_config = types.GenerateContentConfig(
|
83 |
-
temperature=0.12,
|
84 |
-
top_p=0.95,
|
85 |
-
top_k=40,
|
86 |
-
max_output_tokens=8192,
|
87 |
-
response_modalities=[
|
88 |
-
"image",
|
89 |
-
"text",
|
90 |
-
],
|
91 |
-
safety_settings=[
|
92 |
-
types.SafetySetting(
|
93 |
-
category="HARM_CATEGORY_HARASSMENT",
|
94 |
-
threshold=HarmBlockThreshold.BLOCK_NONE,
|
95 |
-
),
|
96 |
-
types.SafetySetting(
|
97 |
-
category="HARM_CATEGORY_HATE_SPEECH",
|
98 |
-
threshold=HarmBlockThreshold.BLOCK_NONE,
|
99 |
-
),
|
100 |
-
types.SafetySetting(
|
101 |
-
category="HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
102 |
-
threshold=HarmBlockThreshold.BLOCK_NONE,
|
103 |
-
),
|
104 |
-
types.SafetySetting(
|
105 |
-
category="HARM_CATEGORY_DANGEROUS_CONTENT",
|
106 |
-
threshold=HarmBlockThreshold.BLOCK_NONE,
|
107 |
-
),
|
108 |
-
],
|
109 |
-
response_mime_type="text/plain",
|
110 |
-
)
|
111 |
|
112 |
-
try:
|
113 |
response = client.models.generate_content(
|
114 |
model="models/gemini-2.0-flash-exp",
|
115 |
contents=contents,
|
116 |
config=generate_content_config,
|
117 |
)
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
121 |
|
122 |
# Process the response
|
123 |
if response and hasattr(response, 'candidates') and response.candidates:
|
@@ -144,28 +160,34 @@ def swap_clothing(person_image, clothing_image):
|
|
144 |
output_text += f"Error processing image: {str(img_error)}\n"
|
145 |
else:
|
146 |
output_text = "The model did not generate a valid response. Please try again with different images."
|
147 |
-
|
148 |
-
except Exception as
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
finally:
|
153 |
-
# Clean up temporary files
|
154 |
for temp_file in temp_files:
|
155 |
if os.path.exists(temp_file):
|
156 |
os.unlink(temp_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
|
158 |
return output_image, output_text
|
159 |
-
|
160 |
-
except Exception as e:
|
161 |
-
# Clean up temporary files in case of error
|
162 |
-
for temp_file in temp_files:
|
163 |
-
if os.path.exists(temp_file):
|
164 |
-
os.unlink(temp_file)
|
165 |
-
|
166 |
-
error_details = f"Error: {str(e)}\n\nType: {type(e).__name__}"
|
167 |
-
print(f"Exception occurred: {error_details}")
|
168 |
-
return None, error_details
|
169 |
|
170 |
# Create the Gradio interface
|
171 |
def create_interface():
|
|
|
8 |
from io import BytesIO
|
9 |
import tempfile
|
10 |
from dotenv import load_dotenv
|
11 |
+
import warnings
|
12 |
+
import io
|
13 |
|
14 |
# Load environment variables from .env file
|
15 |
load_dotenv()
|
|
|
25 |
Returns:
|
26 |
The generated image with the clothing swapped and any relevant messages
|
27 |
"""
|
28 |
+
# Capture warnings in a string buffer
|
29 |
+
warning_buffer = io.StringIO()
|
30 |
+
warnings.filterwarnings('always') # Ensure all warnings are shown
|
31 |
|
32 |
+
# Initialize variables outside the try block
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
temp_files = []
|
34 |
+
uploaded_files = []
|
35 |
+
client = None
|
36 |
+
output_image = None
|
37 |
+
output_text = ""
|
38 |
+
|
39 |
+
with warnings.catch_warnings(record=True) as warning_list:
|
40 |
+
try:
|
41 |
+
# Check if both images are provided
|
42 |
+
if person_image is None or clothing_image is None:
|
43 |
+
return None, "Please upload both images."
|
44 |
+
|
45 |
+
# Get API key from environment variables
|
46 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
47 |
+
if not api_key:
|
48 |
+
return None, "GEMINI_API_KEY not found in environment variables."
|
49 |
+
|
50 |
+
# Create a fresh client instance for each request
|
51 |
+
client = genai.Client(api_key=api_key)
|
52 |
+
|
53 |
+
# Save both uploaded images to temporary files
|
54 |
+
for img, prefix in [(person_image, "person"), (clothing_image, "clothing")]:
|
55 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as temp_file:
|
56 |
+
img.save(temp_file.name)
|
57 |
+
temp_files.append(temp_file.name)
|
58 |
+
|
59 |
+
# Upload both files to Gemini with fresh file uploads
|
60 |
+
uploaded_files = [
|
61 |
+
client.files.upload(file=temp_files[0]), # person image
|
62 |
+
client.files.upload(file=temp_files[1]), # clothing image
|
63 |
+
]
|
64 |
+
|
65 |
+
# Create the prompt
|
66 |
+
prompt = '''
|
67 |
+
Edit the person's clothing by swapping it with the clothing in the clothing image.
|
68 |
+
Retain the same facial features, pose and background from the person image.
|
69 |
+
The output image should be an image of the person wearing the clothing from the clothing image.
|
70 |
+
The image pose and background should be the same as the person image but with the new clothing:
|
71 |
+
'''
|
72 |
+
|
73 |
+
contents = [
|
74 |
+
types.Content(
|
75 |
+
role="user",
|
76 |
+
parts=[
|
77 |
+
types.Part.from_text(text="This is the person image. Do not change the face or features of the person. Pay attention and retain the background, pose, facial features."),
|
78 |
+
types.Part.from_uri(
|
79 |
+
file_uri=uploaded_files[0].uri,
|
80 |
+
mime_type=uploaded_files[0].mime_type,
|
81 |
+
),
|
82 |
+
types.Part.from_text(text="This is the clothing image. Swap the clothing onto the person image."),
|
83 |
+
types.Part.from_uri(
|
84 |
+
file_uri=uploaded_files[1].uri,
|
85 |
+
mime_type=uploaded_files[1].mime_type,
|
86 |
+
),
|
87 |
+
types.Part.from_text(text=prompt),
|
88 |
+
types.Part.from_uri(
|
89 |
+
file_uri=uploaded_files[0].uri,
|
90 |
+
mime_type=uploaded_files[0].mime_type,
|
91 |
+
),
|
92 |
+
],
|
93 |
+
),
|
94 |
+
]
|
95 |
+
|
96 |
+
generate_content_config = types.GenerateContentConfig(
|
97 |
+
temperature=0.099,
|
98 |
+
top_p=0.95,
|
99 |
+
top_k=40,
|
100 |
+
max_output_tokens=8192,
|
101 |
+
response_modalities=[
|
102 |
+
"image",
|
103 |
+
"text",
|
104 |
+
],
|
105 |
+
safety_settings=[
|
106 |
+
types.SafetySetting(
|
107 |
+
category="HARM_CATEGORY_HARASSMENT",
|
108 |
+
threshold=HarmBlockThreshold.BLOCK_NONE,
|
109 |
+
),
|
110 |
+
types.SafetySetting(
|
111 |
+
category="HARM_CATEGORY_HATE_SPEECH",
|
112 |
+
threshold=HarmBlockThreshold.BLOCK_NONE,
|
113 |
),
|
114 |
+
types.SafetySetting(
|
115 |
+
category="HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
116 |
+
threshold=HarmBlockThreshold.BLOCK_NONE,
|
|
|
117 |
),
|
118 |
+
types.SafetySetting(
|
119 |
+
category="HARM_CATEGORY_DANGEROUS_CONTENT",
|
120 |
+
threshold=HarmBlockThreshold.BLOCK_NONE,
|
|
|
121 |
),
|
122 |
],
|
123 |
+
response_mime_type="text/plain",
|
124 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
|
|
126 |
response = client.models.generate_content(
|
127 |
model="models/gemini-2.0-flash-exp",
|
128 |
contents=contents,
|
129 |
config=generate_content_config,
|
130 |
)
|
131 |
+
|
132 |
+
# Add any warnings to the output text
|
133 |
+
if warning_list:
|
134 |
+
output_text += "\nWarnings:\n"
|
135 |
+
for warning in warning_list:
|
136 |
+
output_text += f"- {warning.message}\n"
|
137 |
|
138 |
# Process the response
|
139 |
if response and hasattr(response, 'candidates') and response.candidates:
|
|
|
160 |
output_text += f"Error processing image: {str(img_error)}\n"
|
161 |
else:
|
162 |
output_text = "The model did not generate a valid response. Please try again with different images."
|
163 |
+
|
164 |
+
except Exception as e:
|
165 |
+
error_details = f"Error: {str(e)}\n\nType: {type(e).__name__}"
|
166 |
+
if warning_list:
|
167 |
+
error_details += "\n\nWarnings:\n"
|
168 |
+
for warning in warning_list:
|
169 |
+
error_details += f"- {warning.message}\n"
|
170 |
+
print(f"Exception occurred: {error_details}")
|
171 |
+
return None, error_details
|
172 |
|
173 |
finally:
|
174 |
+
# Clean up all temporary files
|
175 |
for temp_file in temp_files:
|
176 |
if os.path.exists(temp_file):
|
177 |
os.unlink(temp_file)
|
178 |
+
|
179 |
+
# Clean up any uploaded files if possible
|
180 |
+
for uploaded_file in uploaded_files:
|
181 |
+
try:
|
182 |
+
if hasattr(client.files, 'delete') and uploaded_file:
|
183 |
+
client.files.delete(uploaded_file.uri)
|
184 |
+
except:
|
185 |
+
pass # Best effort cleanup
|
186 |
+
|
187 |
+
# Clear the client
|
188 |
+
client = None
|
189 |
|
190 |
return output_image, output_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
|
192 |
# Create the Gradio interface
|
193 |
def create_interface():
|