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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -34,9 +34,13 @@
34
  # if __name__ == "__main__":
35
  # demo.launch()
36
 
 
 
 
37
  import os
38
  import gradio as gr
39
- import google.genai as genai # ✅ correct import
 
40
  from PIL import Image
41
  from io import BytesIO
42
  import base64
@@ -49,17 +53,26 @@ client = genai.Client(api_key=api_key)
49
 
50
  # Function to generate or edit image
51
  def generate_or_edit_image(prompt, uploaded_img):
52
- contents = [prompt]
 
53
 
54
- # If user uploads an image, include it
55
  if uploaded_img is not None:
56
  buffered = BytesIO()
57
  uploaded_img.save(buffered, format="PNG")
58
  image_bytes = buffered.getvalue()
59
- contents.append({
60
- "mime_type": "image/png",
61
- "data": image_bytes
62
- })
 
 
 
 
 
 
 
 
63
 
64
  try:
65
  response = client.models.generate_content(
@@ -67,12 +80,11 @@ def generate_or_edit_image(prompt, uploaded_img):
67
  contents=contents,
68
  )
69
 
70
- # 🔎 Debug: print structure
71
  print("Gemini Response:", response)
72
 
73
  for part in response.candidates[0].content.parts:
74
  if part.inline_data is not None:
75
- # Sometimes Gemini returns base64 string
76
  data = part.inline_data.data
77
  if isinstance(data, str):
78
  image_bytes = base64.b64decode(data)
@@ -82,7 +94,7 @@ def generate_or_edit_image(prompt, uploaded_img):
82
  image = Image.open(BytesIO(image_bytes))
83
  return image
84
 
85
- return "⚠️ No image generated. Try a different prompt."
86
 
87
  except Exception as e:
88
  return f"❌ Error: {e}"
@@ -96,7 +108,7 @@ demo = gr.Interface(
96
  ],
97
  outputs=gr.Image(label="Generated/Edited Image"),
98
  title="Image Generator & Editor",
99
- description="Enter a prompt to generate a new image, or upload a JPG/PNG to edit."
100
  )
101
 
102
  if __name__ == "__main__":
 
34
  # if __name__ == "__main__":
35
  # demo.launch()
36
 
37
+
38
+
39
+
40
  import os
41
  import gradio as gr
42
+ import google.genai as genai
43
+ from google.genai import types
44
  from PIL import Image
45
  from io import BytesIO
46
  import base64
 
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")
63
  image_bytes = buffered.getvalue()
64
+
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
 
77
  try:
78
  response = client.models.generate_content(
 
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:
 
88
  data = part.inline_data.data
89
  if isinstance(data, str):
90
  image_bytes = base64.b64decode(data)
 
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}"
 
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__":