Omarrran commited on
Commit
360b6d9
·
verified ·
1 Parent(s): 21883a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -16
app.py CHANGED
@@ -205,10 +205,13 @@ class TTSDatasetCollector:
205
  self.current_font = font_style
206
  return True, f"Font style set to {font_style}"
207
 
208
- def add_custom_font(self, font_file) -> Tuple[bool, str]:
209
  """Add a custom font from the uploaded TTF file"""
210
  try:
211
- if not font_file.name.endswith('.ttf'):
 
 
 
212
  return False, "Only .ttf font files are supported"
213
 
214
  # Generate a unique font family name
@@ -216,17 +219,13 @@ class TTSDatasetCollector:
216
  font_filename = font_family + '.ttf'
217
  font_dest = self.fonts_path / font_filename
218
 
219
- # Read the font file content
220
- font_file.seek(0)
221
- font_content = font_file.read()
222
-
223
- # Save the font file
224
- with open(font_dest, 'wb') as f:
225
- f.write(font_content)
226
 
227
  # Add to custom fonts
228
  self.custom_fonts[font_family] = {
229
- 'name': font_file.name,
230
  'family': font_family,
231
  'css': f"font-family: '{font_family}', serif;"
232
  }
@@ -235,8 +234,8 @@ class TTSDatasetCollector:
235
  FONT_STYLES[font_family] = self.custom_fonts[font_family]
236
 
237
  # Log success
238
- self.log_operation(f"Added custom font: {font_file.name} as {font_family}")
239
- return True, f"Custom font '{font_file.name}' added successfully"
240
 
241
  except Exception as e:
242
  error_msg = f"Error adding custom font: {str(e)}"
@@ -575,7 +574,7 @@ def create_interface():
575
  label="Upload Custom Font (.ttf)",
576
  file_types=[".ttf"],
577
  elem_classes=["small-input"],
578
- type="file" # Ensure we get a file-like object
579
  )
580
  add_font_btn = gr.Button("Add Custom Font")
581
 
@@ -738,11 +737,11 @@ def create_interface():
738
  status: nav_info['status']
739
  }
740
 
741
- def add_custom_font(font_file):
742
  """Handle adding a custom font"""
743
- if not font_file:
744
  return {status: "⚠️ No font file selected"}
745
- success, msg = collector.add_custom_font(font_file)
746
  if not success:
747
  return {status: f"❌ {msg}"}
748
  # Update font dropdown
 
205
  self.current_font = font_style
206
  return True, f"Font style set to {font_style}"
207
 
208
+ def add_custom_font(self, font_file_path) -> Tuple[bool, str]:
209
  """Add a custom font from the uploaded TTF file"""
210
  try:
211
+ if not font_file_path:
212
+ return False, "No font file provided"
213
+
214
+ if not font_file_path.endswith('.ttf'):
215
  return False, "Only .ttf font files are supported"
216
 
217
  # Generate a unique font family name
 
219
  font_filename = font_family + '.ttf'
220
  font_dest = self.fonts_path / font_filename
221
 
222
+ # Read and save the font file
223
+ with open(font_file_path, 'rb') as f_src, open(font_dest, 'wb') as f_dest:
224
+ f_dest.write(f_src.read())
 
 
 
 
225
 
226
  # Add to custom fonts
227
  self.custom_fonts[font_family] = {
228
+ 'name': os.path.basename(font_file_path),
229
  'family': font_family,
230
  'css': f"font-family: '{font_family}', serif;"
231
  }
 
234
  FONT_STYLES[font_family] = self.custom_fonts[font_family]
235
 
236
  # Log success
237
+ self.log_operation(f"Added custom font: {font_file_path} as {font_family}")
238
+ return True, f"Custom font '{os.path.basename(font_file_path)}' added successfully"
239
 
240
  except Exception as e:
241
  error_msg = f"Error adding custom font: {str(e)}"
 
574
  label="Upload Custom Font (.ttf)",
575
  file_types=[".ttf"],
576
  elem_classes=["small-input"],
577
+ type="filepath"
578
  )
579
  add_font_btn = gr.Button("Add Custom Font")
580
 
 
737
  status: nav_info['status']
738
  }
739
 
740
+ def add_custom_font(font_file_path):
741
  """Handle adding a custom font"""
742
+ if not font_file_path:
743
  return {status: "⚠️ No font file selected"}
744
+ success, msg = collector.add_custom_font(font_file_path)
745
  if not success:
746
  return {status: f"❌ {msg}"}
747
  # Update font dropdown