Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import
|
2 |
import numpy as np
|
3 |
import librosa
|
4 |
import pickle
|
@@ -6,8 +6,6 @@ from jiwer import wer, cer
|
|
6 |
import tensorflow as tf
|
7 |
from io import BytesIO
|
8 |
import soundfile as sf
|
9 |
-
from flask import Flask, request, jsonify
|
10 |
-
from werkzeug.utils import secure_filename
|
11 |
|
12 |
class TwiTranscriptionModel:
|
13 |
def __init__(self, encoder_model, decoder_model, char_tokenizer, max_length=50):
|
@@ -52,8 +50,10 @@ class TwiTranscriptionModel:
|
|
52 |
|
53 |
return transcriptions
|
54 |
|
|
|
55 |
def load_model():
|
56 |
try:
|
|
|
57 |
with open('twi_transcription_model.pkl', 'rb') as f:
|
58 |
model_data = pickle.load(f)
|
59 |
return TwiTranscriptionModel(
|
@@ -63,7 +63,7 @@ def load_model():
|
|
63 |
model_data['max_length']
|
64 |
)
|
65 |
except Exception as e:
|
66 |
-
|
67 |
return None
|
68 |
|
69 |
def extract_mfcc(audio_data, sr=16000, n_mfcc=13):
|
@@ -88,103 +88,84 @@ def calculate_error_rates(reference, hypothesis):
|
|
88 |
except Exception as e:
|
89 |
return None, None
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
#
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
# Calculate error metrics if reference text is provided
|
173 |
-
if reference_text:
|
174 |
-
error_wer, error_cer = calculate_error_rates(reference_text, transcription)
|
175 |
-
if error_wer is not None and error_cer is not None:
|
176 |
-
response_data['error_metrics'] = {
|
177 |
-
'word_error_rate': round(float(error_wer), 4),
|
178 |
-
'character_error_rate': round(float(error_cer), 4)
|
179 |
-
}
|
180 |
-
|
181 |
-
return jsonify(response_data)
|
182 |
-
|
183 |
-
except Exception as e:
|
184 |
-
return jsonify({
|
185 |
-
'status': 'error',
|
186 |
-
'message': str(e)
|
187 |
-
}), 500
|
188 |
-
|
189 |
-
if __name__ == '__main__':
|
190 |
-
app.run(debug=True)
|
|
|
1 |
+
import streamlit as st
|
2 |
import numpy as np
|
3 |
import librosa
|
4 |
import pickle
|
|
|
6 |
import tensorflow as tf
|
7 |
from io import BytesIO
|
8 |
import soundfile as sf
|
|
|
|
|
9 |
|
10 |
class TwiTranscriptionModel:
|
11 |
def __init__(self, encoder_model, decoder_model, char_tokenizer, max_length=50):
|
|
|
50 |
|
51 |
return transcriptions
|
52 |
|
53 |
+
@st.cache_resource
|
54 |
def load_model():
|
55 |
try:
|
56 |
+
# Modify this path if your model is stored differently in Hugging Face
|
57 |
with open('twi_transcription_model.pkl', 'rb') as f:
|
58 |
model_data = pickle.load(f)
|
59 |
return TwiTranscriptionModel(
|
|
|
63 |
model_data['max_length']
|
64 |
)
|
65 |
except Exception as e:
|
66 |
+
st.error(f"Error loading model: {str(e)}")
|
67 |
return None
|
68 |
|
69 |
def extract_mfcc(audio_data, sr=16000, n_mfcc=13):
|
|
|
88 |
except Exception as e:
|
89 |
return None, None
|
90 |
|
91 |
+
def main():
|
92 |
+
st.set_page_config(
|
93 |
+
page_title="Twi Speech Recognition",
|
94 |
+
page_icon="🎤",
|
95 |
+
layout="wide"
|
96 |
+
)
|
97 |
+
|
98 |
+
# Load the model
|
99 |
+
model = load_model()
|
100 |
+
if model is None:
|
101 |
+
st.error("Failed to load model. Please check model file.")
|
102 |
+
return
|
103 |
+
|
104 |
+
st.title("Twi Speech Transcription")
|
105 |
+
st.write("Upload an audio file to transcribe Twi speech")
|
106 |
+
|
107 |
+
# File uploader
|
108 |
+
audio_file = st.file_uploader("Choose an audio file", type=['wav', 'mp3', 'ogg'])
|
109 |
+
|
110 |
+
# Optional reference text
|
111 |
+
reference_text = st.text_area("Reference text (optional)",
|
112 |
+
help="Enter the correct transcription to calculate error rates")
|
113 |
+
|
114 |
+
if audio_file is not None:
|
115 |
+
if st.button("Transcribe"):
|
116 |
+
with st.spinner("Processing audio... This may take a moment."):
|
117 |
+
try:
|
118 |
+
# Read audio file
|
119 |
+
audio_data, sr = librosa.load(audio_file, sr=None)
|
120 |
+
if len(audio_data.shape) > 1:
|
121 |
+
audio_data = np.mean(audio_data, axis=1)
|
122 |
+
|
123 |
+
# Extract features
|
124 |
+
mfcc_features = extract_mfcc(audio_data, sr)
|
125 |
+
mfcc_features = np.expand_dims(mfcc_features, axis=0)
|
126 |
+
|
127 |
+
# Get transcription
|
128 |
+
transcription = model.predict(mfcc_features)[0]
|
129 |
+
|
130 |
+
# Display results
|
131 |
+
st.success("Transcription completed!")
|
132 |
+
|
133 |
+
# Audio Playback
|
134 |
+
st.audio(audio_file, format='audio/wav')
|
135 |
+
|
136 |
+
# Transcription Display
|
137 |
+
st.write("### Transcription:")
|
138 |
+
st.write(transcription)
|
139 |
+
|
140 |
+
# Audio Details
|
141 |
+
st.write("### Audio Details:")
|
142 |
+
st.json({
|
143 |
+
'sample_rate': int(sr),
|
144 |
+
'duration': float(len(audio_data) / sr)
|
145 |
+
})
|
146 |
+
|
147 |
+
# Error Metrics (if reference text provided)
|
148 |
+
if reference_text:
|
149 |
+
error_wer, error_cer = calculate_error_rates(reference_text, transcription)
|
150 |
+
if error_wer is not None and error_cer is not None:
|
151 |
+
st.write("### Error Metrics:")
|
152 |
+
st.json({
|
153 |
+
'word_error_rate': round(float(error_wer), 4),
|
154 |
+
'character_error_rate': round(float(error_cer), 4)
|
155 |
+
})
|
156 |
+
|
157 |
+
except Exception as e:
|
158 |
+
st.error(f"Error processing audio: {str(e)}")
|
159 |
+
|
160 |
+
if __name__ == "__main__":
|
161 |
+
main()
|
162 |
+
|
163 |
+
# Requirements for Hugging Face (create a requirements.txt)
|
164 |
+
"""
|
165 |
+
streamlit
|
166 |
+
numpy
|
167 |
+
librosa
|
168 |
+
tensorflow
|
169 |
+
jiwer
|
170 |
+
soundfile
|
171 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|