Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -16,14 +16,13 @@ def generate_modified_image(uploaded_image, background_description):
|
|
16 |
try:
|
17 |
model = genai.GenerativeModel('gemini-pro-vision')
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
]
|
22 |
|
23 |
prompt_parts = [
|
24 |
"You are an AI that can modify the background of an image based on a text description.",
|
25 |
"Here is the image:",
|
26 |
-
|
27 |
f"Modify the background of this image to: '{background_description}'. Be creative and make the new background look realistic and integrated with the foreground.",
|
28 |
"Output only the modified image."
|
29 |
]
|
@@ -32,8 +31,15 @@ def generate_modified_image(uploaded_image, background_description):
|
|
32 |
response.resolve()
|
33 |
|
34 |
if response and hasattr(response, 'parts') and len(response.parts) > 0:
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
else:
|
38 |
st.error("Failed to generate the modified image.")
|
39 |
return None
|
|
|
16 |
try:
|
17 |
model = genai.GenerativeModel('gemini-pro-vision')
|
18 |
|
19 |
+
# Open the uploaded file as a PIL Image
|
20 |
+
img = Image.open(uploaded_image)
|
|
|
21 |
|
22 |
prompt_parts = [
|
23 |
"You are an AI that can modify the background of an image based on a text description.",
|
24 |
"Here is the image:",
|
25 |
+
img, # Pass the PIL Image object directly
|
26 |
f"Modify the background of this image to: '{background_description}'. Be creative and make the new background look realistic and integrated with the foreground.",
|
27 |
"Output only the modified image."
|
28 |
]
|
|
|
31 |
response.resolve()
|
32 |
|
33 |
if response and hasattr(response, 'parts') and len(response.parts) > 0:
|
34 |
+
for part in response.parts:
|
35 |
+
if hasattr(part.data, 'image/png'):
|
36 |
+
image_bytes = part.data['image/png']
|
37 |
+
return Image.open(io.BytesIO(image_bytes))
|
38 |
+
elif hasattr(part.data, 'image/jpeg'):
|
39 |
+
image_bytes = part.data['image/jpeg']
|
40 |
+
return Image.open(io.BytesIO(image_bytes))
|
41 |
+
st.error("Generated content did not contain an image.")
|
42 |
+
return None
|
43 |
else:
|
44 |
st.error("Failed to generate the modified image.")
|
45 |
return None
|