Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -152,7 +152,145 @@ class TTSDatasetCollector:
|
|
| 152 |
logger.error(traceback.format_exc())
|
| 153 |
return False, error_msg
|
| 154 |
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
def create_interface():
|
| 158 |
"""Create Gradio interface with enhanced features"""
|
|
@@ -288,11 +426,9 @@ if __name__ == "__main__":
|
|
| 288 |
interface.launch(
|
| 289 |
server_name="0.0.0.0",
|
| 290 |
server_port=7860,
|
| 291 |
-
share=True
|
| 292 |
-
enable_queue=True
|
| 293 |
)
|
| 294 |
except Exception as e:
|
| 295 |
logger.error(f"Failed to launch interface: {str(e)}")
|
| 296 |
logger.error(traceback.format_exc())
|
| 297 |
raise
|
| 298 |
-
|
|
|
|
| 152 |
logger.error(traceback.format_exc())
|
| 153 |
return False, error_msg
|
| 154 |
|
| 155 |
+
def get_styled_text(self, text: str) -> str:
|
| 156 |
+
"""Get text with current font styling"""
|
| 157 |
+
font_css = FONT_STYLES[self.current_font]['css']
|
| 158 |
+
return f"<div style='{font_css}'>{text}</div>"
|
| 159 |
+
|
| 160 |
+
def generate_filenames(self, dataset_name: str, speaker_id: str) -> Tuple[str, str]:
|
| 161 |
+
"""Generate unique filenames for audio and text files"""
|
| 162 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 163 |
+
sentence_id = f"{self.current_index+1:04d}"
|
| 164 |
+
base_name = f"{dataset_name}_{speaker_id}_{sentence_id}_{timestamp}"
|
| 165 |
+
return f"{base_name}.wav", f"{base_name}.txt"
|
| 166 |
+
|
| 167 |
+
def save_recording(self, audio_file, speaker_id: str, dataset_name: str) -> Tuple[bool, str]:
|
| 168 |
+
"""Save recording with enhanced error handling and logging"""
|
| 169 |
+
if not all([audio_file, speaker_id, dataset_name]):
|
| 170 |
+
missing = []
|
| 171 |
+
if not audio_file: missing.append("audio recording")
|
| 172 |
+
if not speaker_id: missing.append("speaker ID")
|
| 173 |
+
if not dataset_name: missing.append("dataset name")
|
| 174 |
+
return False, f"Missing required information: {', '.join(missing)}"
|
| 175 |
+
|
| 176 |
+
try:
|
| 177 |
+
# Validate inputs
|
| 178 |
+
if not speaker_id.strip().isalnum():
|
| 179 |
+
return False, "Speaker ID must contain only letters and numbers"
|
| 180 |
+
|
| 181 |
+
if not dataset_name.strip().isalnum():
|
| 182 |
+
return False, "Dataset name must contain only letters and numbers"
|
| 183 |
+
|
| 184 |
+
# Generate filenames
|
| 185 |
+
audio_name, text_name = self.generate_filenames(dataset_name, speaker_id)
|
| 186 |
+
|
| 187 |
+
# Create speaker directories
|
| 188 |
+
audio_dir = self.root_path / 'audio' / speaker_id
|
| 189 |
+
text_dir = self.root_path / 'transcriptions' / speaker_id
|
| 190 |
+
audio_dir.mkdir(exist_ok=True)
|
| 191 |
+
text_dir.mkdir(exist_ok=True)
|
| 192 |
+
|
| 193 |
+
# Save audio file
|
| 194 |
+
audio_path = audio_dir / audio_name
|
| 195 |
+
shutil.copy2(audio_file, audio_path)
|
| 196 |
+
|
| 197 |
+
# Save transcription
|
| 198 |
+
text_path = text_dir / text_name
|
| 199 |
+
self.save_transcription(
|
| 200 |
+
text_path,
|
| 201 |
+
self.sentences[self.current_index],
|
| 202 |
+
{
|
| 203 |
+
'speaker_id': speaker_id,
|
| 204 |
+
'dataset_name': dataset_name,
|
| 205 |
+
'timestamp': datetime.now().isoformat(),
|
| 206 |
+
'audio_file': audio_name,
|
| 207 |
+
'font_style': self.current_font
|
| 208 |
+
}
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
+
# Update metadata
|
| 212 |
+
self.update_metadata(speaker_id, dataset_name)
|
| 213 |
+
|
| 214 |
+
# Log success
|
| 215 |
+
self.log_operation(
|
| 216 |
+
f"Saved recording: Speaker={speaker_id}, Dataset={dataset_name}, "
|
| 217 |
+
f"Audio={audio_name}, Text={text_name}"
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
+
return True, f"Recording saved successfully as {audio_name}"
|
| 221 |
+
|
| 222 |
+
except Exception as e:
|
| 223 |
+
error_msg = f"Error saving recording: {str(e)}"
|
| 224 |
+
self.log_operation(error_msg, "error")
|
| 225 |
+
logger.error(traceback.format_exc())
|
| 226 |
+
return False, error_msg
|
| 227 |
+
|
| 228 |
+
def save_transcription(self, file_path: Path, text: str, metadata: Dict) -> None:
|
| 229 |
+
"""Save transcription with metadata"""
|
| 230 |
+
content = f"""[METADATA]
|
| 231 |
+
Recording_ID: {metadata['audio_file']}
|
| 232 |
+
Speaker_ID: {metadata['speaker_id']}
|
| 233 |
+
Dataset_Name: {metadata['dataset_name']}
|
| 234 |
+
Timestamp: {metadata['timestamp']}
|
| 235 |
+
Font_Style: {metadata['font_style']}
|
| 236 |
+
|
| 237 |
+
[TEXT]
|
| 238 |
+
{text}
|
| 239 |
+
"""
|
| 240 |
+
with open(file_path, 'w', encoding='utf-8') as f:
|
| 241 |
+
f.write(content)
|
| 242 |
+
|
| 243 |
+
def update_metadata(self, speaker_id: str, dataset_name: str) -> None:
|
| 244 |
+
"""Update dataset metadata with error handling"""
|
| 245 |
+
metadata_file = self.root_path / 'metadata' / 'dataset_info.json'
|
| 246 |
+
|
| 247 |
+
try:
|
| 248 |
+
if metadata_file.exists():
|
| 249 |
+
with open(metadata_file, 'r') as f:
|
| 250 |
+
metadata = json.load(f)
|
| 251 |
+
else:
|
| 252 |
+
metadata = {'speakers': {}, 'last_updated': None}
|
| 253 |
+
|
| 254 |
+
# Update speaker data
|
| 255 |
+
if speaker_id not in metadata['speakers']:
|
| 256 |
+
metadata['speakers'][speaker_id] = {
|
| 257 |
+
'total_recordings': 0,
|
| 258 |
+
'datasets': {}
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
if dataset_name not in metadata['speakers'][speaker_id]['datasets']:
|
| 262 |
+
metadata['speakers'][speaker_id]['datasets'][dataset_name] = {
|
| 263 |
+
'recordings': 0,
|
| 264 |
+
'sentences': len(self.sentences),
|
| 265 |
+
'first_recording': datetime.now().isoformat(),
|
| 266 |
+
'last_recording': None,
|
| 267 |
+
'font_styles_used': []
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
# Update counts and timestamps
|
| 271 |
+
metadata['speakers'][speaker_id]['total_recordings'] += 1
|
| 272 |
+
metadata['speakers'][speaker_id]['datasets'][dataset_name]['recordings'] += 1
|
| 273 |
+
metadata['speakers'][speaker_id]['datasets'][dataset_name]['last_recording'] = \
|
| 274 |
+
datetime.now().isoformat()
|
| 275 |
+
|
| 276 |
+
# Update font styles
|
| 277 |
+
if self.current_font not in metadata['speakers'][speaker_id]['datasets'][dataset_name]['font_styles_used']:
|
| 278 |
+
metadata['speakers'][speaker_id]['datasets'][dataset_name]['font_styles_used'].append(
|
| 279 |
+
self.current_font
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
metadata['last_updated'] = datetime.now().isoformat()
|
| 283 |
+
|
| 284 |
+
# Save updated metadata
|
| 285 |
+
with open(metadata_file, 'w') as f:
|
| 286 |
+
json.dump(metadata, f, indent=2)
|
| 287 |
+
|
| 288 |
+
self.log_operation(f"Updated metadata for {speaker_id} in {dataset_name}")
|
| 289 |
+
|
| 290 |
+
except Exception as e:
|
| 291 |
+
error_msg = f"Error updating metadata: {str(e)}"
|
| 292 |
+
self.log_operation(error_msg, "error")
|
| 293 |
+
logger.error(traceback.format_exc())
|
| 294 |
|
| 295 |
def create_interface():
|
| 296 |
"""Create Gradio interface with enhanced features"""
|
|
|
|
| 426 |
interface.launch(
|
| 427 |
server_name="0.0.0.0",
|
| 428 |
server_port=7860,
|
| 429 |
+
share=True
|
|
|
|
| 430 |
)
|
| 431 |
except Exception as e:
|
| 432 |
logger.error(f"Failed to launch interface: {str(e)}")
|
| 433 |
logger.error(traceback.format_exc())
|
| 434 |
raise
|
|
|