forked from Sunbird-AIAssistant/sakhi-api-service
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathio_processing.py
74 lines (65 loc) · 2.74 KB
/
io_processing.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import time
from logger import logger
from env_manager import translate_class as translator
from utils import get_from_env_or_config
DEFAULT_LANGAUGE = get_from_env_or_config('default', 'language', None)
def process_incoming_voice(file_url, input_language):
"""
Main Function for processing audio based queries
"""
error_message = None
try:
regional_text = translator.speech_to_text(file_url, input_language)
try:
english_text = translator.translate_text(text=regional_text, source=input_language, destination=DEFAULT_LANGAUGE)
except Exception as e:
error_message = "Indic translation to English failed"
logger.error(f"Exception occurred: {e}", exc_info=True)
english_text = None
except Exception as e:
error_message = "Speech to text conversion API failed"
logger.error(f"Exception occurred: {e}", exc_info=True)
regional_text = None
english_text = None
return regional_text, english_text, error_message
def process_incoming_text(regional_text, input_language):
"""
Main function for processing text queries
"""
error_message = None
try:
english_text = translator.translate_text(text=regional_text, source=input_language, destination=DEFAULT_LANGAUGE)
except Exception as e:
error_message = "Indic translation to English failed"
english_text = None
logger.error(f"Exception occurred: {e}", exc_info=True)
return english_text, error_message
def process_outgoing_text(english_text, input_language):
"""
Main func for generating text response
"""
error_message = None
try:
regional_text = translator.translate_text(text=english_text, source=DEFAULT_LANGAUGE, destination=input_language)
except Exception as e:
error_message = "English translation to indic language failed"
logger.error(f"Exception occurred: {e}", exc_info=True)
regional_text = None
return regional_text, error_message
def process_outgoing_voice(message, input_language):
"""
Main function for generating audio response
"""
error_message = None
decoded_audio_content = translator.text_to_speech(language=input_language, text=message)
if decoded_audio_content is not None:
logger.info("Creating output MP3 file")
time_stamp = time.strftime("%Y%m%d-%H%M%S")
filename = "audio-output-" + time_stamp + ".mp3"
output_mp3_file = open(filename, "wb")
output_mp3_file.write(decoded_audio_content)
logger.info("Audio Response is saved as a MP3 file.")
return output_mp3_file, error_message
error_message = "Text to Audio conversion failed"
logger.error(error_message)
return None, error_message