IliaLarchenko commited on
Commit
87ae702
1 Parent(s): c57cd9a

Added audio I/O

Browse files
Files changed (2) hide show
  1. app.py +61 -10
  2. llm.py +19 -1
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
 
3
- from llm import end_interview, get_problem, send_request
4
  from options import languages_list, models, topics_list
5
 
6
 
@@ -8,14 +8,43 @@ def hide_settings():
8
  init_acc = gr.Accordion("Settings", open=False)
9
  start_btn = gr.Button("Generate a problem", interactive=False)
10
  solution_acc = gr.Accordion("Solution", open=True)
11
- return init_acc, start_btn, solution_acc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
 
14
  def hide_solution():
15
  solution_acc = gr.Accordion("Solution", open=False)
16
  end_btn = gr.Button("Finish the interview", interactive=False)
17
  problem_acc = gr.Accordion("Problem statement", open=False)
18
- return solution_acc, end_btn, problem_acc
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
 
21
  with gr.Blocks() as demo:
@@ -57,12 +86,22 @@ with gr.Blocks() as demo:
57
  language_select = gr.Dropdown(
58
  label="Select language", choices=languages_list, value="python", container=False, interactive=True
59
  )
60
- code = gr.Code(label="Solution", language=language_select.value, lines=20)
61
- message = gr.Textbox(label="Message", lines=1)
62
- # TODO: add voice input and output
63
  with gr.Column(scale=1):
 
64
  chat = gr.Chatbot(label="Chat history")
65
- end_btn = gr.Button("Finish the interview")
 
 
 
 
 
 
 
 
 
 
 
66
  with gr.Accordion("Feedback", open=True) as feedback_acc:
67
  feedback = gr.Markdown()
68
 
@@ -71,16 +110,28 @@ with gr.Blocks() as demo:
71
  inputs=[requirements, difficulty_select, topic_select, model_select],
72
  outputs=[description, chat_history],
73
  scroll_to_output=True,
74
- ).then(fn=hide_settings, inputs=None, outputs=[init_acc, start_btn, solution_acc])
75
 
76
- message.submit(
77
  fn=send_request,
78
  inputs=[code, previous_code, message, chat_history, chat, model_select],
79
  outputs=[chat_history, chat, message, previous_code],
80
  )
81
 
82
  end_btn.click(fn=end_interview, inputs=[chat_history, model_select], outputs=feedback).then(
83
- fn=hide_solution, inputs=None, outputs=[solution_acc, end_btn, problem_acc]
84
  )
85
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  demo.launch()
 
1
  import gradio as gr
2
 
3
+ from llm import end_interview, get_problem, read_last_message, send_request, transcribe_audio
4
  from options import languages_list, models, topics_list
5
 
6
 
 
8
  init_acc = gr.Accordion("Settings", open=False)
9
  start_btn = gr.Button("Generate a problem", interactive=False)
10
  solution_acc = gr.Accordion("Solution", open=True)
11
+ end_btn = gr.Button("Finish the interview", interactive=True)
12
+ send_btn = gr.Button("Send", interactive=True)
13
+ audio_input = gr.Audio(
14
+ label="Record audio",
15
+ sources=["microphone"],
16
+ type="filepath",
17
+ waveform_options={"show_controls": False},
18
+ interactive=True,
19
+ editable=False,
20
+ )
21
+ chat = [
22
+ (
23
+ None,
24
+ "Welcome to the interview! Please take a moment to read the problem statement. Then you can share you initial thoughts and ask any questions you may have. Good luck!",
25
+ )
26
+ ]
27
+ return init_acc, start_btn, solution_acc, end_btn, send_btn, audio_input, chat
28
 
29
 
30
  def hide_solution():
31
  solution_acc = gr.Accordion("Solution", open=False)
32
  end_btn = gr.Button("Finish the interview", interactive=False)
33
  problem_acc = gr.Accordion("Problem statement", open=False)
34
+ send_btn = gr.Button("Send", interactive=False)
35
+ audio_input = gr.Audio(
36
+ label="Record audio",
37
+ sources=["microphone"],
38
+ type="filepath",
39
+ waveform_options={"show_controls": False},
40
+ interactive=False,
41
+ editable=False,
42
+ )
43
+ return solution_acc, end_btn, problem_acc, send_btn, audio_input
44
+
45
+
46
+ def return_none():
47
+ return None
48
 
49
 
50
  with gr.Blocks() as demo:
 
86
  language_select = gr.Dropdown(
87
  label="Select language", choices=languages_list, value="python", container=False, interactive=True
88
  )
89
+ code = gr.Code(label="Solution", language=language_select.value, lines=35)
 
 
90
  with gr.Column(scale=1):
91
+ end_btn = gr.Button("Finish the interview", interactive=False)
92
  chat = gr.Chatbot(label="Chat history")
93
+ audio_input = gr.Audio(
94
+ label="Record audio",
95
+ sources=["microphone"],
96
+ type="filepath",
97
+ waveform_options={"show_controls": False},
98
+ interactive=False,
99
+ editable=False,
100
+ )
101
+ audio_output = gr.Audio(label="Play audio", autoplay=True, visible=False)
102
+ message = gr.Textbox(label="Message", lines=3)
103
+ send_btn = gr.Button("Send", interactive=False)
104
+
105
  with gr.Accordion("Feedback", open=True) as feedback_acc:
106
  feedback = gr.Markdown()
107
 
 
110
  inputs=[requirements, difficulty_select, topic_select, model_select],
111
  outputs=[description, chat_history],
112
  scroll_to_output=True,
113
+ ).then(fn=hide_settings, inputs=None, outputs=[init_acc, start_btn, solution_acc, end_btn, send_btn, audio_input, chat])
114
 
115
+ send_btn.click(
116
  fn=send_request,
117
  inputs=[code, previous_code, message, chat_history, chat, model_select],
118
  outputs=[chat_history, chat, message, previous_code],
119
  )
120
 
121
  end_btn.click(fn=end_interview, inputs=[chat_history, model_select], outputs=feedback).then(
122
+ fn=hide_solution, inputs=None, outputs=[solution_acc, end_btn, problem_acc, send_btn, audio_input]
123
  )
124
 
125
+ audio_input.stop_recording(fn=transcribe_audio, inputs=[audio_input], outputs=[message]).then(
126
+ fn=return_none, inputs=None, outputs=[audio_input]
127
+ ).then(
128
+ fn=send_request,
129
+ inputs=[code, previous_code, message, chat_history, chat, model_select],
130
+ outputs=[chat_history, chat, message, previous_code],
131
+ )
132
+
133
+ chat.change(fn=read_last_message, inputs=[chat], outputs=[audio_output])
134
+
135
+ audio_output.stop(fn=return_none, inputs=None, outputs=[audio_output])
136
+
137
  demo.launch()
llm.py CHANGED
@@ -68,7 +68,25 @@ def send_request(code, previous_code, message, chat_history, chat_display, model
68
  reply = "There was an error processing your request."
69
 
70
  chat_history.append({"role": "assistant", "content": json_reply})
71
-
72
  chat_display.append([message, str(reply)])
73
 
74
  return chat_history, chat_display, "", code
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  reply = "There was an error processing your request."
69
 
70
  chat_history.append({"role": "assistant", "content": json_reply})
 
71
  chat_display.append([message, str(reply)])
72
 
73
  return chat_history, chat_display, "", code
74
+
75
+
76
+ def transcribe_audio(filename, client=client):
77
+ with open(filename, "rb") as audio_file:
78
+ transcription = client.audio.transcriptions.create(model="whisper-1", file=audio_file, response_format="text")
79
+
80
+ return transcription
81
+
82
+
83
+ def text_to_speech(text, client=client):
84
+ response = client.audio.speech.create(model="tts-1", voice="alloy", input=text)
85
+ return response.content
86
+
87
+
88
+ def read_last_message(chat_display):
89
+ last_message = chat_display[-1][1]
90
+
91
+ audio = text_to_speech(last_message)
92
+ return audio