renhang commited on
Commit
8e872fa
·
1 Parent(s): bd37c28

update code without examples

Browse files
Files changed (3) hide show
  1. app.py +68 -7
  2. model.py +15 -1
  3. utils.py +1 -1
app.py CHANGED
@@ -92,6 +92,27 @@ def load_example(example_idx, examples):
92
  )
93
  return None, "", 120
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # Load examples at startup
96
  examples = load_examples()
97
 
@@ -105,14 +126,36 @@ with gr.Blocks() as demo:
105
  gr.Markdown("# Jamify: Music Generation from Lyrics and Style")
106
  gr.Markdown("Provide your lyrics, a style reference (either an audio file or a text prompt), and a desired duration to generate a song.")
107
 
 
 
 
108
  # Sample buttons section
109
  if examples:
110
  gr.Markdown("### Sample Examples")
111
  with gr.Row():
112
  example_buttons = []
113
  for i, example in enumerate(examples):
114
- button = gr.Button(f"Example {example['id']}", variant="secondary", size="sm")
 
 
 
 
 
 
 
 
 
 
115
  example_buttons.append(button)
 
 
 
 
 
 
 
 
 
116
 
117
  with gr.Row():
118
  with gr.Column():
@@ -120,10 +163,10 @@ with gr.Blocks() as demo:
120
  lyrics_text = gr.Textbox(
121
  label="Lyrics",
122
  lines=10,
123
- placeholder="Enter lyrics in format: word[start:end] word[start:end]...\nExample: It's[4.96:5.52] a[5.52:5.84] long[5.84:6.16] way[6.16:6.48]...",
124
  value=default_lyrics
125
  )
126
- duration_slider = gr.Slider(minimum=5, maximum=230, value=default_duration, step=30, label="Duration (seconds)")
127
 
128
  with gr.Tab("Style from Audio"):
129
  reference_audio = gr.File(label="Reference Audio (.mp3, .wav)", type="filepath", value=default_audio)
@@ -143,13 +186,31 @@ with gr.Blocks() as demo:
143
  api_name="generate_song"
144
  )
145
 
146
- # Connect example buttons to load data
147
  if examples:
 
 
 
 
 
 
 
 
 
 
 
 
148
  for i, button in enumerate(example_buttons):
149
  button.click(
150
- fn=lambda idx=i: load_example(idx, examples),
151
- outputs=[reference_audio, lyrics_text, duration_slider]
152
  )
 
 
 
 
 
 
153
 
154
  # Create necessary temporary directories for Gradio
155
  print("Creating temporary directories...")
@@ -159,4 +220,4 @@ try:
159
  except Exception as e:
160
  print(f"Warning: Could not create temporary directories: {e}")
161
 
162
- demo.queue().launch()
 
92
  )
93
  return None, "", 120
94
 
95
+ def clear_form():
96
+ """Clear all form inputs to allow user to create their own song"""
97
+ return None, "", 120 # audio, lyrics, duration
98
+
99
+ def update_button_styles(selected_idx, total_examples):
100
+ """Update button styles to highlight the selected example"""
101
+ updates = []
102
+ for i in range(total_examples):
103
+ if i == selected_idx:
104
+ updates.append(gr.update(variant="primary"))
105
+ else:
106
+ updates.append(gr.update(variant="secondary"))
107
+
108
+ # Update "Make Your Own" button
109
+ if selected_idx == -1:
110
+ make_your_own_update = gr.update(variant="primary")
111
+ else:
112
+ make_your_own_update = gr.update(variant="secondary")
113
+
114
+ return updates + [make_your_own_update]
115
+
116
  # Load examples at startup
117
  examples = load_examples()
118
 
 
126
  gr.Markdown("# Jamify: Music Generation from Lyrics and Style")
127
  gr.Markdown("Provide your lyrics, a style reference (either an audio file or a text prompt), and a desired duration to generate a song.")
128
 
129
+ # State to track selected example (-1 means "Make Your Own" is selected, 0 is first example)
130
+ selected_example = gr.State(0 if examples else -1)
131
+
132
  # Sample buttons section
133
  if examples:
134
  gr.Markdown("### Sample Examples")
135
  with gr.Row():
136
  example_buttons = []
137
  for i, example in enumerate(examples):
138
+ # Use consistent button width and truncate long IDs if needed
139
+ button_text = example['id'][:12] + "..." if len(example['id']) > 15 else example['id']
140
+ # First button starts as primary (selected), others as secondary
141
+ initial_variant = "primary" if i == 0 else "secondary"
142
+ button = gr.Button(
143
+ button_text,
144
+ variant=initial_variant,
145
+ size="sm",
146
+ scale=1, # Equal width for all buttons
147
+ min_width=80 # Minimum consistent width
148
+ )
149
  example_buttons.append(button)
150
+
151
+ # Add "Make Your Own" button with same sizing (starts as secondary since first example is selected)
152
+ make_your_own_button = gr.Button(
153
+ "🎵 Make Your Own",
154
+ variant="secondary",
155
+ size="sm",
156
+ scale=1,
157
+ min_width=80
158
+ )
159
 
160
  with gr.Row():
161
  with gr.Column():
 
163
  lyrics_text = gr.Textbox(
164
  label="Lyrics",
165
  lines=10,
166
+ placeholder="Enter lyrics with timestamps: word[start_time:end_time] word[start_time:end_time]...\n\nExample: Hello[0.0:1.2] world[1.5:2.8] this[3.0:3.8] is[4.2:4.6] my[5.0:5.8] song[6.2:7.0]\n\nFormat: Each word followed by [start_seconds:end_seconds] in brackets\nTimestamps should be in seconds with up to 2 decimal places",
167
  value=default_lyrics
168
  )
169
+ duration_slider = gr.Slider(minimum=120, maximum=230, value=default_duration, step=1, label="Duration (seconds)")
170
 
171
  with gr.Tab("Style from Audio"):
172
  reference_audio = gr.File(label="Reference Audio (.mp3, .wav)", type="filepath", value=default_audio)
 
186
  api_name="generate_song"
187
  )
188
 
189
+ # Connect example buttons to load data and update selection
190
  if examples:
191
+ def load_example_and_update_selection(idx):
192
+ """Load example data and update button selection state"""
193
+ audio, lyrics, duration = load_example(idx, examples)
194
+ button_updates = update_button_styles(idx, len(examples))
195
+ return [audio, lyrics, duration, idx] + button_updates
196
+
197
+ def clear_form_and_update_selection():
198
+ """Clear form and update button selection state"""
199
+ audio, lyrics, duration = clear_form()
200
+ button_updates = update_button_styles(-1, len(examples))
201
+ return [audio, lyrics, duration, -1] + button_updates
202
+
203
  for i, button in enumerate(example_buttons):
204
  button.click(
205
+ fn=lambda idx=i: load_example_and_update_selection(idx),
206
+ outputs=[reference_audio, lyrics_text, duration_slider, selected_example] + example_buttons + [make_your_own_button]
207
  )
208
+
209
+ # Connect "Make Your Own" button to clear form and update selection
210
+ make_your_own_button.click(
211
+ fn=clear_form_and_update_selection,
212
+ outputs=[reference_audio, lyrics_text, duration_slider, selected_example] + example_buttons + [make_your_own_button]
213
+ )
214
 
215
  # Create necessary temporary directories for Gradio
216
  print("Creating temporary directories...")
 
220
  except Exception as e:
221
  print(f"Warning: Could not create temporary directories: {e}")
222
 
223
+ demo.queue().launch(share=True)
model.py CHANGED
@@ -173,7 +173,21 @@ class Jamify:
173
  if pred_audio.shape[1] > trim_samples:
174
  pred_audio = pred_audio[:, :trim_samples]
175
 
176
- output_path = "generated_song.mp3"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  print(f"Saving audio to {output_path}")
178
  torchaudio.save(output_path, pred_audio, sample_rate, format="mp3")
179
 
 
173
  if pred_audio.shape[1] > trim_samples:
174
  pred_audio = pred_audio[:, :trim_samples]
175
 
176
+ import time
177
+ import glob
178
+
179
+ # Clean up old generated files (keep only last 5 files)
180
+ old_files = sorted(glob.glob("generated_song_*.mp3"))
181
+ if len(old_files) >= 5:
182
+ for old_file in old_files[:-4]: # Keep last 4, delete older ones
183
+ try:
184
+ os.remove(old_file)
185
+ print(f"Cleaned up old file: {old_file}")
186
+ except OSError:
187
+ pass
188
+
189
+ timestamp = int(time.time() * 1000) # Use milliseconds for uniqueness
190
+ output_path = f"generated_song_{timestamp}.mp3"
191
  print(f"Saving audio to {output_path}")
192
  torchaudio.save(output_path, pred_audio, sample_rate, format="mp3")
193
 
utils.py CHANGED
@@ -121,7 +121,7 @@ def json_to_text(json_data: dict) -> str:
121
  words = json_data['word']
122
 
123
  # Group words into segments using the existing regroup_words function
124
- segments = regroup_words(words, max_len=15.0, gap=0.50)
125
 
126
  # Convert each segment to text format
127
  segment_lines = []
 
121
  words = json_data['word']
122
 
123
  # Group words into segments using the existing regroup_words function
124
+ segments = regroup_words(words, max_len=5, gap=0.50)
125
 
126
  # Convert each segment to text format
127
  segment_lines = []