Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,9 +15,9 @@ import tempfile
|
|
15 |
import time
|
16 |
import os
|
17 |
import logging
|
18 |
-
from huggingface_hub import hf_hub_download
|
19 |
|
20 |
-
#
|
21 |
from google import genai
|
22 |
from google.genai import types
|
23 |
|
@@ -194,60 +194,75 @@ def generate_html_from_text(text, temperature=0.3, style="standard"):
|
|
194 |
# 指定されたスタイルのシステムインストラクションを読み込む
|
195 |
system_instruction = load_system_instruction(style)
|
196 |
|
197 |
-
#
|
198 |
-
logger.info(f"Gemini APIにリクエストを送信: テキスト長さ = {len(text)}, 温度 = {temperature}, スタイル = {style}")
|
199 |
-
|
200 |
-
# Gemini APIクライアントの初期化 (新しいSDK方式)
|
201 |
client = genai.Client(api_key=api_key)
|
202 |
|
|
|
|
|
203 |
# プロンプト構築
|
204 |
prompt = f"{system_instruction}\n\n{text}"
|
205 |
|
206 |
-
#
|
207 |
-
generation_config =
|
208 |
-
temperature
|
209 |
-
top_p
|
210 |
-
top_k
|
211 |
-
max_output_tokens
|
212 |
-
candidate_count
|
213 |
-
|
214 |
-
|
215 |
-
#
|
216 |
if model_name == "gemini-2.5-flash-preview-04-17":
|
217 |
-
logger.info("
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
# レスポンスからHTMLを抽出
|
228 |
raw_response = response.text
|
229 |
-
|
230 |
-
# HTML
|
231 |
-
html_start = raw_response.find("```
|
232 |
html_end = raw_response.rfind("```")
|
|
|
233 |
if html_start != -1 and html_end != -1 and html_start < html_end:
|
234 |
-
html_start += 7 # "```
|
235 |
html_code = raw_response[html_start:html_end].strip()
|
236 |
logger.info(f"HTMLの生成に成功: 長さ = {len(html_code)}")
|
|
|
237 |
# Font Awesomeのレイアウト改善
|
238 |
html_code = enhance_font_awesome_layout(html_code)
|
239 |
logger.info("Font Awesomeレイアウトの最適化を適用しました")
|
|
|
240 |
return html_code
|
241 |
else:
|
242 |
# HTMLタグが見つからない場合、レスポンス全体を返す
|
243 |
-
logger.warning("レスポンスから ```html ```
|
244 |
return raw_response
|
245 |
-
|
246 |
except Exception as e:
|
247 |
logger.error(f"HTML生成中にエラーが発生: {e}", exc_info=True)
|
248 |
raise Exception(f"Gemini APIでのHTML生成に失敗しました: {e}")
|
249 |
|
250 |
-
|
251 |
# 画像から余分な空白領域をトリミングする関数
|
252 |
def trim_image_whitespace(image, threshold=250, padding=10):
|
253 |
"""
|
|
|
15 |
import time
|
16 |
import os
|
17 |
import logging
|
18 |
+
from huggingface_hub import hf_hub_download
|
19 |
|
20 |
+
# 新しいGemini関連のインポート(最新のGoogle Gen AI SDK)
|
21 |
from google import genai
|
22 |
from google.genai import types
|
23 |
|
|
|
194 |
# 指定されたスタイルのシステムインストラクションを読み込む
|
195 |
system_instruction = load_system_instruction(style)
|
196 |
|
197 |
+
# Gemini APIクライアントの初期化(最新SDKに対応)
|
|
|
|
|
|
|
198 |
client = genai.Client(api_key=api_key)
|
199 |
|
200 |
+
logger.info(f"Gemini APIにリクエストを送信: テキスト長さ = {len(text)}, 温度 = {temperature}, スタイル = {style}")
|
201 |
+
|
202 |
# プロンプト構築
|
203 |
prompt = f"{system_instruction}\n\n{text}"
|
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 |
+
# gemini-2.5-flash-preview-04-17の場合はthinking_budgetを設定
|
215 |
if model_name == "gemini-2.5-flash-preview-04-17":
|
216 |
+
logger.info("gemini-2.5-flash-preview-04-17モデル検出: thinking_budget=0 を設定")
|
217 |
+
# 新しいSDKでの正しい実装方法
|
218 |
+
response = client.models.generate_content(
|
219 |
+
model=model_name,
|
220 |
+
contents=prompt,
|
221 |
+
config=types.GenerateContentConfig(
|
222 |
+
temperature=temperature,
|
223 |
+
top_p=0.7,
|
224 |
+
top_k=20,
|
225 |
+
max_output_tokens=8192,
|
226 |
+
candidate_count=1,
|
227 |
+
thinking_config=types.ThinkingConfig(
|
228 |
+
thinking_budget=0 # 思考機能を無効化
|
229 |
+
)
|
230 |
+
)
|
231 |
+
)
|
232 |
+
else:
|
233 |
+
# 他のモデルの場合は従来通りの呼び出し
|
234 |
+
response = client.models.generate_content(
|
235 |
+
model=model_name,
|
236 |
+
contents=prompt,
|
237 |
+
generation_config=generation_config
|
238 |
+
)
|
239 |
+
|
240 |
# レスポンスからHTMLを抽出
|
241 |
raw_response = response.text
|
242 |
+
|
243 |
+
# HTMLタグ部分だけを抽出(```html と ``` の間)
|
244 |
+
html_start = raw_response.find("```html")
|
245 |
html_end = raw_response.rfind("```")
|
246 |
+
|
247 |
if html_start != -1 and html_end != -1 and html_start < html_end:
|
248 |
+
html_start += 7 # "```html" の長さ分進める
|
249 |
html_code = raw_response[html_start:html_end].strip()
|
250 |
logger.info(f"HTMLの生成に成功: 長さ = {len(html_code)}")
|
251 |
+
|
252 |
# Font Awesomeのレイアウト改善
|
253 |
html_code = enhance_font_awesome_layout(html_code)
|
254 |
logger.info("Font Awesomeレイアウトの最適化を適用しました")
|
255 |
+
|
256 |
return html_code
|
257 |
else:
|
258 |
# HTMLタグが見つからない場合、レスポンス全体を返す
|
259 |
+
logger.warning("レスポンスから ```html ``` タグが見つかりませんでした。全テキストを返します。")
|
260 |
return raw_response
|
261 |
+
|
262 |
except Exception as e:
|
263 |
logger.error(f"HTML生成中にエラーが発生: {e}", exc_info=True)
|
264 |
raise Exception(f"Gemini APIでのHTML生成に失敗しました: {e}")
|
265 |
|
|
|
266 |
# 画像から余分な空白領域をトリミングする関数
|
267 |
def trim_image_whitespace(image, threshold=250, padding=10):
|
268 |
"""
|