tomo2chin2 commited on
Commit
94ebe32
·
verified ·
1 Parent(s): 6c2fecb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -38,6 +38,10 @@ def generate_html_from_text(text):
38
  logger.error("GEMINI_API_KEY 環境変数が設定されていません")
39
  raise ValueError("GEMINI_API_KEY 環境変数が設定されていません")
40
 
 
 
 
 
41
  # Gemini APIの設定
42
  genai.configure(api_key=api_key)
43
 
@@ -142,19 +146,14 @@ def generate_html_from_text(text):
142
  # モデルを初期化して処理
143
  logger.info(f"Gemini APIにリクエストを送信: テキスト長さ = {len(text)}")
144
 
145
- # 利用可能なモデルの確認
146
  try:
147
- models = genai.list_models()
148
- logger.info(f"利用可能なモデル: {[m.name for m in models if 'gemini' in m.name]}")
149
  except Exception as e:
150
- logger.warning(f"モデル一覧取得エラー: {e}")
151
-
152
- # モデル選択
153
- try:
154
- model = genai.GenerativeModel('gemini-2.0-flash-thinking-exp-01-21')
155
- except Exception as e:
156
- logger.warning(f"gemini-2.0-flash-thinking-exp-01-21モデル利用不可: {e}, gemini-proを試します")
157
- model = genai.GenerativeModel('gemini-pro')
158
 
159
  # 生成設定
160
  generation_config = {
@@ -212,8 +211,8 @@ def render_fullpage_screenshot(html_code: str, extension_percentage: float, is_g
212
 
213
  # Gemini生成コンテンツの場合、拡張率を調整
214
  if is_gemini_content:
215
- # 最低でも10%の拡張を確保
216
- extension_percentage = max(extension_percentage, 10.0)
217
  logger.info(f"Gemini生成コンテンツ用に拡張率を調整: {extension_percentage}%")
218
 
219
  # 1) Save HTML code to a temporary file
@@ -570,7 +569,7 @@ with gr.Blocks(title="Full Page Screenshot (テキスト変換対応)", theme=gr
570
  minimum=0,
571
  maximum=30, # より高い値を許可
572
  step=1.0,
573
- value=8.0, # デフォルト値を増加
574
  label="上下高さ拡張率(%)"
575
  )
576
 
@@ -583,10 +582,15 @@ with gr.Blocks(title="Full Page Screenshot (テキスト変換対応)", theme=gr
583
  outputs=output_image
584
  )
585
 
586
- gr.Markdown("""
 
 
587
  ## APIエンドポイント
588
  - `/api/screenshot` - HTMLコードからスクリーンショットを生成
589
  - `/api/text-to-screenshot` - テキストからインフォグラフィックスクリーンショットを生成
 
 
 
590
  """)
591
 
592
  # --- Mount Gradio App onto FastAPI ---
 
38
  logger.error("GEMINI_API_KEY 環境変数が設定されていません")
39
  raise ValueError("GEMINI_API_KEY 環境変数が設定されていません")
40
 
41
+ # モデル名の取得(環境変数から、なければデフォルト値)
42
+ model_name = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
43
+ logger.info(f"使用するGeminiモデル: {model_name}")
44
+
45
  # Gemini APIの設定
46
  genai.configure(api_key=api_key)
47
 
 
146
  # モデルを初期化して処理
147
  logger.info(f"Gemini APIにリクエストを送信: テキスト長さ = {len(text)}")
148
 
149
+ # モデル初期化とフォールバック処理
150
  try:
151
+ model = genai.GenerativeModel(model_name)
 
152
  except Exception as e:
153
+ # 指定されたモデルが使用できない場合はフォールバック
154
+ fallback_model = "gemini-pro"
155
+ logger.warning(f"{model_name}が利用できません: {e}, フォールバックモデル{fallback_model}を使用します")
156
+ model = genai.GenerativeModel(fallback_model)
 
 
 
 
157
 
158
  # 生成設定
159
  generation_config = {
 
211
 
212
  # Gemini生成コンテンツの場合、拡張率を調整
213
  if is_gemini_content:
214
+ # 最低でも20%の拡張を確保
215
+ extension_percentage = max(extension_percentage, 20.0)
216
  logger.info(f"Gemini生成コンテンツ用に拡張率を調整: {extension_percentage}%")
217
 
218
  # 1) Save HTML code to a temporary file
 
569
  minimum=0,
570
  maximum=30, # より高い値を許可
571
  step=1.0,
572
+ value=15, # デフォルト値を増加
573
  label="上下高さ拡張率(%)"
574
  )
575
 
 
582
  outputs=output_image
583
  )
584
 
585
+ # 環境変数情報を表示
586
+ gemini_model = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
587
+ gr.Markdown(f"""
588
  ## APIエンドポイント
589
  - `/api/screenshot` - HTMLコードからスクリーンショットを生成
590
  - `/api/text-to-screenshot` - テキストからインフォグラフィックスクリーンショットを生成
591
+
592
+ ## 設定情報
593
+ - 使用モデル: {gemini_model} (環境変数 GEMINI_MODEL で変更可能)
594
  """)
595
 
596
  # --- Mount Gradio App onto FastAPI ---