MySafeCode commited on
Commit
f949695
·
verified ·
1 Parent(s): b6d195d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -62
app.py CHANGED
@@ -11,7 +11,7 @@ if not SUNO_KEY:
11
  print("⚠️ SunoKey not set!")
12
 
13
  def generate_lyrics(prompt):
14
- """Final working lyrics generator"""
15
  if not SUNO_KEY:
16
  yield "❌ Error: SunoKey not configured in environment variables"
17
  return
@@ -89,23 +89,19 @@ def generate_lyrics(prompt):
89
  output += "\n```\n"
90
  output += "---\n\n"
91
 
92
- output += f"⏱️ Generated in about {(attempt + 1) * 5} seconds\n\n"
93
- output += "📝 **Now you can:**\n"
94
- output += "1. Select a variant below\n"
95
- output += "2. Edit the text if needed\n"
96
- output += "3. Generate a song!"
97
 
98
- # Prepare data for state
99
  lyrics_data = {
100
  "variants": lyrics_list,
101
  "original_prompt": prompt
102
  }
103
 
104
- # Update dropdown with variants
105
  variant_choices = [f"{i+1}: {lyric.get('title', f'Variant {i+1}')}"
106
  for i, lyric in enumerate(lyrics_list)]
107
 
108
- # Get first variant text for editing
109
  first_text = lyrics_list[0].get('text', '') if lyrics_list else ""
110
 
111
  yield output, lyrics_data, variant_choices, first_text
@@ -133,10 +129,10 @@ def generate_lyrics(prompt):
133
  yield "⏰ Timeout after 150 seconds. Try checking again later.", None, [], ""
134
 
135
  except Exception as e:
136
- yield f"❌ Error: {str(e)}", None, [], []
137
 
138
  def generate_song_from_text(lyrics_text, style, title, instrumental, model, custom_mode):
139
- """Generate a song from text"""
140
  if not SUNO_KEY:
141
  return "❌ Error: SunoKey not configured in environment variables"
142
 
@@ -255,11 +251,11 @@ def generate_song_from_text(lyrics_text, style, title, instrumental, model, cust
255
  except Exception as e:
256
  return f"❌ Error: {str(e)}"
257
 
 
258
  def update_text_from_variant(lyrics_data, variant_choice):
259
- """Update the text area when a variant is selected"""
260
  if lyrics_data and lyrics_data.get("variants") and variant_choice:
261
  try:
262
- # Extract variant index from choice (e.g., "1: Title" -> 0)
263
  variant_idx = int(variant_choice.split(":")[0].strip()) - 1
264
  variants = lyrics_data["variants"]
265
 
@@ -271,11 +267,10 @@ def update_text_from_variant(lyrics_data, variant_choice):
271
  return ""
272
 
273
  def download_text_file(text):
274
- """Create a downloadable text file"""
275
  if not text.strip():
276
  return None
277
 
278
- # Create a temporary file
279
  with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
280
  f.write(text)
281
  temp_path = f.name
@@ -304,47 +299,45 @@ with gr.Blocks(title="Suno Lyrics Generator", theme="soft") as app:
304
  gr.Markdown("# 🎵 Suno Lyrics Generator")
305
  gr.Markdown("Generate song lyrics and turn them into full songs using Suno AI")
306
 
307
- # Store lyrics data between steps
308
  lyrics_state = gr.State()
309
 
310
  with gr.Row():
311
  with gr.Column(scale=1):
312
- # Lyrics Generation Section
313
- gr.Markdown("### Step 1: Generate or Upload Lyrics")
314
 
315
- with gr.Tab("Generate Lyrics"):
316
- prompt = gr.Textbox(
317
- label="Lyrics Prompt",
318
- placeholder="Example: A happy song about sunshine and rainbows",
319
- lines=3
320
- )
321
- lyrics_btn = gr.Button("🎵 Generate Lyrics", variant="primary")
 
 
322
 
323
- with gr.Tab("Upload Lyrics"):
324
  file_upload = gr.File(
325
- label="Upload Text File",
326
  file_types=[".txt"],
327
  type="filepath"
328
  )
329
- upload_btn = gr.Button("📁 Load from File", variant="secondary")
330
- upload_text = gr.Textbox(
331
- label="Uploaded Text",
332
- lines=10,
333
- interactive=False
 
 
334
  )
335
 
336
- # Lyrics Selection & Editing Section
337
- gr.Markdown("### Step 2: Select & Edit Lyrics")
338
-
339
- variant_choice = gr.Dropdown(
340
- label="Select Lyrics Variant",
341
- choices=[],
342
- interactive=True
343
- )
344
 
345
  lyrics_text = gr.Textbox(
346
- label="Lyrics Text (Editable)",
347
- placeholder="Your lyrics will appear here...",
348
  lines=15,
349
  interactive=True
350
  )
@@ -353,33 +346,33 @@ with gr.Blocks(title="Suno Lyrics Generator", theme="soft") as app:
353
  download_btn = gr.Button("💾 Download Text", variant="secondary")
354
  clear_btn = gr.Button("🗑️ Clear", variant="secondary")
355
 
356
- # Song Generation Section
357
- gr.Markdown("### Step 3: Create Song")
358
 
359
  with gr.Row():
360
  custom_mode = gr.Checkbox(
361
  label="Custom Mode",
362
  value=False,
363
- interactive=True
364
  )
365
  instrumental = gr.Checkbox(
366
  label="Instrumental",
367
  value=False,
368
- interactive=True
369
  )
370
 
371
  with gr.Row():
372
  style = gr.Textbox(
373
  label="Music Style",
374
- placeholder="Pop, Rock, Jazz, Classical",
375
- interactive=True,
376
- value="Pop"
377
  )
378
  title = gr.Textbox(
379
  label="Song Title",
380
- placeholder="My Awesome Song",
381
- interactive=True,
382
- value="Generated Song"
383
  )
384
 
385
  model = gr.Dropdown(
@@ -390,14 +383,29 @@ with gr.Blocks(title="Suno Lyrics Generator", theme="soft") as app:
390
  )
391
 
392
  song_btn = gr.Button("🎶 Generate Song", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
393
 
394
  with gr.Column(scale=2):
 
395
  output = gr.Markdown(
396
  label="Results",
397
  value="Your generated lyrics and songs will appear here..."
398
  )
399
 
400
- # Hidden file download component
401
  file_output = gr.File(label="Download Lyrics", visible=False)
402
 
403
  gr.Markdown("---")
@@ -415,47 +423,47 @@ with gr.Blocks(title="Suno Lyrics Generator", theme="soft") as app:
415
 
416
  # Event handlers
417
 
418
- # Generate lyrics from prompt
419
  lyrics_btn.click(
420
  generate_lyrics,
421
  inputs=prompt,
422
  outputs=[output, lyrics_state, variant_choice, lyrics_text]
423
  )
424
 
425
- # Upload text from file
426
  upload_btn.click(
427
  upload_text_file,
428
  inputs=file_upload,
429
  outputs=lyrics_text
430
  ).then(
431
- lambda: "📁 **Text loaded from file!**\n\nYou can now edit the text above and generate a song.",
432
  outputs=output
433
  )
434
 
435
- # Update text when variant is selected
436
  variant_choice.change(
437
  update_text_from_variant,
438
  inputs=[lyrics_state, variant_choice],
439
  outputs=lyrics_text
440
  )
441
 
442
- # Download text as file
443
  download_btn.click(
444
  download_text_file,
445
  inputs=lyrics_text,
446
  outputs=file_output
447
  ).then(
448
- lambda: "💾 **Text ready for download!**\n\nCheck the download button below.",
449
  outputs=output
450
  )
451
 
452
- # Clear text
453
  clear_btn.click(
454
- lambda: ("", ""),
455
  outputs=[lyrics_text, output]
456
  )
457
 
458
- # Generate song from text
459
  song_btn.click(
460
  generate_song_from_text,
461
  inputs=[lyrics_text, style, title, instrumental, model, custom_mode],
 
11
  print("⚠️ SunoKey not set!")
12
 
13
  def generate_lyrics(prompt):
14
+ """Final working lyrics generator - UNCHANGED"""
15
  if not SUNO_KEY:
16
  yield "❌ Error: SunoKey not configured in environment variables"
17
  return
 
89
  output += "\n```\n"
90
  output += "---\n\n"
91
 
92
+ output += f"⏱️ Generated in about {(attempt + 1) * 5} seconds"
 
 
 
 
93
 
94
+ # NEW: Store lyrics for later use
95
  lyrics_data = {
96
  "variants": lyrics_list,
97
  "original_prompt": prompt
98
  }
99
 
100
+ # NEW: Prepare variant choices for dropdown
101
  variant_choices = [f"{i+1}: {lyric.get('title', f'Variant {i+1}')}"
102
  for i, lyric in enumerate(lyrics_list)]
103
 
104
+ # NEW: Get first variant text
105
  first_text = lyrics_list[0].get('text', '') if lyrics_list else ""
106
 
107
  yield output, lyrics_data, variant_choices, first_text
 
129
  yield "⏰ Timeout after 150 seconds. Try checking again later.", None, [], ""
130
 
131
  except Exception as e:
132
+ yield f"❌ Error: {str(e)}", None, [], ""
133
 
134
  def generate_song_from_text(lyrics_text, style, title, instrumental, model, custom_mode):
135
+ """NEW: Generate a song from lyrics text"""
136
  if not SUNO_KEY:
137
  return "❌ Error: SunoKey not configured in environment variables"
138
 
 
251
  except Exception as e:
252
  return f"❌ Error: {str(e)}"
253
 
254
+ # NEW: Helper functions for text handling
255
  def update_text_from_variant(lyrics_data, variant_choice):
256
+ """Update text area when variant is selected"""
257
  if lyrics_data and lyrics_data.get("variants") and variant_choice:
258
  try:
 
259
  variant_idx = int(variant_choice.split(":")[0].strip()) - 1
260
  variants = lyrics_data["variants"]
261
 
 
267
  return ""
268
 
269
  def download_text_file(text):
270
+ """Create downloadable text file"""
271
  if not text.strip():
272
  return None
273
 
 
274
  with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f:
275
  f.write(text)
276
  temp_path = f.name
 
299
  gr.Markdown("# 🎵 Suno Lyrics Generator")
300
  gr.Markdown("Generate song lyrics and turn them into full songs using Suno AI")
301
 
302
+ # Store lyrics data
303
  lyrics_state = gr.State()
304
 
305
  with gr.Row():
306
  with gr.Column(scale=1):
307
+ # Step 1: Generate Lyrics (original layout preserved)
308
+ gr.Markdown("### Step 1: Generate Lyrics")
309
 
310
+ prompt = gr.Textbox(
311
+ label="Lyrics Prompt",
312
+ placeholder="Example: A happy song about sunshine and rainbows",
313
+ lines=3
314
+ )
315
+ lyrics_btn = gr.Button("🎵 Generate Lyrics", variant="primary")
316
+
317
+ # NEW: Text Management Section
318
+ gr.Markdown("### Text Management")
319
 
320
+ with gr.Tab("Upload Text"):
321
  file_upload = gr.File(
322
+ label="Upload Text File (.txt)",
323
  file_types=[".txt"],
324
  type="filepath"
325
  )
326
+ upload_btn = gr.Button("📁 Load Text", variant="secondary")
327
+
328
+ with gr.Tab("Select from Generated"):
329
+ variant_choice = gr.Dropdown(
330
+ label="Select Generated Variant",
331
+ choices=[],
332
+ interactive=True
333
  )
334
 
335
+ # Step 2: Edit Lyrics
336
+ gr.Markdown("### Step 2: Edit Lyrics (Optional)")
 
 
 
 
 
 
337
 
338
  lyrics_text = gr.Textbox(
339
+ label="Lyrics Text",
340
+ placeholder="Your lyrics will appear here. You can edit them or paste your own.",
341
  lines=15,
342
  interactive=True
343
  )
 
346
  download_btn = gr.Button("💾 Download Text", variant="secondary")
347
  clear_btn = gr.Button("🗑️ Clear", variant="secondary")
348
 
349
+ # Step 3: Generate Song (NEW)
350
+ gr.Markdown("### Step 3: Generate Song")
351
 
352
  with gr.Row():
353
  custom_mode = gr.Checkbox(
354
  label="Custom Mode",
355
  value=False,
356
+ info="Enable for advanced settings"
357
  )
358
  instrumental = gr.Checkbox(
359
  label="Instrumental",
360
  value=False,
361
+ info="No vocals"
362
  )
363
 
364
  with gr.Row():
365
  style = gr.Textbox(
366
  label="Music Style",
367
+ placeholder="Pop, Rock, Jazz",
368
+ value="Pop",
369
+ interactive=True
370
  )
371
  title = gr.Textbox(
372
  label="Song Title",
373
+ placeholder="My Song",
374
+ value="Generated Song",
375
+ interactive=True
376
  )
377
 
378
  model = gr.Dropdown(
 
383
  )
384
 
385
  song_btn = gr.Button("🎶 Generate Song", variant="primary")
386
+
387
+ # Instructions
388
+ gr.Markdown("""
389
+ **How it works:**
390
+ 1. Generate lyrics or upload your own
391
+ 2. Edit the text if needed
392
+ 3. Set song parameters
393
+ 4. Generate music!
394
+
395
+ **Song Generation:**
396
+ - Custom Mode: Requires style & title
397
+ - Non-custom: Simple mode with prompt only
398
+ - Instrumental: Music only, no singing
399
+ """)
400
 
401
  with gr.Column(scale=2):
402
+ # Main output (unchanged)
403
  output = gr.Markdown(
404
  label="Results",
405
  value="Your generated lyrics and songs will appear here..."
406
  )
407
 
408
+ # NEW: Hidden download component
409
  file_output = gr.File(label="Download Lyrics", visible=False)
410
 
411
  gr.Markdown("---")
 
423
 
424
  # Event handlers
425
 
426
+ # Original lyrics generation (unchanged except outputs)
427
  lyrics_btn.click(
428
  generate_lyrics,
429
  inputs=prompt,
430
  outputs=[output, lyrics_state, variant_choice, lyrics_text]
431
  )
432
 
433
+ # NEW: Upload text
434
  upload_btn.click(
435
  upload_text_file,
436
  inputs=file_upload,
437
  outputs=lyrics_text
438
  ).then(
439
+ lambda: "📁 **Text loaded from file!**\n\nYou can now edit the text and generate a song.",
440
  outputs=output
441
  )
442
 
443
+ # NEW: Update text when variant selected
444
  variant_choice.change(
445
  update_text_from_variant,
446
  inputs=[lyrics_state, variant_choice],
447
  outputs=lyrics_text
448
  )
449
 
450
+ # NEW: Download text
451
  download_btn.click(
452
  download_text_file,
453
  inputs=lyrics_text,
454
  outputs=file_output
455
  ).then(
456
+ lambda: "💾 **Text ready for download!**",
457
  outputs=output
458
  )
459
 
460
+ # NEW: Clear text
461
  clear_btn.click(
462
+ lambda: ("", "Text cleared."),
463
  outputs=[lyrics_text, output]
464
  )
465
 
466
+ # NEW: Generate song
467
  song_btn.click(
468
  generate_song_from_text,
469
  inputs=[lyrics_text, style, title, instrumental, model, custom_mode],