Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -52,40 +52,40 @@ def enhance_font_awesome_layout(html_code):
|
|
| 52 |
margin-right: 8px !important;
|
| 53 |
vertical-align: middle !important;
|
| 54 |
}
|
| 55 |
-
|
| 56 |
/* テキスト内のアイコン位置調整 */
|
| 57 |
-
h1 [class*="fa-"], h2 [class*="fa-"], h3 [class*="fa-"],
|
| 58 |
h4 [class*="fa-"], h5 [class*="fa-"], h6 [class*="fa-"] {
|
| 59 |
vertical-align: middle !important;
|
| 60 |
margin-right: 10px !important;
|
| 61 |
}
|
| 62 |
-
|
| 63 |
/* 特定パターンの修正 */
|
| 64 |
.fa + span, .fas + span, .far + span, .fab + span,
|
| 65 |
span + .fa, span + .fas, span + .far + span {
|
| 66 |
display: inline-block !important;
|
| 67 |
margin-left: 5px !important;
|
| 68 |
}
|
| 69 |
-
|
| 70 |
/* カード内アイコン修正 */
|
| 71 |
.card [class*="fa-"], .card-body [class*="fa-"] {
|
| 72 |
float: none !important;
|
| 73 |
clear: none !important;
|
| 74 |
position: relative !important;
|
| 75 |
}
|
| 76 |
-
|
| 77 |
/* アイコンと文字が重なる場合の調整 */
|
| 78 |
li [class*="fa-"], p [class*="fa-"] {
|
| 79 |
margin-right: 10px !important;
|
| 80 |
}
|
| 81 |
-
|
| 82 |
/* インラインアイコンのスペーシング */
|
| 83 |
.inline-icon {
|
| 84 |
display: inline-flex !important;
|
| 85 |
align-items: center !important;
|
| 86 |
justify-content: flex-start !important;
|
| 87 |
}
|
| 88 |
-
|
| 89 |
/* アイコン後のテキスト */
|
| 90 |
[class*="fa-"] + span {
|
| 91 |
display: inline-block !important;
|
|
@@ -178,65 +178,106 @@ def load_system_instruction(style="standard"):
|
|
| 178 |
raise ValueError(error_msg)
|
| 179 |
|
| 180 |
def generate_html_from_text(text, temperature=0.3, style="standard"):
|
| 181 |
-
"""テキストからHTML
|
| 182 |
try:
|
|
|
|
| 183 |
api_key = os.environ.get("GEMINI_API_KEY")
|
| 184 |
if not api_key:
|
|
|
|
| 185 |
raise ValueError("GEMINI_API_KEY 環境変数が設定されていません")
|
| 186 |
|
|
|
|
|
|
|
| 187 |
model_name = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
|
| 188 |
logger.info(f"使用するGeminiモデル: {model_name}")
|
|
|
|
|
|
|
| 189 |
genai.configure(api_key=api_key)
|
| 190 |
|
|
|
|
| 191 |
system_instruction = load_system_instruction(style)
|
| 192 |
|
| 193 |
-
#
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
"max_output_tokens": 8192,
|
| 199 |
-
"candidate_count":
|
| 200 |
}
|
| 201 |
|
| 202 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
if model_name == "gemini-2.5-flash-preview-04-17":
|
| 204 |
-
logger.info("thinking_budget=0
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
| 208 |
)
|
| 209 |
else:
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
-
|
| 227 |
-
raw = response.text
|
| 228 |
-
start = raw.find("```html")
|
| 229 |
-
end = raw.rfind("```")
|
| 230 |
-
if start != -1 and end != -1 and start < end:
|
| 231 |
-
html = raw[start + 7:end].strip()
|
| 232 |
-
html = enhance_font_awesome_layout(html)
|
| 233 |
-
return html
|
| 234 |
else:
|
| 235 |
-
|
| 236 |
-
|
|
|
|
| 237 |
|
| 238 |
except Exception as e:
|
| 239 |
-
logger.error(f"HTML
|
| 240 |
raise Exception(f"Gemini APIでのHTML生成に失敗しました: {e}")
|
| 241 |
|
| 242 |
# 画像から余分な空白領域をトリミングする関数
|
|
@@ -741,6 +782,7 @@ with gr.Blocks(title="Full Page Screenshot (テキスト変換対応)", theme=gr
|
|
| 741 |
)
|
| 742 |
|
| 743 |
# 環境変数情報を表示
|
|
|
|
| 744 |
gemini_model = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
|
| 745 |
gr.Markdown(f"""
|
| 746 |
## APIエンドポイント
|
|
|
|
| 52 |
margin-right: 8px !important;
|
| 53 |
vertical-align: middle !important;
|
| 54 |
}
|
| 55 |
+
|
| 56 |
/* テキスト内のアイコン位置調整 */
|
| 57 |
+
h1 [class*="fa-"], h2 [class*="fa-"], h3 [class*="fa-"],
|
| 58 |
h4 [class*="fa-"], h5 [class*="fa-"], h6 [class*="fa-"] {
|
| 59 |
vertical-align: middle !important;
|
| 60 |
margin-right: 10px !important;
|
| 61 |
}
|
| 62 |
+
|
| 63 |
/* 特定パターンの修正 */
|
| 64 |
.fa + span, .fas + span, .far + span, .fab + span,
|
| 65 |
span + .fa, span + .fas, span + .far + span {
|
| 66 |
display: inline-block !important;
|
| 67 |
margin-left: 5px !important;
|
| 68 |
}
|
| 69 |
+
|
| 70 |
/* カード内アイコン修正 */
|
| 71 |
.card [class*="fa-"], .card-body [class*="fa-"] {
|
| 72 |
float: none !important;
|
| 73 |
clear: none !important;
|
| 74 |
position: relative !important;
|
| 75 |
}
|
| 76 |
+
|
| 77 |
/* アイコンと文字が重なる場合の調整 */
|
| 78 |
li [class*="fa-"], p [class*="fa-"] {
|
| 79 |
margin-right: 10px !important;
|
| 80 |
}
|
| 81 |
+
|
| 82 |
/* インラインアイコンのスペーシング */
|
| 83 |
.inline-icon {
|
| 84 |
display: inline-flex !important;
|
| 85 |
align-items: center !important;
|
| 86 |
justify-content: flex-start !important;
|
| 87 |
}
|
| 88 |
+
|
| 89 |
/* アイコン後のテキスト */
|
| 90 |
[class*="fa-"] + span {
|
| 91 |
display: inline-block !important;
|
|
|
|
| 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 |
+
# モデル名の取得(環境変数から、なければデフォルト値)
|
| 190 |
+
# ユーザーの要望通り、デフォルト値は gemini-1.5-pro のままにし、環境変数で指定されたモデルを使用する
|
| 191 |
model_name = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
|
| 192 |
logger.info(f"使用するGeminiモデル: {model_name}")
|
| 193 |
+
|
| 194 |
+
# Gemini APIの設定
|
| 195 |
genai.configure(api_key=api_key)
|
| 196 |
|
| 197 |
+
# 指定されたスタイルのシステムインストラクションを読み込む
|
| 198 |
system_instruction = load_system_instruction(style)
|
| 199 |
|
| 200 |
+
# モデル初期化
|
| 201 |
+
logger.info(f"Gemini APIにリクエストを送信: テキスト長さ = {len(text)}, 温度 = {temperature}, スタイル = {style}")
|
| 202 |
+
|
| 203 |
+
# モデル初期化
|
| 204 |
+
model = genai.GenerativeModel(model_name)
|
| 205 |
+
|
| 206 |
+
# 生成設定 - ばらつきを減らすために設定を調整
|
| 207 |
+
generation_config = {
|
| 208 |
+
"temperature": temperature, # より低い温度を設定
|
| 209 |
+
"top_p": 0.7, # 0.95から0.7に下げて出力の多様性を制限
|
| 210 |
+
"top_k": 20, # 64から20に下げて候補を絞る
|
| 211 |
"max_output_tokens": 8192,
|
| 212 |
+
"candidate_count": 1 # 候補は1つだけ生成
|
| 213 |
}
|
| 214 |
|
| 215 |
+
# 安全設定 - デフォルトの安全設定を使用
|
| 216 |
+
safety_settings = [
|
| 217 |
+
{
|
| 218 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 219 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 220 |
+
},
|
| 221 |
+
{
|
| 222 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 223 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 224 |
+
},
|
| 225 |
+
{
|
| 226 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 227 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 228 |
+
},
|
| 229 |
+
{
|
| 230 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 231 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 232 |
+
}
|
| 233 |
+
]
|
| 234 |
+
|
| 235 |
+
# プロンプト構築
|
| 236 |
+
prompt = f"{system_instruction}\n\n{text}"
|
| 237 |
+
|
| 238 |
+
# コンテンツ生成
|
| 239 |
+
# モデルが gemini-2.5-flash-preview-04-17 の場合のみ thinking_budget=0 を tool_config に含める
|
| 240 |
if model_name == "gemini-2.5-flash-preview-04-17":
|
| 241 |
+
logger.info(f"使用モデルが {model_name} なので tool_config に thinking_budget=0 を追加します。")
|
| 242 |
+
response = model.generate_content(
|
| 243 |
+
prompt,
|
| 244 |
+
generation_config=generation_config,
|
| 245 |
+
safety_settings=safety_settings,
|
| 246 |
+
tool_config={"code_execution": {"enable": True, "thinking_budget": 0}}
|
| 247 |
)
|
| 248 |
else:
|
| 249 |
+
logger.info(f"使用モデルが {model_name} なので thinking_budget は追加しません。")
|
| 250 |
+
response = model.generate_content(
|
| 251 |
+
prompt,
|
| 252 |
+
generation_config=generation_config,
|
| 253 |
+
safety_settings=safety_settings
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
# レスポンスからHTMLを抽出
|
| 258 |
+
raw_response = response.text
|
| 259 |
+
|
| 260 |
+
# HTMLタグ部分だけを抽出(```html と ``` の間)
|
| 261 |
+
html_start = raw_response.find("```html")
|
| 262 |
+
html_end = raw_response.rfind("```")
|
| 263 |
+
|
| 264 |
+
if html_start != -1 and html_end != -1 and html_start < html_end:
|
| 265 |
+
html_start += 7 # "```html" の長さ分進める
|
| 266 |
+
html_code = raw_response[html_start:html_end].strip()
|
| 267 |
+
logger.info(f"HTMLの生成に成功: 長さ = {len(html_code)}")
|
| 268 |
+
|
| 269 |
+
# Font Awesomeのレイアウト改善
|
| 270 |
+
html_code = enhance_font_awesome_layout(html_code)
|
| 271 |
+
logger.info("Font Awesomeレイアウトの最適化を適用しました")
|
| 272 |
|
| 273 |
+
return html_code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
else:
|
| 275 |
+
# HTMLタグが見つからない場合、レスポンス全体を返す
|
| 276 |
+
logger.warning("レスポンスから ```html ``` タグが見つかりませんでした。全テキストを返します。")
|
| 277 |
+
return raw_response
|
| 278 |
|
| 279 |
except Exception as e:
|
| 280 |
+
logger.error(f"HTML生成中にエラーが発生: {e}", exc_info=True)
|
| 281 |
raise Exception(f"Gemini APIでのHTML生成に失敗しました: {e}")
|
| 282 |
|
| 283 |
# 画像から余分な空白領域をトリミングする関数
|
|
|
|
| 782 |
)
|
| 783 |
|
| 784 |
# 環境変数情報を表示
|
| 785 |
+
# デフォルトは gemini-1.5-pro のままにし、環境変数で指定されたモデルが表示されるようにする
|
| 786 |
gemini_model = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
|
| 787 |
gr.Markdown(f"""
|
| 788 |
## APIエンドポイント
|