sirochild commited on
Commit
3476a60
·
verified ·
1 Parent(s): 6bc5c1c

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -15,7 +15,7 @@ if not GEMINI_API_KEY or not GROQ_API_KEY:
15
  raise ValueError("GEMINI_API_KEY と GROQ_API_KEY をSecretsに設定してください。")
16
 
17
  genai.configure(api_key=GEMINI_API_KEY)
18
- gemini_model = genai.GenerativeModel('gemini-2.5-flash-lite')
19
  groq_client = Groq(api_key=GROQ_API_KEY)
20
 
21
  print("日本語感情分析モデルをロード中...")
@@ -123,12 +123,8 @@ def generate_dialogue_with_gemini(history, message, affection, stage_name, scene
123
  """
124
  print(f"Geminiに応答生成をリクエストします (モード: {'シーン遷移' if instruction else '通常会話'})")
125
  try:
126
- # ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
127
- # ★★★★★★★★★★★★★ 修正の中心部分 ① ★★★★★★★★★★★★★★
128
- # ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
129
- # 応答が長くなりすぎないように、トークン数の上限を設定
130
  generation_config = genai.types.GenerationConfig(
131
- max_output_tokens=200, # セリフとして適切な長さに制限
132
  temperature=0.95
133
  )
134
  response = gemini_model.generate_content(
@@ -152,7 +148,8 @@ def update_affection(message, affection):
152
  result = sentiment_analyzer(message)[0]
153
  if result['label'] == 'positive': return min(100, affection + 5)
154
  if result['label'] == 'negative': return max(0, affection - 5)
155
- except Exception: return affection
 
156
  return affection
157
 
158
  # --- 3. メインの応答処理とUI構築 ---
@@ -178,29 +175,31 @@ def respond(message, chat_history, affection, history, scene_params):
178
  new_history = history + [(message, bot_message)]
179
  chat_history.append((message, bot_message))
180
  theme_name = final_scene_params.get("theme", "default")
181
-
182
  background_url = THEME_URLS.get(theme_name, THEME_URLS["default"])
183
- style_override = f"""
184
- <style>
185
- :root {{
186
- --body-background-fill: url('{background_url}') !important;
187
- --body-background-fill-dark: url('{background_url}') !important;
188
- --body-background-fill-transition: background-image 1s ease-in-out !important;
189
- }}
190
- body {{
191
- background-size: cover !important;
192
- background-position: center !important;
 
 
193
  }}
194
- </style>
 
195
  """
196
-
197
- return "", chat_history, new_affection, stage_name, new_affection, new_history, final_scene_params, style_override
198
 
199
  with gr.Blocks(css="style.css", theme=gr.themes.Soft(primary_hue="rose", secondary_hue="pink")) as demo:
200
  scene_state = gr.State(DEFAULT_SCENE_PARAMS)
201
- affection_state = gr.State(30) # 内部状態の初期値
202
  history_state = gr.State([])
203
-
204
  gr.Markdown("# 麻理チャット")
205
  with gr.Row():
206
  with gr.Column(scale=2):
@@ -208,18 +207,14 @@ with gr.Blocks(css="style.css", theme=gr.themes.Soft(primary_hue="rose", seconda
208
  msg_input = gr.Textbox(label="あなたのメッセージ", placeholder="「水族館はどう?」と聞いた後、「いいね、行こう!」のように返してみてください", scale=5)
209
  with gr.Column(scale=1):
210
  stage_display = gr.Textbox(label="現在の関係ステージ", interactive=False)
211
- # ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
212
- # ★★★★★★★★★★★★★ 修正の中心部分 ② ★★★★★★★★★★★★★★
213
- # ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
214
- # UIコンポーネントの初期値を内部状態と一致させる
215
  affection_gauge = gr.Slider(minimum=0, maximum=100, label="麻理の好感度", value=30, interactive=False)
216
- css_injector = gr.HTML(visible=False)
217
 
218
  msg_input.submit(
219
- respond,
220
- [msg_input, chatbot, affection_state, history_state, scene_state],
221
- [msg_input, chatbot, affection_gauge, stage_display, affection_state, history_state, scene_state, css_injector]
222
  )
223
 
224
  if __name__ == "__main__":
225
- demo.launch()
 
15
  raise ValueError("GEMINI_API_KEY と GROQ_API_KEY をSecretsに設定してください。")
16
 
17
  genai.configure(api_key=GEMINI_API_KEY)
18
+ gemini_model = genai.GenerativeModel('gemini-1.5-flash-latest')
19
  groq_client = Groq(api_key=GROQ_API_KEY)
20
 
21
  print("日本語感情分析モデルをロード中...")
 
123
  """
124
  print(f"Geminiに応答生成をリクエストします (モード: {'シーン遷移' if instruction else '通常会話'})")
125
  try:
 
 
 
 
126
  generation_config = genai.types.GenerationConfig(
127
+ max_output_tokens=200,
128
  temperature=0.95
129
  )
130
  response = gemini_model.generate_content(
 
148
  result = sentiment_analyzer(message)[0]
149
  if result['label'] == 'positive': return min(100, affection + 5)
150
  if result['label'] == 'negative': return max(0, affection - 5)
151
+ except Exception:
152
+ return affection
153
  return affection
154
 
155
  # --- 3. メインの応答処理とUI構築 ---
 
175
  new_history = history + [(message, bot_message)]
176
  chat_history.append((message, bot_message))
177
  theme_name = final_scene_params.get("theme", "default")
 
178
  background_url = THEME_URLS.get(theme_name, THEME_URLS["default"])
179
+
180
+ # JSで背景を動的に差し替えるスクリプト
181
+ js_script = f"""
182
+ <script>
183
+ setTimeout(() => {{
184
+ const root = document.querySelector('.gradio-container');
185
+ if (root) {{
186
+ console.log("背景画像を変更: {background_url}");
187
+ root.style.backgroundImage = "url('{background_url}')";
188
+ root.style.backgroundSize = "cover";
189
+ root.style.backgroundPosition = "center";
190
+ root.style.transition = "background-image 1s ease-in-out";
191
  }}
192
+ }}, 100);
193
+ </script>
194
  """
195
+
196
+ return "", chat_history, new_affection, stage_name, new_affection, new_history, final_scene_params, js_script
197
 
198
  with gr.Blocks(css="style.css", theme=gr.themes.Soft(primary_hue="rose", secondary_hue="pink")) as demo:
199
  scene_state = gr.State(DEFAULT_SCENE_PARAMS)
200
+ affection_state = gr.State(30)
201
  history_state = gr.State([])
202
+
203
  gr.Markdown("# 麻理チャット")
204
  with gr.Row():
205
  with gr.Column(scale=2):
 
207
  msg_input = gr.Textbox(label="あなたのメッセージ", placeholder="「水族館はどう?」と聞いた後、「いいね、行こう!」のように返してみてください", scale=5)
208
  with gr.Column(scale=1):
209
  stage_display = gr.Textbox(label="現在の関係ステージ", interactive=False)
 
 
 
 
210
  affection_gauge = gr.Slider(minimum=0, maximum=100, label="麻理の好感度", value=30, interactive=False)
211
+ js_injector = gr.HTML(elem_classes="hidden")
212
 
213
  msg_input.submit(
214
+ respond,
215
+ [msg_input, chatbot, affection_state, history_state, scene_state],
216
+ [msg_input, chatbot, affection_gauge, stage_display, affection_state, history_state, scene_state, js_injector]
217
  )
218
 
219
  if __name__ == "__main__":
220
+ demo.launch()