Skip to content

Commit abe5e65

Browse files
committed
Fix PNG path usage in melody generation
Removed unnecessary path renaming and ensured the correct PNG path is used for image processing and saving, improving consistency in file handling during melody generation. Update app.py Update app.py Increase random string length in melody filenames Changed the random string in generated melody filenames from 5 to 10 characters for improved uniqueness. Also fixed an issue where PNG filenames ending with '-1.png' are now renamed to '.png' to ensure consistent file naming.
1 parent 37c9f50 commit abe5e65

1 file changed

Lines changed: 28 additions & 5 deletions

File tree

playgrounds/app.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import streamlit as st
55
import pandas as pd
66
from PIL import Image, ImageOps
7+
import shutil
78
from datetime import datetime
89

910
# MIDI y MusicXML
@@ -176,7 +177,7 @@ def streamlit_runner(self, generations: int, history: bool = False):
176177

177178
melody = ga.best_individual
178179
date = datetime.now().strftime("%d-%m-%y")
179-
randomstring = ''.join(random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=5))
180+
randomstring = ''.join(random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', k=10))
180181
midi_path = os.path.join(SAVE_PATH, f"melody_{date}_{randomstring}.mid")
181182
midi_file = melody_to_midi(melody, filename=midi_path)
182183

@@ -185,9 +186,30 @@ def streamlit_runner(self, generations: int, history: bool = False):
185186
FluidSynth().midi_to_audio(midi_path, wav_path)
186187

187188
score = converter.parse(midi_file)
188-
png_path = os.path.join(SAVE_PATH, f"melody_{date}-1.png")
189-
score.write('musicxml.png', fp=png_path)
190-
new_path = png_path.replace('.png', '-1.png')
189+
base_png = os.path.join(SAVE_PATH, f"melody_{date}_{randomstring}")
190+
expected_png = f"{base_png}.png"
191+
192+
# Write the score image to the expected PNG location
193+
score.write('musicxml.png', fp=expected_png)
194+
195+
# music21 / Musescore can append "-1" to the filename if it creates an alternative
196+
# file (e.g. melody_...-1.png). Detect that and rename safely to the expected filename.
197+
if not os.path.exists(expected_png):
198+
alt_png = f"{base_png}-1.png"
199+
if os.path.exists(alt_png):
200+
# Use shutil.move (or os.replace) instead of os.move which doesn't exist
201+
shutil.move(alt_png, expected_png)
202+
new_path = expected_png
203+
else:
204+
# Last-resort scan: find a matching PNG in SAVE_PATH that starts with the base name
205+
matches = [p for p in os.listdir(SAVE_PATH) if p.startswith(os.path.basename(base_png)) and p.endswith('.png')]
206+
if matches:
207+
new_path = os.path.join(SAVE_PATH, matches[0])
208+
else:
209+
# If nothing was created, fallback to the expected filename (it might still get created in some cases)
210+
new_path = expected_png
211+
else:
212+
new_path = expected_png
191213

192214
if st.context.theme.type == "dark":
193215
# Snippet para invertir colores de la partitura
@@ -201,7 +223,8 @@ def streamlit_runner(self, generations: int, history: bool = False):
201223
inverted_image = Image.merge("RGBA", (inv_r, inv_g, inv_b, a))
202224
inverted_image.save(new_path)
203225
else:
204-
new_path = png_path
226+
# Already set to expected_png (or detected alternative) above
227+
pass
205228

206229
st.image(new_path, caption="Partitura generada")
207230

0 commit comments

Comments
 (0)