File size: 2,278 Bytes
2d144e4
 
 
 
 
3a856a4
2d144e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77448ff
da2f06b
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
import openai
import gradio as gr
from prompts import prompt_chat_response
from fewshot import FewShot4UAVs

openai.api_key = "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"

fewshot = FewShot4UAVs()


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

    audio_file = open(audio, "rb")
    transcript = openai.Audio.transcribe("whisper-1", audio_file)

    original_transcript = transcript["text"]
    messages.append({"role": "user", "content": original_transcript, "name": "Operator"})

    formatted_command_text = fewshot.get_command(original_transcript)
    messages.append({"role": "function", "content": formatted_command_text, "name": "UAV"})

    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"})

    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().style():
        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 at your position.\n\n"
                    "Land now.")

demo.queue()
demo.launch()