Spaces:
Sleeping
Sleeping
Predict Simple Update
Browse files- README.md +1 -1
- app.py +48 -14
- modules/file_utils.py +29 -0
- modules/user_history.py +27 -19
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🎼
|
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.34.
|
| 8 |
python_version: 3.12.8
|
| 9 |
app_file: app.py
|
| 10 |
pinned: true
|
|
|
|
| 4 |
colorFrom: gray
|
| 5 |
colorTo: red
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.34.1
|
| 8 |
python_version: 3.12.8
|
| 9 |
app_file: app.py
|
| 10 |
pinned: true
|
app.py
CHANGED
|
@@ -33,7 +33,7 @@ import librosa
|
|
| 33 |
import modules.user_history
|
| 34 |
from modules.version_info import versions_html, commit_hash, get_xformers_version
|
| 35 |
from modules.gradio import *
|
| 36 |
-
from modules.file_utils import get_file_parts, get_filename_from_filepath, convert_title_to_filename, get_unique_file_path, delete_file
|
| 37 |
|
| 38 |
MODEL = None
|
| 39 |
MODELS = None
|
|
@@ -248,7 +248,7 @@ def load_melody_filepath(melody_filepath, title, assigned_model, topp, temperatu
|
|
| 248 |
|
| 249 |
return gr.update(value=melody_name), gr.update(maximum=MAX_PROMPT_INDEX, value=-1), gr.update(value=assigned_model, interactive=True), gr.update(value=topp), gr.update(value=temperature), gr.update(value=cfg_coef), gr.update(maximum=MAX_OVERLAP)
|
| 250 |
|
| 251 |
-
def predict(model, text, melody_filepath = None, duration=10, dimension=2, topk=200, topp=0, temperature=1.0, cfg_coef=4.0, background = None, title="UnlimitedMusicGen", settings_font="./assets/arial.ttf", settings_font_color = "#c87f05", seed=-1, overlap=1, prompt_index = 0, include_title = True, include_settings = True, harmony_only = False, profile = gr.OAuthProfile, segment_length = 30, settings_font_size=28, settings_animate_waveform=False, video_orientation="Landscape", excerpt_duration=3.5, progress=gr.Progress(track_tqdm=True)):
|
| 252 |
"""
|
| 253 |
Generate music and video based on the provided parameters and model.
|
| 254 |
|
|
@@ -278,10 +278,11 @@ def predict(model, text, melody_filepath = None, duration=10, dimension=2, topk=
|
|
| 278 |
settings_animate_waveform (bool, optional): Animate waveform in video.
|
| 279 |
video_orientation (str, optional): Video orientation.
|
| 280 |
excerpt_duration (float, optional): Excerpt duration for style conditioning.
|
|
|
|
| 281 |
progress (gr.Progress, optional): Gradio progress tracker.
|
| 282 |
|
| 283 |
Returns:
|
| 284 |
-
tuple: (waveform_video_path, wave_file_path, seed_used)
|
| 285 |
"""
|
| 286 |
global MODEL, INTERRUPTED, INTERRUPTING, MOVE_TO_CPU
|
| 287 |
output_segments = None
|
|
@@ -291,9 +292,14 @@ def predict(model, text, melody_filepath = None, duration=10, dimension=2, topk=
|
|
| 291 |
if melody_filepath in ["None", ""]:
|
| 292 |
melody_filepath = None
|
| 293 |
|
| 294 |
-
if background
|
| 295 |
-
|
|
|
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
if melody_filepath:
|
| 298 |
melody_name, melody_extension = get_filename_from_filepath(melody_filepath)
|
| 299 |
melody = get_melody(melody_filepath)
|
|
@@ -525,7 +531,7 @@ def predict(model, text, melody_filepath = None, duration=10, dimension=2, topk=
|
|
| 525 |
"background": background,
|
| 526 |
"include_title": include_title,
|
| 527 |
"include_settings": include_settings,
|
| 528 |
-
"profile":
|
| 529 |
"commit": commit_hash(),
|
| 530 |
"tag": git_tag(),
|
| 531 |
"version": gr.__version__,
|
|
@@ -557,8 +563,8 @@ def predict(model, text, melody_filepath = None, duration=10, dimension=2, topk=
|
|
| 557 |
print(f"Error renaming file: {e}")
|
| 558 |
|
| 559 |
if waveform_video_path:
|
| 560 |
-
modules.user_history.save_file(
|
| 561 |
-
profile=profile.value,
|
| 562 |
image=background,
|
| 563 |
audio=file.name,
|
| 564 |
video=waveform_video_path,
|
|
@@ -584,11 +590,30 @@ def predict(model, text, melody_filepath = None, duration=10, dimension=2, topk=
|
|
| 584 |
|
| 585 |
#torch.cuda.empty_cache()
|
| 586 |
torch.cuda.ipc_collect()
|
| 587 |
-
return waveform_video_path, file.name, seed
|
| 588 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 590 |
# Add this wrapper function above the gr.api definitions
|
| 591 |
-
def predict_simple(model: str, text: str, duration: int = 10, dimension: int = 2, topk: int = 200, topp: float = 0, temperature: float = 1.0, cfg_coef: float = 4.0, background: str = "./assets/background.png", title: str = "UnlimitedMusicGen", settings_font: str = "./assets/arial.ttf", settings_font_color: str = "#c87f05", seed: int = -1, overlap: int = 1, prompt_index: int =
|
| 592 |
"""
|
| 593 |
Generate music and video based on the provided parameters and model.
|
| 594 |
|
|
@@ -607,7 +632,7 @@ def predict_simple(model: str, text: str, duration: int = 10, dimension: int = 2
|
|
| 607 |
settings_font_color (str, optional): Font color for settings text. Default to "
|
| 608 |
seed (int, optional): Random seed. Default to -1.
|
| 609 |
overlap (int, optional): Segment overlap in seconds. Default to 1.
|
| 610 |
-
prompt_index (int, optional): Melody segment index. Default to
|
| 611 |
include_title (bool, optional): Whether to add title to video. Default to True.
|
| 612 |
include_settings (bool, optional): Whether to add settings to video. Default to True.
|
| 613 |
profile (str, optional): User profile.
|
|
@@ -615,9 +640,10 @@ def predict_simple(model: str, text: str, duration: int = 10, dimension: int = 2
|
|
| 615 |
settings_font_size (int, optional): Font size for settings text.
|
| 616 |
settings_animate_waveform (bool, optional): Animate waveform in video.
|
| 617 |
video_orientation (str, optional): Video orientation
|
|
|
|
| 618 |
|
| 619 |
Returns:
|
| 620 |
-
|
| 621 |
"""
|
| 622 |
profile_username_to_send = "Satoshi Nakamoto"
|
| 623 |
|
|
@@ -636,7 +662,15 @@ def predict_simple(model: str, text: str, duration: int = 10, dimension: int = 2
|
|
| 636 |
elif isinstance(actual_profile_data, str) and actual_profile_data: # string username
|
| 637 |
profile_username_to_send = actual_profile_data
|
| 638 |
|
| 639 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
|
| 641 |
gr.set_static_paths(paths=["fonts/","assets/","images/"])
|
| 642 |
def ui(**kwargs):
|
|
@@ -792,7 +826,7 @@ def ui(**kwargs):
|
|
| 792 |
).then(
|
| 793 |
predict,
|
| 794 |
inputs=[model, text,melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap, prompt_index, include_title, include_settings, harmony_only, user_profile, segment_length, settings_font_size, settings_animate_waveform, video_orientation],
|
| 795 |
-
outputs=[output, wave_file, seed_used], scroll_to_output=True)
|
| 796 |
|
| 797 |
# Show the interface
|
| 798 |
launch_kwargs = {}
|
|
|
|
| 33 |
import modules.user_history
|
| 34 |
from modules.version_info import versions_html, commit_hash, get_xformers_version
|
| 35 |
from modules.gradio import *
|
| 36 |
+
from modules.file_utils import get_file_parts, get_filename_from_filepath, convert_title_to_filename, get_unique_file_path, delete_file, download_and_save_image
|
| 37 |
|
| 38 |
MODEL = None
|
| 39 |
MODELS = None
|
|
|
|
| 248 |
|
| 249 |
return gr.update(value=melody_name), gr.update(maximum=MAX_PROMPT_INDEX, value=-1), gr.update(value=assigned_model, interactive=True), gr.update(value=topp), gr.update(value=temperature), gr.update(value=cfg_coef), gr.update(maximum=MAX_OVERLAP)
|
| 250 |
|
| 251 |
+
def predict(model, text, melody_filepath = None, duration=10, dimension=2, topk=200, topp=0, temperature=1.0, cfg_coef=4.0, background = None, title="UnlimitedMusicGen", settings_font="./assets/arial.ttf", settings_font_color = "#c87f05", seed=-1, overlap=1, prompt_index = 0, include_title = True, include_settings = True, harmony_only = False, profile = gr.OAuthProfile, segment_length = 30, settings_font_size=28, settings_animate_waveform=False, video_orientation="Landscape", excerpt_duration=3.5, return_history_json=False, progress=gr.Progress(track_tqdm=True)):
|
| 252 |
"""
|
| 253 |
Generate music and video based on the provided parameters and model.
|
| 254 |
|
|
|
|
| 278 |
settings_animate_waveform (bool, optional): Animate waveform in video.
|
| 279 |
video_orientation (str, optional): Video orientation.
|
| 280 |
excerpt_duration (float, optional): Excerpt duration for style conditioning.
|
| 281 |
+
return_history_json (bool, optional): Whether to return history JSON instead of typical output. Default to False.
|
| 282 |
progress (gr.Progress, optional): Gradio progress tracker.
|
| 283 |
|
| 284 |
Returns:
|
| 285 |
+
tuple(str,str,str): (waveform_video_path, wave_file_path, seed_used)
|
| 286 |
"""
|
| 287 |
global MODEL, INTERRUPTED, INTERRUPTING, MOVE_TO_CPU
|
| 288 |
output_segments = None
|
|
|
|
| 292 |
if melody_filepath in ["None", ""]:
|
| 293 |
melody_filepath = None
|
| 294 |
|
| 295 |
+
#if background is a url string, download it using download_and_save_image
|
| 296 |
+
if background is None or background in ["None", ""]:
|
| 297 |
+
background = load_background_filepath(video_orientation)
|
| 298 |
|
| 299 |
+
if background.startswith("http://") or background.startswith("https://"):
|
| 300 |
+
username = profile.value.username if hasattr(profile.value, 'username') else "default_user" if (profile is None) else profile
|
| 301 |
+
background = download_and_save_image(background, modules.user_history._user_images_path(username))
|
| 302 |
+
|
| 303 |
if melody_filepath:
|
| 304 |
melody_name, melody_extension = get_filename_from_filepath(melody_filepath)
|
| 305 |
melody = get_melody(melody_filepath)
|
|
|
|
| 531 |
"background": background,
|
| 532 |
"include_title": include_title,
|
| 533 |
"include_settings": include_settings,
|
| 534 |
+
"profile": profile.value.username if hasattr(profile, 'value') and hasattr(profile.value, 'username') else "default_user" if (profile is None) else profile,
|
| 535 |
"commit": commit_hash(),
|
| 536 |
"tag": git_tag(),
|
| 537 |
"version": gr.__version__,
|
|
|
|
| 563 |
print(f"Error renaming file: {e}")
|
| 564 |
|
| 565 |
if waveform_video_path:
|
| 566 |
+
history_results = modules.user_history.save_file(
|
| 567 |
+
profile=profile.value.username if hasattr(profile, 'value') and hasattr(profile.value, 'username') else "default_user" if (profile is None) else profile,
|
| 568 |
image=background,
|
| 569 |
audio=file.name,
|
| 570 |
video=waveform_video_path,
|
|
|
|
| 590 |
|
| 591 |
#torch.cuda.empty_cache()
|
| 592 |
torch.cuda.ipc_collect()
|
|
|
|
| 593 |
|
| 594 |
+
if return_history_json:
|
| 595 |
+
return history_results
|
| 596 |
+
else:
|
| 597 |
+
return waveform_video_path, file.name, seed
|
| 598 |
|
| 599 |
+
def fix_path(path: str) -> str:
|
| 600 |
+
"""
|
| 601 |
+
Strips all characters preceding '_user_history' in the given path and replaces them with "./".
|
| 602 |
+
|
| 603 |
+
If the substring '_user_history' is not found, returns the original path.
|
| 604 |
+
|
| 605 |
+
Args:
|
| 606 |
+
path (str): The input file path.
|
| 607 |
+
|
| 608 |
+
Returns:
|
| 609 |
+
str: The modified file path.
|
| 610 |
+
"""
|
| 611 |
+
index = path.find("_user_history")
|
| 612 |
+
if index != -1:
|
| 613 |
+
return "./" + path[index:].replace("\\", "/")
|
| 614 |
+
return path
|
| 615 |
# Add this wrapper function above the gr.api definitions
|
| 616 |
+
def predict_simple(model: str, text: str, duration: int = 10, dimension: int = 2, topk: int = 200, topp: float = 0.01, temperature: float = 1.0, cfg_coef: float = 4.0, background: str = "./assets/background.png", title: str = "UnlimitedMusicGen", settings_font: str = "./assets/arial.ttf", settings_font_color: str = "#c87f05", seed: int = -1, overlap: int = 1, prompt_index: int = -1, include_title: bool = True, include_settings: bool = True, profile: str = "Satoshi Nakamoto", segment_length: int = 30, settings_font_size: int = 28, settings_animate_waveform: bool = False, video_orientation: str = "Landscape", return_history_json: bool = False) -> tp.List[tp.Tuple[str, str, str]]:
|
| 617 |
"""
|
| 618 |
Generate music and video based on the provided parameters and model.
|
| 619 |
|
|
|
|
| 632 |
settings_font_color (str, optional): Font color for settings text. Default to "
|
| 633 |
seed (int, optional): Random seed. Default to -1.
|
| 634 |
overlap (int, optional): Segment overlap in seconds. Default to 1.
|
| 635 |
+
prompt_index (int, optional): Melody segment index. Default to -1.
|
| 636 |
include_title (bool, optional): Whether to add title to video. Default to True.
|
| 637 |
include_settings (bool, optional): Whether to add settings to video. Default to True.
|
| 638 |
profile (str, optional): User profile.
|
|
|
|
| 640 |
settings_font_size (int, optional): Font size for settings text.
|
| 641 |
settings_animate_waveform (bool, optional): Animate waveform in video.
|
| 642 |
video_orientation (str, optional): Video orientation
|
| 643 |
+
return_history_json (bool, optional): Whether to return history JSON instead of typical output. Default to False.
|
| 644 |
|
| 645 |
Returns:
|
| 646 |
+
tp.List[tp.Tuple[str, str, str]]: [waveform_video_path, wave_file_path, seed_used]
|
| 647 |
"""
|
| 648 |
profile_username_to_send = "Satoshi Nakamoto"
|
| 649 |
|
|
|
|
| 662 |
elif isinstance(actual_profile_data, str) and actual_profile_data: # string username
|
| 663 |
profile_username_to_send = actual_profile_data
|
| 664 |
|
| 665 |
+
UMG_result = predict(model, text, melody_filepath=None, duration=duration, dimension=dimension, topk=topk, topp=topp, temperature=temperature, cfg_coef=cfg_coef, background=background, title=title, settings_font=settings_font, settings_font_color=settings_font_color, seed=seed, overlap=overlap, prompt_index=prompt_index, include_title=include_title, include_settings=include_settings, harmony_only=False, profile=profile, segment_length=segment_length, settings_font_size=settings_font_size, settings_animate_waveform=settings_animate_waveform, video_orientation=video_orientation, excerpt_duration=3.5, return_history_json=return_history_json)
|
| 666 |
+
if return_history_json:
|
| 667 |
+
#content = [UMG_result["image_path"], UMG_result["video_path"], UMG_result["audio_path"], UMG_result["metadata"]["Seed"]]
|
| 668 |
+
#content = {"content":[UMG_result], "isError":False, "message":"Music generated successfully."}
|
| 669 |
+
#content = {fix_path(UMG_result["image_path"]), fix_path(UMG_result["video_path"]), fix_path(UMG_result["audio_path"]), UMG_result["metadata"]["Seed"]}
|
| 670 |
+
content = UMG_result["video_path"], UMG_result["audio_path"], UMG_result["metadata"]["Seed"]
|
| 671 |
+
UMG_result = content
|
| 672 |
+
|
| 673 |
+
return UMG_result
|
| 674 |
|
| 675 |
gr.set_static_paths(paths=["fonts/","assets/","images/"])
|
| 676 |
def ui(**kwargs):
|
|
|
|
| 826 |
).then(
|
| 827 |
predict,
|
| 828 |
inputs=[model, text,melody_filepath, duration, dimension, topk, topp, temperature, cfg_coef, background, title, settings_font, settings_font_color, seed, overlap, prompt_index, include_title, include_settings, harmony_only, user_profile, segment_length, settings_font_size, settings_animate_waveform, video_orientation],
|
| 829 |
+
outputs=[output, wave_file, seed_used], scroll_to_output=True, show_api=False)
|
| 830 |
|
| 831 |
# Show the interface
|
| 832 |
launch_kwargs = {}
|
modules/file_utils.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
# file_utils
|
| 2 |
import os
|
| 3 |
import shutil
|
|
|
|
| 4 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def get_file_parts(file_path: str):
|
| 7 |
# Split the path into directory and filename
|
|
@@ -115,3 +119,28 @@ def get_unique_file_path(directory, filename, file_ext, counter=0):
|
|
| 115 |
|
| 116 |
# Example usage:
|
| 117 |
# new_file_path = get_unique_file_path(video_dir, title_file_name, video_new_ext)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# file_utils
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
+
from PIL import Image
|
| 5 |
from pathlib import Path
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from urllib.parse import urlparse
|
| 8 |
+
import requests
|
| 9 |
|
| 10 |
def get_file_parts(file_path: str):
|
| 11 |
# Split the path into directory and filename
|
|
|
|
| 119 |
|
| 120 |
# Example usage:
|
| 121 |
# new_file_path = get_unique_file_path(video_dir, title_file_name, video_new_ext)
|
| 122 |
+
|
| 123 |
+
def download_and_save_image(url: str, dst_folder: Path) -> Path:
|
| 124 |
+
"""
|
| 125 |
+
Downloads an image from a URL, verifies it with PIL, and saves it in dst_folder with a unique filename.
|
| 126 |
+
|
| 127 |
+
Args:
|
| 128 |
+
url (str): The image URL.
|
| 129 |
+
dst_folder (Path): The destination folder for the image.
|
| 130 |
+
|
| 131 |
+
Returns:
|
| 132 |
+
Path: The saved image's file path.
|
| 133 |
+
"""
|
| 134 |
+
response = requests.get(url)
|
| 135 |
+
response.raise_for_status()
|
| 136 |
+
pil_image = Image.open(BytesIO(response.content))
|
| 137 |
+
|
| 138 |
+
parsed_url = urlparse(url)
|
| 139 |
+
original_filename = os.path.basename(parsed_url.path) # e.g., "background.png"
|
| 140 |
+
base, ext = os.path.splitext(original_filename)
|
| 141 |
+
|
| 142 |
+
# Use get_unique_file_path from file_utils.py to generate a unique file path.
|
| 143 |
+
unique_filepath_str = get_unique_file_path(str(dst_folder), base, ext)
|
| 144 |
+
dst = Path(unique_filepath_str)
|
| 145 |
+
pil_image.save(dst)
|
| 146 |
+
return dst
|
modules/user_history.py
CHANGED
|
@@ -18,7 +18,7 @@ Useful links:
|
|
| 18 |
Update by Surn (Charles Fettinger)
|
| 19 |
"""
|
| 20 |
|
| 21 |
-
__version__ = "0.3.
|
| 22 |
|
| 23 |
import json
|
| 24 |
import os
|
|
@@ -40,7 +40,7 @@ import wave
|
|
| 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 |
from tqdm import tqdm
|
| 45 |
|
| 46 |
user_profile = gr.State(None)
|
|
@@ -165,7 +165,7 @@ def save_image(
|
|
| 165 |
# Ignore images from logged out users
|
| 166 |
if profile is None:
|
| 167 |
return
|
| 168 |
-
username = profile["preferred_username"]
|
| 169 |
|
| 170 |
# Ignore images if user history not used
|
| 171 |
user_history = _UserHistory()
|
|
@@ -202,7 +202,7 @@ def save_file(
|
|
| 202 |
# Ignore files from logged out users
|
| 203 |
if profile is None:
|
| 204 |
return
|
| 205 |
-
username = profile["preferred_username"]
|
| 206 |
|
| 207 |
# Ignore files if user history not used
|
| 208 |
user_history = _UserHistory()
|
|
@@ -304,16 +304,17 @@ def save_file(
|
|
| 304 |
# If no files were saved, nothing to do
|
| 305 |
if image_path is None and video_path is None and audio_path is None and document_path is None:
|
| 306 |
return
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
|
|
|
| 317 |
|
| 318 |
def get_filepath():
|
| 319 |
"""Return the path to the user history folder."""
|
|
@@ -398,7 +399,7 @@ def _fetch_user_history(profile: gr.OAuthProfile | None) -> List[Tuple[str, str]
|
|
| 398 |
if profile is None:
|
| 399 |
user_profile = gr.State(None)
|
| 400 |
return []
|
| 401 |
-
username =
|
| 402 |
|
| 403 |
user_profile = gr.State(profile)
|
| 404 |
|
|
@@ -434,7 +435,7 @@ def _export_user_history(profile: gr.OAuthProfile | None) -> Dict | None:
|
|
| 434 |
# Cannot load history for logged out users
|
| 435 |
if profile is None:
|
| 436 |
return None
|
| 437 |
-
username = profile["preferred_username"]
|
| 438 |
|
| 439 |
user_history = _UserHistory()
|
| 440 |
if not user_history.initialized:
|
|
@@ -463,7 +464,7 @@ def _delete_user_history(profile: gr.OAuthProfile | None) -> None:
|
|
| 463 |
# Cannot load history for logged out users
|
| 464 |
if profile is None:
|
| 465 |
return
|
| 466 |
-
username = profile["preferred_username"]
|
| 467 |
|
| 468 |
user_history = _UserHistory()
|
| 469 |
if not user_history.initialized:
|
|
@@ -482,9 +483,14 @@ def _delete_user_history(profile: gr.OAuthProfile | None) -> None:
|
|
| 482 |
def _copy_image(image: Image.Image | np.ndarray | str | Path, dst_folder: Path, uniqueId: str = "") -> Path:
|
| 483 |
try:
|
| 484 |
"""Copy image to the images folder."""
|
| 485 |
-
#
|
| 486 |
if isinstance(image, str):
|
| 487 |
-
image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 488 |
if isinstance(image, Path):
|
| 489 |
dst = dst_folder / Path(f"{uniqueId}_{Path(image).name}") # keep file ext
|
| 490 |
shutil.copyfile(image, dst)
|
|
@@ -645,6 +651,8 @@ def _display_if_admin() -> Callable:
|
|
| 645 |
return ""
|
| 646 |
if profile["preferred_username"] in _fetch_admins():
|
| 647 |
return _admin_content()
|
|
|
|
|
|
|
| 648 |
return ""
|
| 649 |
|
| 650 |
return _inner
|
|
|
|
| 18 |
Update by Surn (Charles Fettinger)
|
| 19 |
"""
|
| 20 |
|
| 21 |
+
__version__ = "0.3.7"
|
| 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, download_and_save_image
|
| 44 |
from tqdm import tqdm
|
| 45 |
|
| 46 |
user_profile = gr.State(None)
|
|
|
|
| 165 |
# Ignore images from logged out users
|
| 166 |
if profile is None:
|
| 167 |
return
|
| 168 |
+
username = profile if isinstance(profile, str) else profile["preferred_username"]
|
| 169 |
|
| 170 |
# Ignore images if user history not used
|
| 171 |
user_history = _UserHistory()
|
|
|
|
| 202 |
# Ignore files from logged out users
|
| 203 |
if profile is None:
|
| 204 |
return
|
| 205 |
+
username = profile if isinstance(profile, str) else profile["preferred_username"]
|
| 206 |
|
| 207 |
# Ignore files if user history not used
|
| 208 |
user_history = _UserHistory()
|
|
|
|
| 304 |
# If no files were saved, nothing to do
|
| 305 |
if image_path is None and video_path is None and audio_path is None and document_path is None:
|
| 306 |
return
|
| 307 |
+
else:
|
| 308 |
+
# Return the paths of the saved files
|
| 309 |
+
return {
|
| 310 |
+
"image_path": image_path,
|
| 311 |
+
"video_path": video_path,
|
| 312 |
+
"audio_path": audio_path,
|
| 313 |
+
"document_path": document_path,
|
| 314 |
+
"label": label,
|
| 315 |
+
"metadata": metadata
|
| 316 |
+
}
|
| 317 |
+
#, json.dumps(data)
|
| 318 |
|
| 319 |
def get_filepath():
|
| 320 |
"""Return the path to the user history folder."""
|
|
|
|
| 399 |
if profile is None:
|
| 400 |
user_profile = gr.State(None)
|
| 401 |
return []
|
| 402 |
+
username = profile if isinstance(profile, str) else profile["preferred_username"]
|
| 403 |
|
| 404 |
user_profile = gr.State(profile)
|
| 405 |
|
|
|
|
| 435 |
# Cannot load history for logged out users
|
| 436 |
if profile is None:
|
| 437 |
return None
|
| 438 |
+
username = profile if isinstance(profile, str) else profile["preferred_username"]
|
| 439 |
|
| 440 |
user_history = _UserHistory()
|
| 441 |
if not user_history.initialized:
|
|
|
|
| 464 |
# Cannot load history for logged out users
|
| 465 |
if profile is None:
|
| 466 |
return
|
| 467 |
+
username = profile if isinstance(profile, str) else profile["preferred_username"]
|
| 468 |
|
| 469 |
user_history = _UserHistory()
|
| 470 |
if not user_history.initialized:
|
|
|
|
| 483 |
def _copy_image(image: Image.Image | np.ndarray | str | Path, dst_folder: Path, uniqueId: str = "") -> Path:
|
| 484 |
try:
|
| 485 |
"""Copy image to the images folder."""
|
| 486 |
+
# If image is a string, check if it's a URL.
|
| 487 |
if isinstance(image, str):
|
| 488 |
+
if image.startswith("http://") or image.startswith("https://"):
|
| 489 |
+
return download_and_save_image(image, dst_folder)
|
| 490 |
+
else:
|
| 491 |
+
# Assume it's a local filepath string.
|
| 492 |
+
image = Path(image)
|
| 493 |
+
|
| 494 |
if isinstance(image, Path):
|
| 495 |
dst = dst_folder / Path(f"{uniqueId}_{Path(image).name}") # keep file ext
|
| 496 |
shutil.copyfile(image, dst)
|
|
|
|
| 651 |
return ""
|
| 652 |
if profile["preferred_username"] in _fetch_admins():
|
| 653 |
return _admin_content()
|
| 654 |
+
if profile in _fetch_admins():
|
| 655 |
+
return _admin_content()
|
| 656 |
return ""
|
| 657 |
|
| 658 |
return _inner
|