Spaces:
Sleeping
Sleeping
File size: 2,147 Bytes
8c369f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | """
VoiceVerse AI — Utility helpers.
Provides temp file management and error formatting
used across the pipeline.
"""
import os
import tempfile
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
logger = logging.getLogger("voiceverse")
def get_temp_filepath(suffix: str = ".wav") -> str:
"""Return a path to a new temporary file that won't be auto-deleted."""
fd, path = tempfile.mkstemp(suffix=suffix)
os.close(fd)
return path
def format_error(stage: str, error: Exception) -> str:
"""
Return a user-friendly error string.
Hides raw tracebacks; logs the full error for debugging.
"""
logger.error("Error in %s: %s", stage, error, exc_info=True)
friendly_messages = {
"upload": "Could not read the uploaded file. Please try a different PDF or TXT file.",
"rag": "Failed to process the document text. The file may be empty or corrupted.",
"script": "Could not generate the audio script. Please check your HF_TOKEN and try again.",
"tts": "Audio generation failed. The system will retry with a fallback voice.",
}
return friendly_messages.get(stage, f"An unexpected error occurred: {stage}")
def validate_file(file_path: str) -> tuple[bool, str]:
"""
Validate an uploaded file. Returns (is_valid, message).
"""
if file_path is None:
return False, "Please upload a PDF or TXT file first."
if not os.path.exists(file_path):
return False, "The uploaded file could not be found. Please try again."
ext = os.path.splitext(file_path)[1].lower()
if ext not in (".pdf", ".txt"):
return False, f"Unsupported file format '{ext}'. Please upload a PDF or TXT file."
size = os.path.getsize(file_path)
if size == 0:
return False, "The uploaded file is empty. Please upload a file with content."
if size > 20 * 1024 * 1024: # 20 MB limit
return False, "File is too large (>20 MB). Please upload a smaller document."
return True, "File is valid."
|