Ronaldo1111 commited on
Commit
925c6c7
·
verified ·
1 Parent(s): 5faebe1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +210 -130
app.py CHANGED
@@ -166,13 +166,10 @@ def chat(user_input, history):
166
 
167
  import gradio as gr
168
 
169
- background_images = [
170
- f"https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family{i}.jpg" for i in range(10)
171
- ]
172
- background_css_rules = "".join([
173
- f" {i * 10}% {{ background-image: url('{img}'); }}\n" for i, img in enumerate(background_images)
174
- ])
175
  background_css = f"@keyframes backgroundCycle {{\n{background_css_rules}}}"
 
176
  avatar_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/bean.jpg"
177
  cake_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/birthday.jpg"
178
  music1 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/FNG.mp3"
@@ -180,145 +177,228 @@ music2 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/PGY.mp3"
180
  bark_sound = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/voice.mp3"
181
  gift_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/gift.jpg"
182
  popup_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/srkl.jpg"
183
- balloon_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/ballon.jpg"
 
184
  html_template = '''
185
  <style>
186
- body {
187
- margin: 0;
188
- animation: backgroundCycle 60s infinite;
189
- background-size: cover;
190
- background-position: center;
191
- transition: background-image 1s ease-in-out;
192
- }
193
- {background_css}
194
-
195
- /* 半透明聊天气泡 */
196
- .gradio-container .chatbot .message .bubble {
197
- background-color: rgba(255,255,255,0.5) !important;
198
- backdrop-filter: blur(8px);
199
- }
200
- /* 隐藏默认的背景画布 */
201
- canvas#fireworks_old { display: none !important; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  </style>
203
-
204
- <!-- 控件元素 -->
205
  <img id="sophia-avatar" src="{avatar_url}" />
206
  <img id="birthday-cake" src="{cake_url}" />
207
  <img id="gift" src="{gift_url}" />
208
- <div id="popup-close" style="display:none;">×</div>
209
-
210
- <button id="music-control" style="position:fixed;bottom:20px;left:20px;width:50px;border:none;background:url('music.jpg')center/contain no-repeat;cursor:pointer;z-index:9999;opacity:1;">
211
- </button>
212
-
213
- <audio id="bg-music" loop></audio>
 
 
214
  <audio id="bark" src="{bark_sound}"></audio>
215
-
216
  <script>
217
- // 背景音乐 手动触发 (移除 autoplay)
218
- const audio = document.getElementById('bg-music');
219
- const tracks = ['{music1}','{music2}'];
220
- document.getElementById('music-control').addEventListener('click',()=>{
221
- if(audio.paused){
222
- audio.src = tracks[Math.floor(Math.random()*tracks.length)];
223
- audio.play();
224
- document.getElementById('music-control').style.opacity=0.6;
225
- } else {
226
- audio.pause();
227
- document.getElementById('music-control').style.opacity=1;
228
- }
229
- });
230
-
231
- // 头像拖拽与 bark 播放
232
- const avatar = document.getElementById('sophia-avatar');
233
- const bark = document.getElementById('bark');
234
- avatar.onmousedown = e=>{
235
- const dx=e.clientX-avatar.getBoundingClientRect().left;
236
- const dy=e.clientY-avatar.getBoundingClientRect().top;
237
- const move=e=>{ avatar.style.left=e.pageX-dx+'px'; avatar.style.top=e.pageY-dy+'px'; };
238
- document.addEventListener('mousemove',move);
239
- avatar.onmouseup=()=>document.removeEventListener('mousemove',move);
240
- };
241
- avatar.ondragstart=()=>false;
242
- avatar.onclick=()=>{ bark.pause(); bark.currentTime=0; bark.play(); };
243
-
244
- // 蛋糕弹跳已留存,无需修改
245
-
246
- // 礼物随机移动 & 点击切换弹窗
247
- const gift = document.getElementById('gift');
248
- let showPopup=false;
249
- setInterval(()=>{
250
- const x=Math.random()*(window.innerWidth-gift.offsetWidth);
251
- const y=Math.random()*(window.innerHeight-gift.offsetHeight);
252
- gift.style.left=x+'px'; gift.style.top=y+'px';
253
- },2000);
254
- gift.onclick=()=>{
255
- showPopup=!showPopup;
256
- const pop=document.getElementById('popup');
257
- document.getElementById('popup-close').style.display=showPopup?'block':'none';
258
- if(showPopup){
259
- pop.style.display='block';
260
- pop.src='{popup_url}';
261
- pop.style.position='fixed'; pop.style.top='50%'; pop.style.left='50%';
262
- pop.style.transform='translate(-50%,-50%)';
263
- pop.style.maxWidth='80vw'; pop.style.maxHeight='80vh';
264
- pop.style.border='4px solid white'; pop.style.borderRadius='12px';
265
- pop.style.boxShadow='0 0 20px rgba(0,0,0,0.5)'; pop.style.zIndex='10000';
266
- } else {
267
- pop.style.display='none';
268
  }
269
- };
270
-
271
- // 点击关闭弹窗也可二次点击礼物或点击 close
272
- document.getElementById('popup-close').onclick=()=>gift.onclick();
273
-
274
- // 漂浮气球
275
- for(let i=0;i<5;i++){
276
- const b=new Image(); b.src='{balloon_url}'; b.className='balloon';
277
- b.style.position='fixed'; b.style.width='40px'; b.style.bottom='-50px';
278
- b.style.left=Math.random()*90+'vw'; b.style.opacity=0.8;
279
- b.style.animation=`floatUp ${(5+Math.random()*5)}s linear infinite`;
280
- document.body.appendChild(b);
281
- }
282
- const style=document.createElement('style'); style.textContent="@keyframes floatUp{0%{transform:translateY(0)}100%{transform:translateY(-120vh)}}";
283
- document.head.appendChild(style);
284
-
285
- // 点击触发 HSL 烟花动画
286
- const canvas=document.createElement('canvas');
287
- canvas.id='fireworks'; canvas.style='position:fixed;top:0;left:0;pointer-events:none;z-index:0;';
288
- document.body.appendChild(canvas);
289
- const ctx=canvas.getContext('2d'); function resize(){canvas.width=window.innerWidth; canvas.height=window.innerHeight;} resize(); window.addEventListener('resize',resize);
290
- function firework(x,y){ for(let i=0;i<50;i++){ const a=Math.random()*2*Math.PI, s=2+Math.random()*3, dx=Math.cos(a)*s, dy=Math.sin(a)*s, h=Math.random()*360; let t=0; const id=setInterval(()=>{ if(t>15){clearInterval(id);return;} ctx.beginPath(); ctx.arc(x+dx*t,y+dy*t,2,0,2*Math.PI); ctx.fillStyle=`hsl(${h},100%,50%)`; ctx.fill(); t++; },30);} }
291
- document.body.addEventListener('click',e=>firework(e.clientX,e.clientY));
292
  </script>
293
  '''
294
 
295
- html_content = html_template.replace('{background_css}',background_css)
296
- html_content = html_content.replace('{avatar_url}',avatar_url)
297
- html_content = html_content.replace('{cake_url}',cake_url)
298
- html_content = html_content.replace('{music1}',music1)
299
- html_content = html_content.replace('{music2}',music2)
300
- html_content = html_content.replace('{bark_sound}',bark_sound)
301
- html_content = html_content.replace('{gift_url}',gift_url)
302
- html_content = html_content.replace('{popup_url}',popup_url)
303
- html_content = html_content.replace('{balloon_url}',balloon_url)
304
-
 
305
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
306
  gr.HTML(html_content)
307
-
308
- gr.Markdown("### 🎂 🐎Gallery")
309
- gallery = gr.Gallery(
310
- value=background_images,
311
- label="🐎",
312
- show_label=False,
313
- columns=5,
314
- height=200
315
- )
316
-
317
- gr.Markdown("## 🌸 Horse and 7 Agent:欢迎进入豌豆的世界 ")
318
- chatbot = gr.Chatbot(label="Pea", type="messages", show_copy_button=True)
319
- msg = gr.Textbox(label="想对豌豆说啥?", placeholder="小勾巴,你想说什么?", lines=2)
320
  state = gr.State([])
321
-
322
  btn = gr.Button("投喂")
323
  btn.click(chat, inputs=[msg, state], outputs=[chatbot, state])
324
  msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])
 
166
 
167
  import gradio as gr
168
 
169
+ background_images = [f"https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family{i}.jpg" for i in ["",1,2,3,4,5,6,7,8,9]]
170
+ background_css_rules = "".join([f" {i * 10}% {{ background-image: url('{img}'); }}\n" for i, img in enumerate(background_images)])
 
 
 
 
171
  background_css = f"@keyframes backgroundCycle {{\n{background_css_rules}}}"
172
+
173
  avatar_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/bean.jpg"
174
  cake_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/birthday.jpg"
175
  music1 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/FNG.mp3"
 
177
  bark_sound = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/voice.mp3"
178
  gift_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/gift.jpg"
179
  popup_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/srkl.jpg"
180
+
181
+ # ==== HTML / CSS / JS ====
182
  html_template = '''
183
  <style>
184
+ body {
185
+ margin: 0;
186
+ animation: backgroundCycle 60s infinite;
187
+ background-size: cover;
188
+ background-position: center;
189
+ transition: background-image 1s ease-in-out;
190
+ }
191
+ {background_css}
192
+ .gr-chatbot {
193
+ background: rgba(255, 255, 255, 0.5) !important;
194
+ border-radius: 16px;
195
+ padding: 10px;
196
+ }
197
+ .gr-textbox textarea {
198
+ font-family: monospace;
199
+ font-size: 1.1em;
200
+ animation: typewriter 1s steps(40, end);
201
+ }
202
+ @keyframes typewriter {
203
+ from { width: 0 }
204
+ to { width: 100% }
205
+ }
206
+ #sophia-avatar {
207
+ position: fixed;
208
+ top: 40px;
209
+ left: 30px;
210
+ width: 80px;
211
+ height: 80px;
212
+ border-radius: 50%;
213
+ z-index: 9999;
214
+ cursor: grab;
215
+ animation: spinBounce 4s infinite;
216
+ }
217
+ @keyframes spinBounce {
218
+ 0% { transform: rotate(0deg) translateY(0); }
219
+ 50% { transform: rotate(180deg) translateY(-10px); }
220
+ 100% { transform: rotate(360deg) translateY(0); }
221
+ }
222
+ #birthday-cake {
223
+ position: fixed;
224
+ bottom: 20px;
225
+ right: 20px;
226
+ width: 80px;
227
+ animation: bounce 1.5s infinite;
228
+ z-index: 9999;
229
+ }
230
+ @keyframes bounce {
231
+ 0% { transform: translateY(0); }
232
+ 50% { transform: translateY(-15px); }
233
+ 100% { transform: translateY(0); }
234
+ }
235
+ #gift {
236
+ position: fixed;
237
+ width: 60px;
238
+ cursor: pointer;
239
+ z-index: 9998;
240
+ transition: transform 0.5s;
241
+ }
242
+ #popup {
243
+ display: none;
244
+ position: fixed;
245
+ top: 50%; left: 50%;
246
+ transform: translate(-50%, -50%);
247
+ max-width: 80vw;
248
+ max-height: 80vh;
249
+ z-index: 10000;
250
+ border: 4px solid #fff;
251
+ border-radius: 12px;
252
+ box-shadow: 0 0 20px rgba(0,0,0,0.5);
253
+ }
254
+ #popup-close {
255
+ position: absolute;
256
+ top: 8px; right: 12px;
257
+ font-size: 24px;
258
+ color: #fff;
259
+ cursor: pointer;
260
+ z-index: 10001;
261
+ }
262
+ #firework {
263
+ position: fixed;
264
+ top: 50%;
265
+ left: 50%;
266
+ width: 120px;
267
+ height: 120px;
268
+ background: url("https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/firework.gif") no-repeat center center;
269
+ background-size: contain;
270
+ z-index: 99999;
271
+ animation: fadeOut 1s ease-out forwards;
272
+ }
273
+ @keyframes fadeOut {
274
+ 0% { opacity: 1; }
275
+ 100% { opacity: 0; }
276
+ }
277
+ #balloon {
278
+ position: fixed;
279
+ top: 100%;
280
+ left: 50%;
281
+ width: 50px;
282
+ height: 80px;
283
+ background: url("https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/balloon.png") no-repeat center;
284
+ background-size: contain;
285
+ animation: floatBalloon 10s linear infinite;
286
+ z-index: 9999;
287
+ }
288
+ @keyframes floatBalloon {
289
+ 0% { transform: translate(-50%, 0); }
290
+ 100% { transform: translate(-50%, -120vh); }
291
+ }
292
+ #music-toggle {
293
+ position: fixed;
294
+ bottom: 20px;
295
+ left: 20px;
296
+ padding: 8px 12px;
297
+ font-size: 14px;
298
+ background: rgba(255,255,255,0.7);
299
+ border-radius: 8px;
300
+ cursor: pointer;
301
+ z-index: 10000;
302
+ }
303
  </style>
 
 
304
  <img id="sophia-avatar" src="{avatar_url}" />
305
  <img id="birthday-cake" src="{cake_url}" />
306
  <img id="gift" src="{gift_url}" />
307
+ <img id="popup" src="{popup_url}" />
308
+ <div id="popup-close">×</div>
309
+ <div id="music-toggle">⏸️音乐</div>
310
+ <div id="balloon"></div>
311
+ <audio id="bg-music" autoplay loop>
312
+ <source src="{music1}" type="audio/mpeg" />
313
+ <source src="{music2}" type="audio/mpeg" />
314
+ </audio>
315
  <audio id="bark" src="{bark_sound}"></audio>
 
316
  <script>
317
+ // 背景音乐轮播
318
+ const tracks = ["{music1}", "{music2}"];
319
+ const audio = document.getElementById("bg-music");
320
+ let current = 0;
321
+ audio.addEventListener("ended", () => {
322
+ current = (current + 1) % tracks.length;
323
+ audio.src = tracks[current]; audio.load(); audio.play();
324
+ });
325
+
326
+ // 音乐开关按钮
327
+ const toggleBtn = document.getElementById("music-toggle");
328
+ toggleBtn.addEventListener("click", () => {
329
+ if (audio.paused) {
330
+ audio.play();
331
+ toggleBtn.textContent = "⏸️音乐";
332
+ } else {
333
+ audio.pause();
334
+ toggleBtn.textContent = "▶️音乐";
335
+ }
336
+ });
337
+
338
+ // 头像拖拽 & Bark 音效
339
+ const avatar = document.getElementById("sophia-avatar");
340
+ const bark = document.getElementById("bark");
341
+ avatar.onmousedown = function(e) {
342
+ const shiftX = e.clientX - avatar.getBoundingClientRect().left;
343
+ const shiftY = e.clientY - avatar.getBoundingClientRect().top;
344
+ function moveAt(e) {
345
+ avatar.style.left = e.pageX - shiftX + 'px';
346
+ avatar.style.top = e.pageY - shiftY + 'px';
347
+ }
348
+ document.addEventListener('mousemove', moveAt);
349
+ avatar.onmouseup = () => { document.removeEventListener('mousemove', moveAt); avatar.onmouseup = null; };
350
+ };
351
+ avatar.ondragstart = () => false;
352
+ avatar.addEventListener("click", () => {
353
+ bark.pause(); bark.currentTime = 0; bark.play();
354
+ const fw = document.createElement("div");
355
+ fw.id = "firework";
356
+ document.body.appendChild(fw);
357
+ setTimeout(() => fw.remove(), 1200);
358
+ });
359
+
360
+ // 礼物弹窗 + 自动消失
361
+ const gift = document.getElementById("gift");
362
+ const popup = document.getElementById("popup");
363
+ const closeBtn = document.getElementById("popup-close");
364
+ function moveGift() {
365
+ const x = Math.random() * (window.innerWidth - gift.offsetWidth);
366
+ const y = Math.random() * (window.innerHeight - gift.offsetHeight);
367
+ gift.style.transform = `translate(${x}px, ${y}px)`;
368
  }
369
+ setInterval(moveGift, 2000);
370
+ gift.addEventListener("click", () => {
371
+ popup.style.display = 'block';
372
+ closeBtn.style.display = 'block';
373
+ setTimeout(() => {
374
+ popup.style.display = 'none';
375
+ closeBtn.style.display = 'none';
376
+ }, 2000);
377
+ });
378
+ closeBtn.addEventListener("click", () => {
379
+ popup.style.display = 'none';
380
+ closeBtn.style.display = 'none';
381
+ });
 
 
 
 
 
 
 
 
 
 
382
  </script>
383
  '''
384
 
385
+ # 插值填充
386
+ html_content = html_template.replace("{background_css}", background_css) \
387
+ .replace("{avatar_url}", avatar_url) \
388
+ .replace("{cake_url}", cake_url) \
389
+ .replace("{music1}", music1) \
390
+ .replace("{music2}", music2) \
391
+ .replace("{bark_sound}", bark_sound) \
392
+ .replace("{gift_url}", gift_url) \
393
+ .replace("{popup_url}", popup_url)
394
+
395
+ # ==== Gradio UI ====
396
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
397
  gr.HTML(html_content)
398
+ gr.Markdown("## 🌸 Horse and 7 Agent:欢迎进入豌豆的世界 🌸")
399
+ chatbot = gr.Chatbot(label="Sophia", type="messages", show_copy_button=True)
400
+ msg = gr.Textbox(label="想对豌豆说啥?", placeholder="小勾巴,你在干嘛?", lines=2)
 
 
 
 
 
 
 
 
 
 
401
  state = gr.State([])
 
402
  btn = gr.Button("投喂")
403
  btn.click(chat, inputs=[msg, state], outputs=[chatbot, state])
404
  msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])