File size: 3,095 Bytes
2d144e4
 
 
 
 
1c60c6e
2d144e4
 
f4467c1
1b9da36
 
 
 
 
f4467c1
1c60c6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f4467c1
2d144e4
1c60c6e
 
 
 
2d144e4
f4467c1
1c60c6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b9da36
1c60c6e
 
 
 
2d144e4
1c60c6e
2d144e4
 
 
 
 
 
 
f750b58
2d144e4
 
 
 
 
 
 
 
 
 
 
1c60c6e
 
2d144e4
ca3ee35
350a378
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
import openai
import gradio as gr
from prompts import prompt_chat_response
from fewshot import FewShot4UAVs


fewshot = FewShot4UAVs()


def transcribe(audio):
    messages = [
        {"role": "system",
         "content": prompt_chat_response}
    ]

    user_transcript = get_audio_transcript(audio, messages)
    uav_command = get_uav_command(user_transcript, messages)
    uav_response = get_uav_response(messages)

    # command = parse_command(uav_command)
    # send_command(command)

    chat_transcript = build_chat_transcript(user_transcript, uav_command, uav_response)

    return chat_transcript


def get_audio_transcript(audio, messages):
    audio_file = open(audio, "rb")
    transcript = openai.Audio.transcribe("whisper-1", audio_file)
    user_transcript = transcript["text"]
    messages.append({"role": "user", "content": user_transcript, "name": "Operator"})
    return user_transcript


def get_uav_command(user_transcript, messages):
    uav_command = fewshot.get_command(user_transcript)
    messages.append({"role": "function", "content": uav_command, "name": "UAV"})
    return uav_command


def get_uav_response(messages):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages
    )

    uav_response = response["choices"][0]["message"]["content"]
    messages.append({"role": "assistant", "content": uav_response, "name": "Assistant"})
    return uav_response


def build_chat_transcript(user_transcript, uav_command, uav_response):
    messages = [
        {"role": "system", "content": prompt_chat_response},
        {"role": "user", "content": user_transcript, "name": "Operator"},
        {"role": "function", "content": uav_command, "name": "UAV"},
        {"role": "assistant", "content": uav_response, "name": "Assistant"}
    ]

    chat_transcript = ""
    for message in messages:
        if message['role'] != 'system':
            chat_transcript += f"{message['name']}: {message['content']} \n\n"

    return chat_transcript


with gr.Blocks(theme='sudeepshouche/minimalist') as demo:
    gr.Markdown("""
    # Conversational UAV Explorer
    Press record and speak into the microphone to give a command. Make sure to stop recording before pressing "Give Command."\n\n
    Commands: Take picture, Go to, Land, Take off""")
    with gr.Row():
        audio_input = gr.Audio(source="microphone", type="filepath")
        output = gr.Textbox(label="Transcript")
    with gr.Row():
        with gr.Column():
            submit_btn = gr.Button("Give command", variant="primary")
            submit_btn.click(fn=transcribe, inputs=audio_input, outputs=output, api_name="record")
    with gr.Accordion("Examples:"):
        gr.Markdown("Take a picture of the second floor of the green house.\n\n"
                    "Go to the brick house across the street.\n\n"
                    "Head to Libby Hill Park on E Franklin St.\n\n"
                    "Check out the Whole Foods on W Broad St.\n\n"
                    "Take off.\n\n"
                    "Land.")

demo.queue(concurrency_count=4)
demo.launch()