Update app.py
Browse files
app.py
CHANGED
|
@@ -2,13 +2,21 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
-
|
| 6 |
import demoji
|
| 7 |
from textblob import TextBlob
|
| 8 |
from faker import Faker
|
| 9 |
import pandas as pd
|
| 10 |
from difflib import SequenceMatcher
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Function for Image Mirroring
|
| 13 |
def mirror_image(input_img):
|
| 14 |
if input_img is None:
|
|
@@ -16,28 +24,80 @@ def mirror_image(input_img):
|
|
| 16 |
mirrored_img = Image.fromarray(input_img).transpose(Image.FLIP_LEFT_RIGHT)
|
| 17 |
return mirrored_img
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
def download_youtube_mp3(url, destination="."):
|
|
|
|
|
|
|
|
|
|
| 21 |
if not url:
|
| 22 |
return "Please enter a valid URL"
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
try:
|
| 25 |
yt = YouTube(url)
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
if not destination:
|
| 29 |
destination = "."
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Save the file with .mp3 extension
|
| 34 |
base, ext = os.path.splitext(out_file)
|
| 35 |
new_file = base + '.mp3'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
os.rename(out_file, new_file)
|
| 37 |
|
| 38 |
-
return f"{yt.title} has been
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Function for Emoji Detection
|
| 43 |
def detect_emojis(text):
|
|
@@ -149,10 +209,14 @@ with gr.Blocks(title="MultiToolBox") as app:
|
|
| 149 |
# YouTube to MP3 Tab
|
| 150 |
with gr.Tab("YouTube to MP3"):
|
| 151 |
gr.Markdown("### Download YouTube Videos as MP3")
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
yt_destination = gr.Textbox(label="Destination Folder (leave empty for current directory)", placeholder=".")
|
| 154 |
yt_download_btn = gr.Button("Download")
|
| 155 |
-
yt_output = gr.Textbox(label="Result")
|
| 156 |
|
| 157 |
yt_download_btn.click(fn=download_youtube_mp3, inputs=[yt_url, yt_destination], outputs=yt_output)
|
| 158 |
|
|
@@ -163,6 +227,8 @@ with gr.Blocks(title="MultiToolBox") as app:
|
|
| 163 |
3. Click "Download" to convert and save as MP3
|
| 164 |
|
| 165 |
**Example:** https://www.youtube.com/watch?v=dQw4w9WgXcQ
|
|
|
|
|
|
|
| 166 |
""")
|
| 167 |
|
| 168 |
# Emoji Detection Tab
|
|
@@ -221,7 +287,7 @@ with gr.Blocks(title="MultiToolBox") as app:
|
|
| 221 |
**Examples:**
|
| 222 |
- 135 (Disarium number)
|
| 223 |
- 89 (Disarium number: 8^1 + 9^2 = 8 + 81 = 89)
|
| 224 |
-
-
|
| 225 |
""")
|
| 226 |
|
| 227 |
# Fake Data Generator Tab
|
|
|
|
| 2 |
import os
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import re
|
| 6 |
import demoji
|
| 7 |
from textblob import TextBlob
|
| 8 |
from faker import Faker
|
| 9 |
import pandas as pd
|
| 10 |
from difflib import SequenceMatcher
|
| 11 |
|
| 12 |
+
# Try importing pytube with better error handling
|
| 13 |
+
try:
|
| 14 |
+
from pytube import YouTube
|
| 15 |
+
from pytube.exceptions import RegexMatchError, VideoUnavailable
|
| 16 |
+
PYTUBE_AVAILABLE = True
|
| 17 |
+
except ImportError:
|
| 18 |
+
PYTUBE_AVAILABLE = False
|
| 19 |
+
|
| 20 |
# Function for Image Mirroring
|
| 21 |
def mirror_image(input_img):
|
| 22 |
if input_img is None:
|
|
|
|
| 24 |
mirrored_img = Image.fromarray(input_img).transpose(Image.FLIP_LEFT_RIGHT)
|
| 25 |
return mirrored_img
|
| 26 |
|
| 27 |
+
# Improved YouTube URL validation
|
| 28 |
+
def is_valid_youtube_url(url):
|
| 29 |
+
if not url:
|
| 30 |
+
return False
|
| 31 |
+
|
| 32 |
+
# Common YouTube URL patterns
|
| 33 |
+
youtube_regex = (
|
| 34 |
+
r'(https?://)?(www\.)?'
|
| 35 |
+
r'(youtube|youtu|youtube-nocookie)\.(com|be)/'
|
| 36 |
+
r'(watch\?v=|embed/|v/|.+\?v=)?([^&=%\?]{11})')
|
| 37 |
+
|
| 38 |
+
match = re.match(youtube_regex, url)
|
| 39 |
+
return match is not None
|
| 40 |
+
|
| 41 |
+
# Function for YouTube to MP3 Download with improved error handling
|
| 42 |
def download_youtube_mp3(url, destination="."):
|
| 43 |
+
if not PYTUBE_AVAILABLE:
|
| 44 |
+
return "Error: pytube library is not available. Please install it with 'pip install pytube'."
|
| 45 |
+
|
| 46 |
if not url:
|
| 47 |
return "Please enter a valid URL"
|
| 48 |
|
| 49 |
+
if not is_valid_youtube_url(url):
|
| 50 |
+
return "Error: The URL does not appear to be a valid YouTube URL"
|
| 51 |
+
|
| 52 |
try:
|
| 53 |
yt = YouTube(url)
|
| 54 |
+
|
| 55 |
+
# Check if video is available
|
| 56 |
+
title = yt.title # This will raise an exception if the video is unavailable
|
| 57 |
+
|
| 58 |
+
# Get the highest quality audio stream
|
| 59 |
+
audio_stream = yt.streams.filter(only_audio=True).order_by('abr').desc().first()
|
| 60 |
+
|
| 61 |
+
if not audio_stream:
|
| 62 |
+
return "Error: No audio stream found for this video"
|
| 63 |
|
| 64 |
if not destination:
|
| 65 |
destination = "."
|
| 66 |
|
| 67 |
+
# Ensure destination directory exists
|
| 68 |
+
if not os.path.exists(destination):
|
| 69 |
+
try:
|
| 70 |
+
os.makedirs(destination)
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return f"Error creating destination directory: {str(e)}"
|
| 73 |
+
|
| 74 |
+
# Download the file
|
| 75 |
+
out_file = audio_stream.download(output_path=destination)
|
| 76 |
|
| 77 |
# Save the file with .mp3 extension
|
| 78 |
base, ext = os.path.splitext(out_file)
|
| 79 |
new_file = base + '.mp3'
|
| 80 |
+
|
| 81 |
+
# Handle file already exists
|
| 82 |
+
if os.path.exists(new_file):
|
| 83 |
+
os.remove(new_file)
|
| 84 |
+
|
| 85 |
os.rename(out_file, new_file)
|
| 86 |
|
| 87 |
+
return f"Success! '{yt.title}' has been downloaded in MP3 format to: {new_file}"
|
| 88 |
+
|
| 89 |
+
except VideoUnavailable:
|
| 90 |
+
return "Error: This video is unavailable or private"
|
| 91 |
+
except RegexMatchError:
|
| 92 |
+
return "Error: The YouTube URL is not valid"
|
| 93 |
except Exception as e:
|
| 94 |
+
error_message = str(e)
|
| 95 |
+
if "429" in error_message:
|
| 96 |
+
return "Error: Too many requests. YouTube is rate-limiting downloads. Please try again later."
|
| 97 |
+
elif "403" in error_message:
|
| 98 |
+
return "Error: Access forbidden. YouTube may have restricted this content."
|
| 99 |
+
else:
|
| 100 |
+
return f"Error: {error_message}. If this is a recurring issue, YouTube may have updated their site. Try updating pytube with 'pip install --upgrade pytube'."
|
| 101 |
|
| 102 |
# Function for Emoji Detection
|
| 103 |
def detect_emojis(text):
|
|
|
|
| 209 |
# YouTube to MP3 Tab
|
| 210 |
with gr.Tab("YouTube to MP3"):
|
| 211 |
gr.Markdown("### Download YouTube Videos as MP3")
|
| 212 |
+
|
| 213 |
+
if not PYTUBE_AVAILABLE:
|
| 214 |
+
gr.Markdown("⚠️ **Warning:** pytube library is not installed. This feature will not work properly.")
|
| 215 |
+
|
| 216 |
+
yt_url = gr.Textbox(label="YouTube URL", placeholder="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
|
| 217 |
yt_destination = gr.Textbox(label="Destination Folder (leave empty for current directory)", placeholder=".")
|
| 218 |
yt_download_btn = gr.Button("Download")
|
| 219 |
+
yt_output = gr.Textbox(label="Result", lines=3)
|
| 220 |
|
| 221 |
yt_download_btn.click(fn=download_youtube_mp3, inputs=[yt_url, yt_destination], outputs=yt_output)
|
| 222 |
|
|
|
|
| 227 |
3. Click "Download" to convert and save as MP3
|
| 228 |
|
| 229 |
**Example:** https://www.youtube.com/watch?v=dQw4w9WgXcQ
|
| 230 |
+
|
| 231 |
+
**Note:** If you encounter errors, try running `pip install --upgrade pytube` to update the YouTube library
|
| 232 |
""")
|
| 233 |
|
| 234 |
# Emoji Detection Tab
|
|
|
|
| 287 |
**Examples:**
|
| 288 |
- 135 (Disarium number)
|
| 289 |
- 89 (Disarium number: 8^1 + 9^2 = 8 + 81 = 89)
|
| 290 |
+
- 165 (Not a Disarium number)
|
| 291 |
""")
|
| 292 |
|
| 293 |
# Fake Data Generator Tab
|