Ronaldo1111 commited on
Commit
8176d75
·
verified ·
1 Parent(s): 499f7a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -52
app.py CHANGED
@@ -180,37 +180,36 @@ def chat(user_input, history):
180
 
181
 
182
  import gradio as gr
183
- import gradio as gr
184
- import os
185
- # === 背景图与音乐 URL ===
186
  background_images = [
187
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family.jpg",
188
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family1.jpg",
189
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family2.jpg",
190
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family3.jpg",
191
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family4.jpg",
192
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family5.jpg",
193
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family6.jpg",
194
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family7.jpg",
195
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family8.jpg",
196
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family9.jpg"
197
  ]
 
 
 
 
198
  avatar_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/bean.jpg"
 
199
  music1 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/FNG.mp3"
200
  music2 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/PGY.mp3"
201
- cake_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/birthday.jpg"
202
 
203
- # ==== Gradio 页面 ====
204
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
205
- gr.Markdown("# 🎀Horse And 7 Agent🎀")
206
- gr.Markdown("欢迎来到 **豌豆的家**,相信你也是马+7大家庭中的一员。我能在家里拉粑粑吗!💬")
207
 
208
  chatbot = gr.Chatbot(label="Sophia", type="messages", show_copy_button=True)
209
- msg = gr.Textbox(label="旺~你想说啥子哦~", placeholder="快点跟豌豆开始聊天吧!", lines=2)
210
  state = gr.State([{"role": "assistant", "content": "勾巴,我是豌豆,你想聊啥?"}])
211
  btn = gr.Button("投喂")
212
 
213
- # === 页面美化部分 ===
214
  gr.HTML(f"""
215
  <style>
216
  body {{
@@ -221,84 +220,101 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
221
  transition: background-image 1s ease-in-out;
222
  }}
223
  @keyframes backgroundCycle {{
224
- {''.join([f"{i*10}% {{ background-image: url({img}); }}\n" for i, img in enumerate(background_images)])}
225
  }}
226
-
227
  .gr-chatbot {{
228
- background: rgba(255, 255, 255, 0.5); /* 半透明 */
229
  border-radius: 16px;
230
  padding: 10px;
231
  }}
232
-
 
 
 
 
 
 
 
 
233
  #sophia-avatar {{
234
  position: fixed;
235
- top: 60px;
236
  left: 30px;
237
  width: 80px;
238
  height: 80px;
239
  border-radius: 50%;
240
  z-index: 9999;
241
- cursor: move;
 
 
 
 
 
 
242
  }}
243
-
244
  #birthday-cake {{
245
  position: fixed;
246
- bottom: 10px;
247
- right: 10px;
248
  width: 80px;
249
- animation: bounce 2s infinite ease-in-out alternate;
250
  z-index: 9999;
251
  }}
252
-
253
  @keyframes bounce {{
254
- 0% {{ transform: translateY(0px); }}
255
- 100% {{ transform: translateY(-20px); }}
 
256
  }}
257
  </style>
258
 
 
259
  <img id="sophia-avatar" src="{avatar_url}" />
260
  <img id="birthday-cake" src="{cake_url}" />
261
-
262
- <canvas id="fireworks" style="position:fixed; top:0; left:0; width:100vw; height:100vh; pointer-events:none; z-index:999;"></canvas>
263
-
264
  <audio id="bg-music" autoplay loop>
265
- <source src="{music1}" type="audio/mpeg">
266
- <source src="{music2}" type="audio/mpeg">
267
  </audio>
 
 
268
 
269
  <script>
270
- // 音乐自动切换
271
- const musicList = ["{music1}", "{music2}"];
272
- let currentTrack = 0;
273
  const audio = document.getElementById("bg-music");
274
- audio.src = musicList[currentTrack];
275
  audio.addEventListener("ended", () => {{
276
- currentTrack = (currentTrack + 1) % musicList.length;
277
- audio.src = musicList[currentTrack];
278
  audio.play();
279
  }});
280
 
281
  // 拖动头像
282
  const avatar = document.getElementById("sophia-avatar");
283
  avatar.onmousedown = function(e) {{
284
- const offsetX = e.clientX - avatar.offsetLeft;
285
- const offsetY = e.clientY - avatar.offsetTop;
286
- function move(e) {{
287
- avatar.style.left = (e.clientX - offsetX) + 'px';
288
- avatar.style.top = (e.clientY - offsetY) + 'px';
289
  }}
290
- document.addEventListener('mousemove', move);
291
- document.addEventListener('mouseup', () => {{
292
- document.removeEventListener('mousemove', move);
293
- }}, {{ once: true }});
 
294
  }};
 
295
 
296
- // 简易烟花(伪特效)
 
 
 
 
 
297
  const canvas = document.getElementById("fireworks");
298
  const ctx = canvas.getContext("2d");
299
  canvas.width = window.innerWidth;
300
  canvas.height = window.innerHeight;
301
-
302
  function firework() {{
303
  const x = Math.random() * canvas.width;
304
  const y = Math.random() * canvas.height;
@@ -311,12 +327,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
311
  }}, i * 20);
312
  }}
313
  }}
314
- setInterval(firework, 1500);
315
  </script>
316
  """)
317
- # === 按钮绑定 ===
 
318
  btn.click(chat_with_log, inputs=[msg, state], outputs=[chatbot, state])
319
  msg.submit(chat_with_log, inputs=[msg, state], outputs=[chatbot, state])
320
 
321
  demo.launch()
322
-
 
180
 
181
 
182
  import gradio as gr
183
+
184
+ # ==== 静态资源 ====
 
185
  background_images = [
186
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family.jpg",
187
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family1.jpg",
188
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family2.jpg",
189
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family3.jpg",
190
  "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family4.jpg",
191
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family5.jpg"
 
 
 
 
192
  ]
193
+ background_css = "".join([
194
+ f"{i * 20}% {{ background-image: url({img}); }}\n"
195
+ for i, img in enumerate(background_images)
196
+ ])
197
  avatar_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/bean.jpg"
198
+ cake_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/birthday.jpg"
199
  music1 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/FNG.mp3"
200
  music2 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/PGY.mp3"
201
+ bark_sound = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/voice.mp3"
202
 
203
+ # ==== Gradio 页面构建 ====
204
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
205
+ gr.Markdown("## 🌸 Horse and 7 Agent:欢迎进入豌豆的世界 🌸")
 
206
 
207
  chatbot = gr.Chatbot(label="Sophia", type="messages", show_copy_button=True)
208
+ msg = gr.Textbox(label="想对豌豆说啥?", placeholder="比如:你在干嘛?", lines=2)
209
  state = gr.State([{"role": "assistant", "content": "勾巴,我是豌豆,你想聊啥?"}])
210
  btn = gr.Button("投喂")
211
 
212
+ # 页面美化注入 HTML + JS + CSS
213
  gr.HTML(f"""
214
  <style>
215
  body {{
 
220
  transition: background-image 1s ease-in-out;
221
  }}
222
  @keyframes backgroundCycle {{
223
+ {background_css}
224
  }}
 
225
  .gr-chatbot {{
226
+ background: rgba(255, 255, 255, 0.5);
227
  border-radius: 16px;
228
  padding: 10px;
229
  }}
230
+ .gr-textbox textarea {{
231
+ font-family: monospace;
232
+ font-size: 1.1em;
233
+ animation: typewriter 1s steps(40, end);
234
+ }}
235
+ @keyframes typewriter {{
236
+ from {{ width: 0 }}
237
+ to {{ width: 100% }}
238
+ }}
239
  #sophia-avatar {{
240
  position: fixed;
241
+ top: 40px;
242
  left: 30px;
243
  width: 80px;
244
  height: 80px;
245
  border-radius: 50%;
246
  z-index: 9999;
247
+ cursor: grab;
248
+ animation: spinBounce 4s infinite;
249
+ }}
250
+ @keyframes spinBounce {{
251
+ 0% {{ transform: rotate(0deg) translateY(0px); }}
252
+ 50% {{ transform: rotate(180deg) translateY(-10px); }}
253
+ 100% {{ transform: rotate(360deg) translateY(0px); }}
254
  }}
 
255
  #birthday-cake {{
256
  position: fixed;
257
+ bottom: 20px;
258
+ right: 20px;
259
  width: 80px;
260
+ animation: bounce 1.5s infinite;
261
  z-index: 9999;
262
  }}
 
263
  @keyframes bounce {{
264
+ 0% {{ transform: translateY(0); }}
265
+ 50% {{ transform: translateY(-15px); }}
266
+ 100% {{ transform: translateY(0); }}
267
  }}
268
  </style>
269
 
270
+ <!-- 动态头像 + 生日蛋糕 -->
271
  <img id="sophia-avatar" src="{avatar_url}" />
272
  <img id="birthday-cake" src="{cake_url}" />
 
 
 
273
  <audio id="bg-music" autoplay loop>
274
+ <source src="{music1}" type="audio/mpeg" />
275
+ <source src="{music2}" type="audio/mpeg" />
276
  </audio>
277
+ <audio id="bark" src="{bark_sound}"></audio>
278
+ <canvas id="fireworks" style="position:fixed;top:0;left:0;width:100vw;height:100vh;pointer-events:none;z-index:0;"></canvas>
279
 
280
  <script>
281
+ // 播放背景音乐轮播
282
+ const tracks = ["{music1}", "{music2}"];
 
283
  const audio = document.getElementById("bg-music");
284
+ let current = 0;
285
  audio.addEventListener("ended", () => {{
286
+ current = (current + 1) % tracks.length;
287
+ audio.src = tracks[current];
288
  audio.play();
289
  }});
290
 
291
  // 拖动头像
292
  const avatar = document.getElementById("sophia-avatar");
293
  avatar.onmousedown = function(e) {{
294
+ const shiftX = e.clientX - avatar.getBoundingClientRect().left;
295
+ const shiftY = e.clientY - avatar.getBoundingClientRect().top;
296
+ function moveAt(e) {{
297
+ avatar.style.left = e.pageX - shiftX + 'px';
298
+ avatar.style.top = e.pageY - shiftY + 'px';
299
  }}
300
+ document.addEventListener('mousemove', moveAt);
301
+ avatar.onmouseup = function() {{
302
+ document.removeEventListener('mousemove', moveAt);
303
+ avatar.onmouseup = null;
304
+ }};
305
  }};
306
+ avatar.ondragstart = () => false;
307
 
308
+ // 点击头像播放汪汪叫
309
+ avatar.addEventListener("click", () => {{
310
+ document.getElementById("bark").play();
311
+ }});
312
+
313
+ // 烟花特效
314
  const canvas = document.getElementById("fireworks");
315
  const ctx = canvas.getContext("2d");
316
  canvas.width = window.innerWidth;
317
  canvas.height = window.innerHeight;
 
318
  function firework() {{
319
  const x = Math.random() * canvas.width;
320
  const y = Math.random() * canvas.height;
 
327
  }}, i * 20);
328
  }}
329
  }}
330
+ setInterval(firework, 1800);
331
  </script>
332
  """)
333
+
334
+ # 绑定交互
335
  btn.click(chat_with_log, inputs=[msg, state], outputs=[chatbot, state])
336
  msg.submit(chat_with_log, inputs=[msg, state], outputs=[chatbot, state])
337
 
338
  demo.launch()