Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -177,26 +177,23 @@ def load_system_instruction(style="standard"):
|
|
| 177 |
logger.error(error_msg)
|
| 178 |
raise ValueError(error_msg)
|
| 179 |
|
| 180 |
-
def generate_html_from_text(text, temperature=0.
|
| 181 |
"""テキストからHTMLを生成する"""
|
| 182 |
try:
|
| 183 |
-
# API
|
| 184 |
api_key = os.environ.get("GEMINI_API_KEY")
|
| 185 |
if not api_key:
|
| 186 |
logger.error("GEMINI_API_KEY 環境変数が設定されていません")
|
| 187 |
raise ValueError("GEMINI_API_KEY 環境変数が設定されていません")
|
| 188 |
|
| 189 |
-
# モデル名は環境変数から取得(デフォルトは従来と同じ)
|
| 190 |
model_name = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
|
| 191 |
logger.info(f"使用するGeminiモデル: {model_name}")
|
| 192 |
|
| 193 |
-
# Gemini APIの設定
|
| 194 |
genai.configure(api_key=api_key)
|
| 195 |
|
| 196 |
-
# 指定されたスタイルのシステムインストラクションを読み込む
|
| 197 |
system_instruction = load_system_instruction(style)
|
| 198 |
|
| 199 |
-
#
|
| 200 |
generation_config = {
|
| 201 |
"temperature": temperature,
|
| 202 |
"top_p": 0.7,
|
|
@@ -205,41 +202,37 @@ def generate_html_from_text(text, temperature=0.7, style="standard"):
|
|
| 205 |
"candidate_count": 1
|
| 206 |
}
|
| 207 |
|
| 208 |
-
#
|
|
|
|
| 209 |
if model_name == "gemini-2.5-flash-preview-04-17":
|
| 210 |
-
|
| 211 |
-
logger.info("thinking_budget=0
|
| 212 |
|
| 213 |
-
# モデル初期化
|
| 214 |
model = genai.GenerativeModel(model_name)
|
| 215 |
|
| 216 |
-
# プロンプト構築
|
| 217 |
prompt = f"{system_instruction}\n\n{text}"
|
| 218 |
-
|
| 219 |
-
# コンテンツ生成
|
| 220 |
response = model.generate_content(
|
| 221 |
prompt,
|
| 222 |
generation_config=generation_config,
|
| 223 |
safety_settings=[
|
| 224 |
-
{"category": "HARM_CATEGORY_HARASSMENT",
|
| 225 |
-
{"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 226 |
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 227 |
-
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}
|
| 228 |
],
|
|
|
|
| 229 |
)
|
| 230 |
|
| 231 |
-
#
|
| 232 |
raw_response = response.text
|
| 233 |
html_start = raw_response.find("```html")
|
| 234 |
-
html_end
|
| 235 |
|
| 236 |
if html_start != -1 and html_end != -1 and html_start < html_end:
|
| 237 |
-
html_start
|
| 238 |
-
html_code = raw_response[html_start:html_end].strip()
|
| 239 |
-
logger.info(f"HTMLの生成に成功: 長さ = {len(html_code)}")
|
| 240 |
-
|
| 241 |
html_code = enhance_font_awesome_layout(html_code)
|
| 242 |
-
logger.info("
|
| 243 |
return html_code
|
| 244 |
else:
|
| 245 |
logger.warning("```html``` タグが見つかりませんでした。全文を返します。")
|
|
@@ -249,7 +242,6 @@ def generate_html_from_text(text, temperature=0.7, style="standard"):
|
|
| 249 |
logger.error(f"HTML生成中にエラーが発生: {e}", exc_info=True)
|
| 250 |
raise Exception(f"Gemini APIでのHTML生成に失敗しました: {e}")
|
| 251 |
|
| 252 |
-
|
| 253 |
# 画像から余分な空白領域をトリミングする関数
|
| 254 |
def trim_image_whitespace(image, threshold=250, padding=10):
|
| 255 |
"""
|
|
|
|
| 177 |
logger.error(error_msg)
|
| 178 |
raise ValueError(error_msg)
|
| 179 |
|
| 180 |
+
def generate_html_from_text(text, temperature=0.3, style="standard"):
|
| 181 |
"""テキストからHTMLを生成する"""
|
| 182 |
try:
|
| 183 |
+
# --- API キー & モデル判定 ---
|
| 184 |
api_key = os.environ.get("GEMINI_API_KEY")
|
| 185 |
if not api_key:
|
| 186 |
logger.error("GEMINI_API_KEY 環境変数が設定されていません")
|
| 187 |
raise ValueError("GEMINI_API_KEY 環境変数が設定されていません")
|
| 188 |
|
|
|
|
| 189 |
model_name = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
|
| 190 |
logger.info(f"使用するGeminiモデル: {model_name}")
|
| 191 |
|
|
|
|
| 192 |
genai.configure(api_key=api_key)
|
| 193 |
|
|
|
|
| 194 |
system_instruction = load_system_instruction(style)
|
| 195 |
|
| 196 |
+
# --- GenerationConfig (thinking_budget は含めない) ---
|
| 197 |
generation_config = {
|
| 198 |
"temperature": temperature,
|
| 199 |
"top_p": 0.7,
|
|
|
|
| 202 |
"candidate_count": 1
|
| 203 |
}
|
| 204 |
|
| 205 |
+
# --- 追加: Flash‑preview 用の追加 kwargs ---
|
| 206 |
+
extra_kwargs = {}
|
| 207 |
if model_name == "gemini-2.5-flash-preview-04-17":
|
| 208 |
+
extra_kwargs["thinking_budget"] = 0
|
| 209 |
+
logger.info("thinking_budget=0 をトップレベルで付与しました")
|
| 210 |
|
| 211 |
+
# --- モデル初期化 & 生成 ---
|
| 212 |
model = genai.GenerativeModel(model_name)
|
| 213 |
|
|
|
|
| 214 |
prompt = f"{system_instruction}\n\n{text}"
|
|
|
|
|
|
|
| 215 |
response = model.generate_content(
|
| 216 |
prompt,
|
| 217 |
generation_config=generation_config,
|
| 218 |
safety_settings=[
|
| 219 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 220 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 221 |
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 222 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 223 |
],
|
| 224 |
+
**extra_kwargs # ← ここで thinking_budget を渡す
|
| 225 |
)
|
| 226 |
|
| 227 |
+
# --- レスポンス処理は従来どおり ---
|
| 228 |
raw_response = response.text
|
| 229 |
html_start = raw_response.find("```html")
|
| 230 |
+
html_end = raw_response.rfind("```")
|
| 231 |
|
| 232 |
if html_start != -1 and html_end != -1 and html_start < html_end:
|
| 233 |
+
html_code = raw_response[html_start + 7:html_end].strip()
|
|
|
|
|
|
|
|
|
|
| 234 |
html_code = enhance_font_awesome_layout(html_code)
|
| 235 |
+
logger.info(f"HTMLの生成に成功 (len={len(html_code)})")
|
| 236 |
return html_code
|
| 237 |
else:
|
| 238 |
logger.warning("```html``` タグが見つかりませんでした。全文を返します。")
|
|
|
|
| 242 |
logger.error(f"HTML生成中にエラーが発生: {e}", exc_info=True)
|
| 243 |
raise Exception(f"Gemini APIでのHTML生成に失敗しました: {e}")
|
| 244 |
|
|
|
|
| 245 |
# 画像から余分な空白領域をトリミングする関数
|
| 246 |
def trim_image_whitespace(image, threshold=250, padding=10):
|
| 247 |
"""
|