rairo commited on
Commit
817a0bc
·
verified ·
1 Parent(s): f759364

Update image_gen.py

Browse files
Files changed (1) hide show
  1. image_gen.py +60 -1
image_gen.py CHANGED
@@ -35,6 +35,7 @@ from PIL import ImageFont, ImageDraw, Image
35
  import seaborn as sns
36
 
37
 
 
38
  logging.basicConfig(level=logging.INFO)
39
  logger = logging.getLogger(__name__)
40
 
@@ -207,4 +208,62 @@ def generate_image_with_retry(prompt_text, style, model="hf", max_retries=3):
207
  logger.error(f"Attempt {attempt+1} failed: {e}")
208
  if attempt == max_retries - 1:
209
  raise
210
- return None, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  import seaborn as sns
36
 
37
 
38
+
39
  logging.basicConfig(level=logging.INFO)
40
  logger = logging.getLogger(__name__)
41
 
 
208
  logger.error(f"Attempt {attempt+1} failed: {e}")
209
  if attempt == max_retries - 1:
210
  raise
211
+ return None, None
212
+
213
+ # edit image function
214
+ def edit_section_image(image_url: str, gemini_prompt: str):
215
+ """
216
+ Downloads the existing image from image_url, uses Google Gemini to edit it
217
+ according to gemini_prompt, and returns the new edited image (as a PIL.Image).
218
+ """
219
+ try:
220
+ # 1) Download the original image
221
+ resp = requests.get(image_url)
222
+ if resp.status_code != 200:
223
+ logger.error(f"Failed to download image from {image_url}")
224
+ return None
225
+
226
+ original_image = Image.open(io.BytesIO(resp.content))
227
+
228
+ # 2) Initialize Gemini client
229
+ g_api_key = os.getenv("GEMINI")
230
+ if not g_api_key:
231
+ logger.error("GEMINI_API_KEY not found in environment variables")
232
+ print("Google Gemini API key is missing. Please set the GEMINI_API_KEY environment variable.")
233
+ return None
234
+
235
+ genai.configure(api_key=g_api_key)
236
+ client = genai.Client(api_key=g_api_key)
237
+
238
+ # 3) Prepare the prompt_with_image: a list with [ prompt_text, PIL.Image ]
239
+ prompt_with_image = [
240
+ gemini_prompt,
241
+ original_image
242
+ ]
243
+
244
+ # 4) Call the Gemini model to edit the image
245
+ response = client.models.generate_content(
246
+ model="models/gemini-2.0-flash-exp",
247
+ contents=prompt_with_image,
248
+ config=types.GenerateContentConfig(
249
+ response_modalities=['Text', 'Image']
250
+ )
251
+ )
252
+
253
+ # 5) Extract the edited image from the response
254
+ # Typically, the 'response' might have text + image. We want the image part.
255
+ edited_image = None
256
+ for part in response.candidates[0].content.parts:
257
+ if part.inline_data is not None:
258
+ edited_image = Image.open(io.BytesIO(part.inline_data.data))
259
+ break
260
+
261
+ if not edited_image:
262
+ logger.error("No edited image found in Gemini response")
263
+ return None
264
+
265
+ return edited_image
266
+
267
+ except Exception as e:
268
+ logger.error(f"Error editing section image: {e}")
269
+ return None