Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,21 +2,12 @@ import gradio as gr
|
|
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,81 +15,6 @@ def mirror_image(input_img):
|
|
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):
|
104 |
if not text:
|
@@ -206,31 +122,6 @@ with gr.Blocks(title="MultiToolBox") as app:
|
|
206 |
3. The result will appear in the right panel
|
207 |
""")
|
208 |
|
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 |
-
|
223 |
-
gr.Markdown("""
|
224 |
-
**How to use:**
|
225 |
-
1. Enter a valid YouTube URL (e.g., https://www.youtube.com/watch?v=dQw4w9WgXcQ)
|
226 |
-
2. Optionally specify a destination folder
|
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
|
235 |
with gr.Tab("Emoji Detector"):
|
236 |
gr.Markdown("### Detect Emojis in Text")
|
|
|
2 |
import os
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
|
|
5 |
import demoji
|
6 |
from textblob import TextBlob
|
7 |
from faker import Faker
|
8 |
import pandas as pd
|
9 |
from difflib import SequenceMatcher
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Function for Image Mirroring
|
12 |
def mirror_image(input_img):
|
13 |
if input_img is None:
|
|
|
15 |
mirrored_img = Image.fromarray(input_img).transpose(Image.FLIP_LEFT_RIGHT)
|
16 |
return mirrored_img
|
17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# Function for Emoji Detection
|
19 |
def detect_emojis(text):
|
20 |
if not text:
|
|
|
122 |
3. The result will appear in the right panel
|
123 |
""")
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
# Emoji Detection Tab
|
126 |
with gr.Tab("Emoji Detector"):
|
127 |
gr.Markdown("### Detect Emojis in Text")
|