leave-everything commited on
Commit
57de023
·
verified ·
1 Parent(s): 9f05764

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -19
app.py CHANGED
@@ -111,27 +111,55 @@ def generate_image_with_gemini(prompt: str, gemini_api_key: str, model: str = "g
111
 
112
  logger.info(f"Generating image with {model}: {enhanced_prompt[:100]}...")
113
 
114
- # 新SDK: GenerateContentConfigを使用
115
- size_map = {
116
- (512, 512): "1K",
117
- (768, 768): "1K",
118
- (1024, 1024): "1K",
119
- (1024, 768): "1K",
120
- (768, 1024): "1K",
121
- (1536, 1536): "2K",
122
- (2048, 2048): "2K",
 
123
  }
124
- size_str = size_map.get((width, height), "1K")
125
-
126
- config = types.GenerateContentConfig(
127
- temperature=1.0,
128
- response_modalities=["TEXT", "IMAGE"], # 必須!
129
- image_generation_config=types.ImageGenerationConfig(
130
- size=size_str,
131
- allow_people_generation=True,
132
- image_output_format="png"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  )
134
- )
135
 
136
  # 新SDK: generate_contentの呼び出し
137
  response = client.models.generate_content(
 
111
 
112
  logger.info(f"Generating image with {model}: {enhanced_prompt[:100]}...")
113
 
114
+ # 新SDK: ImageConfigを使用
115
+ # アスペクト比のマッピング(全モデル共通)
116
+ aspect_ratio_map = {
117
+ (512, 512): "1:1",
118
+ (768, 768): "1:1",
119
+ (1024, 1024): "1:1",
120
+ (1024, 768): "4:3",
121
+ (768, 1024): "3:4",
122
+ (1536, 1536): "1:1",
123
+ (2048, 2048): "1:1",
124
  }
125
+ aspect_ratio = aspect_ratio_map.get((width, height), "1:1")
126
+
127
+ # モデル別のImageConfig設定
128
+ if model == "gemini-3-pro-image-preview":
129
+ # Gemini 3 Pro: image_sizeパラメータをサポート
130
+ size_map = {
131
+ (512, 512): "1K",
132
+ (768, 768): "1K",
133
+ (1024, 1024): "1K",
134
+ (1024, 768): "1K",
135
+ (768, 1024): "1K",
136
+ (1536, 1536): "2K",
137
+ (2048, 2048): "4K",
138
+ }
139
+ image_size = size_map.get((width, height), "1K")
140
+
141
+ logger.info(f"Using Gemini 3 Pro with image_size={image_size}, aspect_ratio={aspect_ratio}")
142
+
143
+ config = types.GenerateContentConfig(
144
+ temperature=1.0,
145
+ response_modalities=[types.Modality.TEXT, types.Modality.IMAGE],
146
+ image_config=types.ImageConfig(
147
+ aspect_ratio=aspect_ratio,
148
+ image_size=image_size, # ✅ Gemini 3 Proでのみ指定
149
+ )
150
+ )
151
+ else:
152
+ # Gemini 2.5 Flash: aspect_ratioのみサポート、image_sizeは指定しない
153
+ logger.info(f"Using Gemini 2.5 Flash with aspect_ratio={aspect_ratio} (1024px固定)")
154
+
155
+ config = types.GenerateContentConfig(
156
+ temperature=1.0,
157
+ response_modalities=[types.Modality.TEXT, types.Modality.IMAGE],
158
+ image_config=types.ImageConfig(
159
+ aspect_ratio=aspect_ratio,
160
+ # image_sizeは指定しない(デフォルト1024px)
161
+ )
162
  )
 
163
 
164
  # 新SDK: generate_contentの呼び出し
165
  response = client.models.generate_content(