sagar-g commited on
Commit
cf12451
·
verified ·
1 Parent(s): df22955

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -15
app.py CHANGED
@@ -51,12 +51,18 @@ api_key = os.getenv("Gemini_api_key")
51
  # Initialize Gemini client
52
  client = genai.Client(api_key=api_key)
53
 
54
- # Function to generate or edit image
55
  def generate_or_edit_image(prompt, uploaded_img):
56
- # Always start with prompt as text
57
- contents = [types.Content(role="user", parts=[types.Part(text=prompt)])]
58
 
59
- # If an image is uploaded, add it properly as a Blob
 
 
 
 
 
 
 
 
60
  if uploaded_img is not None:
61
  buffered = BytesIO()
62
  uploaded_img.save(buffered, format="PNG")
@@ -65,12 +71,17 @@ def generate_or_edit_image(prompt, uploaded_img):
65
  contents.append(
66
  types.Content(
67
  role="user",
68
- parts=[types.Part(
69
- inline_data=types.Blob(
70
- mime_type="image/png",
71
- data=image_bytes
 
 
 
 
 
72
  )
73
- )]
74
  )
75
  )
76
 
@@ -80,8 +91,8 @@ def generate_or_edit_image(prompt, uploaded_img):
80
  contents=contents,
81
  )
82
 
83
- # Debug
84
- print("Gemini Response:", response)
85
 
86
  for part in response.candidates[0].content.parts:
87
  if part.inline_data is not None:
@@ -90,16 +101,15 @@ def generate_or_edit_image(prompt, uploaded_img):
90
  image_bytes = base64.b64decode(data)
91
  else:
92
  image_bytes = data
93
-
94
  image = Image.open(BytesIO(image_bytes))
95
  return image
96
 
97
- return "⚠️ No image returned. Try another prompt."
98
 
99
  except Exception as e:
100
  return f"❌ Error: {e}"
101
 
102
- # Gradio UI
103
  demo = gr.Interface(
104
  fn=generate_or_edit_image,
105
  inputs=[
@@ -108,7 +118,10 @@ demo = gr.Interface(
108
  ],
109
  outputs=gr.Image(label="Generated/Edited Image"),
110
  title="Image Generator & Editor",
111
- description="Enter a prompt to generate a new image, or upload a JPG/PNG to edit it."
 
 
 
112
  )
113
 
114
  if __name__ == "__main__":
 
51
  # Initialize Gemini client
52
  client = genai.Client(api_key=api_key)
53
 
 
54
  def generate_or_edit_image(prompt, uploaded_img):
55
+ contents = []
 
56
 
57
+ # Add prompt always
58
+ contents.append(
59
+ types.Content(
60
+ role="user",
61
+ parts=[types.Part(text=prompt)]
62
+ )
63
+ )
64
+
65
+ # If an image is uploaded, include it as reference
66
  if uploaded_img is not None:
67
  buffered = BytesIO()
68
  uploaded_img.save(buffered, format="PNG")
 
71
  contents.append(
72
  types.Content(
73
  role="user",
74
+ parts=[
75
+ types.Part(
76
+ text="Use this image as reference while applying my changes."
77
+ ),
78
+ types.Part(
79
+ inline_data=types.Blob(
80
+ mime_type="image/png",
81
+ data=image_bytes
82
+ )
83
  )
84
+ ]
85
  )
86
  )
87
 
 
91
  contents=contents,
92
  )
93
 
94
+ # Debug log
95
+ print("Gemini Response received.")
96
 
97
  for part in response.candidates[0].content.parts:
98
  if part.inline_data is not None:
 
101
  image_bytes = base64.b64decode(data)
102
  else:
103
  image_bytes = data
 
104
  image = Image.open(BytesIO(image_bytes))
105
  return image
106
 
107
+ return "⚠️ No image generated. Try refining your prompt."
108
 
109
  except Exception as e:
110
  return f"❌ Error: {e}"
111
 
112
+ # Gradio app
113
  demo = gr.Interface(
114
  fn=generate_or_edit_image,
115
  inputs=[
 
118
  ],
119
  outputs=gr.Image(label="Generated/Edited Image"),
120
  title="Image Generator & Editor",
121
+ description=(
122
+ "Enter a text prompt to generate a new image.\n"
123
+ "If you upload an image, Gemini will use it as a reference and apply your requested changes."
124
+ )
125
  )
126
 
127
  if __name__ == "__main__":