Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -177,7 +177,7 @@ 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キーの取得と設定
|
|
@@ -185,88 +185,71 @@ def generate_html_from_text(text, temperature=0.3, style="standard"):
|
|
| 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 |
-
logger.info(f"Gemini APIにリクエストを送信: テキスト長さ = {len(text)}, 温度 = {temperature}, スタイル = {style}")
|
| 201 |
-
|
| 202 |
-
# モデル初期化
|
| 203 |
-
model = genai.GenerativeModel(model_name)
|
| 204 |
-
|
| 205 |
-
# 生成設定 - ばらつきを減らすために設定を調整
|
| 206 |
generation_config = {
|
| 207 |
-
"temperature": temperature,
|
| 208 |
-
"top_p": 0.7,
|
| 209 |
-
"top_k": 20,
|
| 210 |
"max_output_tokens": 8192,
|
| 211 |
-
"candidate_count": 1
|
| 212 |
}
|
| 213 |
-
|
| 214 |
-
#
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 223 |
-
},
|
| 224 |
-
{
|
| 225 |
-
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 226 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 227 |
-
},
|
| 228 |
-
{
|
| 229 |
-
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 230 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 231 |
-
}
|
| 232 |
-
]
|
| 233 |
-
|
| 234 |
# プロンプト構築
|
| 235 |
prompt = f"{system_instruction}\n\n{text}"
|
| 236 |
-
|
| 237 |
# コンテンツ生成
|
| 238 |
response = model.generate_content(
|
| 239 |
prompt,
|
| 240 |
generation_config=generation_config,
|
| 241 |
-
safety_settings=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
)
|
| 243 |
-
|
| 244 |
-
#
|
| 245 |
raw_response = response.text
|
| 246 |
-
|
| 247 |
-
# HTMLタグ部分だけを抽出(```html と ``` の間)
|
| 248 |
html_start = raw_response.find("```html")
|
| 249 |
html_end = raw_response.rfind("```")
|
| 250 |
-
|
| 251 |
if html_start != -1 and html_end != -1 and html_start < html_end:
|
| 252 |
-
html_start += 7
|
| 253 |
html_code = raw_response[html_start:html_end].strip()
|
| 254 |
logger.info(f"HTMLの生成に成功: 長さ = {len(html_code)}")
|
| 255 |
-
|
| 256 |
-
# Font Awesomeのレイアウト改善
|
| 257 |
html_code = enhance_font_awesome_layout(html_code)
|
| 258 |
logger.info("Font Awesomeレイアウトの最適化を適用しました")
|
| 259 |
-
|
| 260 |
return html_code
|
| 261 |
else:
|
| 262 |
-
|
| 263 |
-
logger.warning("レスポンスから```html```タグが見つかり��せんでした。全テキストを返します。")
|
| 264 |
return raw_response
|
| 265 |
-
|
| 266 |
except Exception as e:
|
| 267 |
logger.error(f"HTML生成中にエラーが発生: {e}", exc_info=True)
|
| 268 |
raise Exception(f"Gemini APIでのHTML生成に失敗しました: {e}")
|
| 269 |
|
|
|
|
| 270 |
# 画像から余分な空白領域をトリミングする関数
|
| 271 |
def trim_image_whitespace(image, threshold=250, padding=10):
|
| 272 |
"""
|
|
|
|
| 177 |
logger.error(error_msg)
|
| 178 |
raise ValueError(error_msg)
|
| 179 |
|
| 180 |
+
def generate_html_from_text(text, temperature=0.7, style="standard"):
|
| 181 |
"""テキストからHTMLを生成する"""
|
| 182 |
try:
|
| 183 |
# APIキーの取得と設定
|
|
|
|
| 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,
|
| 203 |
+
"top_k": 20,
|
| 204 |
"max_output_tokens": 8192,
|
| 205 |
+
"candidate_count": 1
|
| 206 |
}
|
| 207 |
+
|
| 208 |
+
# ★ 追加: 2.5‑Flash プレビューを使う場合だけ thinking_budget=0 を付与
|
| 209 |
+
if model_name == "gemini-2.5-flash-preview-04-17":
|
| 210 |
+
generation_config["thinking_budget"] = 0
|
| 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", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 225 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
|
| 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 = raw_response.rfind("```")
|
| 235 |
+
|
| 236 |
if html_start != -1 and html_end != -1 and html_start < html_end:
|
| 237 |
+
html_start += 7
|
| 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("Font Awesomeレイアウトの最適化を適用しました")
|
|
|
|
| 243 |
return html_code
|
| 244 |
else:
|
| 245 |
+
logger.warning("```html``` タグが見つかりませんでした。全文を返します。")
|
|
|
|
| 246 |
return raw_response
|
| 247 |
+
|
| 248 |
except Exception as e:
|
| 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 |
"""
|