File size: 1,671 Bytes
1ef3798 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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()
|