Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -166,13 +166,10 @@ def chat(user_input, history):
|
|
166 |
|
167 |
import gradio as gr
|
168 |
|
169 |
-
background_images = [
|
170 |
-
|
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 |
-
|
|
|
184 |
html_template = '''
|
185 |
<style>
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
<
|
209 |
-
|
210 |
-
<
|
211 |
-
|
212 |
-
|
213 |
-
<
|
|
|
|
|
214 |
<audio id="bark" src="{bark_sound}"></audio>
|
215 |
-
|
216 |
<script>
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
avatar
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
}
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
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 |
-
|
296 |
-
html_content =
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
|
|
305 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
306 |
gr.HTML(html_content)
|
307 |
-
|
308 |
-
gr.
|
309 |
-
|
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])
|