File size: 2,834 Bytes
811d3bb
 
ad6977e
0d1b680
262be18
7402cfe
63054ac
2f6fb63
0d1b680
7402cfe
bac7e09
c89dae8
0d1b680
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bac7e09
 
 
0d1b680
bac7e09
0d1b680
 
 
bac7e09
 
7402cfe
ad6977e
 
bac7e09
 
 
0d1b680
 
 
 
 
 
 
 
 
 
 
 
7402cfe
0d1b680
 
 
 
 
 
c89dae8
0d1b680
 
c89dae8
7402cfe
0d1b680
bac7e09
 
7402cfe
bac7e09
0d1b680
bac7e09
 
0d1b680
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
75
76
77
78
79
80
import gradio as gr
import time
from transformers import pipeline
from gtts import gTTS

# Initialize the chatbot model
pipe = pipeline("text-generation", model="sriramgs/RPL_gpt2_new")
asr = pipeline("automatic-speech-recognition", model="openai/whisper-small")
global bot_message
# Create a Gradio interface
with gr.Blocks() as demo:

    chatbot = gr.Chatbot(avatar_images=("human.png", "bot.png"), value=[[None, "Welcome to the Indore-Ekk Number Superstore family! We're thrilled to have you on board. \n How can I assist you today?"]])
    with gr.Row(label="Voice Input and Output"):
        with gr.Column(variant="panel"):
            audio_file = gr.Audio(label='Voice based Input',source="microphone",type="filepath",optional=True)
        with gr.Column(variant="panel"):
            play_audio = gr.Audio(label='Output Audio', autoplay=True)
    audio_out = gr.Textbox(visible=False)

    with gr.Row(label="Voice Input and Output"):
        with gr.Column(label='Text Based Input', variant="panel"):
            msg = gr.Textbox(placeholder="Ask me your doubts")
        with gr.Column(variant="panel"):
          with gr.Row():
            clear = gr.Button("Clear the Chatbot Conversation")

    def text_to_speech(text):
      var = gTTS(text = text,lang = 'en')
      var.save('eng.mp3')
      return gr.Audio.update(value='eng.mp3')

    def user(user_message, history):
        global query
        global fck
        query = user_message
        fck = model_response(query)
        print(user_message,fck)
        return '', history + [[user_message, None]],gr.Textbox.update(value=fck)

    def model_response(query):
        global a
        a = pipe("Q: " + str(query))
        a = a[0]['generated_text'].split('\n')[1][3:]
        return a

    def bot(history):
      global bot_message
      bot_message = model_response(query)
      history[-1][1] = ""
      for character in fck:
          history[-1][1] += character
          time.sleep(0.05)
          yield history


    def speech_to_text(audio_file,history):
      if audio_file == None:
        return "", history + [[None, None]]
      else:
        global query
        global fck
        text = asr(audio_file)["text"]
        query = text
        fck = model_response(query)
        print(text)
        return None, history + [[text, None]],gr.Textbox.update(value=fck)
        #return text

    audio_file.stop_recording(speech_to_text, [audio_file,chatbot], [audio_file,chatbot,audio_out], queue=False, show_progress=False).then(bot, chatbot, chatbot)

    msg.submit(user, [msg, chatbot], [msg, chatbot,audio_out], queue=False).then(
        bot, chatbot, chatbot
    )

    clear.click(lambda: None, None, chatbot, queue=False)
    audio_out.change(text_to_speech,inputs=[audio_out], outputs=play_audio)

demo.queue()
demo.launch(debug=True)