sudo-soldier commited on
Commit
5ba25ef
·
verified ·
1 Parent(s): 1344028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -48
app.py CHANGED
@@ -2,21 +2,7 @@ import gradio as gr
2
  import yt_dlp
3
  import os
4
  from pydub import AudioSegment
5
- from redis import Redis
6
  import re
7
- import requests
8
-
9
- # Redis configuration
10
- redis = Redis(
11
- host="amused-walleye-31373.upstash.io",
12
- port=31373,
13
- password="67212443600c45ffa962e19ea9202605",
14
- ssl=True,
15
- db=0
16
- )
17
-
18
- # Cloudflare Worker base URL
19
- worker_base_url = "https://server.jessejesse.workers.dev"
20
 
21
  # Create downloads directory if it doesn't exist
22
  os.makedirs("downloads", exist_ok=True)
@@ -25,33 +11,6 @@ def sanitize_filename(filename):
25
  """Sanitize the filename by removing or replacing special characters."""
26
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
27
 
28
- def save_ringtone_to_worker(song_name, file_path):
29
- """Upload the ringtone to Cloudflare Worker storage (R2 or KV) and save its URL in Redis."""
30
- try:
31
- sanitized_song_name = sanitize_filename(song_name)
32
-
33
- with open(file_path, 'rb') as file:
34
- file_data = file.read()
35
-
36
- response = requests.put(
37
- f"{worker_base_url}/{sanitized_song_name}",
38
- data=file_data,
39
- headers={"Content-Type": "audio/mpeg"}
40
- )
41
-
42
- if response.status_code == 200:
43
- file_url = f"{worker_base_url}/{sanitized_song_name}"
44
- redis.set(sanitized_song_name, file_url) # Save the URL to Redis
45
- print(f"Saved {sanitized_song_name} URL to Redis: {file_url}")
46
- return file_url # Return the URL for later use
47
- else:
48
- print(f"Failed to upload file. Status code: {response.status_code}")
49
- return None
50
-
51
- except Exception as e:
52
- print(f"Error uploading to Worker storage: {e}")
53
- return None
54
-
55
  def process_youtube_url(url, uploaded_file):
56
  """Process the YouTube URL or uploaded file to create ringtones."""
57
  try:
@@ -92,15 +51,12 @@ def process_youtube_url(url, uploaded_file):
92
  ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
93
  ringtone_audio.export(ringtone_filename_m4r, format="mp4")
94
 
95
- # Upload both files to Worker and get URLs
96
- mp3_url = save_ringtone_to_worker(f"{song_name}.mp3", mp3_filename)
97
- m4r_url = save_ringtone_to_worker(f"{song_name}.m4r", ringtone_filename_m4r)
98
-
99
- return mp3_url, m4r_url
100
 
101
  except Exception as e:
102
  print(f"Error: {e}")
103
- return None, None
104
 
105
  # Gradio UI
106
  with gr.Blocks() as interface:
@@ -114,11 +70,12 @@ with gr.Blocks() as interface:
114
  mp3_upload = gr.File(label="Upload MP3 (Optional)")
115
 
116
  with gr.Row():
 
117
  mp3_download = gr.File(label="Download Android Ringtone")
118
  iphone_ringtone = gr.File(label="Download iPhone Ringtone")
119
 
120
  process_button = gr.Button("Create Ringtones")
121
- process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[mp3_download, iphone_ringtone])
122
 
123
  interface.launch(share=True)
124
 
@@ -136,3 +93,4 @@ interface.launch(share=True)
136
 
137
 
138
 
 
 
2
  import yt_dlp
3
  import os
4
  from pydub import AudioSegment
 
5
  import re
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  # Create downloads directory if it doesn't exist
8
  os.makedirs("downloads", exist_ok=True)
 
11
  """Sanitize the filename by removing or replacing special characters."""
12
  return re.sub(r'[^a-zA-Z0-9_-]', '_', filename)
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  def process_youtube_url(url, uploaded_file):
15
  """Process the YouTube URL or uploaded file to create ringtones."""
16
  try:
 
51
  ringtone_audio = AudioSegment.from_file(mp3_filename)[:20000] # 20 seconds
52
  ringtone_audio.export(ringtone_filename_m4r, format="mp4")
53
 
54
+ # Return file paths and song name for download
55
+ return song_name, mp3_filename, ringtone_filename_m4r
 
 
 
56
 
57
  except Exception as e:
58
  print(f"Error: {e}")
59
+ return None, None, None
60
 
61
  # Gradio UI
62
  with gr.Blocks() as interface:
 
70
  mp3_upload = gr.File(label="Upload MP3 (Optional)")
71
 
72
  with gr.Row():
73
+ song_name_output = gr.Textbox(label="Song Name", interactive=False)
74
  mp3_download = gr.File(label="Download Android Ringtone")
75
  iphone_ringtone = gr.File(label="Download iPhone Ringtone")
76
 
77
  process_button = gr.Button("Create Ringtones")
78
+ process_button.click(process_youtube_url, inputs=[youtube_url, mp3_upload], outputs=[song_name_output, mp3_download, iphone_ringtone])
79
 
80
  interface.launch(share=True)
81
 
 
93
 
94
 
95
 
96
+