Spaces:
Build error
Build error
merged with git repo
Browse files- gpt_mavplot.py +85 -0
gpt_mavplot.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from llm.gptPlotCreator import PlotCreator
|
| 4 |
+
|
| 5 |
+
plot_creator = PlotCreator()
|
| 6 |
+
|
| 7 |
+
def add_text(history, text):
|
| 8 |
+
history = history + [(text, None)]
|
| 9 |
+
return history, ""
|
| 10 |
+
|
| 11 |
+
def add_file(history, file):
|
| 12 |
+
history = history + [((file.name,), None)]
|
| 13 |
+
return history
|
| 14 |
+
|
| 15 |
+
def format_history(history):
|
| 16 |
+
return "\n".join([f"Human: {entry[0]}\nAI: {entry[1]}" for entry in history ])
|
| 17 |
+
|
| 18 |
+
def bot(history):
|
| 19 |
+
# Get the last input from the user
|
| 20 |
+
user_input = history[-1][0] if history and history[-1][0] else None
|
| 21 |
+
|
| 22 |
+
print(user_input)
|
| 23 |
+
|
| 24 |
+
# Check if it is a string
|
| 25 |
+
if isinstance(user_input, str):
|
| 26 |
+
|
| 27 |
+
history[-1][1] = "I am figuring out what data types are relevant for the plot...\n"
|
| 28 |
+
yield history
|
| 29 |
+
data_types_str = plot_creator.find_relevant_data_types(user_input)
|
| 30 |
+
|
| 31 |
+
history[-1][1] += "I am now generating a script to plot the data...\n"
|
| 32 |
+
yield history
|
| 33 |
+
plot_creator.create_plot(user_input, data_types_str)
|
| 34 |
+
|
| 35 |
+
history[-1][1] += "I am now running the script I just Generated...\n"
|
| 36 |
+
yield history
|
| 37 |
+
response = plot_creator.run_script()
|
| 38 |
+
|
| 39 |
+
history = history + [(None, f"Here is the code used to generate the plot:")]
|
| 40 |
+
history = history + [(None, f"{response[1]}")]
|
| 41 |
+
history = history + response[0]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
yield history
|
| 45 |
+
else:
|
| 46 |
+
file_path = user_input[0]
|
| 47 |
+
plot_creator.set_logfile_name(file_path)
|
| 48 |
+
|
| 49 |
+
# get only base name
|
| 50 |
+
filename, extension = os.path.splitext(os.path.basename(file_path))
|
| 51 |
+
|
| 52 |
+
history[-1][0] = f"user uploaded file: {filename}{extension}"
|
| 53 |
+
history[-1][1] = "processing file..."
|
| 54 |
+
yield history
|
| 55 |
+
|
| 56 |
+
data_types = plot_creator.parse_mavlink_log()
|
| 57 |
+
history = history + [(None, f"I am done processing the file. Now you can ask me to generate a plot.")]
|
| 58 |
+
yield history
|
| 59 |
+
|
| 60 |
+
return history
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
with gr.Blocks() as demo:
|
| 64 |
+
gr.Markdown("# GPT MAVPlot\n\nThis web-based tool allows users to upload mavlink tlogs in which the chat bot will use to generate plots from. It does this by creating a python script using pymavlink and matplotlib. The output includes the plot and the code used to generate it. ")
|
| 65 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
|
| 66 |
+
|
| 67 |
+
with gr.Row():
|
| 68 |
+
with gr.Column(scale=0.85):
|
| 69 |
+
txt = gr.Textbox(
|
| 70 |
+
show_label=False,
|
| 71 |
+
placeholder="Enter text and press enter, or upload an image",
|
| 72 |
+
).style(container=False)
|
| 73 |
+
with gr.Column(scale=0.15, min_width=0):
|
| 74 |
+
btn = gr.UploadButton("📁", file_types=["file"])
|
| 75 |
+
|
| 76 |
+
txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(
|
| 77 |
+
bot, chatbot, chatbot
|
| 78 |
+
)
|
| 79 |
+
btn.upload(add_file, [chatbot, btn], [chatbot]).then(
|
| 80 |
+
bot, chatbot, chatbot
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
demo.queue()
|
| 85 |
+
demo.launch()
|