skjaini commited on
Commit
d364ff4
·
verified ·
1 Parent(s): c7602de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
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
- 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
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