Surn commited on
Commit
0ffc43b
1 Parent(s): d42362b

Update description, fix font issues

Browse files
Files changed (2) hide show
  1. app.py +4 -3
  2. audiocraft/utils/extend.py +43 -7
app.py CHANGED
@@ -150,7 +150,7 @@ def predict(model, text, melody, duration, dimension, topk, topp, temperature, c
150
  output = output.detach().cpu().float()[0]
151
  with NamedTemporaryFile("wb", suffix=".wav", delete=False) as file:
152
  if include_settings:
153
- video_description = f"{text}\n Duration: {str(initial_duration)} Dimension: {dimension}\n Top-k:{topk} Top-p:{topp}\n Randomness:{temperature}\n cfg:{cfg_coef} overlap: {overlap}\n Seed: {seed}"
154
  background = add_settings_to_image(title, video_description, background_path=background, font=settings_font, font_color=settings_font_color)
155
  audio_write(
156
  file.name, output, MODEL.sample_rate, strategy="loudness",
@@ -171,11 +171,12 @@ def ui(**kwargs):
171
  """
172
  with gr.Blocks(title="UnlimitedMusicGen", css=css) as demo:
173
  gr.Markdown(
174
- """
175
- # Disclaimer: This won't run on CPU only. Clone this App and run on GPU instance!!!
176
  # UnlimitedMusicGen
177
  This is your private demo for [UnlimitedMusicGen](https://github.com/Oncorporation/audiocraft), a simple and controllable model for music generation
178
  presented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284)
 
 
179
  """
180
  )
181
  if IS_SHARED_SPACE:
 
150
  output = output.detach().cpu().float()[0]
151
  with NamedTemporaryFile("wb", suffix=".wav", delete=False) as file:
152
  if include_settings:
153
+ video_description = f"{text}\n Duration: {str(initial_duration)} Dimension: {dimension}\n Top-k:{topk} Top-p:{topp}\n Randomness:{temperature}\n cfg:{cfg_coef} overlap: {overlap}\n Seed: {seed}\n Melody File:#todo"
154
  background = add_settings_to_image(title, video_description, background_path=background, font=settings_font, font_color=settings_font_color)
155
  audio_write(
156
  file.name, output, MODEL.sample_rate, strategy="loudness",
 
171
  """
172
  with gr.Blocks(title="UnlimitedMusicGen", css=css) as demo:
173
  gr.Markdown(
174
+ """
 
175
  # UnlimitedMusicGen
176
  This is your private demo for [UnlimitedMusicGen](https://github.com/Oncorporation/audiocraft), a simple and controllable model for music generation
177
  presented at: ["Simple and Controllable Music Generation"](https://huggingface.co/papers/2306.05284)
178
+
179
+ Disclaimer: This won't run on CPU only. Clone this App and run on GPU instance!
180
  """
181
  )
182
  if IS_SHARED_SPACE:
audiocraft/utils/extend.py CHANGED
@@ -7,6 +7,8 @@ import string
7
  import tempfile
8
  import os
9
  import textwrap
 
 
10
  from huggingface_hub import hf_hub_download
11
 
12
  def separate_audio_segments(audio, segment_duration=30, overlap=1):
@@ -123,16 +125,50 @@ def hex_to_rgba(hex_color):
123
  rgba = (255,255,0,255)
124
  return rgba
125
 
126
- def getFont(font_name, size:int=16):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  try:
128
- font = ImageFont.truetype(font_name, size)
129
- except:
130
  try:
131
- font = ImageFont.truetype(hf_hub_download("assets", font_name), encoding="UTF-8")
132
  except:
133
- font = ImageFont.load_default()
 
 
 
 
 
 
 
134
  return font
135
 
 
136
  def add_settings_to_image(title: str = "title", description: str = "", width: int = 768, height: int = 512, background_path: str = "", font: str = "arial.ttf", font_color: str = "#ffffff"):
137
  # Create a new RGBA image with the specified dimensions
138
  image = Image.new("RGBA", (width, height), (255, 255, 255, 0))
@@ -149,7 +185,7 @@ def add_settings_to_image(title: str = "title", description: str = "", width: in
149
  text_x = width // 2
150
  text_y = height // 2
151
  # Draw the title text at the center top
152
- title_font = getFont(font, 26) # Replace with your desired font and size
153
 
154
  title_text = '\n'.join(textwrap.wrap(title, width // 12))
155
  title_x, title_y, title_text_width, title_text_height = title_font.getbbox(title_text)
@@ -158,7 +194,7 @@ def add_settings_to_image(title: str = "title", description: str = "", width: in
158
  title_draw = ImageDraw.Draw(image)
159
  title_draw.multiline_text((title_x, title_y), title, fill=font_color, font=title_font, align="center")
160
  # Draw the description text two lines below the title
161
- description_font = getFont(font, 16) # Replace with your desired font and size
162
  description_text = '\n'.join(textwrap.wrap(description, width // 12))
163
  description_x, description_y, description_text_width, description_text_height = description_font.getbbox(description_text)
164
  description_x = max(text_x - (description_text_width // 2), description_x, 0)
 
7
  import tempfile
8
  import os
9
  import textwrap
10
+ import requests
11
+ from io import BytesIO
12
  from huggingface_hub import hf_hub_download
13
 
14
  def separate_audio_segments(audio, segment_duration=30, overlap=1):
 
125
  rgba = (255,255,0,255)
126
  return rgba
127
 
128
+ def load_font(font_name, font_size=16):
129
+ """
130
+ Load a font using the provided font name and font size.
131
+
132
+ Parameters:
133
+ font_name (str): The name of the font to load. Can be a font name recognized by the system, a URL to download the font file,
134
+ a local file path, or a Hugging Face model hub identifier.
135
+ font_size (int, optional): The size of the font. Default is 16.
136
+
137
+ Returns:
138
+ ImageFont.FreeTypeFont: The loaded font object.
139
+
140
+ Notes:
141
+ This function attempts to load the font using various methods until a suitable font is found. If the provided font_name
142
+ cannot be loaded, it falls back to a default font.
143
+
144
+ The font_name can be one of the following:
145
+ - A font name recognized by the system, which can be loaded using ImageFont.truetype.
146
+ - A URL pointing to the font file, which is downloaded using requests and then loaded using ImageFont.truetype.
147
+ - A local file path to the font file, which is loaded using ImageFont.truetype.
148
+ - A Hugging Face model hub identifier, which downloads the font file from the Hugging Face model hub using hf_hub_download
149
+ and then loads it using ImageFont.truetype.
150
+
151
+ Example:
152
+ font = load_font("Arial.ttf", font_size=20)
153
+ """
154
+
155
  try:
156
+ font = ImageFont.truetype(font_name, font_size)
157
+ except (FileNotFoundError, OSError):
158
  try:
159
+ font = ImageFont.truetype(font_name, font_size)
160
  except:
161
+ try:
162
+ req = requests.get(font_name)
163
+ font = ImageFont.truetype(BytesIO(req.content), font_size)
164
+ except:
165
+ try:
166
+ font = ImageFont.truetype(hf_hub_download(".assets", font_name), encoding="UTF-8")
167
+ except:
168
+ font = ImageFont.load_default()
169
  return font
170
 
171
+
172
  def add_settings_to_image(title: str = "title", description: str = "", width: int = 768, height: int = 512, background_path: str = "", font: str = "arial.ttf", font_color: str = "#ffffff"):
173
  # Create a new RGBA image with the specified dimensions
174
  image = Image.new("RGBA", (width, height), (255, 255, 255, 0))
 
185
  text_x = width // 2
186
  text_y = height // 2
187
  # Draw the title text at the center top
188
+ title_font = load_font(font, 26) # Replace with your desired font and size
189
 
190
  title_text = '\n'.join(textwrap.wrap(title, width // 12))
191
  title_x, title_y, title_text_width, title_text_height = title_font.getbbox(title_text)
 
194
  title_draw = ImageDraw.Draw(image)
195
  title_draw.multiline_text((title_x, title_y), title, fill=font_color, font=title_font, align="center")
196
  # Draw the description text two lines below the title
197
+ description_font = load_font(font, 16) # Replace with your desired font and size
198
  description_text = '\n'.join(textwrap.wrap(description, width // 12))
199
  description_x, description_y, description_text_width, description_text_height = description_font.getbbox(description_text)
200
  description_x = max(text_x - (description_text_width // 2), description_x, 0)