anthonyyha102 commited on
Commit
3486e4b
·
verified ·
1 Parent(s): e69457b

Upload streamlit_app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. streamlit_app.py +447 -0
streamlit_app.py ADDED
@@ -0,0 +1,447 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import urllib.parse
4
+
5
+ # 页面配置
6
+ st.set_page_config(
7
+ page_title="视频搜索 - 轻松找视频",
8
+ page_icon="🎬",
9
+ layout="wide"
10
+ )
11
+
12
+ # 自定义CSS - 老年人友好设计
13
+ st.markdown("""
14
+ <style>
15
+ /* 整体字体放大 */
16
+ .stApp {
17
+ font-size: 20px !important;
18
+ }
19
+
20
+ /* 标题样式 */
21
+ h1 {
22
+ font-size: 42px !important;
23
+ color: #d97706 !important;
24
+ text-align: center;
25
+ padding: 20px 0;
26
+ }
27
+
28
+ h2 {
29
+ font-size: 28px !important;
30
+ color: #92400e !important;
31
+ }
32
+
33
+ h3 {
34
+ font-size: 24px !important;
35
+ color: #b45309 !important;
36
+ }
37
+
38
+ /* 输入框样式 */
39
+ .stTextInput > div > div > input {
40
+ font-size: 24px !important;
41
+ padding: 15px !important;
42
+ border-radius: 15px !important;
43
+ border: 3px solid #fbbf24 !important;
44
+ }
45
+
46
+ .stTextInput > div > div > input:focus {
47
+ border-color: #d97706 !important;
48
+ box-shadow: 0 0 10px rgba(217, 119, 6, 0.3) !important;
49
+ }
50
+
51
+ /* 按钮样式 */
52
+ .stButton > button {
53
+ font-size: 22px !important;
54
+ padding: 15px 40px !important;
55
+ border-radius: 15px !important;
56
+ background-color: #f59e0b !important;
57
+ color: white !important;
58
+ border: none !important;
59
+ min-height: 60px !important;
60
+ transition: all 0.3s ease !important;
61
+ }
62
+
63
+ .stButton > button:hover {
64
+ background-color: #d97706 !important;
65
+ transform: scale(1.02) !important;
66
+ }
67
+
68
+ /* 视频卡片样式 */
69
+ .video-card {
70
+ background: linear-gradient(135deg, #fffbeb 0%, #fef3c7 100%);
71
+ border-radius: 20px;
72
+ padding: 20px;
73
+ margin: 15px 0;
74
+ border: 2px solid #fbbf24;
75
+ box-shadow: 0 4px 15px rgba(251, 191, 36, 0.2);
76
+ transition: all 0.3s ease;
77
+ }
78
+
79
+ .video-card:hover {
80
+ transform: translateY(-5px);
81
+ box-shadow: 0 8px 25px rgba(251, 191, 36, 0.3);
82
+ }
83
+
84
+ .video-title {
85
+ font-size: 22px !important;
86
+ font-weight: bold;
87
+ color: #92400e;
88
+ margin-bottom: 10px;
89
+ line-height: 1.4;
90
+ }
91
+
92
+ .video-channel {
93
+ font-size: 18px;
94
+ color: #b45309;
95
+ margin-bottom: 8px;
96
+ }
97
+
98
+ .video-stats {
99
+ font-size: 16px;
100
+ color: #78716c;
101
+ }
102
+
103
+ /* 快捷按钮样式 */
104
+ .quick-btn {
105
+ display: inline-block;
106
+ padding: 12px 20px;
107
+ margin: 5px;
108
+ background-color: #fef3c7;
109
+ border: 2px solid #fbbf24;
110
+ border-radius: 25px;
111
+ font-size: 18px;
112
+ cursor: pointer;
113
+ transition: all 0.3s ease;
114
+ }
115
+
116
+ .quick-btn:hover {
117
+ background-color: #fde68a;
118
+ }
119
+
120
+ /* 提示文字 */
121
+ .tip-text {
122
+ font-size: 18px;
123
+ color: #78716c;
124
+ text-align: center;
125
+ padding: 20px;
126
+ background-color: #fffbeb;
127
+ border-radius: 15px;
128
+ margin: 20px 0;
129
+ }
130
+
131
+ /* 链接样式 */
132
+ a {
133
+ color: #d97706 !important;
134
+ text-decoration: none !important;
135
+ font-weight: bold;
136
+ }
137
+
138
+ a:hover {
139
+ color: #b45309 !important;
140
+ text-decoration: underline !important;
141
+ }
142
+
143
+ /* 页脚 */
144
+ .footer {
145
+ text-align: center;
146
+ padding: 30px;
147
+ color: #78716c;
148
+ font-size: 16px;
149
+ }
150
+
151
+ /* 搜索结果数量 */
152
+ .result-count {
153
+ font-size: 20px;
154
+ color: #92400e;
155
+ text-align: center;
156
+ padding: 15px;
157
+ background-color: #fef3c7;
158
+ border-radius: 10px;
159
+ margin: 20px 0;
160
+ }
161
+
162
+ /* 平台选择器 */
163
+ .stSelectbox > div > div {
164
+ font-size: 20px !important;
165
+ }
166
+
167
+ /* 分隔线 */
168
+ hr {
169
+ border: none;
170
+ height: 3px;
171
+ background: linear-gradient(90deg, #fbbf24, #f59e0b, #fbbf24);
172
+ margin: 30px 0;
173
+ border-radius: 2px;
174
+ }
175
+ </style>
176
+ """, unsafe_allow_html=True)
177
+
178
+ # 标题
179
+ st.markdown("# 🎬 视频搜索")
180
+ st.markdown("<p style='text-align: center; font-size: 22px; color: #78716c;'>输入想看的内容,帮您找到好看的视频!</p>", unsafe_allow_html=True)
181
+ st.markdown("<p style='text-align: center; font-size: 14px;'><a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>Built with anycoder</a></p>", unsafe_allow_html=True)
182
+
183
+ st.markdown("---")
184
+
185
+ # 初始化session state
186
+ if 'search_query' not in st.session_state:
187
+ st.session_state.search_query = ""
188
+ if 'search_results' not in st.session_state:
189
+ st.session_state.search_results = []
190
+ if 'searched' not in st.session_state:
191
+ st.session_state.searched = False
192
+
193
+ # 搜索函数 - 使用Invidious API (YouTube的开源前端)
194
+ def search_videos(query, max_results=12):
195
+ """搜索视频"""
196
+ if not query or not query.strip():
197
+ return []
198
+
199
+ # 使用多个Invidious实例作为备选
200
+ invidious_instances = [
201
+ "https://vid.puffyan.us",
202
+ "https://invidious.snopyta.org",
203
+ "https://yewtu.be",
204
+ "https://invidious.kavin.rocks",
205
+ ]
206
+
207
+ results = []
208
+
209
+ for instance in invidious_instances:
210
+ try:
211
+ # 搜索API
212
+ search_url = f"{instance}/api/v1/search"
213
+ params = {
214
+ "q": query,
215
+ "type": "video",
216
+ "sort_by": "relevance"
217
+ }
218
+
219
+ response = requests.get(search_url, params=params, timeout=10)
220
+
221
+ if response.status_code == 200:
222
+ data = response.json()
223
+
224
+ for item in data[:max_results]:
225
+ if item.get("type") == "video":
226
+ video_id = item.get("videoId", "")
227
+
228
+ # 格式化观看次数
229
+ view_count = item.get("viewCount", 0)
230
+ if view_count >= 100000000:
231
+ views_str = f"{view_count // 100000000}亿次观看"
232
+ elif view_count >= 10000:
233
+ views_str = f"{view_count // 10000}万次观看"
234
+ else:
235
+ views_str = f"{view_count}次观看"
236
+
237
+ # 格式化时长
238
+ length_seconds = item.get("lengthSeconds", 0)
239
+ if length_seconds >= 3600:
240
+ hours = length_seconds // 3600
241
+ minutes = (length_seconds % 3600) // 60
242
+ duration_str = f"{hours}小时{minutes}分钟"
243
+ else:
244
+ minutes = length_seconds // 60
245
+ seconds = length_seconds % 60
246
+ duration_str = f"{minutes}分{seconds}秒"
247
+
248
+ results.append({
249
+ "title": item.get("title", "未知标题"),
250
+ "video_id": video_id,
251
+ "channel": item.get("author", "未知频道"),
252
+ "views": views_str,
253
+ "duration": duration_str,
254
+ "thumbnail": f"https://img.youtube.com/vi/{video_id}/mqdefault.jpg",
255
+ "url": f"https://www.youtube.com/watch?v={video_id}",
256
+ "embed_url": f"https://www.youtube.com/embed/{video_id}"
257
+ })
258
+
259
+ if results:
260
+ return results
261
+
262
+ except Exception as e:
263
+ continue
264
+
265
+ return results
266
+
267
+ # 生成模拟搜索结果(当API不可用时的备选方案)
268
+ def generate_search_links(query):
269
+ """生成各平台的搜索链接"""
270
+ encoded_query = urllib.parse.quote(query)
271
+
272
+ return {
273
+ "YouTube": f"https://www.youtube.com/results?search_query={encoded_query}",
274
+ "哔哩哔哩": f"https://search.bilibili.com/all?keyword={encoded_query}",
275
+ "抖音": f"https://www.douyin.com/search/{encoded_query}",
276
+ "西瓜视频": f"https://www.ixigua.com/search/{encoded_query}",
277
+ "腾讯视频": f"https://v.qq.com/x/search/?q={encoded_query}",
278
+ "优酷": f"https://so.youku.com/search_video/q_{encoded_query}",
279
+ "爱奇艺": f"https://so.iqiyi.com/so/q_{encoded_query}"
280
+ }
281
+
282
+ # 搜索区域
283
+ col1, col2, col3 = st.columns([1, 4, 1])
284
+
285
+ with col2:
286
+ # 搜索输入框
287
+ search_input = st.text_input(
288
+ "🔍 输入您想看的内容",
289
+ value=st.session_state.search_query,
290
+ placeholder="比如:太极拳教学、京剧、健康养生...",
291
+ key="search_input"
292
+ )
293
+
294
+ # 搜索按钮
295
+ col_btn1, col_btn2, col_btn3 = st.columns([1, 2, 1])
296
+ with col_btn2:
297
+ search_clicked = st.button("🔍 开始搜索", use_container_width=True, type="primary")
298
+
299
+ # 快捷搜索标签
300
+ st.markdown("### 💡 热门推荐(点击即可搜索)")
301
+
302
+ # 快捷搜索选项
303
+ quick_searches = [
304
+ ("🎭 京剧表演", "京剧经典唱段"),
305
+ ("🥋 太极拳", "太极拳教学"),
306
+ ("🌿 养生保健", "老年人养生保健"),
307
+ ("🍳 家常菜", "家常菜做法教程"),
308
+ ("🎵 老歌经典", "经典老歌怀旧"),
309
+ ("💃 广场舞", "广场舞教学"),
310
+ ("🎨 书法教学", "书法入门教程"),
311
+ ("🌸 园艺种花", "阳台种花教程"),
312
+ ("♟️ 象棋教学", "中国象棋教程"),
313
+ ("📺 相声小品", "经典相声小品"),
314
+ ("🧘 健身操", "中老年健身操"),
315
+ ("🎹 戏曲欣赏", "经典戏曲合集")
316
+ ]
317
+
318
+ # 创建快捷搜索按钮网格
319
+ cols = st.columns(4)
320
+ for idx, (label, query) in enumerate(quick_searches):
321
+ with cols[idx % 4]:
322
+ if st.button(label, key=f"quick_{idx}", use_container_width=True):
323
+ st.session_state.search_query = query
324
+ st.session_state.searched = True
325
+ st.rerun()
326
+
327
+ st.markdown("---")
328
+
329
+ # 执行搜索
330
+ if search_clicked and search_input:
331
+ st.session_state.search_query = search_input
332
+ st.session_state.searched = True
333
+
334
+ # 显示搜索结果
335
+ if st.session_state.searched and st.session_state.search_query:
336
+ query = st.session_state.search_query
337
+
338
+ st.markdown(f"## 🎯 "{query}" 的搜索结果")
339
+
340
+ # 显示加载状态
341
+ with st.spinner("🔍 正在为您搜索视频,请稍候..."):
342
+ results = search_videos(query)
343
+
344
+ if results:
345
+ st.markdown(f"<div class='result-count'>✨ 找到 {len(results)} 个相关视频</div>", unsafe_allow_html=True)
346
+
347
+ # 显示视频结果
348
+ for i in range(0, len(results), 3):
349
+ cols = st.columns(3)
350
+ for j, col in enumerate(cols):
351
+ if i + j < len(results):
352
+ video = results[i + j]
353
+ with col:
354
+ st.markdown(f"""
355
+ <div class='video-card'>
356
+ <img src="{video['thumbnail']}" style="width: 100%; border-radius: 10px; margin-bottom: 10px;">
357
+ <div class='video-title'>{video['title'][:50]}{'...' if len(video['title']) > 50 else ''}</div>
358
+ <div class='video-channel'>📺 {video['channel']}</div>
359
+ <div class='video-stats'>👁️ {video['views']} | ⏱️ {video['duration']}</div>
360
+ </div>
361
+ """, unsafe_allow_html=True)
362
+
363
+ if st.button(f"▶️ 观看视频", key=f"watch_{i+j}", use_container_width=True):
364
+ st.markdown(f"[点击这里打开视频]({video['url']})")
365
+ # 嵌入视频播放器
366
+ st.video(video['url'])
367
+
368
+ # 显示各平台搜索链接
369
+ st.markdown("---")
370
+ st.markdown("### 🌐 在其他平台搜索")
371
+ st.markdown("<p style='font-size: 18px; color: #78716c;'>点击下方按钮,在对应平台搜索更多视频</p>", unsafe_allow_html=True)
372
+
373
+ search_links = generate_search_links(query)
374
+
375
+ platform_cols = st.columns(4)
376
+ platforms = list(search_links.items())
377
+
378
+ for idx, (platform, url) in enumerate(platforms):
379
+ with platform_cols[idx % 4]:
380
+ st.markdown(f"""
381
+ <a href="{url}" target="_blank" style="
382
+ display: block;
383
+ text-align: center;
384
+ padding: 15px;
385
+ margin: 10px 0;
386
+ background: linear-gradient(135deg, #fef3c7, #fde68a);
387
+ border-radius: 15px;
388
+ border: 2px solid #fbbf24;
389
+ color: #92400e !important;
390
+ font-size: 20px;
391
+ font-weight: bold;
392
+ text-decoration: none;
393
+ transition: all 0.3s ease;
394
+ ">
395
+ 🔗 {platform}
396
+ </a>
397
+ """, unsafe_allow_html=True)
398
+
399
+ else:
400
+ # 未搜索时显示提示
401
+ st.markdown("""
402
+ <div class='tip-text'>
403
+ <p style='font-size: 24px; margin-bottom: 15px;'>👆 在上方输入框中输入您想看的内容</p>
404
+ <p style='font-size: 20px; margin-bottom: 10px;'>或者点击"热门推荐"中的标签快速搜索</p>
405
+ <p style='font-size: 18px; color: #a8a29e;'>例如:太极拳、京剧、养生保健、广场舞...</p>
406
+ </div>
407
+ """, unsafe_allow_html=True)
408
+
409
+ # 显示使用说明
410
+ with st.expander("📖 使用说明(点击展开)", expanded=False):
411
+ st.markdown("""
412
+ ### 🎯 如何使用视频搜索?
413
+
414
+ **方法一:直接搜索**
415
+ 1. 在搜索框中输入您想看的内容
416
+ 2. 点击"开始搜索"按钮
417
+ 3. 等待搜索结果出现
418
+ 4. 点击视频卡片下方的"观看视频"按钮
419
+
420
+ **方法二:快捷搜索**
421
+ 1. 在"热门推荐"中找到感兴趣的标签
422
+ 2. 直接点击标签即可搜索
423
+
424
+ ### 💡 搜索小技巧
425
+ - 搜索词越具体,结果越准确
426
+ - 可以搜索:教学、表演、讲解、合集等
427
+ - 如果找不到想要的,试试换个说法
428
+
429
+ ### 🌐 支持的视频平台
430
+ - YouTube(油管)
431
+ - 哔哩哔哩(B站)
432
+ - 抖音
433
+ - 西瓜视频
434
+ - 腾讯视频
435
+ - 优酷
436
+ - 爱奇艺
437
+ """)
438
+
439
+ # 页脚
440
+ st.markdown("---")
441
+ st.markdown("""
442
+ <div class='footer'>
443
+ <p>🎬 视频搜索 - 轻松找到您想看的视频</p>
444
+ <p style='font-size: 14px; color: #a8a29e;'>温馨提示:观看视频时注意休息眼睛,每看30分钟休息5分钟 👀</p>
445
+ <p style='font-size: 14px;'><a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>Built with anycoder</a></p>
446
+ </div>
447
+ """, unsafe_allow_html=True)