File size: 6,977 Bytes
1dc20d9
4c10907
7d35d1e
a7334d4
 
6394bbc
85aaa31
7d35d1e
68f7846
85aaa31
68f7846
a7334d4
1dc20d9
68f7846
7d35d1e
 
 
 
 
eabc43b
6394bbc
85aaa31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6394bbc
eabc43b
a7334d4
7d35d1e
d2cd103
eabc43b
68f7846
7d35d1e
 
 
85aaa31
 
 
7d35d1e
4474af1
85aaa31
7d35d1e
 
85aaa31
7d35d1e
68f7846
a7334d4
 
68f7846
df6c3f5
a7334d4
 
 
 
7d35d1e
a7334d4
 
 
 
 
 
 
 
 
 
 
 
7d35d1e
a7334d4
 
 
 
 
df6c3f5
a7334d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30f9d01
 
85aaa31
 
 
 
 
 
 
 
 
 
7d35d1e
 
 
 
a7334d4
 
 
 
 
 
 
 
 
7d35d1e
 
 
a7334d4
 
 
 
 
 
054eb24
7d35d1e
 
 
 
 
 
 
 
 
 
 
 
 
 
a7334d4
7d35d1e
 
 
a7334d4
 
 
 
 
 
 
 
7d35d1e
a7334d4
 
 
 
 
 
 
7d35d1e
85aaa31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import spaces
import gradio as gr
import os
import json
import tempfile
from pathlib import Path
import ast
from model import Jamify
<<<<<<< HEAD
import json
=======
from utils import json_to_text, text_to_json

>>>>>>> working
# Initialize the Jamify model once
print("Initializing Jamify model...")
jamify_model = Jamify()
print("Jamify model ready.")




def parse(file):
    parsed_data = []
    with open(file, 'r') as f:
        content = f.read()
        for line in f:
            line = line.strip()
            if not line:
                continue
            start,end,word = line.split(' ')
            data_point = {
                    "word": word,
                    "start": float(start),
                    "end": float(end)
                }
            parsed_data.append(data_point)
        
    with open('temp.json','w') as f:
        json.dump(parsed_data,f)

    return content

@spaces.GPU(duration=100)
def generate_song(reference_audio, lyrics_text, style_prompt, duration):
    # We need to save the uploaded files to temporary paths to pass to the model
    reference_audio = reference_audio not in ("", None) and reference_audio or None
    
<<<<<<< HEAD

    # The model expects paths, so we write the prompt to a temp file if needed
    # (This part of the model could be improved to accept the string directly)
    #print(type(lyrics_file))
    #parse(lyrics_file)
    #parse(lyrics_file)
    output_path = jamify_model.predict(
        reference_audio_path=reference_audio,
        lyrics_json_path='temp.json',
        style_prompt=style_prompt,
        duration_sec=duration

    )
=======
    # Convert text format to JSON and save to temporary file
    lyrics_json = text_to_json(lyrics_text)
>>>>>>> working
    
    # Create temporary file for lyrics JSON
    with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f:
        json.dump(lyrics_json, f, indent=2)
        lyrics_file = f.name

    try:
        output_path = jamify_model.predict(
            reference_audio_path=reference_audio,
            lyrics_json_path=lyrics_file,
            style_prompt=style_prompt,
            duration_sec=duration
        )
        return output_path
    finally:
        # Clean up temporary file
        if os.path.exists(lyrics_file):
            os.unlink(lyrics_file)

# Load and cache examples
def load_examples():
    """Load examples from the examples directory and pre-compute text formats"""
    examples = []
    examples_file = "examples/input.json"
    
    if os.path.exists(examples_file):
        print("Loading and caching examples...")
        with open(examples_file, 'r') as f:
            examples_data = json.load(f)
            
        for example in examples_data:
            example_id = example.get('id', '')
            audio_path = example.get('audio_path', '')
            lrc_path = example.get('lrc_path', '')
            duration = example.get('duration', 120)
            
            # Load lyrics and convert to text format (pre-computed/cached)
            lyrics_text = ""
            if os.path.exists(lrc_path):
                try:
                    with open(lrc_path, 'r') as f:
                        lyrics_json = json.load(f)
                    lyrics_text = json_to_text(lyrics_json)
                    print(f"Cached example {example_id}: {len(lyrics_text)} chars")
                except Exception as e:
                    print(f"Error loading lyrics from {lrc_path}: {e}")
            
            examples.append({
                'id': example_id,
                'audio_path': audio_path if os.path.exists(audio_path) else None,
                'lyrics_text': lyrics_text,
                'duration': duration
            })
    
    print(f"Loaded {len(examples)} cached examples")
    return examples

def load_example(example_idx, examples):
    """Load a specific example and return its data"""
    if 0 <= example_idx < len(examples):
        example = examples[example_idx]
        return (
            example['audio_path'],
            example['lyrics_text'],
            example['duration']
        )
    return None, "", 120

# Load examples at startup
examples = load_examples()

# Gradio interface
def process_text_file(file_obj):
    """
    Reads the content of an uploaded text file.
    """
    if file_obj is not None:
        with open(file_obj.name, 'r') as f:
            content = f.read()
        return content
    return "No file uploaded."

with gr.Blocks() as demo:
    gr.Markdown("# Jamify: Music Generation from Lyrics and Style")
    gr.Markdown("Provide your lyrics, a style reference (either an audio file or a text prompt), and a desired duration to generate a song.")
    
    # Sample buttons section
    if examples:
        gr.Markdown("### Sample Examples")
        with gr.Row():
            example_buttons = []
            for i, example in enumerate(examples):
                button = gr.Button(f"Example {example['id']}", variant="secondary", size="sm")
                example_buttons.append(button)
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### Inputs")
            lyrics_text = gr.Textbox(
                label="Lyrics", 
                lines=10, 
                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]...",
                value=""
            )
            duration_slider = gr.Slider(minimum=5, maximum=230, value=120, step=30, label="Duration (seconds)")
            
            with gr.Tab("Style from Audio"):
                reference_audio = gr.File(label="Reference Audio (.mp3, .wav)", type="filepath")
            with gr.Tab("Style from Text"):
                style_prompt = gr.Textbox(label="Style Prompt", lines=3, placeholder="e.g., A high-energy electronic dance track with a strong bassline and euphoric synths.")
            
            generate_button = gr.Button("Generate Song", variant="primary")
            
        with gr.Column():
            gr.Markdown("### Output")
            output_audio = gr.Audio(label="Generated Song")

    generate_button.click(
        fn=generate_song,
        inputs=[reference_audio, lyrics_text, style_prompt, duration_slider],
        outputs=output_audio,
        api_name="generate_song"
    )
    
    # Connect example buttons to load data
    if examples:
        for i, button in enumerate(example_buttons):
            button.click(
                fn=lambda idx=i: load_example(idx, examples),
                outputs=[reference_audio, lyrics_text, duration_slider]
            )

# Create necessary temporary directories for Gradio
print("Creating temporary directories...")
try:
    os.makedirs("/tmp/gradio", exist_ok=True)
    print("Temporary directories created successfully.")
except Exception as e:
    print(f"Warning: Could not create temporary directories: {e}")

demo.queue().launch()