Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -105,16 +105,33 @@ def render_file_uploader():
|
|
| 105 |
def save_uploaded_file(uploaded_file):
|
| 106 |
"""Save uploaded file to a temporary file and return the path."""
|
| 107 |
try:
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
return tmp_file.name
|
| 111 |
except Exception as e:
|
| 112 |
st.error(f"Error saving file: {str(e)}")
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
def transcribe_audio(file_path, model_name):
|
| 116 |
"""Transcribe audio using the specified model."""
|
| 117 |
try:
|
|
|
|
|
|
|
|
|
|
| 118 |
# Debug: Show authentication status
|
| 119 |
hf_token = os.getenv('HUGGINGFACE_TOKEN') or os.getenv('HF_TOKEN')
|
| 120 |
st.sidebar.info(f"π Using model: {model_name}")
|
|
@@ -124,29 +141,29 @@ def transcribe_audio(file_path, model_name):
|
|
| 124 |
|
| 125 |
# Read the audio file
|
| 126 |
try:
|
| 127 |
-
audio_data, sample_rate = librosa.load(file_path, sr=16000)
|
| 128 |
-
st.sidebar.info(f"π Loaded audio: {len(audio_data)}
|
|
|
|
| 129 |
except Exception as e:
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
# Transcribe
|
| 134 |
with st.spinner("π Transcribing audio..."):
|
| 135 |
result = transcriber.transcribe_audio(audio_data, sample_rate)
|
| 136 |
|
| 137 |
if not result or 'text' not in result:
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
return result
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
-
st.error(
|
| 145 |
-
st.error("This might be due to:")
|
| 146 |
-
st.error("1. Invalid or missing Hugging Face token")
|
| 147 |
-
st.error("2. Insufficient permissions for the model")
|
| 148 |
-
st.error("3. Network connectivity issues")
|
| 149 |
-
st.error("4. Model not found or not accessible")
|
|
|
|
| 150 |
|
| 151 |
# Add debug info
|
| 152 |
st.sidebar.error("π Debug Info:")
|
|
|
|
| 105 |
def save_uploaded_file(uploaded_file):
|
| 106 |
"""Save uploaded file to a temporary file and return the path."""
|
| 107 |
try:
|
| 108 |
+
#Validate file type
|
| 109 |
+
allowed_types = ["wav", "mp3", "ogg"]
|
| 110 |
+
file_ext = os.path.splitext(uploaded_file.name)[1].lower()
|
| 111 |
+
if file_ext not in allowed_types:
|
| 112 |
+
raise ValueError(f"Unsupported file type: {file_ext}. Allowed: {', '.join(allowed_types)}")
|
| 113 |
+
|
| 114 |
+
#Create temp file
|
| 115 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=f".{file_ext}") as tmp_file:
|
| 116 |
+
#Read files in chunks to handle large files
|
| 117 |
+
for chunk in uploaded_file.chunks(4096):
|
| 118 |
+
tmp_file.write(chunk)
|
| 119 |
return tmp_file.name
|
| 120 |
except Exception as e:
|
| 121 |
st.error(f"Error saving file: {str(e)}")
|
| 122 |
+
if 'tmp_file' in locals() and os.path.exists(tmp_file.name):
|
| 123 |
+
try:
|
| 124 |
+
os.unlink(tmp_file.name)
|
| 125 |
+
except:
|
| 126 |
+
pass
|
| 127 |
+
raise
|
| 128 |
|
| 129 |
def transcribe_audio(file_path, model_name):
|
| 130 |
"""Transcribe audio using the specified model."""
|
| 131 |
try:
|
| 132 |
+
#debug info
|
| 133 |
+
st.sidebar.info("βΉοΈ Starting transcription...")
|
| 134 |
+
|
| 135 |
# Debug: Show authentication status
|
| 136 |
hf_token = os.getenv('HUGGINGFACE_TOKEN') or os.getenv('HF_TOKEN')
|
| 137 |
st.sidebar.info(f"π Using model: {model_name}")
|
|
|
|
| 141 |
|
| 142 |
# Read the audio file
|
| 143 |
try:
|
| 144 |
+
audio_data, sample_rate = librosa.load(file_path, sr=16000, mono=True)
|
| 145 |
+
st.sidebar.info(f"π Loaded audio: {len(audio_data)/sample_rate:.2f} seconds @ {sample_rate}Hz")
|
| 146 |
+
|
| 147 |
except Exception as e:
|
| 148 |
+
raise Exception(f"β Error loading audio file: {str(e)}")
|
| 149 |
+
|
|
|
|
| 150 |
# Transcribe
|
| 151 |
with st.spinner("π Transcribing audio..."):
|
| 152 |
result = transcriber.transcribe_audio(audio_data, sample_rate)
|
| 153 |
|
| 154 |
if not result or 'text' not in result:
|
| 155 |
+
raise ValueError("β No transcription results returned. The model might not be accessible.")
|
| 156 |
+
|
|
|
|
| 157 |
return result
|
| 158 |
|
| 159 |
except Exception as e:
|
| 160 |
+
st.sidebar.error("β Transcription failed: {str(e)}")
|
| 161 |
+
st.sidebar.error("This might be due to:")
|
| 162 |
+
st.sidebar.error("1. Invalid or missing Hugging Face token")
|
| 163 |
+
st.sidebar.error("2. Insufficient permissions for the model")
|
| 164 |
+
st.sidebar.error("3. Network connectivity issues")
|
| 165 |
+
st.sidebar.error("4. Model not found or not accessible")
|
| 166 |
+
raise
|
| 167 |
|
| 168 |
# Add debug info
|
| 169 |
st.sidebar.error("π Debug Info:")
|