NullClass_Task-3 / Task 3 /english_to_hindi.py
prajwath's picture
Upload english_to_hindi.py
1ef3798 verified
import speech_recognition as sr
from deep_translator import GoogleTranslator
from datetime import datetime, time
import pytz
def hours():
ist = pytz.timezone('Asia/Kolkata')
current_time = datetime.now(ist).time()
start_time = time(18, 0, 0) # 6 PM IST
end_time = time(23, 59, 59) # Midnight
return start_time <= current_time <= end_time
def audio():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Please speak now:")
audio = r.listen(source)
try:
text = r.recognize_google(audio)
print("You said: " + text)
return text
except sr.UnknownValueError:
print("Sorry,could you Please repeat the sentence again.")
return None
except sr.RequestError:
print("Network error. Please check your connection.")
return None
def text(text):
translator = GoogleTranslator(source='en', target='hi')
words = text.split()
translated_words = []
for word in words:
if word[0].upper() not in ['M', 'O']:
translated_word = translator.translate(word)
translated_words.append(translated_word)
else:
translated_words.append(word)
return ' '.join(translated_words)
def main():
if not hours():
print("Translation feature is available only after 6 PM IST.")
return
text = None
while text is None:
text = audio()
if text:
translated_text = text(text)
print("Translated text:", translated_text)
if __name__ == "__main__":
main()