tomo2chin2 commited on
Commit
e2eb5a0
·
verified ·
1 Parent(s): f4b6730

Upload 4 files

Browse files
Files changed (3) hide show
  1. README.md +1 -1
  2. app.py +44 -40
  3. requirements.txt +3 -3
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🏢
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 5.25.2
8
  app_file: app.py
9
  pinned: true
10
  ---
 
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 4.19.2
8
  app_file: app.py
9
  pinned: true
10
  ---
app.py CHANGED
@@ -15,11 +15,10 @@ import tempfile
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
 
24
  # ロギング設定
25
  logging.basicConfig(level=logging.INFO)
@@ -191,51 +190,56 @@ def generate_html_from_text(text, temperature=0.3, style="standard"):
191
  model_name = os.environ.get("GEMINI_MODEL", "gemini-1.5-pro")
192
  logger.info(f"使用するGeminiモデル: {model_name}")
193
 
 
 
 
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
 
15
  import time
16
  import os
17
  import logging
18
+ from huggingface_hub import hf_hub_download # 追加: HuggingFace Hubからファイルを直接ダウンロード
19
 
20
+ # 正しいGemini関連のインポート
21
+ import google.generativeai as genai
 
22
 
23
  # ロギング設定
24
  logging.basicConfig(level=logging.INFO)
 
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, # 0.95から0.7に下げて出力の多様性を制限
209
+ "top_k": 20, # 64から20に下げて候補を絞る
210
  "max_output_tokens": 8192,
211
+ "candidate_count": 1 # 候補は1つだけ生成
212
  }
213
 
214
+ # 安全設定 - デフォルトの安全設定を使用
215
+ safety_settings = [
216
+ {
217
+ "category": "HARM_CATEGORY_HARASSMENT",
218
+ "threshold": "BLOCK_MEDIUM_AND_ABOVE"
219
+ },
220
+ {
221
+ "category": "HARM_CATEGORY_HATE_SPEECH",
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=safety_settings
242
+ )
243
 
244
  # レスポンスからHTMLを抽出
245
  raw_response = response.text
requirements.txt CHANGED
@@ -1,11 +1,11 @@
1
  # --- START OF FILE requirements.txt ---
2
 
3
  selenium
4
- gradio>=5.25.2 # 4.19.2から更新
5
  Pillow
6
  fastapi
7
  uvicorn[standard]
8
- google-genai # google-generativeaiから更新
9
  huggingface_hub
10
 
11
- # --- END OF FILE requirements.txt ---
 
1
  # --- START OF FILE requirements.txt ---
2
 
3
  selenium
4
+ gradio==4.19.2
5
  Pillow
6
  fastapi
7
  uvicorn[standard]
8
+ google-generativeai
9
  huggingface_hub
10
 
11
+ # --- END OF FILE requirements.txt ---