Spaces:
Build error
Build error
Commit
·
f92a70f
1
Parent(s):
3814576
updated chatbot to be more user friendly
Browse files- .gitignore +3 -1
- README.md +16 -4
- gpt_mavplot.py +22 -9
- llm/gptPlotCreator.py +33 -22
- requirements.txt +10 -0
.gitignore
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
.env
|
| 2 |
*.pyc
|
| 3 |
plot.py
|
| 4 |
-
plot.png
|
|
|
|
|
|
|
|
|
| 1 |
.env
|
| 2 |
*.pyc
|
| 3 |
plot.py
|
| 4 |
+
plot.png
|
| 5 |
+
.venv
|
| 6 |
+
.chroma
|
README.md
CHANGED
|
@@ -10,20 +10,32 @@ MAVPlot is a Python-based project which uses Gradio as an interface and GPT-X po
|
|
| 10 |
|
| 11 |
## Installation
|
| 12 |
|
| 13 |
-
|
| 14 |
|
| 15 |
```shell
|
| 16 |
git clone https://github.com/yourusername/mavplot.git
|
| 17 |
```
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
```shell
|
| 23 |
pip install -r requirements.txt
|
| 24 |
```
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
Copy the `template.env` file to a file named `.env` in your root directory. Add your Openai API key to the file
|
| 29 |
|
|
|
|
| 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 |
|
gpt_mavplot.py
CHANGED
|
@@ -23,15 +23,25 @@ def bot(history):
|
|
| 23 |
|
| 24 |
# Check if it is a string
|
| 25 |
if isinstance(user_input, str):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
history = history + [(None, f"Here is the code used to generate the plot:")]
|
| 34 |
history = history + [(None, f"{response[1]}")]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
else:
|
| 36 |
file_path = user_input[0]
|
| 37 |
plot_creator.set_logfile_name(file_path)
|
|
@@ -41,10 +51,12 @@ def bot(history):
|
|
| 41 |
|
| 42 |
history[-1][0] = f"user uploaded file: {filename}{extension}"
|
| 43 |
history[-1][1] = "processing file..."
|
|
|
|
|
|
|
| 44 |
data_types = plot_creator.parse_mavlink_log()
|
| 45 |
-
history = history + [(None, f"here are the data types in the log {data_types}")]
|
| 46 |
history = history + [(None, f"I am done processing the file. Now you can ask me to generate a plot.")]
|
| 47 |
-
|
|
|
|
| 48 |
return history
|
| 49 |
|
| 50 |
|
|
@@ -59,7 +71,7 @@ with gr.Blocks() as demo:
|
|
| 59 |
placeholder="Enter text and press enter, or upload an image",
|
| 60 |
).style(container=False)
|
| 61 |
with gr.Column(scale=0.15, min_width=0):
|
| 62 |
-
btn = gr.UploadButton("📁", file_types=["
|
| 63 |
|
| 64 |
txt.submit(add_text, [chatbot, txt], [chatbot, txt]).then(
|
| 65 |
bot, chatbot, chatbot
|
|
@@ -69,4 +81,5 @@ with gr.Blocks() as demo:
|
|
| 69 |
)
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
|
|
|
| 72 |
demo.launch()
|
|
|
|
| 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)
|
|
|
|
| 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 |
|
|
|
|
| 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
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
+
demo.queue()
|
| 85 |
demo.launch()
|
llm/gptPlotCreator.py
CHANGED
|
@@ -136,28 +136,49 @@ class PlotCreator:
|
|
| 136 |
"""
|
| 137 |
self.logfile_name = filename
|
| 138 |
|
| 139 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
"""
|
| 141 |
Create a plot based on the input provided by the human.
|
| 142 |
|
| 143 |
:param human_input: Input provided by the human.
|
| 144 |
:type human_input: str
|
| 145 |
"""
|
|
|
|
| 146 |
# Create a history of generated scripts if one exists
|
| 147 |
if self.last_code != "":
|
| 148 |
history = "\n\nLast script generated:\n\n" + self.last_code
|
| 149 |
else:
|
| 150 |
history = ""
|
| 151 |
|
| 152 |
-
# Search the database for documents that are similar to the human input
|
| 153 |
-
docs = self.db.similarity_search(human_input)
|
| 154 |
-
|
| 155 |
-
# Concatenate the content of the documents into a string
|
| 156 |
-
data_type_info_text = ""
|
| 157 |
-
for doc in docs:
|
| 158 |
-
data_type_info_text += doc.page_content + "\n\n"
|
| 159 |
-
|
| 160 |
-
print(docs)
|
| 161 |
|
| 162 |
# Generate a response by running the chain with the relevant data types, history, file name and human input
|
| 163 |
response = self.chain.run({"data_types" : data_type_info_text, "history" : history, "file": self.logfile_name, "human_input": human_input})
|
|
@@ -169,21 +190,11 @@ class PlotCreator:
|
|
| 169 |
# Write the code to a file named "plot.py"
|
| 170 |
self.write_plot_script("plot.py", code[0])
|
| 171 |
|
| 172 |
-
# Run the script and if it doesn't work, capture the output and call attempt_to_fix_script
|
| 173 |
-
try:
|
| 174 |
-
subprocess.check_output(["python", "plot.py"], stderr=subprocess.STDOUT)
|
| 175 |
-
except subprocess.CalledProcessError as e:
|
| 176 |
-
print(e.output.decode())
|
| 177 |
-
code = self.attempt_to_fix_sctript("plot.py", e.output.decode())
|
| 178 |
-
except Exception as e:
|
| 179 |
-
print(e)
|
| 180 |
-
code = self.attempt_to_fix_sctript("plot.py", str(e))
|
| 181 |
-
|
| 182 |
# Store the code for the next iteration
|
| 183 |
self.last_code = code[0]
|
| 184 |
|
| 185 |
-
|
| 186 |
-
return
|
| 187 |
|
| 188 |
|
| 189 |
def parse_mavlink_log(self):
|
|
|
|
| 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})
|
|
|
|
| 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):
|
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
|