bisoye commited on
Commit
012bf4e
1 Parent(s): 2685421

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -65
app.py CHANGED
@@ -1,66 +1,67 @@
1
- import gradio as gr
2
- from gradio import ChatMessage
3
- from utils import audio_to_text, text_to_speech
4
- from main import get_chatbot_response, load_peft_model_and_tokenizer
5
-
6
-
7
- PEFT_MODEL = 'microsoft/phi-2'
8
- BASE_MODEL = 'bisoye/phi-2-for-mental-health-2'
9
-
10
-
11
- tokenizer, model = load_peft_model_and_tokenizer(PEFT_MODEL, BASE_MODEL)
12
-
13
-
14
- def respond_to_audio(audio):
15
-
16
- text_from_audio = audio_to_text(audio)
17
- response = get_chatbot_response(model, tokenizer,
18
- text_from_audio)
19
-
20
- return response
21
-
22
-
23
- def chat_history(message: str, history: list):
24
-
25
- response = get_chatbot_response(model, tokenizer, message)
26
- history.append(ChatMessage(role = 'user',
27
- content = message))
28
- history.append(ChatMessage(role = 'assistant',
29
- content = response))
30
-
31
- audio_filename = text_to_speech(response) # convert response to audio
32
- return history, audio_filename
33
-
34
-
35
- with gr.Blocks(css='custom_css') as demo:
36
-
37
- with gr.Row():
38
-
39
- with gr.Column():
40
- chatbot = gr.Chatbot(label='Mental Health Chatbot',
41
- type = 'messages')
42
-
43
- input_text = gr.Textbox(label = 'Enter your question here: ')
44
- input_audio = gr.Audio(label='Send question as audio: ',
45
- sources='microphone',
46
- type='filepath')
47
-
48
- send_audio_button = gr.Button(value = 'Send Audio')
49
-
50
- with gr.Column():
51
- output_audio = gr.Audio(label='AI audio response: ',
52
- sources='upload',
53
- type='filepath',
54
- interactive=False,
55
- autoplay=True)
56
-
57
- clear_button = gr.ClearButton(components=[
58
- input_text,
59
- input_audio,
60
- output_audio]
61
- )
62
-
63
- input_text.submit(chat_history, inputs=[input_text, chatbot], outputs=[chatbot])
64
- send_audio_button.click(respond_to_audio, inputs=[input_audio], outputs=[chatbot, output_audio])
65
-
 
66
  demo.launch()
 
1
+ import gradio as gr
2
+ from gradio import ChatMessage
3
+ from utils import audio_to_text, text_to_speech
4
+ from main import get_chatbot_response, load_peft_model_and_tokenizer
5
+
6
+
7
+ PEFT_MODEL = 'microsoft/phi-2'
8
+ BASE_MODEL = 'bisoye/phi-2-for-mental-health-2'
9
+
10
+
11
+ # tokenizer, model = load_peft_model_and_tokenizer(PEFT_MODEL, BASE_MODEL)
12
+ tokenizer, model = load_peft_model_and_tokenizer(PEFT_MODEL)
13
+
14
+
15
+ def respond_to_audio(audio):
16
+
17
+ text_from_audio = audio_to_text(audio)
18
+ response = get_chatbot_response(model, tokenizer,
19
+ text_from_audio)
20
+
21
+ return response
22
+
23
+
24
+ def chat_history(message: str, history: list):
25
+
26
+ response = get_chatbot_response(model, tokenizer, message)
27
+ history.append(ChatMessage(role = 'user',
28
+ content = message))
29
+ history.append(ChatMessage(role = 'assistant',
30
+ content = response))
31
+
32
+ audio_filename = text_to_speech(response) # convert response to audio
33
+ return history, audio_filename
34
+
35
+
36
+ with gr.Blocks(css='custom_css') as demo:
37
+
38
+ with gr.Row():
39
+
40
+ with gr.Column():
41
+ chatbot = gr.Chatbot(label='Mental Health Chatbot',
42
+ type = 'messages')
43
+
44
+ input_text = gr.Textbox(label = 'Enter your question here: ')
45
+ input_audio = gr.Audio(label='Send question as audio: ',
46
+ sources='microphone',
47
+ type='filepath')
48
+
49
+ send_audio_button = gr.Button(value = 'Send Audio')
50
+
51
+ with gr.Column():
52
+ output_audio = gr.Audio(label='AI audio response: ',
53
+ sources='upload',
54
+ type='filepath',
55
+ interactive=False,
56
+ autoplay=True)
57
+
58
+ clear_button = gr.ClearButton(components=[
59
+ input_text,
60
+ input_audio,
61
+ output_audio]
62
+ )
63
+
64
+ input_text.submit(chat_history, inputs=[input_text, chatbot], outputs=[chatbot])
65
+ send_audio_button.click(respond_to_audio, inputs=[input_audio], outputs=[chatbot, output_audio])
66
+
67
  demo.launch()