sirochild commited on
Commit
e0e8193
·
verified ·
1 Parent(s): 5cfb951

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +38 -23
  2. style.css +11 -34
app.py CHANGED
@@ -26,12 +26,23 @@ except Exception as e:
26
  sentiment_analyzer = None
27
  print(f"モデルのロードエラー: {e}")
28
 
 
 
 
 
 
 
 
 
 
 
 
29
  DEFAULT_SCENE_PARAMS = {
30
  "theme": "default", "personality_mod": "ストレートだが根は優しい",
31
  "tone": "普通のトーン", "constraints": ["キャラクター設定から逸脱しない"]
32
  }
33
 
34
- # --- 2. LLMによる役割分担システム ---
35
  def detect_scene_change(history, message):
36
  history_text = "\n".join([f"ユーザー: {u}\n麻理: {m}" for u, m in history[-4:]])
37
  prompt = f"""
@@ -81,7 +92,6 @@ def generate_scene_instruction_with_groq(affection, stage_name, scene, previous_
81
  model="llama3-8b-8192", temperature=0.8, response_format={"type": "json_object"},
82
  )
83
  params = json.loads(chat_completion.choices[0].message.content)
84
- print("生成された指示書JSON:", params)
85
  return params
86
  except Exception as e:
87
  print(f"指示書生成エラー(Groq): {e}")
@@ -128,6 +138,8 @@ def update_affection(message, affection):
128
  except Exception: return affection
129
  return affection
130
 
 
 
131
  def respond(message, chat_history, affection, history, scene_params):
132
  new_affection = update_affection(message, affection)
133
  stage_name = get_relationship_stage(new_affection)
@@ -148,43 +160,46 @@ def respond(message, chat_history, affection, history, scene_params):
148
  chat_history.append((message, bot_message))
149
  theme_name = final_scene_params.get("theme", "default")
150
 
151
- # JavaScriptを、#main_containerのクラスを書き換えるように変更
152
- js_script = f"""
153
- <script>
154
- setTimeout(() => {{
155
- const container = document.getElementById('main_container');
156
- if(container){{
157
- console.log("Applying theme to #main_container:", "{theme_name}");
158
- const themes = ["theme-default", "theme-room_night", "theme-beach_sunset", "theme-festival_night", "theme-shrine_day", "theme-cafe_afternoon", "theme-aquarium_night"];
159
- container.classList.remove(...themes);
160
- container.classList.add("theme-{theme_name}");
161
- }}
162
- }}, 50);
163
- </script>
 
 
 
 
164
  """
165
 
166
- return "", chat_history, new_affection, stage_name, new_affection, new_history, final_scene_params, js_script
167
 
168
- # ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
169
- # ★★★★★★★★★★★★★ 修正の中心部分 ★★★★★★★★★★★★★★
170
- # ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
171
- with gr.Blocks(css="style.css", theme=None, elem_id="main_container") as demo:
172
  affection_state = gr.State(30)
173
  history_state = gr.State([])
174
  scene_state = gr.State(DEFAULT_SCENE_PARAMS)
175
  gr.Markdown("# 麻理チャット")
176
  with gr.Row():
177
  with gr.Column(scale=2):
178
- chatbot = gr.Chatbot(label="麻理との会話", height=500, bubble_full_width=False)
179
  msg_input = gr.Textbox(label="あなたのメッセージ", placeholder="「水族館はどう?」と聞いた後、「いいね、行こう!」のように返してみてください", scale=5)
180
  with gr.Column(scale=1):
181
  stage_display = gr.Textbox(label="現在の関係ステージ", interactive=False)
182
  affection_gauge = gr.Slider(minimum=0, maximum=100, label="麻理の好感度", interactive=False)
183
- js_runner = gr.HTML(visible=False)
 
 
184
  msg_input.submit(
185
  respond,
186
  [msg_input, chatbot, affection_state, history_state, scene_state],
187
- [msg_input, chatbot, affection_gauge, stage_display, affection_state, history_state, scene_state, js_runner]
188
  )
189
 
190
  if __name__ == "__main__":
 
26
  sentiment_analyzer = None
27
  print(f"モデルのロードエラー: {e}")
28
 
29
+ # テーマ名と背景画像のURLをマッピング
30
+ THEME_URLS = {
31
+ "default": "https://i.ibb.co/XzP6K2Y/room-day.png",
32
+ "room_night": "https://i.ibb.co/d5m821p/room-night.png",
33
+ "beach_sunset": "https://i.ibb.co/Q9r56s4/beach-sunset.png",
34
+ "festival_night": "https://i.ibb.co/3zdJ6Bw/festival-night.png",
35
+ "shrine_day": "https://i.ibb.co/L51Jd3x/shrine-day.png",
36
+ "cafe_afternoon": "https://i.ibb.co/yQxG4vs/cafe-afternoon.png",
37
+ "aquarium_night": "https://i.ibb.co/dK5r5rc/aquarium-night.png"
38
+ }
39
+
40
  DEFAULT_SCENE_PARAMS = {
41
  "theme": "default", "personality_mod": "ストレートだが根は優しい",
42
  "tone": "普通のトーン", "constraints": ["キャラクター設定から逸脱しない"]
43
  }
44
 
45
+ # --- 2. LLMによる役割分担システム (変更なし) ---
46
  def detect_scene_change(history, message):
47
  history_text = "\n".join([f"ユーザー: {u}\n麻理: {m}" for u, m in history[-4:]])
48
  prompt = f"""
 
92
  model="llama3-8b-8192", temperature=0.8, response_format={"type": "json_object"},
93
  )
94
  params = json.loads(chat_completion.choices[0].message.content)
 
95
  return params
96
  except Exception as e:
97
  print(f"指示書生成エラー(Groq): {e}")
 
138
  except Exception: return affection
139
  return affection
140
 
141
+ # --- 3. メインの応答処理とUI構築 ---
142
+
143
  def respond(message, chat_history, affection, history, scene_params):
144
  new_affection = update_affection(message, affection)
145
  stage_name = get_relationship_stage(new_affection)
 
160
  chat_history.append((message, bot_message))
161
  theme_name = final_scene_params.get("theme", "default")
162
 
163
+ # ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
164
+ # ★★★★★★★★★★★★★ 修正の中心部分 ★★★★★★★★★★★★★★
165
+ # ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
166
+ # Gradio 5.xに対応した、CSS変数を直接上書きする<style>タグを生成
167
+ background_url = THEME_URLS.get(theme_name, THEME_URLS["default"])
168
+ style_override = f"""
169
+ <style>
170
+ :root {{
171
+ --body-background-fill: url('{background_url}') !important;
172
+ --body-background-fill-dark: url('{background_url}') !important;
173
+ --body-background-fill-transition: background-image 1s ease-in-out !important;
174
+ }}
175
+ body {{
176
+ background-size: cover !important;
177
+ background-position: center !important;
178
+ }}
179
+ </style>
180
  """
181
 
182
+ return "", chat_history, new_affection, stage_name, new_affection, new_history, final_scene_params, style_override
183
 
184
+ with gr.Blocks(css="style.css", theme=gr.themes.Soft(primary_hue="rose", secondary_hue="pink")) as demo:
 
 
 
185
  affection_state = gr.State(30)
186
  history_state = gr.State([])
187
  scene_state = gr.State(DEFAULT_SCENE_PARAMS)
188
  gr.Markdown("# 麻理チャット")
189
  with gr.Row():
190
  with gr.Column(scale=2):
191
+ chatbot = gr.Chatbot(label="麻理との会話", height=600, bubble_full_width=False)
192
  msg_input = gr.Textbox(label="あなたのメッセージ", placeholder="「水族館はどう?」と聞いた後、「いいね、行こう!」のように返してみてください", scale=5)
193
  with gr.Column(scale=1):
194
  stage_display = gr.Textbox(label="現在の関係ステージ", interactive=False)
195
  affection_gauge = gr.Slider(minimum=0, maximum=100, label="麻理の好感度", interactive=False)
196
+ # CSSを注入するための非表示のHTMLコンポーネント
197
+ css_injector = gr.HTML(visible=False)
198
+
199
  msg_input.submit(
200
  respond,
201
  [msg_input, chatbot, affection_state, history_state, scene_state],
202
+ [msg_input, chatbot, affection_gauge, stage_display, affection_state, history_state, scene_state, css_injector]
203
  )
204
 
205
  if __name__ == "__main__":
style.css CHANGED
@@ -1,38 +1,15 @@
1
- /* Gradioのルート要素(#main_container)を直接ターゲットにする */
2
- #main_container {
3
- /* テーマが適用されていない時のデフォルト背景 */
4
- background-image: url('https://i.ibb.co/XzP6K2Y/room-day.png') !important;
5
- background-size: cover !important;
6
- background-position: center !important;
7
- transition: background-image 1s ease-in-out;
8
  }
9
-
10
- /* 各テーマの定義 */
11
- #main_container.theme-default {
12
- background-image: url('https://i.ibb.co/XzP6K2Y/room-day.png') !important;
13
  }
14
- #main_container.theme-room_night {
15
- background-image: url('https://i.ibb.co/d5m821p/room-night.png') !important;
16
- }
17
- #main_container.theme-beach_sunset {
18
- background-image: url('https://i.ibb.co/Q9r56s4/beach-sunset.png') !important;
19
- }
20
- #main_container.theme-festival_night {
21
- background-image: url('https://i.ibb.co/3zdJ6Bw/festival-night.png') !important;
22
- }
23
- #main_container.theme-shrine_day {
24
- background-image: url('https://i.ibb.co/L51Jd3x/shrine-day.png') !important;
25
- }
26
- #main_container.theme-cafe_afternoon {
27
- background-image: url('https://i.ibb.co/yQxG4vs/cafe-afternoon.png') !important;
28
- }
29
- #main_container.theme-aquarium_night {
30
- background-image: url('https://i.ibb.co/dK5r5rc/aquarium-night.png') !important;
31
- }
32
-
33
- /* チャットウィンドウなどのスタイル */
34
- .gradio-chatbot .chatbot, .gradio-textbox, .gradio-slider, .gradio-markdown {
35
  background-color: rgba(255, 255, 255, 0.8) !important;
36
- backdrop-filter: blur(5px);
37
- border: 1px solid rgba(255, 255, 255, 0.2);
38
  }
 
1
+ /* チャットウィンドウや入力ボックスのスタイルのみを定義 */
2
+ .gradio-chatbot .chatbot {
3
+ background-color: rgba(255, 255, 255, 0.75) !important;
4
+ backdrop-filter: blur(8px);
5
+ border-radius: 12px;
 
 
6
  }
7
+ .gradio-textbox, .gradio-slider {
8
+ background-color: rgba(255, 255, 255, 0.85) !important;
9
+ border-radius: 10px !important;
 
10
  }
11
+ .gradio-markdown {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  background-color: rgba(255, 255, 255, 0.8) !important;
13
+ border-radius: 10px !important;
14
+ padding: 1rem !important;
15
  }