Ronaldo1111 commited on
Commit
ab4027a
·
verified ·
1 Parent(s): f43dcc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -92
app.py CHANGED
@@ -152,64 +152,41 @@ def chat(user_input, history):
152
 
153
  import gradio as gr
154
 
155
- # 音频文件 URL
156
- audio_url_1 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/FNG.mp3"
157
- audio_url_2 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/PGY.mp3"
158
-
159
- # HTML 和 JavaScript 用于音频循环播放
160
- html_audio_player = f"""
161
- <audio id="audio-player" autoplay loop>
162
- <source src="{audio_url_1}" type="audio/mp3">
163
- </audio>
164
- <script>
165
- var audioPlayer = document.getElementById('audio-player');
166
- var audioFiles = ['{audio_url_1}', '{audio_url_2}'];
167
- var currentTrack = 0;
168
-
169
- audioPlayer.addEventListener('ended', function() {{
170
- currentTrack = (currentTrack + 1) % audioFiles.length;
171
- audioPlayer.src = audioFiles[currentTrack];
172
- audioPlayer.play();
173
- }});
174
- </script>
175
- """
176
 
177
- # Gradio界面显示
178
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
179
  gr.Markdown("# 🎀 Sophia Chat Agent")
180
  gr.Markdown("欢迎来到 **Sophia Jr**,相信你也是马+7大家庭中的一员。快来和我聊聊吧!💬")
181
-
182
- # 背景图片路径
183
- background_images = [
184
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family.jpg",
185
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family1.jpg",
186
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family2.jpg",
187
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family3.jpg",
188
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family4.jpg",
189
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family5.jpg",
190
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family6.jpg",
191
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family7.jpg",
192
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family8.jpg",
193
- "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family9.jpg"
194
- ]
195
-
196
- # 头像图片链接
197
- avatar_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/bean.jpg"
198
-
199
- # Chatbot 组件
200
- chatbot = gr.Chatbot(label="Sophia", type="messages")
201
  msg = gr.Textbox(label="你想说啥子哦~", placeholder="快点跟 Sophia 开始聊天吧!", lines=2)
202
  state = gr.State([{"role": "assistant", "content": "你好,我是 Sophia~你想聊啥?"}])
203
  btn = gr.Button("发送")
204
 
205
- # Gradio界面显示,应用背景图片和头像
206
  gr.HTML(f"""
207
  <style>
208
  body {{
209
- background-image: url({background_images[0]});
210
- background-size: cover;
211
- background-repeat: no-repeat;
212
  animation: backgroundCycle 30s infinite;
 
 
 
213
  }}
214
  @keyframes backgroundCycle {{
215
  0% {{ background-image: url({background_images[0]}); }}
@@ -223,61 +200,55 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
223
  80% {{ background-image: url({background_images[8]}); }}
224
  90% {{ background-image: url({background_images[9]}); }}
225
  }}
226
-
227
- /* 动态头像样式 */
 
 
 
 
 
228
  #sophia-avatar {{
 
 
 
229
  width: 60px;
230
  height: 60px;
231
  border-radius: 50%;
232
- position: absolute;
233
- top: 10px;
234
- left: 10px;
235
- animation: jump 2s ease-in-out infinite, rotate 4s linear infinite;
236
- }}
237
-
238
- @keyframes jump {{
239
- 0%, 100% {{
240
- transform: translateY(0);
241
- }}
242
- 50% {{
243
- transform: translateY(-10px);
244
- }}
245
  }}
246
-
247
- @keyframes rotate {{
248
- 0% {{
249
- transform: rotate(0deg);
250
- }}
251
- 100% {{
252
- transform: rotate(360deg);
253
- }}
254
  }}
255
-
256
- /* 自定义聊天框样式 */
257
- .gradio-chatbot .message {
258
- display: flex;
259
- align-items: center;
260
- background-color: rgba(255, 182, 193, 0.8); /* 浪漫粉色 */
261
- border-radius: 10px;
262
- margin-bottom: 10px;
263
- padding: 8px;
264
- }
265
-
266
- .gradio-chatbot .message .user-message img {
267
- margin-right: 10px;
268
- }
269
-
270
- /* 提高聊天框和背景的对比*/
271
- .gradio-chatbot .message.sophia {
272
- background-color: rgba(173, 216, 230, 0.8); /* 浅蓝色 */
273
- }
274
  </style>
275
- """)
276
 
277
- # 插入音频播放器
278
- gr.HTML(html_audio_player)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
280
- # 聊天回调(不修改)
281
  btn.click(chat, inputs=[msg, state], outputs=[chatbot, state])
282
  msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])
283
 
 
152
 
153
  import gradio as gr
154
 
155
+ # === 背景图与音乐 URL ===
156
+ background_images = [
157
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family.jpg",
158
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family1.jpg",
159
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family2.jpg",
160
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family3.jpg",
161
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family4.jpg",
162
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family5.jpg",
163
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family6.jpg",
164
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family7.jpg",
165
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family8.jpg",
166
+ "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/family9.jpg"
167
+ ]
168
+ avatar_url = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/bean.jpg"
169
+ music1 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/FNG.mp3"
170
+ music2 = "https://huggingface.co/spaces/Ronaldo1111/Sophia/resolve/main/PGY.mp3"
 
 
 
 
 
171
 
 
172
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
173
  gr.Markdown("# 🎀 Sophia Chat Agent")
174
  gr.Markdown("欢迎来到 **Sophia Jr**,相信你也是马+7大家庭中的一员。快来和我聊聊吧!💬")
175
+
176
+ chatbot = gr.Chatbot(label="Sophia", type="messages", show_copy_button=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  msg = gr.Textbox(label="你想说啥子哦~", placeholder="快点跟 Sophia 开始聊天吧!", lines=2)
178
  state = gr.State([{"role": "assistant", "content": "你好,我是 Sophia~你想聊啥?"}])
179
  btn = gr.Button("发送")
180
 
181
+ # 插入 CSS、背景图轮播、头像动画、聊天框美化、背景音乐播放
182
  gr.HTML(f"""
183
  <style>
184
  body {{
185
+ margin: 0;
 
 
186
  animation: backgroundCycle 30s infinite;
187
+ background-size: cover;
188
+ background-position: center;
189
+ transition: background-image 1s ease-in-out;
190
  }}
191
  @keyframes backgroundCycle {{
192
  0% {{ background-image: url({background_images[0]}); }}
 
200
  80% {{ background-image: url({background_images[8]}); }}
201
  90% {{ background-image: url({background_images[9]}); }}
202
  }}
203
+
204
+ .gr-chatbot {{
205
+ background: linear-gradient(135deg, #fbd3e9, #bb377d);
206
+ border-radius: 12px;
207
+ padding: 10px;
208
+ }}
209
+
210
  #sophia-avatar {{
211
+ position: fixed;
212
+ top: 10px;
213
+ left: 10px;
214
  width: 60px;
215
  height: 60px;
216
  border-radius: 50%;
217
+ animation: bounceSpin 4s infinite;
218
+ z-index: 9999;
 
 
 
 
 
 
 
 
 
 
 
219
  }}
220
+
221
+ @keyframes bounceSpin {{
222
+ 0%, 100% {{ transform: translateY(0) rotate(0deg); }}
223
+ 25% {{ transform: translateY(-10px) rotate(90deg); }}
224
+ 50% {{ transform: translateY(0) rotate(180deg); }}
225
+ 75% {{ transform: translateY(10px) rotate(270deg); }}
 
 
226
  }}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  </style>
 
228
 
229
+ <img id="sophia-avatar" src="{avatar_url}" />
230
+
231
+ <audio id="bg-music" autoplay loop>
232
+ <source src="{music1}" type="audio/mpeg">
233
+ <source src="{music2}" type="audio/mpeg">
234
+ Your browser does not support the audio tag.
235
+ </audio>
236
+
237
+ <script>
238
+ // 自动循环播放多首歌曲
239
+ const musicList = ["{music1}", "{music2}"];
240
+ let currentTrack = 0;
241
+ const audio = document.getElementById("bg-music");
242
+ audio.src = musicList[currentTrack];
243
+ audio.addEventListener("ended", () => {{
244
+ currentTrack = (currentTrack + 1) % musicList.length;
245
+ audio.src = musicList[currentTrack];
246
+ audio.play();
247
+ }});
248
+ </script>
249
+ """)
250
 
251
+ # 聊天逻辑按钮触发
252
  btn.click(chat, inputs=[msg, state], outputs=[chatbot, state])
253
  msg.submit(chat, inputs=[msg, state], outputs=[chatbot, state])
254