deepugaur's picture
Create app.py
a18c706 verified
raw
history blame
1.26 kB
from datetime import datetime
import tensorflow as tf
from tensorflow.keras.models import load_model
import numpy as np
import librosa
# Load pre-trained models
speech_to_text_model = load_model('speech_to_text_model.h5')
translation_model = load_model('translation_model.h5')
def preprocess_audio(file_path):
# Load and preprocess the audio file
audio, sr = librosa.load(file_path, sr=16000)
mfccs = librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=13)
return np.expand_dims(mfccs, axis=0)
def translate_speech_to_text(audio_file):
# Preprocess the audio file
audio_features = preprocess_audio(audio_file)
# Predict text from audio
predicted_text = speech_to_text_model.predict(audio_features)
# Translate text
translated_text = translation_model.predict([predicted_text])
return translated_text
def is_after_six_pm():
current_time = datetime.now()
return current_time.hour >= 18
def main(audio_file):
if is_after_six_pm():
translated_text = translate_speech_to_text(audio_file)
print("Translated Text:", translated_text)
else:
print("Service available only after 6 PM IST.")
# Example usage
audio_file_path = 'path/to/your/audiofile.wav'
main(audio_file_path)