codelion commited on
Commit
4a8bcb2
·
verified ·
1 Parent(s): e730056

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -17
app.py CHANGED
@@ -10,34 +10,34 @@ import os
10
  client = genai.Client(api_key=os.environ['GEMINI_API_KEY'])
11
 
12
  def generate_item(tag):
13
- """
14
- Generate a single feed item consisting of text from Gemini LLM and an image from Imagen.
15
-
16
- Args:
17
- tag (str): The tag to base the content on.
18
-
19
- Returns:
20
- dict: A dictionary with 'text' (str) and 'image_base64' (str).
21
- """
22
  # Generate text using Gemini LLM
23
  prompt = f"Generate a short, engaging post about {tag} in the style of a TikTok caption."
24
- text_response = client.models.generate_content(model='gemini-2.5-flash-preview-04-17',contents=[prompt])
 
 
 
25
  text = text_response.text.strip()
26
 
27
- # Generate an image using Imagen based on the generated text
28
  image_response = client.models.generate_images(
29
  model='imagen-3.0-generate-002',
30
- prompt=text,
31
  config=types.GenerateImagesConfig(
32
  number_of_images=1,
33
- aspect_ratio="9:16", # TikTok-style vertical video ratio
34
- person_generation="DONT_ALLOW" # Avoid generating people
35
  )
36
  )
37
- generated_image = image_response.generated_images[0]
38
- image = Image.open(BytesIO(generated_image.image.image_bytes))
39
 
40
- # Convert the image to base64 for HTML display
 
 
 
 
 
 
 
 
41
  buffered = BytesIO()
42
  image.save(buffered, format="PNG")
43
  img_str = base64.b64encode(buffered.getvalue()).decode()
 
10
  client = genai.Client(api_key=os.environ['GEMINI_API_KEY'])
11
 
12
  def generate_item(tag):
 
 
 
 
 
 
 
 
 
13
  # Generate text using Gemini LLM
14
  prompt = f"Generate a short, engaging post about {tag} in the style of a TikTok caption."
15
+ text_response = client.models.generate_content(
16
+ model='gemini-2.5-flash-preview-04-17',
17
+ contents=[prompt]
18
+ )
19
  text = text_response.text.strip()
20
 
21
+ # Generate an image based on the text or tag
22
  image_response = client.models.generate_images(
23
  model='imagen-3.0-generate-002',
24
+ prompt=text, # Using the generated text as the prompt
25
  config=types.GenerateImagesConfig(
26
  number_of_images=1,
27
+ aspect_ratio="9:16",
28
+ person_generation="DONT_ALLOW"
29
  )
30
  )
 
 
31
 
32
+ # Check if images were generated
33
+ if image_response.generated_images and len(image_response.generated_images) > 0:
34
+ generated_image = image_response.generated_images[0]
35
+ image = Image.open(BytesIO(generated_image.image.image_bytes))
36
+ else:
37
+ # Fallback to a placeholder image if no images are generated
38
+ image = Image.new('RGB', (300, 533), color='gray') # Size matches 9:16 aspect ratio
39
+
40
+ # Convert the image to base64
41
  buffered = BytesIO()
42
  image.save(buffered, format="PNG")
43
  img_str = base64.b64encode(buffered.getvalue()).decode()