File size: 1,823 Bytes
065e2bf
 
 
 
59314b3
065e2bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1e9e90
065e2bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f1e9e90
 
 
 
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
"""Deploying AI Voice Chatbot Gradio App."""
from gradio import Audio, Interface, Textbox
from typing import Tuple

from Voicechat_Hindi.utils import (TextGenerationPipeline, from_en_translation,
                   html_audio_autoplay, stt, to_en_translation, tts,
                   tts_to_bytesio)

max_answer_length = 100
desired_language = "hi"
response_generator_pipe = TextGenerationPipeline(max_length=max_answer_length)


def main(audio: object) -> Tuple[str, str, str, object]:
    """Calls functions for deploying gradio app.
    It responds both verbally and in text
    by taking voice input from user.
    Args:
        audio (object): recorded speech of user
    Returns:
        tuple containing
        - user_speech_text (str) : recognized speech
        - bot_response_hi (str) : translated answer of bot
        - bot_response_en (str) : bot's original answer
        - html (object) : autoplayer for bot's speech
    """
    user_speech_text = stt(audio, desired_language)
    tranlated_text = to_en_translation(user_speech_text, desired_language)
    bot_response_en = response_generator_pipe(tranlated_text)
    bot_response_hi = from_en_translation(bot_response_en, desired_language)
    bot_voice = tts(bot_response_hi, desired_language)
    bot_voice_bytes = tts_to_bytesio(bot_voice)
    html = html_audio_autoplay(bot_voice_bytes)
    return user_speech_text, bot_response_hi, bot_response_en, html


interface = Interface(
    fn=main,
    inputs=[
        Audio(
            source="microphone",
            type="filepath",
        ),
    ],
    outputs=[
        Textbox(label="You said: "),
        Textbox(label="AI: "),
        Textbox(label="AI (English): "),
        "html",
    ],
    live=True,
    allow_flagging="never",
)

if __name__ == '__main__':
    interface.launch(debug=True)