tomo2chin2 commited on
Commit
3ce0735
·
verified ·
1 Parent(s): 382785d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -28
app.py CHANGED
@@ -16,9 +16,8 @@ import time
16
  import os
17
  import logging
18
 
19
- # 新しいGemini関連のインポート
20
  import google.generativeai as genai
21
- from google.generativeai import types
22
 
23
  # ロギング設定
24
  logging.basicConfig(level=logging.INFO)
@@ -30,20 +29,16 @@ class GeminiRequest(BaseModel):
30
  text: str
31
  extension_percentage: float = 8.0 # Default value same as Gradio slider
32
 
33
- def init_gemini_client():
34
- """Gemini APIクライアントを初期化する"""
35
- api_key = os.environ.get("GEMINI_API_KEY")
36
- if not api_key:
37
- logger.error("GEMINI_API_KEY 環境変数が設定されていません")
38
- raise ValueError("GEMINI_API_KEY 環境変数が設定されていません")
39
-
40
- return genai.Client(api_key=api_key)
41
-
42
  def generate_html_from_text(text):
43
  """テキストからHTMLを生成する"""
44
  try:
45
- client = init_gemini_client()
46
- model = "gemini-2.5-pro-exp-03-25" # リクエスト例と同じモデルを使用
 
 
 
 
 
47
 
48
  # システムプロンプト(リクエスト例と同じものを使用)
49
  system_instruction = """# グラフィックレコーディング風インフォグラフィック変換プロンプト V2
@@ -143,26 +138,32 @@ def generate_html_from_text(text):
143
  ## 変換する文章/記事
144
  ーーー<ユーザーが入力(または添付)>ーーー"""
145
 
146
- # 実際のユーザー入力を指定
147
- contents = [
148
- types.Content(
149
- role="user",
150
- parts=[types.Part.from_text(text=text)]
151
- )
152
- ]
153
-
154
  # 生成設定
155
- generate_content_config = types.GenerateContentConfig(
156
- response_mime_type="text/plain",
157
- system_instruction=[types.Part.from_text(text=system_instruction)],
 
 
 
 
 
 
 
 
158
  )
159
 
160
  # HTMLを生成
161
  logger.info(f"Gemini APIにリクエストを送信: テキスト長さ = {len(text)}")
162
- response = client.models.generate_content(
163
- model=model,
164
- contents=contents,
165
- config=generate_content_config,
 
 
 
 
 
 
166
  )
167
 
168
  # レスポンスからHTMLを抽出
 
16
  import os
17
  import logging
18
 
19
+ # 新しいGemini関連のインポート - 最新バージョン修正版
20
  import google.generativeai as genai
 
21
 
22
  # ロギング設定
23
  logging.basicConfig(level=logging.INFO)
 
29
  text: str
30
  extension_percentage: float = 8.0 # Default value same as Gradio slider
31
 
 
 
 
 
 
 
 
 
 
32
  def generate_html_from_text(text):
33
  """テキストからHTMLを生成する"""
34
  try:
35
+ # APIキーの設定
36
+ api_key = os.environ.get("GEMINI_API_KEY")
37
+ if not api_key:
38
+ logger.error("GEMINI_API_KEY 環境変数が設定されていません")
39
+ raise ValueError("GEMINI_API_KEY 環境変数が設定されていません")
40
+
41
+ genai.configure(api_key=api_key)
42
 
43
  # システムプロンプト(リクエスト例と同じものを使用)
44
  system_instruction = """# グラフィックレコーディング風インフォグラフィック変換プロンプト V2
 
138
  ## 変換する文章/記事
139
  ーーー<ユーザーが入力(または添付)>ーーー"""
140
 
 
 
 
 
 
 
 
 
141
  # 生成設定
142
+ generation_config = {
143
+ "temperature": 0.7,
144
+ "top_p": 0.95,
145
+ "top_k": 64,
146
+ "max_output_tokens": 8192,
147
+ }
148
+
149
+ # 利用可能なモデルを使用(gemini-1.5-proが最新の安定バージョン)
150
+ model = genai.GenerativeModel(
151
+ model_name="gemini-1.5-pro",
152
+ generation_config=generation_config
153
  )
154
 
155
  # HTMLを生成
156
  logger.info(f"Gemini APIにリクエストを送信: テキスト長さ = {len(text)}")
157
+
158
+ # システムインストラクションとユーザープロンプトを設定
159
+ response = model.generate_content(
160
+ contents=[
161
+ {
162
+ "role": "user",
163
+ "parts": [text]
164
+ }
165
+ ],
166
+ system_instruction=system_instruction
167
  )
168
 
169
  # レスポンスからHTMLを抽出