Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,11 +1,14 @@
|
|
| 1 |
import openai
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from prompts import prompt_chat_response
|
| 4 |
from fewshot import FewShot4UAVs
|
|
|
|
| 5 |
|
| 6 |
fewshot = FewShot4UAVs()
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
url = f"http://url/{command}"
|
| 10 |
data = {}
|
| 11 |
if location:
|
|
@@ -20,61 +23,40 @@ def parse_command(text):
|
|
| 20 |
if len(tokens) >= 2:
|
| 21 |
return tokens[1]
|
| 22 |
else:
|
| 23 |
-
return 'none'
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
def transcribe_audio(audio):
|
| 27 |
audio_file = open(audio, "rb")
|
| 28 |
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
| 29 |
-
return transcript["text"]
|
| 30 |
-
|
| 31 |
|
| 32 |
-
|
| 33 |
-
messages.append({"role": "user", "content":
|
| 34 |
|
| 35 |
-
|
| 36 |
-
def append_formatted_command(messages, user_transcript):
|
| 37 |
-
formatted_command_text = fewshot.get_command(user_transcript)
|
| 38 |
messages.append({"role": "function", "content": formatted_command_text, "name": "UAV"})
|
| 39 |
-
return formatted_command_text
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
def append_uav_response(messages):
|
| 43 |
response = openai.ChatCompletion.create(
|
| 44 |
model="gpt-3.5-turbo",
|
| 45 |
messages=messages
|
| 46 |
)
|
|
|
|
| 47 |
uav_response = response["choices"][0]["message"]["content"]
|
| 48 |
messages.append({"role": "assistant", "content": uav_response, "name": "Assistant"})
|
| 49 |
-
return uav_response
|
| 50 |
-
|
| 51 |
|
| 52 |
-
def build_chat_transcript(messages):
|
| 53 |
chat_transcript = ""
|
| 54 |
for message in messages:
|
| 55 |
if message['role'] != 'system':
|
| 56 |
chat_transcript += f"{message['name']}: {message['content']} \n\n"
|
| 57 |
-
return chat_transcript
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def transcribe(audio):
|
| 61 |
-
messages = [
|
| 62 |
-
{"role": "system",
|
| 63 |
-
"content": prompt_chat_response}
|
| 64 |
-
]
|
| 65 |
-
|
| 66 |
-
user_transcript = transcribe_audio(audio)
|
| 67 |
-
append_user_transcript(messages, user_transcript)
|
| 68 |
-
|
| 69 |
-
formatted_command_text = append_formatted_command(messages, user_transcript)
|
| 70 |
-
|
| 71 |
-
# turn on the below to invoke API
|
| 72 |
-
# command = parse_command(formatted_command_text)
|
| 73 |
-
# send_command(command)
|
| 74 |
-
|
| 75 |
-
uav_response = append_uav_response(messages, formatted_command_text)
|
| 76 |
-
|
| 77 |
-
chat_transcript = build_chat_transcript(messages)
|
| 78 |
|
| 79 |
return chat_transcript
|
| 80 |
|
|
@@ -100,4 +82,4 @@ with gr.Blocks(theme='sudeepshouche/minimalist') as demo:
|
|
| 100 |
"Land now.")
|
| 101 |
|
| 102 |
demo.queue()
|
| 103 |
-
demo.launch()
|
|
|
|
| 1 |
import openai
|
| 2 |
+
import re
|
| 3 |
import gradio as gr
|
| 4 |
from prompts import prompt_chat_response
|
| 5 |
from fewshot import FewShot4UAVs
|
| 6 |
+
from flask import requests
|
| 7 |
|
| 8 |
fewshot = FewShot4UAVs()
|
| 9 |
|
| 10 |
+
|
| 11 |
+
def send_command(command, location=None):
|
| 12 |
url = f"http://url/{command}"
|
| 13 |
data = {}
|
| 14 |
if location:
|
|
|
|
| 23 |
if len(tokens) >= 2:
|
| 24 |
return tokens[1]
|
| 25 |
else:
|
| 26 |
+
return 'none'
|
| 27 |
+
|
| 28 |
|
| 29 |
+
def transcribe(audio):
|
| 30 |
+
messages = [
|
| 31 |
+
{"role": "system",
|
| 32 |
+
"content": prompt_chat_response}
|
| 33 |
+
]
|
| 34 |
|
|
|
|
| 35 |
audio_file = open(audio, "rb")
|
| 36 |
transcript = openai.Audio.transcribe("whisper-1", audio_file)
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
original_transcript = transcript["text"]
|
| 39 |
+
messages.append({"role": "user", "content": original_transcript, "name": "Operator"})
|
| 40 |
|
| 41 |
+
formatted_command_text = fewshot.get_command(original_transcript)
|
|
|
|
|
|
|
| 42 |
messages.append({"role": "function", "content": formatted_command_text, "name": "UAV"})
|
|
|
|
| 43 |
|
| 44 |
+
# turn on the below to invoke API
|
| 45 |
+
# command = parse_command(formatted_command_text)
|
| 46 |
+
# send_command(command)
|
| 47 |
|
|
|
|
| 48 |
response = openai.ChatCompletion.create(
|
| 49 |
model="gpt-3.5-turbo",
|
| 50 |
messages=messages
|
| 51 |
)
|
| 52 |
+
|
| 53 |
uav_response = response["choices"][0]["message"]["content"]
|
| 54 |
messages.append({"role": "assistant", "content": uav_response, "name": "Assistant"})
|
|
|
|
|
|
|
| 55 |
|
|
|
|
| 56 |
chat_transcript = ""
|
| 57 |
for message in messages:
|
| 58 |
if message['role'] != 'system':
|
| 59 |
chat_transcript += f"{message['name']}: {message['content']} \n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
return chat_transcript
|
| 62 |
|
|
|
|
| 82 |
"Land now.")
|
| 83 |
|
| 84 |
demo.queue()
|
| 85 |
+
demo.launch()
|