File size: 7,157 Bytes
ec74f30
 
df25732
 
3667c7a
 
 
 
 
 
 
 
 
 
 
1b279d5
 
 
 
 
 
 
 
 
 
a10bc68
c57cd9a
 
 
 
70bf9cc
 
 
 
c57cd9a
87ae702
1b279d5
70bf9cc
1b279d5
 
c57cd9a
 
 
 
1b279d5
 
c57cd9a
 
e98b248
 
 
 
 
 
 
 
70bf9cc
 
fdae8f6
2d989a9
 
70bf9cc
15f2bc2
e98b248
72abfd9
6ba2996
2d989a9
 
6ba2996
2d989a9
e98b248
 
 
4518e15
6ba2996
3667c7a
2d989a9
6ba2996
2d989a9
6ba2996
 
3667c7a
2d989a9
6ba2996
2d989a9
 
 
 
 
 
 
 
 
 
 
 
 
 
6ba2996
1b279d5
df25732
 
 
a10bc68
 
1b279d5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c57cd9a
a10bc68
891f3b9
a10bc68
c57cd9a
df25732
 
1b279d5
70bf9cc
fb73ff7
 
a10bc68
df25732
87ae702
1b279d5
 
 
87ae702
df25732
 
 
70bf9cc
15f2bc2
1b279d5
 
1f19f64
166b0a2
3667c7a
72abfd9
a10bc68
 
15f2bc2
1f19f64
f6e34f2
c57cd9a
1b279d5
 
 
 
 
1f19f64
 
c8e8be4
3667c7a
1f19f64
4518e15
3667c7a
72abfd9
f913a0b
87ae702
 
3447ff0
87ae702
1b279d5
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
import os

import gradio as gr

from api.audio import STTManager, TTSManager
from api.llm import LLMManager
from config import config
from docs.instruction import instruction
from resources.data import fixed_messages, topics_list
from resources.prompts import prompts
from utils.ui import add_candidate_message, add_interviewer_message

llm = LLMManager(config, prompts)
tts = TTSManager(config)
stt = STTManager(config)

default_audio_params = {
    "label": "Record answer",
    "sources": ["microphone"],
    "type": "numpy",
    "waveform_options": {"show_controls": False},
    "editable": False,
    "container": False,
    "show_share_button": False,
}


def hide_settings():
    init_acc = gr.Accordion("Settings", open=False)
    start_btn = gr.Button("Generate a problem", interactive=False)
    return init_acc, start_btn


def show_solution():
    solution_acc = gr.Accordion("Solution", open=True)
    end_btn = gr.Button("Finish the interview", interactive=True)
    audio_input = gr.Audio(interactive=True, **default_audio_params)
    return solution_acc, end_btn, audio_input


def hide_solution():
    solution_acc = gr.Accordion("Solution", open=False)
    end_btn = gr.Button("Finish the interview", interactive=False)
    problem_acc = gr.Accordion("Problem statement", open=False)
    audio_input = gr.Audio(interactive=False, **default_audio_params)
    return solution_acc, end_btn, problem_acc, audio_input


def get_status_color(obj):
    if obj.status:
        if obj.streaming:
            return "🟒"
        return "🟑"
    return "πŸ”΄"


# Interface

with gr.Blocks(title="AI Interviewer") as demo:
    if os.getenv("IS_DEMO"):
        gr.Markdown(instruction["demo"])

    started_coding = gr.State(False)
    audio_output = gr.Audio(label="Play audio", autoplay=True, visible=os.environ["DEBUG"], streaming=tts.streaming)
    with gr.Tab("Instruction") as instruction_tab:
        with gr.Row():
            with gr.Column(scale=2):
                gr.Markdown(instruction["introduction"])
            with gr.Column(scale=1):
                space = " " * 10

                tts_status = get_status_color(tts)
                gr.Markdown(f"TTS status: {tts_status}{space}{config.tts.name}")

                try:
                    text_test = stt.speech_to_text(audio_test, False)
                    gr.Markdown(f"STT status: 🟒{space} {config.stt.name}")
                except:
                    gr.Markdown(f"STT status: πŸ”΄{space} {config.stt.name}")

                try:
                    llm.test_connection()
                    gr.Markdown(f"LLM status: 🟒{space} {config.llm.name}")
                except:
                    gr.Markdown(f"LLM status: πŸ”΄{space} {config.llm.name}")

        gr.Markdown(instruction["quick_start"])
        with gr.Row():
            with gr.Column(scale=2):
                gr.Markdown(instruction["interface"])
            with gr.Column(scale=1):
                gr.Markdown("Bot interaction area will look like this. Use Record button to record your answer.")
                chat_example = gr.Chatbot(
                    label="Chat", show_label=False, show_share_button=False, value=[["Candidate message", "Interviewer message"]]
                )
                audio_input_example = gr.Audio(interactive=True, **default_audio_params)
        gr.Markdown(instruction["models"])
        gr.Markdown(instruction["acknowledgements"])

    with gr.Tab("Coding") as coding_tab:
        chat_history = gr.State([])
        previous_code = gr.State("")
        with gr.Accordion("Settings") as init_acc:
            with gr.Row():
                with gr.Column():
                    gr.Markdown("##### Problem settings")
                    with gr.Row():
                        gr.Markdown("Difficulty")
                        difficulty_select = gr.Dropdown(
                            label="Select difficulty",
                            choices=["Easy", "Medium", "Hard"],
                            value="Medium",
                            container=False,
                            allow_custom_value=True,
                        )
                    with gr.Row():
                        gr.Markdown("Topic (can type custom value)")
                        topic_select = gr.Dropdown(
                            label="Select topic", choices=topics_list, value="Arrays", container=False, allow_custom_value=True
                        )
                with gr.Column(scale=2):
                    requirements = gr.Textbox(label="Requirements", placeholder="Specify additional requirements", lines=5)
                    start_btn = gr.Button("Generate a problem")

        with gr.Accordion("Problem statement", open=True) as problem_acc:
            description = gr.Markdown()
        with gr.Accordion("Solution", open=False) as solution_acc:
            with gr.Row() as content:
                with gr.Column(scale=2):
                    code = gr.Code(
                        label="Please write your code here. You can use any language, but only Python syntax highlighting is available.",
                        language="python",
                        lines=35,
                    )
                with gr.Column(scale=1):
                    end_btn = gr.Button("Finish the interview", interactive=False)
                    chat = gr.Chatbot(label="Chat", show_label=False, show_share_button=False)
                    audio_input = gr.Audio(interactive=False, **default_audio_params)
                    message = gr.Textbox(label="Message", lines=3, visible=False)

        with gr.Accordion("Feedback", open=True) as feedback_acc:
            feedback = gr.Markdown()

    # Events
    coding_tab.select(fn=add_interviewer_message(fixed_messages["intro"]), inputs=[chat, started_coding], outputs=[chat])

    start_btn.click(fn=add_interviewer_message(fixed_messages["start"]), inputs=[chat], outputs=[chat]).then(
        fn=lambda: True, outputs=[started_coding]
    ).then(fn=hide_settings, outputs=[init_acc, start_btn]).then(
        fn=llm.get_problem,
        inputs=[requirements, difficulty_select, topic_select],
        outputs=[description, chat_history],
        scroll_to_output=True,
    ).then(
        fn=show_solution, outputs=[solution_acc, end_btn, audio_input]
    )

    end_btn.click(
        fn=add_interviewer_message(fixed_messages["end"]),
        inputs=[chat],
        outputs=[chat],
    ).then(
        fn=hide_solution, outputs=[solution_acc, end_btn, problem_acc, audio_input]
    ).then(fn=llm.end_interview, inputs=[description, chat_history], outputs=[feedback])

    audio_input.stop_recording(fn=stt.speech_to_text, inputs=[audio_input], outputs=[message]).then(
        fn=lambda: None, outputs=[audio_input]
    ).then(fn=add_candidate_message, inputs=[message, chat], outputs=[chat]).then(
        fn=llm.send_request,
        inputs=[code, previous_code, message, chat_history, chat],
        outputs=[chat_history, chat, message, previous_code],
    )

    chat.change(fn=tts.read_last_message, inputs=[chat], outputs=[audio_output])

demo.launch(show_api=False)