admin08077 commited on
Commit
22dece8
·
verified ·
1 Parent(s): 42c2064

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -18
app.py CHANGED
@@ -1,8 +1,9 @@
1
 
2
  import gradio as gr
3
  import os
4
- import google.generativeai as genai
5
- from google.generativeai.types import HarmCategory, HarmBlockThreshold
 
6
  import json
7
  from PIL import Image
8
  from io import BytesIO
@@ -13,12 +14,21 @@ import re
13
 
14
  # --- Configuration and Initialization ---
15
 
16
- # Configure the Gemini API key from Hugging Face secrets
 
 
17
  try:
18
- API_KEY = os.environ["API_KEY"]
19
- genai.configure(api_key=API_KEY)
20
- except KeyError:
21
- raise ValueError("API_KEY environment variable not set. Please add it to your Hugging Face Space secrets.")
 
 
 
 
 
 
 
22
 
23
  # --- Default Data Structures (Ported from TypeScript) ---
24
 
@@ -86,12 +96,13 @@ def generate_image(prompt: str, aspect_ratio: str):
86
  """Generates an image using Gemini and returns a base64 data URL."""
87
  print(f"Generating image for prompt: {prompt}")
88
  try:
89
- model = genai.GenerativeModel("gemini-2.0-flash-preview-image-generation")
90
  full_prompt = f'Generate a single, high-quality image. Description: "{prompt}". The image should have a {aspect_ratio} aspect ratio.'
91
 
92
- response = model.generate_content(
 
93
  contents=full_prompt,
94
- generation_config=genai.types.GenerateContentConfig(response_modalities=['TEXT', 'IMAGE'])
95
  )
96
 
97
  image_part = next((part for part in response.candidates[0].content.parts if part.inline_data), None)
@@ -295,7 +306,7 @@ def create_gradio_app():
295
 
296
  # --- Step 1: Generate Layout ---
297
  try:
298
- model = genai.GenerativeModel('gemini-2.5-flash')
299
  prompt = create_prompt_from_answers(answers)
300
  schema = {
301
  "type": "object",
@@ -319,17 +330,19 @@ def create_gradio_app():
319
  }
320
  }
321
 
322
- response = model.generate_content(
323
- prompt,
324
- generation_config=genai.GenerationConfig(
 
325
  response_mime_type="application/json",
326
  response_schema=schema
327
  ),
 
328
  safety_settings={
329
- HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
330
- HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
331
- HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
332
- HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
333
  }
334
  )
335
  design_plan = json.loads(response.text)
 
1
 
2
  import gradio as gr
3
  import os
4
+ # Updated import to use the new 'google.genai' SDK
5
+ from google import genai
6
+ from google.genai import types
7
  import json
8
  from PIL import Image
9
  from io import BytesIO
 
14
 
15
  # --- Configuration and Initialization ---
16
 
17
+ # Configure the Gemini API client. It will automatically pick up the
18
+ # GEMINI_API_KEY from the environment variables.
19
+ # Make sure to set this secret in your Hugging Face Space.
20
  try:
21
+ client = genai.Client()
22
+ # A quick check to see if the client is configured.
23
+ # This will throw an exception if the key is not found.
24
+ client.get_api_key()
25
+ except Exception as e:
26
+ raise ValueError(
27
+ "Could not configure Gemini API client. "
28
+ "Please make sure the GEMINI_API_KEY secret is set in your Hugging Face Space. "
29
+ f"Original error: {e}"
30
+ )
31
+
32
 
33
  # --- Default Data Structures (Ported from TypeScript) ---
34
 
 
96
  """Generates an image using Gemini and returns a base64 data URL."""
97
  print(f"Generating image for prompt: {prompt}")
98
  try:
99
+ # Refactored to use the new client.models.generate_content method
100
  full_prompt = f'Generate a single, high-quality image. Description: "{prompt}". The image should have a {aspect_ratio} aspect ratio.'
101
 
102
+ response = client.models.generate_content(
103
+ model="gemini-2.0-flash-preview-image-generation",
104
  contents=full_prompt,
105
+ config=types.GenerateContentConfig(response_modalities=['TEXT', 'IMAGE'])
106
  )
107
 
108
  image_part = next((part for part in response.candidates[0].content.parts if part.inline_data), None)
 
306
 
307
  # --- Step 1: Generate Layout ---
308
  try:
309
+ # Refactored to use the new client.models.generate_content method
310
  prompt = create_prompt_from_answers(answers)
311
  schema = {
312
  "type": "object",
 
330
  }
331
  }
332
 
333
+ response = client.models.generate_content(
334
+ model='gemini-2.5-flash',
335
+ contents=prompt,
336
+ config=types.GenerateContentConfig(
337
  response_mime_type="application/json",
338
  response_schema=schema
339
  ),
340
+ # Updated to use types.HarmCategory and types.HarmBlockThreshold
341
  safety_settings={
342
+ types.HarmCategory.HARM_CATEGORY_HARASSMENT: types.HarmBlockThreshold.BLOCK_NONE,
343
+ types.HarmCategory.HARM_CATEGORY_HATE_SPEECH: types.HarmBlockThreshold.BLOCK_NONE,
344
+ types.HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: types.HarmBlockThreshold.BLOCK_NONE,
345
+ types.HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: types.HarmBlockThreshold.BLOCK_NONE,
346
  }
347
  )
348
  design_plan = json.loads(response.text)