baouws commited on
Commit
b5697db
·
verified ·
1 Parent(s): 516a0a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -5
app.py CHANGED
@@ -417,14 +417,64 @@ await initHydra({{feedStrudel:5}})
417
  return complete_code
418
 
419
  # Main generation function
420
- def generate_interface(prompt, genre, complexity, include_visuals, visual_style, use_ai):
421
  """Main interface function that generates working code"""
422
 
423
  if not prompt.strip():
424
  return "Please enter a description of the music you want to create."
425
 
426
- # Always use curated pattern generation (removed AI for simplicity and speed)
427
- strudel_code = generate_working_strudel_code(prompt, genre, complexity)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
428
 
429
  # Create complete working template with random visuals based on genre
430
  complete_code = create_complete_strudel_code(strudel_code, include_visuals, genre)
@@ -473,6 +523,11 @@ with gr.Blocks(title="Working Strudel Generator", theme=gr.themes.Soft()) as app
473
  label="🎨 Include visuals",
474
  value=True
475
  )
 
 
 
 
 
476
 
477
  generate_btn = gr.Button("Generate Code", variant="primary", size="lg")
478
 
@@ -522,8 +577,7 @@ with gr.Blocks(title="Working Strudel Generator", theme=gr.themes.Soft()) as app
522
  genre_dropdown,
523
  complexity_dropdown,
524
  include_visuals,
525
- gr.State("random"), # dummy visual_style
526
- gr.State(False) # dummy use_ai
527
  ],
528
  outputs=output_code
529
  )
 
417
  return complete_code
418
 
419
  # Main generation function
420
+ def generate_interface(prompt, genre, complexity, include_visuals, use_ai):
421
  """Main interface function that generates working code"""
422
 
423
  if not prompt.strip():
424
  return "Please enter a description of the music you want to create."
425
 
426
+ if use_ai:
427
+ # Use AI generation with better examples
428
+ system_prompt = f"""Generate working Strudel live coding pattern for: {prompt}
429
+ Genre: {genre}, Complexity: {complexity}
430
+
431
+ Example Strudel patterns:
432
+ s("bd*4, hh*8").gain(0.8)
433
+ stack(s("bd ~ sn ~"), n("0 2 4").s("sine").octave(3).gain(0.7))
434
+ n("[0 3 5 7]*2").s("sawtooth").octave(2).lpf(1200).gain(0.8).scale("a:minor")
435
+
436
+ Generate a {complexity} {genre} pattern:"""
437
+
438
+ try:
439
+ outputs = code_generator(
440
+ system_prompt,
441
+ max_length=len(system_prompt.split()) + 80,
442
+ temperature=0.8,
443
+ do_sample=True,
444
+ top_p=0.9,
445
+ num_return_sequences=1
446
+ )
447
+
448
+ generated_text = outputs[0]['generated_text']
449
+ strudel_code = generated_text[len(system_prompt):].strip()
450
+
451
+ # Clean the AI output more thoroughly
452
+ lines = strudel_code.split('\n')
453
+ clean_lines = []
454
+ for line in lines[:8]: # Limit to 8 lines
455
+ line = line.strip()
456
+ # More robust pattern matching for Strudel code
457
+ if line and (line.startswith('s(') or line.startswith('n(') or line.startswith('stack(') or
458
+ '.gain(' in line or '.s(' in line or '.octave(' in line or '.scale(' in line or
459
+ line.endswith(')') or line.endswith(',') or '.lpf(' in line):
460
+ clean_lines.append(line)
461
+ # Stop at obvious non-Strudel patterns
462
+ elif any(stop_word in line.lower() for stop_word in ['function', 'var ', 'let ', 'const ', 'import', '//']):
463
+ break
464
+
465
+ if clean_lines:
466
+ strudel_code = '\n'.join(clean_lines)
467
+ strudel_code = f"// AI Generated: {prompt}\n{strudel_code}"
468
+ else:
469
+ # Fallback to curated patterns if AI fails
470
+ strudel_code = generate_working_strudel_code(prompt, genre, complexity)
471
+ except Exception as e:
472
+ # Fallback to curated patterns if AI fails
473
+ print(f"AI generation failed: {e}")
474
+ strudel_code = generate_working_strudel_code(prompt, genre, complexity)
475
+ else:
476
+ # Use curated pattern generation - ALWAYS RANDOM
477
+ strudel_code = generate_working_strudel_code(prompt, genre, complexity)
478
 
479
  # Create complete working template with random visuals based on genre
480
  complete_code = create_complete_strudel_code(strudel_code, include_visuals, genre)
 
523
  label="🎨 Include visuals",
524
  value=True
525
  )
526
+
527
+ use_ai = gr.Checkbox(
528
+ label="🤖 Use AI (slower)",
529
+ value=False
530
+ )
531
 
532
  generate_btn = gr.Button("Generate Code", variant="primary", size="lg")
533
 
 
577
  genre_dropdown,
578
  complexity_dropdown,
579
  include_visuals,
580
+ use_ai
 
581
  ],
582
  outputs=output_code
583
  )