Surn commited on
Commit
9959300
·
1 Parent(s): d5c194b

Fix file location bug

Browse files
Files changed (2) hide show
  1. app.py +5 -5
  2. modules/user_history.py +20 -15
app.py CHANGED
@@ -482,7 +482,7 @@ def ui(**kwargs):
482
  topk = gr.Number(label="Top-k", value=280, precision=0, interactive=True)
483
  topp = gr.Number(label="Top-p", value=1150, precision=0, interactive=True)
484
  temperature = gr.Number(label="Randomness Temperature", value=0.7, precision=None, interactive=True)
485
- cfg_coef = gr.Number(label="Classifier Free Guidance", value=3.0, precision=None, interactive=True)
486
  with gr.Row():
487
  seed = gr.Number(label="Seed", value=-1, precision=0, interactive=True)
488
  gr.Button('\U0001f3b2\ufe0f', elem_classes="small-btn").click(fn=lambda: -1, outputs=[seed], queue=False)
@@ -505,7 +505,7 @@ def ui(**kwargs):
505
  "80s Pop Synth",
506
  950,
507
  0.6,
508
- 3.1
509
  ],
510
  [
511
  "4/4 120bpm 320kbps 48khz, A cheerful country song with acoustic guitars",
@@ -532,16 +532,16 @@ def ui(**kwargs):
532
  "EDM my Bach",
533
  500,
534
  0.7,
535
- 3.25
536
  ],
537
  [
538
  "4/4 320kbps 48khz, lofi slow bpm electro chill with organic samples",
539
  None,
540
  "medium",
541
  "LoFi Chill",
542
- 1150,
543
  0.7,
544
- 3.0
545
  ],
546
  ],
547
  inputs=[text, melody_filepath, model, title, topp, temperature, cfg_coef],
 
482
  topk = gr.Number(label="Top-k", value=280, precision=0, interactive=True)
483
  topp = gr.Number(label="Top-p", value=1150, precision=0, interactive=True)
484
  temperature = gr.Number(label="Randomness Temperature", value=0.7, precision=None, interactive=True)
485
+ cfg_coef = gr.Number(label="Classifier Free Guidance", value=3.5, precision=None, interactive=True)
486
  with gr.Row():
487
  seed = gr.Number(label="Seed", value=-1, precision=0, interactive=True)
488
  gr.Button('\U0001f3b2\ufe0f', elem_classes="small-btn").click(fn=lambda: -1, outputs=[seed], queue=False)
 
505
  "80s Pop Synth",
506
  950,
507
  0.6,
508
+ 3.0
509
  ],
510
  [
511
  "4/4 120bpm 320kbps 48khz, A cheerful country song with acoustic guitars",
 
532
  "EDM my Bach",
533
  500,
534
  0.7,
535
+ 3.5
536
  ],
537
  [
538
  "4/4 320kbps 48khz, lofi slow bpm electro chill with organic samples",
539
  None,
540
  "medium",
541
  "LoFi Chill",
542
+ 0,
543
  0.7,
544
+ 4.0
545
  ],
546
  ],
547
  inputs=[text, melody_filepath, model, title, topp, temperature, cfg_coef],
modules/user_history.py CHANGED
@@ -18,7 +18,7 @@ Useful links:
18
  Update by Surn (Charles Fettinger)
19
  """
20
 
21
- __version__ = "0.2.1"
22
 
23
  import json
24
  import os
@@ -40,6 +40,7 @@ import wave
40
  from mutagen.mp3 import MP3, EasyMP3
41
  import torchaudio
42
  import subprocess
 
43
 
44
  user_profile = gr.State(None)
45
 
@@ -216,19 +217,19 @@ def save_file(
216
 
217
  video_path = None
218
  # Copy video to storage
219
- if video is not None:
220
  video_path = _copy_file(video, dst_folder=user_history._user_file_path(username, "videos"))
221
  video_path = _add_metadata(video_path, metadata)
222
 
223
  audio_path = None
224
  # Copy audio to storage
225
- if audio is not None:
226
  audio_path = _copy_file(audio, dst_folder=user_history._user_file_path(username, "audios"))
227
  audio_path = _add_metadata(audio_path, metadata)
228
 
229
  document_path = None
230
  # Copy document to storage
231
- if document is not None:
232
  document_path = _copy_file(document, dst_folder=user_history._user_file_path(username, "documents"))
233
  document_path = _add_metadata(document_path, metadata)
234
 
@@ -241,7 +242,7 @@ def save_file(
241
  with user_history._user_lock(username):
242
  with user_history._user_jsonl_path(username).open("a") as f:
243
  f.write(json.dumps(data) + "\n")
244
-
245
 
246
  #############
247
  # Internals #
@@ -439,6 +440,9 @@ def _add_metadata(file_location: Path, metadata: Dict[str, Any]) -> Path:
439
  if file_type not in valid_file_types:
440
  raise ValueError("Invalid file type. Valid file types are .wav, .mp3, .mp4, .png")
441
 
 
 
 
442
  if file_type == ".wav":
443
  # Open and process .wav file
444
  with wave.open(file_location, 'rb') as wav_file:
@@ -448,13 +452,11 @@ def _add_metadata(file_location: Path, metadata: Dict[str, Any]) -> Path:
448
  # Update metadata
449
  current_metadata.update(metadata)
450
 
451
- # Reopen the WAV file in write mode
452
- with wave.open(file_location, 'wb') as wav_output_file:
453
- # Set the new metadata
454
  wav_output_file.setparams(wav_file.getparams())
455
-
456
- # Save the WAV file (overwriting the previous version)
457
- wav_output_file.close()
458
  elif file_type == ".mp3":
459
  # Open and process .mp3 file
460
  audio = EasyMP3(file_location)
@@ -463,8 +465,9 @@ def _add_metadata(file_location: Path, metadata: Dict[str, Any]) -> Path:
463
  for key, value in metadata.items():
464
  audio[key] = value
465
 
466
- # Save the MP3 file (overwriting the previous version)
467
- audio.save()
 
468
  elif file_type == ".mp4":
469
  # Open and process .mp4 file
470
  # Add metadata to the file
@@ -478,19 +481,21 @@ def _add_metadata(file_location: Path, metadata: Dict[str, Any]) -> Path:
478
  # Use ffmpeg to add metadata to the video file
479
  metadata_args = [f"{key}={value}" for key, value in metadata.items()]
480
  ffmpeg_metadata = ":".join(metadata_args)
481
- ffmpeg_cmd = f'ffmpeg -y -i "{file_location}" -i "{wav_file_location}" -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -metadata "{ffmpeg_metadata}" "{file_location}"'
482
  subprocess.run(ffmpeg_cmd, shell=True, check=True)
483
 
484
  # Remove temporary WAV file
485
  if not wave_exists:
486
  wav_file_location.unlink()
 
487
  elif file_type == ".png":
488
  # Open and process .png file
489
  image = Image.open(file_location)
490
  exif_data = image.info.get("exif", {})
491
  exif_data.update(metadata)
492
  # Add metadata to the file
493
- image.save(file_location, exif=exif_data)
 
494
 
495
  return file_location # Return the path to the modified file
496
 
 
18
  Update by Surn (Charles Fettinger)
19
  """
20
 
21
+ __version__ = "0.2.2"
22
 
23
  import json
24
  import os
 
40
  from mutagen.mp3 import MP3, EasyMP3
41
  import torchaudio
42
  import subprocess
43
+ from modules.file_utils import get_file_parts, rename_file_to_lowercase_extension
44
 
45
  user_profile = gr.State(None)
46
 
 
217
 
218
  video_path = None
219
  # Copy video to storage
220
+ if video is not None:
221
  video_path = _copy_file(video, dst_folder=user_history._user_file_path(username, "videos"))
222
  video_path = _add_metadata(video_path, metadata)
223
 
224
  audio_path = None
225
  # Copy audio to storage
226
+ if audio is not None:
227
  audio_path = _copy_file(audio, dst_folder=user_history._user_file_path(username, "audios"))
228
  audio_path = _add_metadata(audio_path, metadata)
229
 
230
  document_path = None
231
  # Copy document to storage
232
+ if document is not None:
233
  document_path = _copy_file(document, dst_folder=user_history._user_file_path(username, "documents"))
234
  document_path = _add_metadata(document_path, metadata)
235
 
 
242
  with user_history._user_lock(username):
243
  with user_history._user_jsonl_path(username).open("a") as f:
244
  f.write(json.dumps(data) + "\n")
245
+
246
 
247
  #############
248
  # Internals #
 
440
  if file_type not in valid_file_types:
441
  raise ValueError("Invalid file type. Valid file types are .wav, .mp3, .mp4, .png")
442
 
443
+ directory, filename, name, ext, new_ext = get_file_parts(file_location)
444
+ new_file_location = rename_file_to_lowercase_extension(directory + name +"_h"+ new_ext)
445
+
446
  if file_type == ".wav":
447
  # Open and process .wav file
448
  with wave.open(file_location, 'rb') as wav_file:
 
452
  # Update metadata
453
  current_metadata.update(metadata)
454
 
455
+ # Copy the WAV file
456
+ with wave.open(new_file_location, 'wb') as wav_output_file:
 
457
  wav_output_file.setparams(wav_file.getparams())
458
+ wav_output_file.writeframes(wav_file.readframes(wav_file.getnframes()))
459
+ return new_file_location
 
460
  elif file_type == ".mp3":
461
  # Open and process .mp3 file
462
  audio = EasyMP3(file_location)
 
465
  for key, value in metadata.items():
466
  audio[key] = value
467
 
468
+ # Save the MP3 file to the new file location
469
+ audio.save(new_file_location)
470
+ return new_file_location
471
  elif file_type == ".mp4":
472
  # Open and process .mp4 file
473
  # Add metadata to the file
 
481
  # Use ffmpeg to add metadata to the video file
482
  metadata_args = [f"{key}={value}" for key, value in metadata.items()]
483
  ffmpeg_metadata = ":".join(metadata_args)
484
+ ffmpeg_cmd = f'ffmpeg -y -i "{file_location}" -i "{wav_file_location}" -map 0:v:0 -map 1:a:0 -c:v copy -c:a aac -metadata "{ffmpeg_metadata}" "{new_file_location}"'
485
  subprocess.run(ffmpeg_cmd, shell=True, check=True)
486
 
487
  # Remove temporary WAV file
488
  if not wave_exists:
489
  wav_file_location.unlink()
490
+ return new_file_location
491
  elif file_type == ".png":
492
  # Open and process .png file
493
  image = Image.open(file_location)
494
  exif_data = image.info.get("exif", {})
495
  exif_data.update(metadata)
496
  # Add metadata to the file
497
+ image.save(new_file_location, exif=exif_data)
498
+ return new_file_location
499
 
500
  return file_location # Return the path to the modified file
501