Spaces:
Running
Running
Upload app.py
Browse files
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:
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
(
|
| 118 |
-
(
|
| 119 |
-
(1024,
|
| 120 |
-
(
|
| 121 |
-
(
|
| 122 |
-
(
|
|
|
|
| 123 |
}
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(
|