Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
from bs4 import BeautifulSoup
|
@@ -39,7 +45,39 @@ def youtube_search(query, max_results=50):
|
|
39 |
print(f"Error during YouTube web scraping request: {e}")
|
40 |
return []
|
41 |
|
42 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
with gr.Blocks() as demo:
|
44 |
gr.Markdown("## YouTube Video Search, Selection, and Playback")
|
45 |
|
|
|
1 |
+
import subprocess
|
2 |
+
import sys
|
3 |
+
|
4 |
+
# Ensure compatible versions of httpx and httpcore are installed
|
5 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "httpx==0.18.2", "httpcore==0.13.6"])
|
6 |
+
|
7 |
import gradio as gr
|
8 |
import requests
|
9 |
from bs4 import BeautifulSoup
|
|
|
45 |
print(f"Error during YouTube web scraping request: {e}")
|
46 |
return []
|
47 |
|
48 |
+
# Function to display the video using the video URL
|
49 |
+
def show_video(video_url):
|
50 |
+
# Regular expression to extract the YouTube video ID from the URL
|
51 |
+
video_id = None
|
52 |
+
patterns = [
|
53 |
+
r"youtube\.com/watch\?v=([^&?\/]+)",
|
54 |
+
r"youtube\.com/embed/([^&?\/]+)",
|
55 |
+
r"youtube\.com/v/([^&?\/]+)",
|
56 |
+
r"youtu\.be/([^&?\/]+)"
|
57 |
+
]
|
58 |
+
|
59 |
+
for pattern in patterns:
|
60 |
+
match = re.search(pattern, video_url)
|
61 |
+
if match:
|
62 |
+
video_id = match.group(1)
|
63 |
+
break
|
64 |
+
|
65 |
+
# If no video ID is found, return an error message
|
66 |
+
if not video_id:
|
67 |
+
return "Invalid YouTube URL. Please enter a valid YouTube video link."
|
68 |
+
|
69 |
+
# Create the embed URL
|
70 |
+
embed_url = f"https://www.youtube.com/embed/{video_id}"
|
71 |
+
|
72 |
+
# Return an iframe with the video
|
73 |
+
html_code = f'''
|
74 |
+
<iframe width="560" height="315" src="{embed_url}"
|
75 |
+
frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
|
76 |
+
allowfullscreen></iframe>
|
77 |
+
'''
|
78 |
+
return html_code
|
79 |
+
|
80 |
+
# Create the Gradio interface
|
81 |
with gr.Blocks() as demo:
|
82 |
gr.Markdown("## YouTube Video Search, Selection, and Playback")
|
83 |
|