ericjohnson97 commited on
Commit
02cd084
·
1 Parent(s): 6f51eb2

moving to hugging face

Browse files
Files changed (9) hide show
  1. .gitignore +6 -0
  2. LICENSE +21 -0
  3. README.md +58 -13
  4. app.py +85 -0
  5. docs/GPT_MAVPlot_Arch.png +0 -0
  6. docs/chat_bot_if.PNG +0 -0
  7. llm/gptPlotCreator.py +272 -0
  8. requirements.txt +10 -0
  9. template.env +2 -0
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ .env
2
+ *.pyc
3
+ plot.py
4
+ plot.png
5
+ .venv
6
+ .chroma
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Eric Johnson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,13 +1,58 @@
1
- ---
2
- title: Gpt Mavplot
3
- emoji:
4
- colorFrom: gray
5
- colorTo: green
6
- sdk: gradio
7
- sdk_version: 3.32.0
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # GPT_MAVPlot
2
+
3
+ MAVPlot is a Python-based project which uses Gradio as an interface and GPT-X powered by OpenAI as a chatbot to generate and plot MAVLink data. It provides an easy-to-use, chatbot-like interface for users to describe the plot they would like to generate.
4
+
5
+ ![chat bot](docs/chat_bot_if.PNG)
6
+
7
+ ## Architecture
8
+
9
+ ![arch](docs/GPT_MAVPlot_Arch.png)
10
+
11
+ ## Installation
12
+
13
+ Clone the repository:
14
+
15
+ ```shell
16
+ git clone https://github.com/yourusername/mavplot.git
17
+ ```
18
+
19
+ Setup Python Virtual Environment:
20
+
21
+ ```shell
22
+ python3 -m venv .venv
23
+ ```
24
+
25
+ Activate the virtual environment:
26
+
27
+ ```shell
28
+ source .venv/bin/activate
29
+ ```
30
+
31
+
32
+ Install the requirements:
33
+
34
+ ```shell
35
+ pip install -r requirements.txt
36
+ ```
37
+
38
+ Setup .env File
39
+
40
+ Copy the `template.env` file to a file named `.env` in your root directory. Add your Openai API key to the file
41
+
42
+ ## Usage
43
+
44
+ After installing all dependencies, run the main script using:
45
+
46
+ ```shell
47
+ python gpt_mavplot.py
48
+ ```
49
+
50
+ A web-based Gradio interface will launch. You can upload a mavlink tlog then prompt the bot to generate plots from the log. The chatbot will process your request and generate the corresponding plot, which will be displayed in the chat interface. The script use to generate the log will also be posted to the chat interface.
51
+
52
+ ## Contributing
53
+
54
+ Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.
55
+
56
+ ## License
57
+
58
+ [MIT](https://choosealicense.com/licenses/mit/)
app.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()
docs/GPT_MAVPlot_Arch.png ADDED
docs/chat_bot_if.PNG ADDED
llm/gptPlotCreator.py ADDED
@@ -0,0 +1,272 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import random
3
+ import linecache
4
+ import subprocess
5
+ from langchain.prompts import PromptTemplate
6
+ from langchain.chat_models import ChatOpenAI
7
+ from langchain.chains import LLMChain
8
+ from langchain.llms import OpenAI
9
+ from langchain.chains import ConversationChain
10
+ from langchain.memory import ConversationBufferMemory
11
+ from langchain.prompts.chat import (
12
+ ChatPromptTemplate,
13
+ HumanMessagePromptTemplate,
14
+ )
15
+ from langchain.vectorstores import Chroma
16
+ from langchain.embeddings.openai import OpenAIEmbeddings
17
+ import os
18
+ from dotenv import load_dotenv
19
+ from PIL import Image
20
+
21
+ # Import required modules
22
+ from pymavlink import mavutil
23
+ import json
24
+
25
+ class PlotCreator:
26
+ """
27
+ PlotCreator is a class that generates Python scripts to plot MAVLink data
28
+ provided by a user, leveraging OpenAI's models for conversational agents.
29
+ """
30
+
31
+ last_code = "" # stores the last code generated
32
+
33
+ def __init__(self):
34
+ """
35
+ Initialize an instance of PlotCreator.
36
+ """
37
+
38
+ load_dotenv() # load environment variables from a .env file
39
+
40
+ self.model = os.getenv("OPENAI_MODEL") # get the name of the OpenAI model to use
41
+
42
+ # create an instance of ChatOpenAI with the specified model, maximum tokens, and temperature
43
+ llm = ChatOpenAI(model_name=self.model, max_tokens=2000, temperature=0)
44
+
45
+ # define the input variables and template for the prompt to generate Python scripts
46
+ mavlink_data_prompt = PromptTemplate(
47
+ input_variables=["data_types", "history", "human_input", "file"],
48
+ template="You are an AI conversation agent that will be used for generating python scripts to plot mavlink data provided by the user. Please create a python script using matplotlib and pymavlink's mavutil to plot the data provided by the user. Please do not explain the code just return the script. Please plot each independent variable over time in seconds. Please save the plot to file named plot.png in the same directory as plot.py with at least 400 dpi. please use blocking=false in your call to recv_match and be sure to break the loop if a msg in None. here are the relevant data types in the log:\n\n{data_types} \n\nChat History:\n{history} \n\nHUMAN: {human_input} \n\nplease read this data from the file {file}.",
49
+ )
50
+
51
+ # create an instance of LLMChain with the defined prompt and verbosity
52
+ self.chain = LLMChain(verbose=True, llm=llm, prompt=mavlink_data_prompt)
53
+
54
+ @staticmethod
55
+ def extract_code_snippets(text):
56
+ """
57
+ Extracts code snippets from a text.
58
+
59
+ This function searches the text for substrings enclosed in '```', which are assumed to be code snippets.
60
+
61
+ Args:
62
+ text (str): The text to search for code snippets.
63
+
64
+ Returns:
65
+ list: A list of code snippets found in the text. If no snippets are found, returns a list containing the original text.
66
+ """
67
+
68
+ pattern = r'```.*?\n(.*?)```' # pattern to match code snippets enclosed in '```'
69
+ # use regex to find all matches of the pattern in the text
70
+ snippets = re.findall(pattern, text, re.DOTALL | re.MULTILINE)
71
+ if len(snippets) == 0: # if no snippets were found
72
+ snippets = [text] # treat the entire text as a single snippet
73
+ return snippets
74
+
75
+ @staticmethod
76
+ def write_plot_script(filename, text):
77
+ """
78
+ Writes a script to a file.
79
+
80
+ Args:
81
+ filename (str): The name of the file to write the script to.
82
+ text (str): The script to write to the file.
83
+ """
84
+
85
+ with open(filename, 'w') as file: # open the file for writing
86
+ file.write(text) # write the script to the file
87
+
88
+ def attempt_to_fix_sctript(self, filename, error_message):
89
+ """
90
+ Attempts to fix a script that caused an error.
91
+
92
+ Args:
93
+ filename (str): The name of the file containing the script.
94
+ error_message (str): The error message produced by the script.
95
+
96
+ Returns:
97
+ list: A list containing the fixed script, or the original script with an error message if it couldn't be fixed.
98
+ """
99
+
100
+ # create an instance of ChatOpenAI with the specified model, maximum tokens, and temperature
101
+ llm = ChatOpenAI(model_name=self.model , max_tokens=8000, temperature=0)
102
+
103
+ # define the input variables and template for the prompt to generate Python scripts
104
+ fix_plot_script_template = PromptTemplate(
105
+ input_variables=["data_types", "error", "script"],
106
+ template="You are an AI agent that is designed to debug scripts created to plot mavlink data using matplotlib and pymavlink's mavutil. the following script produced this error: \n\n{script}\n\nThe error is: \n\n{error}\n\n Here are message definitions that are possibly relevant for the script:\n\n {data_types}\n\n. Please fix the script so that it produces the correct plot. please return the fixed script in a markdown code block.",
107
+ )
108
+
109
+ # read script from file
110
+ with open(filename, 'r') as file:
111
+ script = file.read()
112
+
113
+ # create an instance of LLMChain with the defined prompt and verbosity
114
+ chain = LLMChain(verbose=True, llm=llm, prompt=fix_plot_script_template)
115
+ try:
116
+ response = chain.run({"data_types" : self.message_types, "error": error_message, "script": script}) # run the LLMChain with the error and script as input
117
+ except:
118
+ return "Sorry I couldn't fix the script. Here is the original script I tried:\n\n" + script
119
+ print(response)
120
+ code = PlotCreator.extract_code_snippets(response) # extract the fixed script from the response
121
+ PlotCreator.write_plot_script("plot.py", code[0]) # write the fixed script to a file
122
+
123
+ # run the fixed script
124
+ try:
125
+ subprocess.check_output(["python", "plot.py"], stderr=subprocess.STDOUT)
126
+ except:
127
+ code[0] = "Sorry I was unable to fix the script.\nThis is my attempt to fix it:\n\n" + code[0]
128
+ return code
129
+
130
+ def set_logfile_name(self, filename):
131
+ """
132
+ Set the name of the log file.
133
+
134
+ :param filename: The name of the log file.
135
+ :type filename: str
136
+ """
137
+ self.logfile_name = filename
138
+
139
+ def find_relevant_data_types(self, human_input):
140
+ # Search the database for documents that are similar to the human input
141
+ docs = self.db.similarity_search(human_input)
142
+
143
+ # Concatenate the content of the documents into a string
144
+ data_type_info_text = ""
145
+ for doc in docs:
146
+ data_type_info_text += doc.page_content + "\n\n"
147
+
148
+ return data_type_info_text
149
+
150
+ def run_script(self):
151
+ # Run the script and if it doesn't work, capture the output and call attempt_to_fix_script
152
+ try:
153
+ subprocess.check_output(["python", "plot.py"], stderr=subprocess.STDOUT)
154
+ except subprocess.CalledProcessError as e:
155
+ print(e.output.decode())
156
+ code = self.attempt_to_fix_sctript("plot.py", e.output.decode())
157
+ self.last_code = code[0]
158
+
159
+ except Exception as e:
160
+ print(e)
161
+ code = self.attempt_to_fix_sctript("plot.py", str(e))
162
+ self.last_code = code[0]
163
+
164
+
165
+ # Return a list containing the filename of the plot and the code used to generate it
166
+ return [[(None, ("plot.png",))], self.last_code]
167
+
168
+ def create_plot(self, human_input, data_type_info_text):
169
+ """
170
+ Create a plot based on the input provided by the human.
171
+
172
+ :param human_input: Input provided by the human.
173
+ :type human_input: str
174
+ """
175
+
176
+ # Create a history of generated scripts if one exists
177
+ if self.last_code != "":
178
+ history = "\n\nLast script generated:\n\n" + self.last_code
179
+ else:
180
+ history = ""
181
+
182
+
183
+ # Generate a response by running the chain with the relevant data types, history, file name and human input
184
+ response = self.chain.run({"data_types" : data_type_info_text, "history" : history, "file": self.logfile_name, "human_input": human_input})
185
+ print(response)
186
+
187
+ # Parse the code from the response
188
+ code = self.extract_code_snippets(response)
189
+
190
+ # Write the code to a file named "plot.py"
191
+ self.write_plot_script("plot.py", code[0])
192
+
193
+ # Store the code for the next iteration
194
+ self.last_code = code[0]
195
+
196
+
197
+ return code[0]
198
+
199
+
200
+ def parse_mavlink_log(self):
201
+ """
202
+ Parse the MAVLink log to extract unique message types and their fields.
203
+
204
+ :return: A JSON string representation of the unique message types and their fields.
205
+ :rtype: str
206
+ """
207
+ # Initialize a dictionary to store unique message types and their fields
208
+ self.message_types = {}
209
+
210
+ # Establish a MAVLink connection
211
+ mav_log = mavutil.mavlink_connection(self.logfile_name)
212
+
213
+ # Loop through the log file and extract all unique message types
214
+ while True:
215
+ try:
216
+ # Receive a message
217
+ msg = mav_log.recv_match(blocking=False, type=None)
218
+ # Check if we received a message
219
+ if msg is None:
220
+ break
221
+
222
+ # Store the unique message types and their fields in the dictionary
223
+ if msg.get_type() not in self.message_types:
224
+ # Add the message type and its fields to the dictionary
225
+ self.message_types[msg.get_type()] = {
226
+ "count": 1,
227
+ "fields": {field: type(getattr(msg, field)).__name__ for field in msg.get_fieldnames()}
228
+ }
229
+ else:
230
+ # Increment the count for this message type
231
+ self.message_types[msg.get_type()]["count"] += 1
232
+
233
+ except KeyboardInterrupt:
234
+ break
235
+ except:
236
+ print("Unknown error")
237
+ break
238
+
239
+ # Create embeddings for the message types
240
+ self.create_embeddings(self.message_types)
241
+
242
+ # Return a JSON string of the message types
243
+ return json.dumps(self.message_types, indent=4)
244
+
245
+ def create_embedding(self, texts):
246
+ """
247
+ Create OpenAI embeddings for a list of texts.
248
+
249
+ :param texts: A list of texts to create embeddings for.
250
+ :type texts: list of str
251
+ """
252
+ # Initialize a dictionary to store the embeddings
253
+ embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
254
+ self.db = Chroma.from_texts(texts, embeddings)
255
+
256
+ def create_embeddings(self, message_types):
257
+ """
258
+ Create OpenAI embeddings for a dictionary of message types.
259
+
260
+ :param message_types: A dictionary of message types to create embeddings for.
261
+ :type message_types: dict
262
+ """
263
+ print(message_types)
264
+
265
+ # Convert the message types to a list of JSON strings
266
+ texts = []
267
+ for message_type in message_types:
268
+ texts.append(json.dumps({ message_type : message_types[message_type]}))
269
+
270
+ print(f"Texts: {texts}")
271
+ # Create the embeddings
272
+ self.create_embedding(texts)
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio==3.32.0
2
+ langchain==0.0.183
3
+ Pillow==9.5.0
4
+ pymavlink==2.4.37
5
+ python-dotenv==1.0.0
6
+ openai==0.27.7
7
+ chromadb==0.3.25
8
+ pymavlink==2.4.37
9
+ matplotlib==3.7.1
10
+ tiktoken==0.4.0
template.env ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ OPENAI_API_KEY=
2
+ OPENAI_MODEL=gpt-3.5-turbo