Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,31 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
import torch
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
MODEL_NAME = "
|
| 9 |
-
|
| 10 |
-
model
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Strudel code examples for few-shot prompting
|
| 18 |
STRUDEL_EXAMPLES = """
|
|
@@ -33,40 +46,35 @@ stack(
|
|
| 33 |
n("[0 2 4]/3").s("sine").octave(3).slow(4).room(0.9).gain(0.5)
|
| 34 |
"""
|
| 35 |
|
| 36 |
-
def generate_strudel_code(prompt, genre="general", complexity="simple", max_length=
|
| 37 |
-
"""Generate Strudel code using
|
| 38 |
|
| 39 |
# Build context-aware prompt
|
| 40 |
-
system_prompt = f"""// Strudel
|
| 41 |
-
// Generate {complexity} {genre} music
|
| 42 |
// Examples:
|
| 43 |
{STRUDEL_EXAMPLES}
|
| 44 |
|
| 45 |
-
//
|
| 46 |
-
//
|
| 47 |
"""
|
| 48 |
|
| 49 |
try:
|
| 50 |
-
#
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
outputs = model.generate(
|
| 56 |
-
inputs,
|
| 57 |
-
max_length=len(inputs[0]) + max_length,
|
| 58 |
-
temperature=temperature,
|
| 59 |
-
do_sample=True,
|
| 60 |
-
top_p=0.95,
|
| 61 |
-
pad_token_id=tokenizer.eos_token_id,
|
| 62 |
-
stop_strings=["//", "\n\n"],
|
| 63 |
-
num_return_sequences=1
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
# Decode output
|
| 67 |
-
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 68 |
|
| 69 |
-
# Extract only the generated part
|
| 70 |
strudel_code = generated_text[len(system_prompt):].strip()
|
| 71 |
|
| 72 |
# Clean up the code
|
|
@@ -75,7 +83,7 @@ def generate_strudel_code(prompt, genre="general", complexity="simple", max_leng
|
|
| 75 |
return strudel_code
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
-
return f"Error generating code: {str(e)}"
|
| 79 |
|
| 80 |
def clean_strudel_code(code):
|
| 81 |
"""Clean and format generated Strudel code"""
|
|
@@ -84,33 +92,56 @@ def clean_strudel_code(code):
|
|
| 84 |
|
| 85 |
for line in lines:
|
| 86 |
line = line.strip()
|
| 87 |
-
# Stop at
|
| 88 |
-
if line.startswith('//') and any(word in line.lower() for word in ['user', 'request', 'example']):
|
| 89 |
break
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
| 91 |
cleaned_lines.append(line)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
def create_full_strudel_template(generated_code, include_visuals=True):
|
| 96 |
"""Wrap generated code in a complete Strudel template"""
|
| 97 |
|
| 98 |
-
visual_code = """
|
| 99 |
-
// Hydra visuals
|
| 100 |
await initHydra({feedStrudel:5})
|
| 101 |
|
| 102 |
osc(10, 0.1, 0.8)
|
| 103 |
.kaleid(4)
|
| 104 |
.color(1.5, 0.8, 1.2)
|
| 105 |
.out()
|
|
|
|
| 106 |
""" if include_visuals else ""
|
| 107 |
|
| 108 |
-
template = f"""{visual_code}
|
| 109 |
-
// Generated Strudel Music Code
|
| 110 |
{generated_code}
|
| 111 |
|
| 112 |
-
// Global effects
|
| 113 |
-
all(x => x.fft(5).scope())
|
| 114 |
"""
|
| 115 |
|
| 116 |
return template
|
|
@@ -137,17 +168,19 @@ def generate_interface(prompt, genre, complexity, include_visuals, max_length, t
|
|
| 137 |
return full_code
|
| 138 |
|
| 139 |
# Create Gradio app
|
| 140 |
-
with gr.Blocks(title="Strudel Code Generator", theme=gr.themes.
|
| 141 |
gr.Markdown("""
|
| 142 |
-
# 🎵 Strudel Code Generator
|
| 143 |
|
| 144 |
-
Generate live coding music patterns using AI! Powered by
|
| 145 |
|
| 146 |
**How to use:**
|
| 147 |
-
1. Describe the music you want (e.g., "techno beat with bass", "ambient soundscape"
|
| 148 |
2. Choose genre and complexity
|
| 149 |
3. Click Generate
|
| 150 |
4. Copy the code to [strudel.cc](https://strudel.cc) to hear it!
|
|
|
|
|
|
|
| 151 |
""")
|
| 152 |
|
| 153 |
with gr.Row():
|
|
@@ -161,7 +194,7 @@ with gr.Blocks(title="Strudel Code Generator", theme=gr.themes.Monochrome()) as
|
|
| 161 |
with gr.Row():
|
| 162 |
genre_dropdown = gr.Dropdown(
|
| 163 |
choices=["general", "techno", "house", "ambient", "jazz", "rock", "experimental"],
|
| 164 |
-
value="
|
| 165 |
label="Genre"
|
| 166 |
)
|
| 167 |
|
|
@@ -179,49 +212,51 @@ with gr.Blocks(title="Strudel Code Generator", theme=gr.themes.Monochrome()) as
|
|
| 179 |
with gr.Accordion("Advanced Settings", open=False):
|
| 180 |
max_length_slider = gr.Slider(
|
| 181 |
minimum=50,
|
| 182 |
-
maximum=
|
| 183 |
-
value=
|
| 184 |
-
step=
|
| 185 |
label="Max code length"
|
| 186 |
)
|
| 187 |
|
| 188 |
temperature_slider = gr.Slider(
|
| 189 |
-
minimum=0.
|
| 190 |
maximum=1.0,
|
| 191 |
value=0.7,
|
| 192 |
step=0.1,
|
| 193 |
label="Creativity (temperature)"
|
| 194 |
)
|
| 195 |
|
| 196 |
-
generate_btn = gr.Button("🎵 Generate Strudel Code", variant="primary")
|
| 197 |
|
| 198 |
with gr.Column():
|
| 199 |
output_code = gr.Code(
|
| 200 |
label="Generated Strudel Code",
|
| 201 |
language="javascript",
|
| 202 |
-
lines=
|
| 203 |
)
|
| 204 |
|
| 205 |
gr.Markdown("""
|
| 206 |
**Next steps:**
|
| 207 |
-
1. Copy the generated code
|
| 208 |
-
2. Go to [strudel.cc](https://strudel.cc)
|
| 209 |
-
3. Paste and
|
| 210 |
-
4. Enjoy your AI-generated music! 🎶
|
|
|
|
|
|
|
| 211 |
""")
|
| 212 |
|
| 213 |
# Example buttons
|
| 214 |
-
gr.Markdown("### Quick Examples")
|
| 215 |
with gr.Row():
|
| 216 |
examples = [
|
| 217 |
-
["Create a minimal techno beat
|
| 218 |
-
["Ambient soundscape with reverb
|
| 219 |
-
["
|
| 220 |
-
["
|
| 221 |
]
|
| 222 |
|
| 223 |
for example_text, example_genre, example_complexity in examples:
|
| 224 |
-
btn = gr.Button(f"
|
| 225 |
btn.click(
|
| 226 |
lambda t=example_text, g=example_genre, c=example_complexity: (t, g, c),
|
| 227 |
outputs=[prompt_input, genre_dropdown, complexity_dropdown]
|
|
@@ -243,8 +278,4 @@ with gr.Blocks(title="Strudel Code Generator", theme=gr.themes.Monochrome()) as
|
|
| 243 |
|
| 244 |
# Launch the app
|
| 245 |
if __name__ == "__main__":
|
| 246 |
-
app.launch(
|
| 247 |
-
server_name="0.0.0.0",
|
| 248 |
-
server_port=7860,
|
| 249 |
-
share=True
|
| 250 |
-
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 5 |
import torch
|
| 6 |
|
| 7 |
+
# Use CodeGen instead of StarCoder (no authentication needed)
|
| 8 |
+
MODEL_NAME = "Salesforce/codegen-350M-mono"
|
| 9 |
+
|
| 10 |
+
# Initialize model with pipeline for easier usage
|
| 11 |
+
try:
|
| 12 |
+
code_generator = pipeline(
|
| 13 |
+
"text-generation",
|
| 14 |
+
model=MODEL_NAME,
|
| 15 |
+
tokenizer=MODEL_NAME,
|
| 16 |
+
torch_dtype=torch.float16,
|
| 17 |
+
device_map="auto" if torch.cuda.is_available() else None,
|
| 18 |
+
trust_remote_code=True
|
| 19 |
+
)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Error loading model: {e}")
|
| 22 |
+
# Fallback to CPU if GPU fails
|
| 23 |
+
code_generator = pipeline(
|
| 24 |
+
"text-generation",
|
| 25 |
+
model=MODEL_NAME,
|
| 26 |
+
tokenizer=MODEL_NAME,
|
| 27 |
+
device_map=None
|
| 28 |
+
)
|
| 29 |
|
| 30 |
# Strudel code examples for few-shot prompting
|
| 31 |
STRUDEL_EXAMPLES = """
|
|
|
|
| 46 |
n("[0 2 4]/3").s("sine").octave(3).slow(4).room(0.9).gain(0.5)
|
| 47 |
"""
|
| 48 |
|
| 49 |
+
def generate_strudel_code(prompt, genre="general", complexity="simple", max_length=150, temperature=0.7):
|
| 50 |
+
"""Generate Strudel code using CodeGen"""
|
| 51 |
|
| 52 |
# Build context-aware prompt
|
| 53 |
+
system_prompt = f"""// Strudel live coding language for music
|
| 54 |
+
// Generate {complexity} {genre} music patterns
|
| 55 |
// Examples:
|
| 56 |
{STRUDEL_EXAMPLES}
|
| 57 |
|
| 58 |
+
// Create: {prompt}
|
| 59 |
+
// Code:
|
| 60 |
"""
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
# Generate code using pipeline
|
| 64 |
+
outputs = code_generator(
|
| 65 |
+
system_prompt,
|
| 66 |
+
max_length=len(system_prompt.split()) + max_length,
|
| 67 |
+
temperature=temperature,
|
| 68 |
+
do_sample=True,
|
| 69 |
+
top_p=0.9,
|
| 70 |
+
num_return_sequences=1,
|
| 71 |
+
pad_token_id=code_generator.tokenizer.eos_token_id
|
| 72 |
+
)
|
| 73 |
|
| 74 |
+
# Extract generated text
|
| 75 |
+
generated_text = outputs[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# Extract only the new generated part
|
| 78 |
strudel_code = generated_text[len(system_prompt):].strip()
|
| 79 |
|
| 80 |
# Clean up the code
|
|
|
|
| 83 |
return strudel_code
|
| 84 |
|
| 85 |
except Exception as e:
|
| 86 |
+
return f"// Error generating code: {str(e)}\n// Fallback pattern:\ns(\"bd hh sn hh\").gain(0.8)"
|
| 87 |
|
| 88 |
def clean_strudel_code(code):
|
| 89 |
"""Clean and format generated Strudel code"""
|
|
|
|
| 92 |
|
| 93 |
for line in lines:
|
| 94 |
line = line.strip()
|
| 95 |
+
# Stop at comments that indicate end of generation
|
| 96 |
+
if line.startswith('//') and any(word in line.lower() for word in ['user', 'request', 'example', 'create']):
|
| 97 |
break
|
| 98 |
+
# Include actual code lines
|
| 99 |
+
if line and (line.startswith('s(') or line.startswith('n(') or line.startswith('stack(') or
|
| 100 |
+
line.startswith('$:') or line.startswith('all(') or line.endswith(')') or
|
| 101 |
+
line.endswith(',') or 'gain(' in line or 'lpf(' in line):
|
| 102 |
cleaned_lines.append(line)
|
| 103 |
+
# Stop if we hit obvious non-Strudel code
|
| 104 |
+
elif any(keyword in line for keyword in ['function', 'var ', 'let ', 'const ', 'import', 'export']):
|
| 105 |
+
break
|
| 106 |
+
|
| 107 |
+
if len(cleaned_lines) >= 8: # Limit output length
|
| 108 |
+
break
|
| 109 |
|
| 110 |
+
# If no valid code was generated, provide a fallback
|
| 111 |
+
if not cleaned_lines:
|
| 112 |
+
return generate_fallback_pattern(genre)
|
| 113 |
+
|
| 114 |
+
return '\n'.join(cleaned_lines)
|
| 115 |
+
|
| 116 |
+
def generate_fallback_pattern(genre):
|
| 117 |
+
"""Generate a simple fallback pattern based on genre"""
|
| 118 |
+
patterns = {
|
| 119 |
+
"techno": 'stack(\n s("bd*4").gain(0.8),\n s("~ hh ~ hh").gain(0.6),\n n("0 ~ 3 ~").s("sawtooth").octave(2)\n)',
|
| 120 |
+
"house": 'stack(\n s("bd ~ ~ ~ bd ~ ~ ~").gain(0.7),\n s("~ hh ~ hh").gain(0.5),\n n("0 2 4 7").s("sine").octave(3)\n)',
|
| 121 |
+
"ambient": 'n("[0 2 4]/3").s("sine").octave(3).slow(4).room(0.9).gain(0.5)',
|
| 122 |
+
"jazz": 'stack(\n s("bd ~ sn ~").gain(0.7),\n s("~ ~ hh ~").gain(0.4),\n n("0 3 5 7").s("triangle").octave(4)\n)',
|
| 123 |
+
"rock": 'stack(\n s("bd sn bd sn").gain(0.8),\n s("hh*8").gain(0.5),\n n("0 0 3 5").s("square").octave(2)\n)'
|
| 124 |
+
}
|
| 125 |
+
return patterns.get(genre, 's("bd hh sn hh").gain(0.8)')
|
| 126 |
|
| 127 |
def create_full_strudel_template(generated_code, include_visuals=True):
|
| 128 |
"""Wrap generated code in a complete Strudel template"""
|
| 129 |
|
| 130 |
+
visual_code = """// Hydra visuals
|
|
|
|
| 131 |
await initHydra({feedStrudel:5})
|
| 132 |
|
| 133 |
osc(10, 0.1, 0.8)
|
| 134 |
.kaleid(4)
|
| 135 |
.color(1.5, 0.8, 1.2)
|
| 136 |
.out()
|
| 137 |
+
|
| 138 |
""" if include_visuals else ""
|
| 139 |
|
| 140 |
+
template = f"""{visual_code}// AI-Generated Strudel Music Code
|
|
|
|
| 141 |
{generated_code}
|
| 142 |
|
| 143 |
+
// Global effects (optional)
|
| 144 |
+
// all(x => x.fft(5).scope())
|
| 145 |
"""
|
| 146 |
|
| 147 |
return template
|
|
|
|
| 168 |
return full_code
|
| 169 |
|
| 170 |
# Create Gradio app
|
| 171 |
+
with gr.Blocks(title="Strudel Code Generator", theme=gr.themes.Soft()) as app:
|
| 172 |
gr.Markdown("""
|
| 173 |
+
# 🎵 AI Strudel Code Generator
|
| 174 |
|
| 175 |
+
Generate live coding music patterns using AI! Powered by CodeGen-350M.
|
| 176 |
|
| 177 |
**How to use:**
|
| 178 |
+
1. Describe the music you want (e.g., "techno beat with bass", "ambient soundscape")
|
| 179 |
2. Choose genre and complexity
|
| 180 |
3. Click Generate
|
| 181 |
4. Copy the code to [strudel.cc](https://strudel.cc) to hear it!
|
| 182 |
+
|
| 183 |
+
*Note: AI-generated code may need tweaking for best results.*
|
| 184 |
""")
|
| 185 |
|
| 186 |
with gr.Row():
|
|
|
|
| 194 |
with gr.Row():
|
| 195 |
genre_dropdown = gr.Dropdown(
|
| 196 |
choices=["general", "techno", "house", "ambient", "jazz", "rock", "experimental"],
|
| 197 |
+
value="techno",
|
| 198 |
label="Genre"
|
| 199 |
)
|
| 200 |
|
|
|
|
| 212 |
with gr.Accordion("Advanced Settings", open=False):
|
| 213 |
max_length_slider = gr.Slider(
|
| 214 |
minimum=50,
|
| 215 |
+
maximum=300,
|
| 216 |
+
value=150,
|
| 217 |
+
step=25,
|
| 218 |
label="Max code length"
|
| 219 |
)
|
| 220 |
|
| 221 |
temperature_slider = gr.Slider(
|
| 222 |
+
minimum=0.3,
|
| 223 |
maximum=1.0,
|
| 224 |
value=0.7,
|
| 225 |
step=0.1,
|
| 226 |
label="Creativity (temperature)"
|
| 227 |
)
|
| 228 |
|
| 229 |
+
generate_btn = gr.Button("🎵 Generate Strudel Code", variant="primary", size="lg")
|
| 230 |
|
| 231 |
with gr.Column():
|
| 232 |
output_code = gr.Code(
|
| 233 |
label="Generated Strudel Code",
|
| 234 |
language="javascript",
|
| 235 |
+
lines=15
|
| 236 |
)
|
| 237 |
|
| 238 |
gr.Markdown("""
|
| 239 |
**Next steps:**
|
| 240 |
+
1. **Copy** the generated code above
|
| 241 |
+
2. **Go to** [strudel.cc](https://strudel.cc)
|
| 242 |
+
3. **Paste** the code and **click play**
|
| 243 |
+
4. **Enjoy** your AI-generated music! 🎶
|
| 244 |
+
|
| 245 |
+
*Tip: You can modify the generated code to customize the sound!*
|
| 246 |
""")
|
| 247 |
|
| 248 |
# Example buttons
|
| 249 |
+
gr.Markdown("### 💡 Quick Examples")
|
| 250 |
with gr.Row():
|
| 251 |
examples = [
|
| 252 |
+
["Create a minimal techno beat", "techno", "simple"],
|
| 253 |
+
["Ambient soundscape with reverb", "ambient", "simple"],
|
| 254 |
+
["House music with bass line", "house", "moderate"],
|
| 255 |
+
["Jazz drum pattern", "jazz", "moderate"],
|
| 256 |
]
|
| 257 |
|
| 258 |
for example_text, example_genre, example_complexity in examples:
|
| 259 |
+
btn = gr.Button(f"{example_text}", size="sm")
|
| 260 |
btn.click(
|
| 261 |
lambda t=example_text, g=example_genre, c=example_complexity: (t, g, c),
|
| 262 |
outputs=[prompt_input, genre_dropdown, complexity_dropdown]
|
|
|
|
| 278 |
|
| 279 |
# Launch the app
|
| 280 |
if __name__ == "__main__":
|
| 281 |
+
app.launch()
|
|
|
|
|
|
|
|
|
|
|
|