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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
from pytube import YouTube
|
6 |
+
import demoji
|
7 |
+
import sounddevice
|
8 |
+
from scipy.io.wavfile import write
|
9 |
+
from textblob import TextBlob
|
10 |
+
from faker import Faker
|
11 |
+
import pandas as pd
|
12 |
+
from difflib import SequenceMatcher
|
13 |
+
|
14 |
+
# Function for Image Mirroring
|
15 |
+
def mirror_image(input_img):
|
16 |
+
if input_img is None:
|
17 |
+
return None
|
18 |
+
mirrored_img = Image.fromarray(input_img).transpose(Image.FLIP_LEFT_RIGHT)
|
19 |
+
return mirrored_img
|
20 |
+
|
21 |
+
# Function for YouTube to MP3 Download
|
22 |
+
def download_youtube_mp3(url, destination="."):
|
23 |
+
if not url:
|
24 |
+
return "Please enter a valid URL"
|
25 |
+
|
26 |
+
try:
|
27 |
+
yt = YouTube(url)
|
28 |
+
video = yt.streams.filter(only_audio=True).first()
|
29 |
+
|
30 |
+
if not destination:
|
31 |
+
destination = "."
|
32 |
+
|
33 |
+
out_file = video.download(output_path=destination)
|
34 |
+
|
35 |
+
# Save the file with .mp3 extension
|
36 |
+
base, ext = os.path.splitext(out_file)
|
37 |
+
new_file = base + '.mp3'
|
38 |
+
os.rename(out_file, new_file)
|
39 |
+
|
40 |
+
return f"{yt.title} has been successfully downloaded in .mp3 format at {new_file}"
|
41 |
+
except Exception as e:
|
42 |
+
return f"Error: {str(e)}"
|
43 |
+
|
44 |
+
# Function for Emoji Detection
|
45 |
+
def detect_emojis(text):
|
46 |
+
if not text:
|
47 |
+
return "Please enter text containing emojis"
|
48 |
+
|
49 |
+
emoji_dict = demoji.findall(text)
|
50 |
+
if emoji_dict:
|
51 |
+
result = "Emojis found:\n"
|
52 |
+
for emoji, desc in emoji_dict.items():
|
53 |
+
result += f"{emoji}: {desc}\n"
|
54 |
+
return result
|
55 |
+
else:
|
56 |
+
return "No emojis found in the text"
|
57 |
+
|
58 |
+
# Function for Voice Recording
|
59 |
+
def record_voice(seconds):
|
60 |
+
if seconds <= 0:
|
61 |
+
return "Please enter a positive number of seconds"
|
62 |
+
|
63 |
+
try:
|
64 |
+
fs = 44100 # Sample rate
|
65 |
+
recording = sounddevice.rec(int(seconds * fs), samplerate=fs, channels=2)
|
66 |
+
sounddevice.wait()
|
67 |
+
|
68 |
+
output_file = "recording.wav"
|
69 |
+
write(output_file, fs, recording)
|
70 |
+
|
71 |
+
return f"Recording completed! Saved as {output_file}", output_file
|
72 |
+
except Exception as e:
|
73 |
+
return f"Error recording: {str(e)}", None
|
74 |
+
|
75 |
+
# Function for Spell Correction
|
76 |
+
def correct_spelling(text):
|
77 |
+
if not text:
|
78 |
+
return "Please enter text to correct"
|
79 |
+
|
80 |
+
words = text.split()
|
81 |
+
corrected_words = []
|
82 |
+
|
83 |
+
for word in words:
|
84 |
+
corrected_words.append(str(TextBlob(word).correct()))
|
85 |
+
|
86 |
+
return f"Original: {text}\nCorrected: {' '.join(corrected_words)}"
|
87 |
+
|
88 |
+
# Function to Check for Disarium Number
|
89 |
+
def check_disarium(number):
|
90 |
+
try:
|
91 |
+
number = int(number)
|
92 |
+
if number <= 0:
|
93 |
+
return "Please enter a positive integer"
|
94 |
+
|
95 |
+
length = len(str(number))
|
96 |
+
temp = number
|
97 |
+
sum_val = 0
|
98 |
+
|
99 |
+
while temp > 0:
|
100 |
+
rem = temp % 10
|
101 |
+
sum_val += rem ** length
|
102 |
+
temp = temp // 10
|
103 |
+
length -= 1
|
104 |
+
|
105 |
+
if sum_val == number:
|
106 |
+
return f"{number} is a Disarium Number"
|
107 |
+
else:
|
108 |
+
return f"{number} is NOT a Disarium Number"
|
109 |
+
except ValueError:
|
110 |
+
return "Please enter a valid integer"
|
111 |
+
|
112 |
+
# Function to Generate Fake Data
|
113 |
+
def generate_fake_data(count=1, include_profile=False):
|
114 |
+
if count <= 0:
|
115 |
+
return "Please enter a positive number"
|
116 |
+
|
117 |
+
fake = Faker()
|
118 |
+
|
119 |
+
if include_profile:
|
120 |
+
data = [fake.profile() for _ in range(count)]
|
121 |
+
df = pd.DataFrame(data)
|
122 |
+
return df.to_string()
|
123 |
+
else:
|
124 |
+
result = ""
|
125 |
+
for _ in range(count):
|
126 |
+
result += f"Name: {fake.name()}\n"
|
127 |
+
result += f"Address: {fake.address()}\n"
|
128 |
+
result += f"Text: {fake.text()}\n\n"
|
129 |
+
return result
|
130 |
+
|
131 |
+
# Function to Compare Text Similarity
|
132 |
+
def compare_texts(text1, text2):
|
133 |
+
if not text1 or not text2:
|
134 |
+
return "Please enter both texts to compare"
|
135 |
+
|
136 |
+
similarity = SequenceMatcher(None, text1, text2).ratio()
|
137 |
+
return f"The texts are {similarity * 100:.2f}% similar"
|
138 |
+
|
139 |
+
# Create the Gradio interface with tabs
|
140 |
+
with gr.Blocks(title="MultiToolBox") as app:
|
141 |
+
gr.Markdown("# MultiToolBox")
|
142 |
+
gr.Markdown("A versatile utility toolkit with multiple functions")
|
143 |
+
|
144 |
+
with gr.Tabs():
|
145 |
+
# Image Mirroring Tab
|
146 |
+
with gr.Tab("Image Mirror"):
|
147 |
+
gr.Markdown("### Mirror an Image Horizontally")
|
148 |
+
with gr.Row():
|
149 |
+
with gr.Column():
|
150 |
+
img_input = gr.Image(label="Upload Image")
|
151 |
+
mirror_btn = gr.Button("Mirror Image")
|
152 |
+
with gr.Column():
|
153 |
+
img_output = gr.Image(label="Mirrored Image")
|
154 |
+
|
155 |
+
mirror_btn.click(fn=mirror_image, inputs=img_input, outputs=img_output)
|
156 |
+
|
157 |
+
gr.Markdown("""
|
158 |
+
**How to use:**
|
159 |
+
1. Upload an image using the upload button
|
160 |
+
2. Click "Mirror Image" to flip it horizontally
|
161 |
+
3. The result will appear in the right panel
|
162 |
+
""")
|
163 |
+
|
164 |
+
# YouTube to MP3 Tab
|
165 |
+
with gr.Tab("YouTube to MP3"):
|
166 |
+
gr.Markdown("### Download YouTube Videos as MP3")
|
167 |
+
yt_url = gr.Textbox(label="YouTube URL")
|
168 |
+
yt_destination = gr.Textbox(label="Destination Folder (leave empty for current directory)", placeholder=".")
|
169 |
+
yt_download_btn = gr.Button("Download")
|
170 |
+
yt_output = gr.Textbox(label="Result")
|
171 |
+
|
172 |
+
yt_download_btn.click(fn=download_youtube_mp3, inputs=[yt_url, yt_destination], outputs=yt_output)
|
173 |
+
|
174 |
+
gr.Markdown("""
|
175 |
+
**How to use:**
|
176 |
+
1. Enter a valid YouTube URL (e.g., https://www.youtube.com/watch?v=dQw4w9WgXcQ)
|
177 |
+
2. Optionally specify a destination folder
|
178 |
+
3. Click "Download" to convert and save as MP3
|
179 |
+
|
180 |
+
**Example:** https://www.youtube.com/watch?v=dQw4w9WgXcQ
|
181 |
+
""")
|
182 |
+
|
183 |
+
# Emoji Detection Tab
|
184 |
+
with gr.Tab("Emoji Detector"):
|
185 |
+
gr.Markdown("### Detect Emojis in Text")
|
186 |
+
emoji_input = gr.Textbox(label="Enter text with emojis")
|
187 |
+
emoji_detect_btn = gr.Button("Detect Emojis")
|
188 |
+
emoji_output = gr.Textbox(label="Results")
|
189 |
+
|
190 |
+
emoji_detect_btn.click(fn=detect_emojis, inputs=emoji_input, outputs=emoji_output)
|
191 |
+
|
192 |
+
gr.Markdown("""
|
193 |
+
**How to use:**
|
194 |
+
1. Enter text containing emojis
|
195 |
+
2. Click "Detect Emojis" to identify and describe them
|
196 |
+
|
197 |
+
**Example:** "I love reading books 📚❤️🌹"
|
198 |
+
""")
|
199 |
+
|
200 |
+
# Voice Recorder Tab
|
201 |
+
with gr.Tab("Voice Recorder"):
|
202 |
+
gr.Markdown("### Record Audio")
|
203 |
+
rec_seconds = gr.Number(label="Recording Duration (seconds)", value=5)
|
204 |
+
rec_button = gr.Button("Start Recording")
|
205 |
+
rec_output = gr.Textbox(label="Recording Status")
|
206 |
+
audio_output = gr.Audio(label="Recorded Audio")
|
207 |
+
|
208 |
+
rec_button.click(fn=record_voice, inputs=rec_seconds, outputs=[rec_output, audio_output])
|
209 |
+
|
210 |
+
gr.Markdown("""
|
211 |
+
**How to use:**
|
212 |
+
1. Enter the desired recording duration in seconds
|
213 |
+
2. Click "Start Recording"
|
214 |
+
3. Wait for the recording to complete
|
215 |
+
4. Play back the recorded audio using the player
|
216 |
+
|
217 |
+
**Example:** Enter 5 for a 5-second recording
|
218 |
+
""")
|
219 |
+
|
220 |
+
# Spell Correction Tab
|
221 |
+
with gr.Tab("Spell Checker"):
|
222 |
+
gr.Markdown("### Correct Spelling Errors")
|
223 |
+
spell_input = gr.Textbox(label="Enter text with spelling errors")
|
224 |
+
spell_btn = gr.Button("Correct Spelling")
|
225 |
+
spell_output = gr.Textbox(label="Corrected Text")
|
226 |
+
|
227 |
+
spell_btn.click(fn=correct_spelling, inputs=spell_input, outputs=spell_output)
|
228 |
+
|
229 |
+
gr.Markdown("""
|
230 |
+
**How to use:**
|
231 |
+
1. Enter text with spelling mistakes
|
232 |
+
2. Click "Correct Spelling" to fix errors
|
233 |
+
|
234 |
+
**Example:** "I havv a problm with speling"
|
235 |
+
""")
|
236 |
+
|
237 |
+
# Disarium Number Tab
|
238 |
+
with gr.Tab("Disarium Checker"):
|
239 |
+
gr.Markdown("### Check if a Number is a Disarium Number")
|
240 |
+
gr.Markdown("""
|
241 |
+
A Disarium number is a number where the sum of its digits raised to their respective positions equals the number itself.
|
242 |
+
Example: 135 is a Disarium number because 1^1 + 3^2 + 5^3 = 1 + 9 + 125 = 135
|
243 |
+
""")
|
244 |
+
|
245 |
+
disarium_input = gr.Textbox(label="Enter a number")
|
246 |
+
disarium_btn = gr.Button("Check")
|
247 |
+
disarium_output = gr.Textbox(label="Result")
|
248 |
+
|
249 |
+
disarium_btn.click(fn=check_disarium, inputs=disarium_input, outputs=disarium_output)
|
250 |
+
|
251 |
+
gr.Markdown("""
|
252 |
+
**How to use:**
|
253 |
+
1. Enter a positive integer
|
254 |
+
2. Click "Check" to determine if it's a Disarium number
|
255 |
+
|
256 |
+
**Examples:**
|
257 |
+
- 135 (Disarium number)
|
258 |
+
- 89 (Disarium number: 8^1 + 9^2 = 8 + 81 = 89)
|
259 |
+
- 175 (Not a Disarium number)
|
260 |
+
""")
|
261 |
+
|
262 |
+
# Fake Data Generator Tab
|
263 |
+
with gr.Tab("Fake Data Generator"):
|
264 |
+
gr.Markdown("### Generate Fake Data")
|
265 |
+
|
266 |
+
with gr.Row():
|
267 |
+
fake_count = gr.Number(label="Number of entries", value=1, min=1, step=1)
|
268 |
+
fake_profile = gr.Checkbox(label="Generate detailed profiles")
|
269 |
+
|
270 |
+
fake_btn = gr.Button("Generate")
|
271 |
+
fake_output = gr.Textbox(label="Generated Data", lines=10)
|
272 |
+
|
273 |
+
fake_btn.click(fn=generate_fake_data, inputs=[fake_count, fake_profile], outputs=fake_output)
|
274 |
+
|
275 |
+
gr.Markdown("""
|
276 |
+
**How to use:**
|
277 |
+
1. Enter the number of fake data entries to generate
|
278 |
+
2. Choose whether to generate detailed profiles
|
279 |
+
3. Click "Generate" to create fake data
|
280 |
+
|
281 |
+
**Example:** Generate 5 entries with detailed profiles
|
282 |
+
""")
|
283 |
+
|
284 |
+
# Text Similarity Tab
|
285 |
+
with gr.Tab("Text Similarity"):
|
286 |
+
gr.Markdown("### Compare Text Similarity")
|
287 |
+
|
288 |
+
text1_input = gr.Textbox(label="First Text")
|
289 |
+
text2_input = gr.Textbox(label="Second Text")
|
290 |
+
compare_btn = gr.Button("Compare")
|
291 |
+
similarity_output = gr.Textbox(label="Similarity Result")
|
292 |
+
|
293 |
+
compare_btn.click(fn=compare_texts, inputs=[text1_input, text2_input], outputs=similarity_output)
|
294 |
+
|
295 |
+
gr.Markdown("""
|
296 |
+
**How to use:**
|
297 |
+
1. Enter the first text for comparison
|
298 |
+
2. Enter the second text for comparison
|
299 |
+
3. Click "Compare" to calculate similarity percentage
|
300 |
+
|
301 |
+
**Example:**
|
302 |
+
- Text 1: "Hello, how are you today?"
|
303 |
+
- Text 2: "Hello, how are you doing today?"
|
304 |
+
""")
|
305 |
+
|
306 |
+
if __name__ == "__main__":
|
307 |
+
app.launch()
|