File size: 2,106 Bytes
afd8033
 
 
 
0e59554
afd8033
0e59554
afd8033
0e59554
afd8033
 
 
 
 
 
 
0e59554
 
afd8033
 
 
 
 
 
 
0e59554
 
 
afd8033
0e59554
afd8033
 
 
 
 
 
47c71f9
afd8033
 
 
0e59554
afd8033
 
0e59554
afd8033
 
b2e67d3
0e59554
afd8033
 
 
 
 
 
0e59554
afd8033
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from main import index, run
from gtts import gTTS
import os

from transformers import pipeline

p = pipeline("automatic-speech-recognition")

"""Use text to call chat method from main.py"""
def add_text(history, text):
    print("Question asked: " + text)
    response = run_model(text)
    history = history + [(text, response)]
    print(history)
    return history, ""


def run_model(text):
    response = run(question=text)
    # If response contains string `SOURCES:`, then add a \n before `SOURCES`
    if "SOURCES:" in response:
        response = response.replace("SOURCES:", "\nSOURCES:")
    print(response)
    return response



def fill_input_textbox(history, audio):

    txt = p(audio)["text"]
    response = run_model(txt)
    # Remove all text from SOURCES: to the end of the string
    trimmed_response = response.split("SOURCES:")[0]
    myobj = gTTS(text=trimmed_response, lang='en', slow=False)
    myobj.save("response.wav")
    audio = audio + ".wav"
    history = history + [((audio, ), ('response.wav', ))]
    print(history)
    return history

def bot(history):
    return history

with gr.Blocks() as demo:
    index()
    chatbot = gr.Chatbot([(None,'Learn about <a href="https://www.coursera.org/learn/3d-printing-revolution/home">3D printing Revolution</a> course with referred sources. Try out the new voice to voice Q&A on the course! ')], elem_id="chatbot").style(height=750)

    with gr.Row():
        with gr.Column(scale=0.85):
            txt = gr.Textbox(
                label="Coursera Voice Q&A Bot",
                placeholder="Enter text and press enter, or upload an image", lines=1
            ).style(container=False)

        with gr.Column(scale=0.15):
            audio = gr.Audio(source="microphone", type="filepath").style(container=False)

    txt.submit(add_text, [chatbot, txt], [chatbot, txt], postprocess=False).then(
        bot, chatbot, chatbot
    )

    audio.change(fn=fill_input_textbox,inputs=[chatbot, audio], outputs=[chatbot]).then(
        bot, chatbot, chatbot
    )

if __name__ == "__main__":
    demo.launch()