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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
def greet(name):
|
| 4 |
-
return "Hello " + name + "!!"
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
NotaGen Gradio Inference App for HuggingFace Spaces
|
| 3 |
+
|
| 4 |
+
This app provides a simple interface for generating symbolic music using NotaGen.
|
| 5 |
+
It accepts three parameters (period, composer, instrumentation) and returns ABC notation.
|
| 6 |
+
|
| 7 |
+
The ABC notation can then be processed by WeaveMuse locally for XML/PDF conversion.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
import gradio as gr
|
| 11 |
+
import torch
|
| 12 |
+
import logging
|
| 13 |
+
import traceback
|
| 14 |
+
from weavemuse.tools.notagen_tool import NotaGenTool
|
| 15 |
+
|
| 16 |
+
# Set up logging
|
| 17 |
+
logging.basicConfig(level=logging.INFO)
|
| 18 |
+
logger = logging.getLogger(__name__)
|
| 19 |
+
|
| 20 |
+
# Initialize the NotaGen tool
|
| 21 |
+
# This will use GPU if available in the HuggingFace Space
|
| 22 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 23 |
+
logger.info(f"Initializing NotaGen tool on device: {device}")
|
| 24 |
+
|
| 25 |
+
try:
|
| 26 |
+
notagen_tool = NotaGenTool(device=device)
|
| 27 |
+
logger.info("β
NotaGen tool initialized successfully!")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
logger.error(f"β Failed to initialize NotaGen tool: {e}")
|
| 30 |
+
logger.error(traceback.format_exc())
|
| 31 |
+
notagen_tool = None
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def generate_abc(period: str, composer: str, instrumentation: str) -> str:
|
| 35 |
+
"""
|
| 36 |
+
Generate ABC notation using NotaGen.
|
| 37 |
+
|
| 38 |
+
Args:
|
| 39 |
+
period: Musical period (e.g., Baroque, Classical, Romantic)
|
| 40 |
+
composer: Composer style (e.g., Bach, Mozart, Chopin)
|
| 41 |
+
instrumentation: Instruments (e.g., Piano, Violin, Orchestra)
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
ABC notation string or error message
|
| 45 |
+
"""
|
| 46 |
+
if notagen_tool is None:
|
| 47 |
+
error_msg = "β NotaGen tool not initialized. Please check server logs."
|
| 48 |
+
logger.error(error_msg)
|
| 49 |
+
return error_msg
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
logger.info(f"Generating ABC for: {period}-{composer}-{instrumentation}")
|
| 53 |
+
|
| 54 |
+
# Call the NotaGen tool's forward method
|
| 55 |
+
# This will return a formatted string with file paths
|
| 56 |
+
result = notagen_tool.forward(
|
| 57 |
+
period=period,
|
| 58 |
+
composer=composer,
|
| 59 |
+
instrumentation=instrumentation
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
# Extract just the ABC content from the result
|
| 63 |
+
# The result contains file paths and other info, but we only need the ABC
|
| 64 |
+
if "Error:" in result:
|
| 65 |
+
logger.error(f"Generation error: {result}")
|
| 66 |
+
return result
|
| 67 |
+
|
| 68 |
+
# Parse the result to extract ABC content
|
| 69 |
+
# The result format is: "Successfully generated...\n\nABC notation preview:\n```\n{abc}```"
|
| 70 |
+
if "ABC notation preview:" in result:
|
| 71 |
+
abc_start = result.find("```\n") + 4
|
| 72 |
+
abc_end = result.rfind("\n```")
|
| 73 |
+
if abc_start > 3 and abc_end > abc_start:
|
| 74 |
+
abc_content = result[abc_start:abc_end]
|
| 75 |
+
logger.info(f"β
Successfully generated ABC notation ({len(abc_content)} chars)")
|
| 76 |
+
return abc_content
|
| 77 |
+
|
| 78 |
+
# If we can't parse it, return the full result
|
| 79 |
+
logger.warning("Could not parse ABC from result, returning full result")
|
| 80 |
+
return result
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
error_msg = f"β Error during generation: {str(e)}\n\n{traceback.format_exc()}"
|
| 84 |
+
logger.error(error_msg)
|
| 85 |
+
return error_msg
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# Create Gradio interface
|
| 89 |
+
with gr.Blocks(title="NotaGen - Symbolic Music Generation") as demo:
|
| 90 |
+
gr.Markdown("""
|
| 91 |
+
# π΅ NotaGen - Symbolic Music Generation
|
| 92 |
+
|
| 93 |
+
Generate symbolic music in ABC notation format using NotaGen.
|
| 94 |
+
|
| 95 |
+
This space returns only the **ABC notation** as text. For conversion to PDF, XML, or MIDI,
|
| 96 |
+
use the WeaveMuse package locally or through its API.
|
| 97 |
+
|
| 98 |
+
### Usage
|
| 99 |
+
1. Select a musical **period** (e.g., Baroque, Classical, Romantic)
|
| 100 |
+
2. Choose a **composer** style (e.g., Bach, Mozart, Chopin)
|
| 101 |
+
3. Specify **instrumentation** (Piano recommended for best results)
|
| 102 |
+
4. Click **Generate** to receive ABC notation
|
| 103 |
+
|
| 104 |
+
The generated ABC can be processed by WeaveMuse for full music score rendering.
|
| 105 |
+
""")
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
with gr.Column():
|
| 109 |
+
period_input = gr.Textbox(
|
| 110 |
+
label="Musical Period",
|
| 111 |
+
placeholder="e.g., Classical, Romantic, Baroque",
|
| 112 |
+
value="Classical"
|
| 113 |
+
)
|
| 114 |
+
composer_input = gr.Textbox(
|
| 115 |
+
label="Composer Style",
|
| 116 |
+
placeholder="e.g., Mozart, Chopin, Bach",
|
| 117 |
+
value="Mozart"
|
| 118 |
+
)
|
| 119 |
+
instrumentation_input = gr.Textbox(
|
| 120 |
+
label="Instrumentation",
|
| 121 |
+
placeholder="e.g., Piano, Violin, Orchestra",
|
| 122 |
+
value="Piano"
|
| 123 |
+
)
|
| 124 |
+
generate_btn = gr.Button("πΌ Generate ABC Notation", variant="primary")
|
| 125 |
+
|
| 126 |
+
with gr.Column():
|
| 127 |
+
output_text = gr.Textbox(
|
| 128 |
+
label="Generated ABC Notation",
|
| 129 |
+
placeholder="ABC notation will appear here...",
|
| 130 |
+
lines=20,
|
| 131 |
+
max_lines=30
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
gr.Examples(
|
| 135 |
+
examples=[
|
| 136 |
+
["Classical", "Mozart", "Piano"],
|
| 137 |
+
["Romantic", "Chopin", "Piano"],
|
| 138 |
+
["Baroque", "Bach", "Piano"],
|
| 139 |
+
["Classical", "Beethoven", "Piano"],
|
| 140 |
+
["Romantic", "Liszt", "Piano"],
|
| 141 |
+
],
|
| 142 |
+
inputs=[period_input, composer_input, instrumentation_input],
|
| 143 |
+
label="Example Prompts"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# Connect the button to the function
|
| 147 |
+
generate_btn.click(
|
| 148 |
+
fn=generate_abc,
|
| 149 |
+
inputs=[period_input, composer_input, instrumentation_input],
|
| 150 |
+
outputs=output_text,
|
| 151 |
+
api_name="infer" # Important: This creates the /infer endpoint
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
gr.Markdown("""
|
| 155 |
+
---
|
| 156 |
+
### About
|
| 157 |
+
|
| 158 |
+
**NotaGen** is a symbolic music generation model that creates music in ABC notation format.
|
| 159 |
+
|
| 160 |
+
- **ABC Notation**: A text-based music notation format that can be converted to PDF, MIDI, XML, etc.
|
| 161 |
+
- **Model**: Uses the quantized NotaGen model for efficient inference
|
| 162 |
+
- **Integration**: Designed to work seamlessly with WeaveMuse for full music generation pipelines
|
| 163 |
+
|
| 164 |
+
**Note**: This space only generates ABC notation. For complete score rendering (PDF, MP3, etc.),
|
| 165 |
+
use WeaveMuse locally or via its remote tools.
|
| 166 |
+
""")
|
| 167 |
|
|
|
|
|
|
|
| 168 |
|
| 169 |
+
# Launch the app
|
| 170 |
+
if __name__ == "__main__":
|
| 171 |
+
demo.launch(
|
| 172 |
+
server_name="0.0.0.0", # Allow external access
|
| 173 |
+
server_port=7860, # Default HF Spaces port
|
| 174 |
+
share=False # No need to share, HF handles this
|
| 175 |
+
)
|