Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,48 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
with gr.Blocks(title="
|
| 8 |
-
gr.Markdown("#
|
| 9 |
-
gr.Markdown("
|
| 10 |
|
| 11 |
chatbot = gr.ChatInterface(
|
| 12 |
-
fn=
|
| 13 |
-
title="
|
| 14 |
examples=[
|
| 15 |
-
"
|
| 16 |
-
"
|
| 17 |
-
"What model should I use for Persian text?"
|
| 18 |
]
|
| 19 |
)
|
| 20 |
|
| 21 |
if __name__ == "__main__":
|
| 22 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import yt_dlp
|
| 3 |
+
import os
|
| 4 |
+
import re
|
| 5 |
|
| 6 |
+
def is_youtube_url(url):
|
| 7 |
+
youtube_regex = (
|
| 8 |
+
r'(https?://)?(www\.)?'
|
| 9 |
+
r'(youtube|youtu|youtube-nocookie)\.(com|be)/'
|
| 10 |
+
r'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})'
|
| 11 |
+
)
|
| 12 |
+
return re.match(youtube_regex, url) is not None
|
| 13 |
+
|
| 14 |
+
def chat_handler(message, history):
|
| 15 |
+
if is_youtube_url(message.strip()):
|
| 16 |
+
os.makedirs('downloads', exist_ok=True)
|
| 17 |
+
ydl_opts = {
|
| 18 |
+
'format': 'best[height<=720]',
|
| 19 |
+
'outtmpl': 'downloads/%(title)s.%(ext)s',
|
| 20 |
+
}
|
| 21 |
+
try:
|
| 22 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 23 |
+
info = ydl.extract_info(message, download=True)
|
| 24 |
+
filename = ydl.prepare_filename(info)
|
| 25 |
+
ext = os.path.splitext(filename)[1].lstrip('.')
|
| 26 |
+
video_src = f"/file/{os.path.basename(filename)}"
|
| 27 |
+
response = f"Here's the downloaded video:\n<video width=\"640\" controls><source src=\"{video_src}\" type=\"video/{ext}\"></video>"
|
| 28 |
+
return response
|
| 29 |
+
except Exception as e:
|
| 30 |
+
return f"Error downloading the video: {str(e)}"
|
| 31 |
+
else:
|
| 32 |
+
return "Hi! Send me a YouTube video link, and I'll download it for you."
|
| 33 |
|
| 34 |
+
with gr.Blocks(title="YouTube Downloader Chat") as demo:
|
| 35 |
+
gr.Markdown("# 📹 YouTube Video Downloader Chat")
|
| 36 |
+
gr.Markdown("Paste a YouTube link in the chat, and I'll fetch the video for you.")
|
| 37 |
|
| 38 |
chatbot = gr.ChatInterface(
|
| 39 |
+
fn=chat_handler,
|
| 40 |
+
title="YouTube Downloader Assistant",
|
| 41 |
examples=[
|
| 42 |
+
"https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
| 43 |
+
"https://youtu.be/dQw4w9WgXcQ"
|
|
|
|
| 44 |
]
|
| 45 |
)
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
| 48 |
+
demo.launch()
|