Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,7 @@ from PIL import Image
|
|
| 3 |
import google.generativeai as genai
|
| 4 |
import io
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
# Configure Google Gemini API (replace with your actual API key)
|
| 8 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
@@ -14,7 +15,6 @@ genai.configure(api_key=GOOGLE_API_KEY)
|
|
| 14 |
# Function to generate the modified image
|
| 15 |
def generate_modified_image(uploaded_image, background_description):
|
| 16 |
try:
|
| 17 |
-
# Update the model name here
|
| 18 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 19 |
|
| 20 |
img = Image.open(uploaded_image)
|
|
@@ -24,7 +24,7 @@ def generate_modified_image(uploaded_image, background_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 |
]
|
| 29 |
|
| 30 |
response = model.generate_content(prompt_parts, stream=False)
|
|
@@ -32,20 +32,25 @@ def generate_modified_image(uploaded_image, background_description):
|
|
| 32 |
|
| 33 |
if response and hasattr(response, 'parts') and len(response.parts) > 0:
|
| 34 |
for part in response.parts:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
return None
|
| 43 |
else:
|
| 44 |
-
st.error("Failed to
|
| 45 |
return None
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
-
st.error(f"An error occurred: {e}")
|
| 49 |
return None
|
| 50 |
|
| 51 |
# Streamlit web app
|
|
|
|
| 3 |
import google.generativeai as genai
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
+
import base64
|
| 7 |
|
| 8 |
# Configure Google Gemini API (replace with your actual API key)
|
| 9 |
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY")
|
|
|
|
| 15 |
# Function to generate the modified image
|
| 16 |
def generate_modified_image(uploaded_image, background_description):
|
| 17 |
try:
|
|
|
|
| 18 |
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 19 |
|
| 20 |
img = Image.open(uploaded_image)
|
|
|
|
| 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. Ensure the output is a valid image format (like PNG or JPEG) encoded as bytes."
|
| 28 |
]
|
| 29 |
|
| 30 |
response = model.generate_content(prompt_parts, stream=False)
|
|
|
|
| 32 |
|
| 33 |
if response and hasattr(response, 'parts') and len(response.parts) > 0:
|
| 34 |
for part in response.parts:
|
| 35 |
+
try:
|
| 36 |
+
# Try to get the image data directly if it's a blob
|
| 37 |
+
if hasattr(part, 'blob'):
|
| 38 |
+
image_bytes = part.blob.data
|
| 39 |
+
return Image.open(io.BytesIO(image_bytes))
|
| 40 |
+
elif hasattr(part, 'text'):
|
| 41 |
+
st.warning(f"Received text response instead of image: {part.text}")
|
| 42 |
+
else:
|
| 43 |
+
st.warning(f"Unexpected part type in response: {part}")
|
| 44 |
+
except Exception as part_err:
|
| 45 |
+
st.error(f"Error processing response part: {part_err}")
|
| 46 |
+
st.error("No valid image data found in the response.")
|
| 47 |
return None
|
| 48 |
else:
|
| 49 |
+
st.error("Failed to get a valid response from the model.")
|
| 50 |
return None
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
+
st.error(f"An error occurred during generation: {e}")
|
| 54 |
return None
|
| 55 |
|
| 56 |
# Streamlit web app
|