File size: 7,938 Bytes
b8b4d41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a50564f
b8b4d41
d1e907c
75ef897
 
7c7af18
75ef897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7034157
 
 
 
 
 
 
75ef897
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c7af18
 
75ef897
 
7c7af18
 
 
75ef897
 
 
 
 
 
 
 
 
 
b8b4d41
d1e907c
b8b4d41
87a8f85
3ebcb20
f93363c
b8b4d41
 
87a8f85
b8b4d41
 
 
 
 
87a8f85
 
 
 
 
 
 
 
 
 
 
 
 
 
8e27621
87a8f85
 
 
 
8e27621
 
87a8f85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b8b4d41
 
87a8f85
 
b8b4d41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec74e25
b8b4d41
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python
# coding: utf-8

# In[1]:


# pip install langdetect
# pip install sentencepiece
# pip install boto3
# pip install awscli
# pip install sacremoses



import gradio as gr
from transformers import pipeline, AutoTokenizer, TFAutoModelForSeq2SeqLM
from dotenv import load_dotenv
import os
import subprocess
import torch
import tempfile
from langdetect import detect
from transformers import MarianMTModel, MarianTokenizer
import re
import boto3


# import functions from functions file

from functions import handle_query, transcribe_audio_original, submit_question, polly_text_to_speech, translate, translate_and_speech, clear_inputs, voice_map, language_map, default_language, languages

# build app theme

theme = gr.themes.Soft(
    secondary_hue="teal",
    neutral_hue="slate",
    font=[gr.themes.GoogleFont('Source Sans Pro'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
).set(
    background_fill_primary='white',
    
    #change background color here
    body_background_fill='*primary_100',
    
    shadow_drop='rgba(0,0,0,0.05) 0px 1px 2px 0px',
    shadow_drop_lg='0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1)',
    shadow_spread='3px',
    block_background_fill='*background_fill_primary',
    block_border_width='1px',
    block_border_width_dark='1px',
    block_label_background_fill='*background_fill_primary',
    block_label_background_fill_dark='*background_fill_secondary',
    block_label_text_color='*neutral_500',
    block_label_text_color_dark='*neutral_200',
    block_label_margin='0',
    block_label_padding='*spacing_sm *spacing_lg',
    block_label_radius='calc(*radius_lg - 1px) 0 calc(*radius_lg - 1px) 0',
    block_label_text_size='*text_sm',
    block_label_text_weight='400',
    block_title_background_fill='none',
    block_title_background_fill_dark='none',
    block_title_text_color='*neutral_500',
    block_title_text_color_dark='*neutral_200',
    block_title_padding='0',
    block_title_radius='none',
    block_title_text_weight='400',
    
    #panel border width change here
    panel_border_width='1px',
    
    #try panel border here
    panel_border_color='*neutral_900',
    
    panel_border_width_dark='0',
    checkbox_background_color_selected='*secondary_600',
    checkbox_background_color_selected_dark='*secondary_600',
    checkbox_border_color='*neutral_300',
    checkbox_border_color_dark='*neutral_700',
    checkbox_border_color_focus='*secondary_500',
    checkbox_border_color_focus_dark='*secondary_500',
    checkbox_border_color_selected='*secondary_600',
    checkbox_border_color_selected_dark='*secondary_600',
    checkbox_border_width='*input_border_width',
    checkbox_shadow='*input_shadow',
    checkbox_label_background_fill_selected='*checkbox_label_background_fill',
    checkbox_label_background_fill_selected_dark='*checkbox_label_background_fill',
    checkbox_label_shadow='none',
    checkbox_label_text_color_selected='*checkbox_label_text_color',
    input_background_fill='*neutral_100',
    input_border_color='*border_color_primary',
    input_shadow='none',
    input_shadow_dark='none',
    input_shadow_focus='*input_shadow',
    input_shadow_focus_dark='*input_shadow',
    slider_color='#2563eb',
    slider_color_dark='#2563eb',
    button_shadow='none',
    button_shadow_active='none',
    button_shadow_hover='none',
    
    #change button color here
    button_primary_background_fill='linear-gradient(45deg, *primary_500, *secondary_200)',
    # *primary_300',
    button_primary_background_fill_hover='*button_primary_background_fill',
    button_primary_background_fill_hover_dark='*button_primary_background_fill',
    
    #change button text color here
    button_primary_text_color='*neutral_900',
    
    #change button color here
    button_secondary_background_fill='*secondary_500',
    button_secondary_background_fill_hover='*button_secondary_background_fill',
    button_secondary_background_fill_hover_dark='*button_secondary_background_fill',
    button_secondary_text_color='*neutral_700',
    button_cancel_background_fill_hover='*button_cancel_background_fill',
    button_cancel_background_fill_hover_dark='*button_cancel_background_fill'
)


# Build Gradio App interface

instructions="""
# Diabetes Assistant! 
## Have questions about Diabetes? Submit questions here through audio recording/text. Receive a response. Translate to selected language."""


with gr.Blocks(theme=theme) as app2:
    
    with gr.Row():
        gr.Markdown(instructions)
        
    with gr.Row():
        with gr.Column(variant="panel", scale=1):
            # gr.Markdown("## Step 1: Enter question by recording audio")
            input_audio = gr.Audio(
                label="Click the microphone to record audio",
                type="filepath",
                waveform_options=gr.WaveformOptions(
                    waveform_color="#01C6FF",
                    waveform_progress_color="#0066B4",
                    skip_length=2,
                    show_controls=False,)
            )
            # gr.Markdown("## Step 2: Transcribe audio into text")
            transcribe_button = gr.Button("Transcribe audio", variant="primary")
            # gr.Markdown("## Step 3: Transcribed audio appears here. Or, type your question here.")
            query_text = gr.Textbox(placeholder="Transcribed audio appears here. Or, type a question here.", label="Your query")
            # gr.Markdown("## Submit your question")
            submit_button = gr.Button("Submit your question", variant="primary")
            
        with gr.Column(variant="panel", scale=2): 
            response_text = gr.Textbox(label="Assistant response")
            response_speech = gr.Audio(label="Assistant response speech",
                                       waveform_options=gr.WaveformOptions(
                                           waveform_color="#01C6FF",
                                           waveform_progress_color="#0066B4",
                                           skip_length=2,
                                           show_controls=False,))
            
        with gr.Column(variant="panel", scale=2):
            language_dropdown = gr.Dropdown(label="Click the middle of the dropdown bar to select translation language",
                                            choices=list(language_map.keys()), value=default_language, type='value')
            translate_button = gr.Button("Translate the response", variant="primary")
            output_translated = gr.Textbox(label="Translated text")
            output_translated_speech = gr.Audio(label="Translated speech",
                                                waveform_options=gr.WaveformOptions(
                                                    waveform_color="#01C6FF",
                                                    waveform_progress_color="#0066B4",
                                                    skip_length=2,
                                                    show_controls=False,))
    
    with gr.Row():
        clear_button = gr.Button("Clear All", variant="primary") 
                

    # Audio transcription
    transcribe_button.click(
        fn=transcribe_audio_original,
        inputs=[input_audio],
        outputs=[query_text]
    )
    
    submit_button.click(
        fn=submit_question,
        inputs=[input_audio, query_text, language_dropdown],
        outputs=[response_text, response_speech]
    )
        
    # Translation
    translate_button.click(
        fn=translate_and_speech,
        inputs=[response_text, language_dropdown],
        outputs=[output_translated, output_translated_speech]
    )
        
    #Clearing all inputs and outputs
    clear_button.click(
    fn=clear_inputs,
    inputs=[],
    outputs=[input_audio, query_text, response_text, response_speech, output_translated, output_translated_speech]
    )

app2.launch(show_error=True)