MySafeCode commited on
Commit
552d4af
·
verified ·
1 Parent(s): 7474488

Update app2.py

Browse files
Files changed (1) hide show
  1. app2.py +11 -23
app2.py CHANGED
@@ -2,11 +2,8 @@ import gradio as gr
2
  from yt_dlp import YoutubeDL
3
  import os
4
  from pydub import AudioSegment
5
- import tempfile
6
- import json
7
 
8
- # Use temp directory for better security
9
- DOWNLOADS_FOLDER = tempfile.mkdtemp(prefix="audio_downloader_")
10
  os.makedirs(DOWNLOADS_FOLDER, exist_ok=True)
11
 
12
  def download_youtube_audio(url, file_format, duration_sec):
@@ -74,11 +71,7 @@ def download_youtube_audio(url, file_format, duration_sec):
74
  if output_file != original_file and os.path.exists(original_file):
75
  os.remove(original_file)
76
 
77
- # For Gradio 6+, return a dictionary for gr.File component
78
- return {
79
- "path": output_file,
80
- "name": os.path.basename(output_file)
81
- }, f"✅ Downloaded: {os.path.basename(output_file)}"
82
 
83
  except Exception as e:
84
  return None, f"❌ Error: {str(e)}"
@@ -145,17 +138,13 @@ def download_soundcloud_audio(url, file_format, duration_sec):
145
  if output_file != original_file and os.path.exists(original_file):
146
  os.remove(original_file)
147
 
148
- # For Gradio 6+, return a dictionary for gr.File component
149
- return {
150
- "path": output_file,
151
- "name": os.path.basename(output_file)
152
- }, f"✅ Downloaded: {os.path.basename(output_file)}"
153
 
154
  except Exception as e:
155
  return None, f"❌ Error: {str(e)}"
156
 
157
- # Create Gradio interface
158
- with gr.Blocks(title="Audio Downloader") as iface: # Removed theme from here
159
 
160
  gr.Markdown("# 🎵 Audio Downloader")
161
  gr.Markdown("Download audio from YouTube or SoundCloud")
@@ -200,8 +189,7 @@ with gr.Blocks(title="Audio Downloader") as iface: # Removed theme from here
200
 
201
  youtube_output = gr.File(
202
  label="Downloaded File",
203
- interactive=False,
204
- file_count="single" # Explicitly specify file count
205
  )
206
 
207
  # YouTube examples
@@ -256,8 +244,7 @@ with gr.Blocks(title="Audio Downloader") as iface: # Removed theme from here
256
 
257
  soundcloud_output = gr.File(
258
  label="Downloaded File",
259
- interactive=False,
260
- file_count="single" # Explicitly specify file count
261
  )
262
 
263
  # SoundCloud examples
@@ -290,7 +277,7 @@ with gr.Blocks(title="Audio Downloader") as iface: # Removed theme from here
290
  - **WAV**: Lossless, large file size
291
 
292
  ### Notes:
293
- - Files are saved in a temporary folder
294
  - Some content may have download restrictions
295
  - Long videos may take time to process
296
  """)
@@ -308,12 +295,13 @@ with gr.Blocks(title="Audio Downloader") as iface: # Removed theme from here
308
  outputs=[soundcloud_output, soundcloud_status]
309
  )
310
 
311
- # Launch the app
312
  if __name__ == "__main__":
313
  iface.launch(
314
  server_name="127.0.0.1",
315
  server_port=7860,
316
  show_error=True,
317
  share=False,
318
- theme=gr.themes.Soft() # Moved theme to launch() method
 
319
  )
 
2
  from yt_dlp import YoutubeDL
3
  import os
4
  from pydub import AudioSegment
 
 
5
 
6
+ DOWNLOADS_FOLDER = "downloads"
 
7
  os.makedirs(DOWNLOADS_FOLDER, exist_ok=True)
8
 
9
  def download_youtube_audio(url, file_format, duration_sec):
 
71
  if output_file != original_file and os.path.exists(original_file):
72
  os.remove(original_file)
73
 
74
+ return output_file, f"✅ Downloaded: {os.path.basename(output_file)}"
 
 
 
 
75
 
76
  except Exception as e:
77
  return None, f"❌ Error: {str(e)}"
 
138
  if output_file != original_file and os.path.exists(original_file):
139
  os.remove(original_file)
140
 
141
+ return output_file, f"✅ Downloaded: {os.path.basename(output_file)}"
 
 
 
 
142
 
143
  except Exception as e:
144
  return None, f"❌ Error: {str(e)}"
145
 
146
+ # Create Gradio interface - REMOVE theme parameter from here
147
+ with gr.Blocks(title="Audio Downloader") as iface:
148
 
149
  gr.Markdown("# 🎵 Audio Downloader")
150
  gr.Markdown("Download audio from YouTube or SoundCloud")
 
189
 
190
  youtube_output = gr.File(
191
  label="Downloaded File",
192
+ interactive=False
 
193
  )
194
 
195
  # YouTube examples
 
244
 
245
  soundcloud_output = gr.File(
246
  label="Downloaded File",
247
+ interactive=False
 
248
  )
249
 
250
  # SoundCloud examples
 
277
  - **WAV**: Lossless, large file size
278
 
279
  ### Notes:
280
+ - Files are saved in the 'downloads' folder
281
  - Some content may have download restrictions
282
  - Long videos may take time to process
283
  """)
 
295
  outputs=[soundcloud_output, soundcloud_status]
296
  )
297
 
298
+ # Launch the app - MOVE theme parameter to here
299
  if __name__ == "__main__":
300
  iface.launch(
301
  server_name="127.0.0.1",
302
  server_port=7860,
303
  show_error=True,
304
  share=False,
305
+ theme=gr.themes.Soft(),
306
+ ssr_mode=False # Disable SSR to avoid warning
307
  )