Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
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 |
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()
|