Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,40 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from datetime import datetime
|
4 |
import tensorflow as tf
|
5 |
-
from tensorflow.keras.models import load_model
|
6 |
import numpy as np
|
7 |
import librosa
|
|
|
8 |
|
9 |
-
# Load
|
10 |
-
speech_to_text_model = load_model('speech_to_text_model.h5')
|
11 |
-
translation_model = load_model('translation_model.h5')
|
12 |
|
13 |
-
def preprocess_audio(
|
14 |
-
|
15 |
-
audio, sr = librosa.load(file_path, sr=16000)
|
16 |
mfccs = librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=13)
|
17 |
return np.expand_dims(mfccs, axis=0)
|
18 |
|
19 |
def translate_speech_to_text(audio_file):
|
20 |
-
# Preprocess the audio file
|
21 |
audio_features = preprocess_audio(audio_file)
|
22 |
-
|
23 |
-
# Predict text from audio
|
24 |
predicted_text = speech_to_text_model.predict(audio_features)
|
25 |
-
|
26 |
-
# Translate text
|
27 |
translated_text = translation_model.predict([predicted_text])
|
28 |
-
|
29 |
return translated_text
|
30 |
|
31 |
def is_after_six_pm():
|
32 |
current_time = datetime.now()
|
33 |
return current_time.hour >= 18
|
34 |
|
35 |
-
def main(
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
main(audio_file_path)
|
|
|
1 |
+
import streamlit as st
|
|
|
|
|
2 |
import tensorflow as tf
|
|
|
3 |
import numpy as np
|
4 |
import librosa
|
5 |
+
from datetime import datetime
|
6 |
|
7 |
+
# Load models
|
8 |
+
speech_to_text_model = tf.keras.models.load_model('speech_to_text_model.h5')
|
9 |
+
translation_model = tf.keras.models.load_model('translation_model.h5')
|
10 |
|
11 |
+
def preprocess_audio(file):
|
12 |
+
audio, sr = librosa.load(file, sr=16000)
|
|
|
13 |
mfccs = librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=13)
|
14 |
return np.expand_dims(mfccs, axis=0)
|
15 |
|
16 |
def translate_speech_to_text(audio_file):
|
|
|
17 |
audio_features = preprocess_audio(audio_file)
|
|
|
|
|
18 |
predicted_text = speech_to_text_model.predict(audio_features)
|
|
|
|
|
19 |
translated_text = translation_model.predict([predicted_text])
|
|
|
20 |
return translated_text
|
21 |
|
22 |
def is_after_six_pm():
|
23 |
current_time = datetime.now()
|
24 |
return current_time.hour >= 18
|
25 |
|
26 |
+
def main():
|
27 |
+
st.title("Audio Translation App")
|
28 |
+
|
29 |
+
uploaded_file = st.file_uploader("Choose an audio file", type="wav")
|
30 |
+
|
31 |
+
if uploaded_file is not None:
|
32 |
+
if is_after_six_pm():
|
33 |
+
st.write("Processing...")
|
34 |
+
translated_text = translate_speech_to_text(uploaded_file)
|
35 |
+
st.write("Translated Text:", translated_text)
|
36 |
+
else:
|
37 |
+
st.write("Service available only after 6 PM IST.")
|
38 |
|
39 |
+
if __name__ == "__main__":
|
40 |
+
main()
|
|