Spaces:
Runtime error
Runtime error
Haseeb
commited on
Commit
β’
95bc5d9
1
Parent(s):
eabad6b
Added BardCoder
Browse files- CHANGELOGS.md +27 -0
- LICENSE.txt +9 -0
- README.md +164 -11
- app.py +629 -0
- bash_src/clear_cache.sh +14 -0
- bash_src/code_runner.sh +113 -0
- lib/__pycache__/blacklist_commands.cpython-311.pyc +0 -0
- lib/bardcoder_lib.py +440 -0
- lib/blacklist_commands.py +369 -0
- lib/extensions_map.py +58 -0
- lib/sharegpt_api.py +29 -0
- requirements.txt +12 -0
- resources/upload_data_ui.png +0 -0
- uploads/.DS_Store +0 -0
CHANGELOGS.md
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Changelog π
|
2 |
+
|
3 |
+
All notable changes to this project will be documented in this file.
|
4 |
+
## [1.3] - 2023-05-2
|
5 |
+
### Added
|
6 |
+
- **Updated** with totaly new _UI_ and _UX_. π¨
|
7 |
+
- Updated security for code checking and prompt checking. π
|
8 |
+
- Added new Help section. π
|
9 |
+
- Fix API Key bugs. π
|
10 |
+
|
11 |
+
## [1.2] - 2023-05-28
|
12 |
+
### Added
|
13 |
+
- **Advanced security** for code checking and prompt checking. π
|
14 |
+
- Support for graphs, charts, and tables. π
|
15 |
+
- More libraries for data science. π§¬
|
16 |
+
|
17 |
+
## [1.1] - 2023-05-22
|
18 |
+
### Added
|
19 |
+
- Upload files option. π€
|
20 |
+
- API key settings. π
|
21 |
+
### Fixed
|
22 |
+
- Error handling from server. π
|
23 |
+
|
24 |
+
## [1.0] - 2023-05-21
|
25 |
+
### Added
|
26 |
+
- Auto barcode generator. π·
|
27 |
+
- Auto barcode interpreter. π
|
LICENSE.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2023 HeavenHM
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6 |
+
|
7 |
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8 |
+
|
9 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
README.md
CHANGED
@@ -1,13 +1,166 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
-
title: AutoBard Coder
|
3 |
-
emoji: π¦
|
4 |
-
colorFrom: red
|
5 |
-
colorTo: purple
|
6 |
-
sdk: streamlit
|
7 |
-
sdk_version: 1.21.0
|
8 |
-
app_file: app.py
|
9 |
-
pinned: false
|
10 |
-
license: mit
|
11 |
-
---
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# AutoBard-Coder
|
2 |
+
![cover_logo](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/logo.png?raw=true "")</br>
|
3 |
+
- **Support this Project** <a href="https://www.buymeacoffee.com/haseebheaven"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee&emoji=&slug=haseebheaven&button_colour=40DCA5&font_colour=ffffff&font_family=Cookie&outline_colour=000000&coffee_colour=FFDD00" /></a>
|
4 |
+
|
5 |
+
[![Plugin](https://img.shields.io/badge/Auto%20Bard-Repo-blue)](https://replit.com/@HaseebMir/AutoBard-Coder)
|
6 |
+
[![Plugin](https://img.shields.io/badge/Auto%20Bard-Replit-blue)](https://autobard-coder.haseebmir.repl.co)
|
7 |
+
[![Plugin](https://img.shields.io/badge/Code%20Interpreter-Hosted-blue)](https://haseeb-heaven-autobard-coder-bardcode-interpreter-5g2n5e.streamlit.app/)
|
8 |
+
[![Plugin](https://img.shields.io/badge/Code%20Interpreter-CodeSpace-blue)](https://haseeb-heaven-legendary-guide-x555j7vpv4phprg6-8501.preview.app.github.dev/)</br>
|
9 |
+
|
10 |
+
## General Information
|
11 |
+
AutoBard-Coder is a code generator for bard.ππ©βπ»π¨βπ» It uses the Bard API to interact with bard and refine the results for coding purposes. The main purpose of this is for research and educational purposes. This can generate code from prompts and fix itself unless the code is fixed. It is written in Python and has dependencies on streamlit and bard-coder.
|
12 |
+
|
13 |
+
## Application main UI.
|
14 |
+
### BardCoder:
|
15 |
+
![bard_coder](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/bardcoder_main.png?raw=true "")</br>
|
16 |
+
|
17 |
+
### Coder Interpreter:
|
18 |
+
![code_interpreter](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/code_interpreter_main_ui.png?raw=true "")</br>
|
19 |
+
|
20 |
+
## Setup Process
|
21 |
+
To set up AutoBard-Coder, you will need to install the dependencies: `streamlit` and `bardapi`. </br>
|
22 |
+
You will also need to authenticate with the Bard API by visiting https://bard.google.com/, </br>
|
23 |
+
1.Opening the console with F12 </br>
|
24 |
+
2.Going to **Application** β **Cookies**, and copying the value of the **__Secure-1PSID** cookie.</br>
|
25 |
+
3.Then you can setup the key to your **local environment** by running the following command:</br>
|
26 |
+
```export _BARD_API_KEY=<__Secure-1PSID value>``` Remember the `_` before `BARD_API_KEY` is important. </br>
|
27 |
+
Or you can use `bardcoder_lib.py` to set the key to your **local environment** by running the following method `bard_coder.set_api_key(<__Secure-1PSID value>)` </br>
|
28 |
+
![showcase_api_key](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/showcase_api_key.png?raw=true "")</br>
|
29 |
+
|
30 |
+
4.Then you can run the application by running the following command:
|
31 |
+
```python bardcoder.py``` </br>
|
32 |
+
5.You can also run the code interpreter by running the following command:
|
33 |
+
```python code_interpreter.py``` </br>
|
34 |
+
|
35 |
+
## Installation Guide
|
36 |
+
Do not expose the `__Secure-1PSID value`.
|
37 |
+
Note that while it is referred to as an `API KEY` for convenience, it is not an officially provided API KEY.
|
38 |
+
|
39 |
+
To install the dependencies, you can use pip to install from the **requirements.txt** file:
|
40 |
+
This will install streamlit, bard-coder-api, and any other dependencies listed in the `requirements.txt` file.
|
41 |
+
|
42 |
+
## Usage
|
43 |
+
There are 3 components of this application:
|
44 |
+
1. BardCoder: A coding assistant from Bard which automatically generates code from bard responses and refines it for coding purposes. This can be used by developers to get the correct code from Bard when they need help because the output is refined and fixed multiple times to provide the correct code.
|
45 |
+
- File: `bardcoder.py`
|
46 |
+
|
47 |
+
![bardcoder_main](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/bardcoder_main.png?raw=true "")</br>
|
48 |
+
|
49 |
+
2. AutoBard-Coder Interpreter: This is an interpreter for bardcoder which can be used to run the code generated from bardcoder. This can be very useful for all users who want to do data analysis and machine learning with bard. This can generate graphs and charts from bard responses and can be used to do data analysis.
|
50 |
+
- File: `bardcode_interpreter.py`
|
51 |
+
|
52 |
+
3. BardCoder Lib: This is a library for bardcoder which can be used to generate code from bard responses and refine it for coding purposes. You can use it to build your own application using bard.
|
53 |
+
- File: `bardcoder_lib.py`
|
54 |
+
|
55 |
+
## Using BardCoder:
|
56 |
+
To use BardCoder, you will need to run the following command:
|
57 |
+
```python bardcoder.py```
|
58 |
+
This will open the BardCoder UI in the terminal. And will be asked to enter prompts for bard.
|
59 |
+
![bard_coder](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/bardcoder_main.png?raw=true "")</br>
|
60 |
+
Not this assumes you have already set the `API KEY` in your local environment. If you have not, please refer to the setup process.
|
61 |
+
The code generated will be placed in folder `codes` and the response will be placed in folder `response`.
|
62 |
+
|
63 |
+
## Using Code Interpreter:
|
64 |
+
To use Code Interpreter, you will need to run the following command:
|
65 |
+
```python bardcode_interpreter.py```
|
66 |
+
This will open the Code Interpreter UI in the Web Browser. And will be asked to enter prompts for bard.
|
67 |
+
![code_interpreter](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/code_interpreter_main_ui.png?raw=true "")</br>
|
68 |
+
|
69 |
+
Now this assumes you have already set the `API KEY` in your local environment. If you have not, please refer to the setup process.
|
70 |
+
You can also setup the `API KEY` in the UI settings.
|
71 |
+
![code_interpreter_settings](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/code_interpreter_settings.png?raw=true "")</br>
|
72 |
+
|
73 |
+
Before prompting you should check the **Options** to change **Filenames** and **Filepaths** and other settings.
|
74 |
+
|
75 |
+
### Uploading data for data analysis:
|
76 |
+
You can set the `Upload Data` in **Options** to upload data for data analysis.
|
77 |
+
![upload_data_ui](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/upload_data_ui.png?raw=true "")</br>
|
78 |
+
|
79 |
+
After uploading the files you can ask for the prompts from bard. And you will get the output like this.
|
80 |
+
![upload_data_output](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/upload_data_output.png?raw=true "")</br>
|
81 |
+
|
82 |
+
You can also set the `Expected Output` in **Options** to get the **Accuracy** of the code generated.
|
83 |
+
![expected_output](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/code_interpreter_expected_output.png?raw=true "")</br>
|
84 |
+
|
85 |
+
And output will be generated like this with hilighting the **Accuracy** of the code generated.
|
86 |
+
![expected_output](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/code_interpreter_output.png?raw=true "")</br>
|
87 |
+
|
88 |
+
### Sharing code with others:
|
89 |
+
You can share code with others by using the **ShareGPT** feature. You will get sharable link for your code.
|
90 |
+
![bard_code_share](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/bard_code_share.png?raw=true "")</br>
|
91 |
+
|
92 |
+
### Code Interpreter Demo:
|
93 |
+
[Code Interpreter Demo](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/code_interpreter_demo.webm)</br>
|
94 |
+
[![code_interpreter_demo](https://img.youtube.com/vi/Flg1qUzs9ew/0.jpg)](https://youtube.com/shorts/Flg1qUzs9ew)
|
95 |
+
|
96 |
+
The code generated will be placed in folder `codes` and the response will be placed in folder `response` and upload files will be placed in folder `uploads`.
|
97 |
+
|
98 |
+
### Safety and Measures:
|
99 |
+
This now has **safety measures** built in _Code Interpreter_ and now it can detect the **safety** of the code generated and can **fix** it.</br>
|
100 |
+
So now all the repo and code in **READ_ONLY** mode. And you can only run the code in **READ_ONLY** mode.</br>
|
101 |
+
All the commands which **__changes,move,delete,rename__** files are disabled in **READ_ONLY** mode.</br>
|
102 |
+
This has advanced **safety measures** and can detect the **safety** of the code generated and can **fix** it.</br>
|
103 |
+
![safety_output](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/safety_output.png?raw=true "")</br>
|
104 |
+
|
105 |
+
|
106 |
+
### Graphs and Visual Charts output:
|
107 |
+
To get Graphs and Charts output for your data visualization, you will install python packages `matplotlib`, `seaborn`, `cartopy`, `plotnine` and more. and run the following command: </br>
|
108 |
+
```python bardcode_interpreter.py``` </br>
|
109 |
+
And ask for the prompts from bard. And you will get the output like this:
|
110 |
+
Example Prompt:
|
111 |
+
```
|
112 |
+
In Python write me program to.
|
113 |
+
1.Read the data from file called 'employees.json'
|
114 |
+
2.Count no. of Males and Females in file.
|
115 |
+
4.Draw PIE graph of these in using Python Matplotlib.
|
116 |
+
5.Show me that output.
|
117 |
+
```
|
118 |
+
|
119 |
+
And you will get output like this.
|
120 |
+
![expected_output](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/employees_chart.png?raw=true "")</br>
|
121 |
+
|
122 |
+
## Directory Structure
|
123 |
+
This application uses the Bard API to interact with bard and refine the results for coding purposes. All data that is generated is stored in the following directories:
|
124 |
+
- `response`: This directory contains all the responses from bard.
|
125 |
+
- `codes`: This directory contains all the code generated from bard.
|
126 |
+
- `uploads`: This directory contains all the data that is uploaded to bard.
|
127 |
+
|
128 |
+
## Features
|
129 |
+
Some of the features of AutoBard-Coder include:
|
130 |
+
- Automatic code generation from bard responses
|
131 |
+
- Refinement of code for coding purposes
|
132 |
+
- Ability to fix its own code
|
133 |
+
- Ability to debug its own code
|
134 |
+
- Ability to generate single or multiple responses from bard
|
135 |
+
- Ability to save responses and run them locally
|
136 |
+
- Ability to access local files for code interpretation
|
137 |
+
- Data analysis and machine learning capabilities
|
138 |
+
- Graphs and charts generation from bard responses
|
139 |
+
- Ability to upload files for data analysis and machine learning.
|
140 |
+
- Share code with others with powerered by ShareGPT.
|
141 |
+
- Advanced safety measures to detect the safety of the code generated.
|
142 |
+
|
143 |
+
## Help Section.
|
144 |
+
Now you can get help directly from the application. You can get help by clicking on the **Help** button in the UI.
|
145 |
+
![code_interpreter_help](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/resources/code_interpreter_help.png?raw=true "")</br>
|
146 |
+
|
147 |
+
## Changelog π
|
148 |
+
|
149 |
+
All notable changes to this project will be documented in this file.
|
150 |
+
|
151 |
+
[CHANGELOGS](https://github.com/haseeb-heaven/AutoBard-Coder/blob/master/CHANGELOGS.md)
|
152 |
+
|
153 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
+
| Version | Date | Added | Fixed |
|
156 |
+
| ------- | ---------- | -------------------------------------------------- | -------------------- |
|
157 |
+
| 1.3 | 2023-05-29 | - **Updated** with totally new _UI_ and _UX_. π¨<br>- Updated security for code checking and prompt checking. π<br>- Added new Help section. π | - Fixed API Key issues. |
|
158 |
+
| 1.2 | 2023-05-28 | - Advanced security for code and prompt checking. π<br>- Support for graphs, charts, and tables. π<br>- More libraries for data science. 𧬠| |
|
159 |
+
| 1.1 | 2023-05-27 | - Upload files option. π€<br>- API key settings. π | - Error handling from server. π |
|
160 |
+
| 1.0 | 2023-05-26 | - Auto barcode generator. π·<br>- Auto barcode interpreter. π | |
|
161 |
+
|
162 |
+
|
163 |
+
|
164 |
+
|
165 |
+
## License and Author
|
166 |
+
AutoBard-Coder was created by HeavenHM and is licensed under the MIT license.
|
app.py
ADDED
@@ -0,0 +1,629 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Details : AutoBard-Coder is code genrator for bard. It is used to generate code from bard response.
|
3 |
+
its using Bard API to interact with bard and refine the results for coding purpose.
|
4 |
+
The main purpose of this is for research and educational purpose.
|
5 |
+
This is using unofficial bard api and not affiliated with bard in any way - So use it at your own risk.
|
6 |
+
This can generate the code from prompt and fix itself unless the code is fixed.
|
7 |
+
Language : Python
|
8 |
+
Dependencies : streamlit, bard-coder
|
9 |
+
Author : HeavenHM.
|
10 |
+
License : MIT
|
11 |
+
Date : 21-05-2023
|
12 |
+
"""
|
13 |
+
|
14 |
+
# Import the required libraries
|
15 |
+
import logging
|
16 |
+
import sys
|
17 |
+
import streamlit as st
|
18 |
+
from os import path
|
19 |
+
import time
|
20 |
+
import traceback
|
21 |
+
from lib.bardcoder_lib import BardCoder
|
22 |
+
import subprocess
|
23 |
+
from io import StringIO
|
24 |
+
import re
|
25 |
+
from lib.sharegpt_api import sharegpt_get_url
|
26 |
+
from lib.blacklist_commands import harmful_commands_python, harmful_commands_cpp, harmful_prompts
|
27 |
+
from PIL import Image
|
28 |
+
import tokenize
|
29 |
+
import io
|
30 |
+
|
31 |
+
# The input limit of Bard is 4,000 character (As per the Bard API documentation)
|
32 |
+
# But you can give more input upto 10,000 characters. so we are gonna stick to that.
|
33 |
+
BARD_FILE_SIZE_LIMIT = 10000
|
34 |
+
|
35 |
+
# Function to measure the accuracy of the code
|
36 |
+
|
37 |
+
|
38 |
+
def measure_accuracy(counter):
|
39 |
+
accuracy = 1 / (counter + 1)
|
40 |
+
accuracy_percentage = accuracy * 100
|
41 |
+
st.info(
|
42 |
+
f"Output has been fixed {counter} times with accuracy {accuracy_percentage:.0f}%")
|
43 |
+
|
44 |
+
|
45 |
+
def show_content(content):
|
46 |
+
# Open the file and read its contents
|
47 |
+
with open(content, "r") as f:
|
48 |
+
markdown_text = f.read()
|
49 |
+
|
50 |
+
# Display the Markdown text in the app
|
51 |
+
st.markdown(markdown_text)
|
52 |
+
|
53 |
+
|
54 |
+
# method to execute the bard coder process
|
55 |
+
def auto_bard_execute(prompt, code_file='code.txt', code_choices='code_choice', expected_output=None, exec_type='single', rate_limiter_delay=5):
|
56 |
+
try:
|
57 |
+
# Additional prompt for class clarification.
|
58 |
+
# prompt += "\n" + f"Note: The Name the class should be {code_file} if Java language is requested"
|
59 |
+
|
60 |
+
# Additional prompt for no input clarification.
|
61 |
+
prompt += "\n" + "Dont ask the input from user.If input values are provided in code just use them. otherwise, you can hardcode the input values in code."
|
62 |
+
|
63 |
+
# Setting the prompt.
|
64 |
+
prompt_status, error_reason = st.session_state.bard_coder.set_prompt(prompt)
|
65 |
+
if not prompt_status:
|
66 |
+
st.error(f"Error no data was recieved from Server, Reason {error_reason}")
|
67 |
+
st.stop()
|
68 |
+
|
69 |
+
# Get the code from the response.
|
70 |
+
code = st.session_state.bard_coder.get_code()
|
71 |
+
# Save the code to file
|
72 |
+
saved_file = st.session_state.bard_coder.save_code(code_file, code)
|
73 |
+
if saved_file:
|
74 |
+
st.info(f"Code saved to file {saved_file}")
|
75 |
+
else:
|
76 |
+
st.info("Code not saved to file")
|
77 |
+
return None, None, False
|
78 |
+
|
79 |
+
st.info("Executing primary code")
|
80 |
+
# check for safe code to run.
|
81 |
+
code = st.session_state.bard_coder.read_file(saved_file)
|
82 |
+
safe_code = False
|
83 |
+
code_snippet = None
|
84 |
+
code_command = None
|
85 |
+
safe_code_dict = []
|
86 |
+
|
87 |
+
if code:
|
88 |
+
safe_code_dict = is_code_safe(code)
|
89 |
+
# Get tuple from list of tuples code_safe_dict
|
90 |
+
safe_code = safe_code_dict[0][0]
|
91 |
+
code_command = safe_code_dict[0][1]
|
92 |
+
code_snippet = safe_code_dict[0][2]
|
93 |
+
|
94 |
+
if safe_code:
|
95 |
+
code_output = st.session_state.bard_coder.execute_code(saved_file)
|
96 |
+
if code_output and code_output != None and code_output.__len__() > 0:
|
97 |
+
if 'error' in code_output.lower() or 'exception' in code_output.lower():
|
98 |
+
st.info(f"Error in executing code with type {exec_type}")
|
99 |
+
return code_output, saved_file, False
|
100 |
+
|
101 |
+
# Check if expected output is in code output.
|
102 |
+
if expected_output and expected_output in code_output:
|
103 |
+
code_choices_output = [code_output]
|
104 |
+
return code_output, saved_file, True
|
105 |
+
else:
|
106 |
+
if exec_type == 'single':
|
107 |
+
time.sleep(rate_limiter_delay)
|
108 |
+
return code_output, saved_file, False
|
109 |
+
else:
|
110 |
+
time.sleep(rate_limiter_delay)
|
111 |
+
else:
|
112 |
+
return code_output, saved_file, False
|
113 |
+
|
114 |
+
# Save all the code choices to file
|
115 |
+
if exec_type == 'multiple':
|
116 |
+
st.session_state.bard_coder.save_code_choices(code_choices)
|
117 |
+
|
118 |
+
st.info("Executing code choices")
|
119 |
+
code_choices_output = st.session_state.bard_coder.execute_code_choices()
|
120 |
+
code_choices_output.append(code_output)
|
121 |
+
st.info(f"Output: {code_choices_output}")
|
122 |
+
|
123 |
+
return code_choices_output, saved_file, False
|
124 |
+
else:
|
125 |
+
for safe_codes in safe_code_dict:
|
126 |
+
if safe_codes[0]: # Skip if code is safe
|
127 |
+
continue
|
128 |
+
|
129 |
+
safe_code = safe_codes[0]
|
130 |
+
code_command = safe_codes[1]
|
131 |
+
code_snippet = safe_codes[2]
|
132 |
+
st.error(f"Error: Cannot execute the code because of illegal command found '{code_command}' in code snippet '{code_snippet}'")
|
133 |
+
BardCoder.write_log(f"Cannot run the code:\n'{code}'\nbecause of illegal command found '{code_command}' in code snippet '{code_snippet}'")
|
134 |
+
st.stop()
|
135 |
+
return None, None, False
|
136 |
+
|
137 |
+
except Exception as e:
|
138 |
+
# show_outputf the stack trace
|
139 |
+
stack_trace = traceback.format_exc()
|
140 |
+
st.info(stack_trace)
|
141 |
+
st.info(str(e))
|
142 |
+
|
143 |
+
|
144 |
+
# method to execute the bard coder process
|
145 |
+
def auto_bard_setup(prompt, code_file='code.txt', code_choices='code_choice', expected_output=None, exec_type='single', rate_limiter_delay=5):
|
146 |
+
|
147 |
+
# Append the codes directory to filename
|
148 |
+
code_file = path.join("codes", code_file)
|
149 |
+
test_cases_output = 0 # Test cases for output.
|
150 |
+
|
151 |
+
# Start the bard coder process
|
152 |
+
code_choices_output, saved_file, status = auto_bard_execute(
|
153 |
+
prompt, code_file, code_choices, expected_output, exec_type)
|
154 |
+
code_output = None
|
155 |
+
|
156 |
+
# Check if file is saved.
|
157 |
+
if not saved_file:
|
158 |
+
return code_choices_output, saved_file, status
|
159 |
+
|
160 |
+
if status:
|
161 |
+
st.info(
|
162 |
+
f"Expected output found in file {saved_file}\nOutput: {code_choices_output}")
|
163 |
+
else:
|
164 |
+
st.info(
|
165 |
+
f"Expected output not found in file {saved_file}\nOutput: {code_choices_output}")
|
166 |
+
if code_choices_output:
|
167 |
+
code_output = ''.join(code_choices_output)
|
168 |
+
if code_output and code_output != None and code_output.__len__() > 0:
|
169 |
+
|
170 |
+
# Check for errors like 'error' or 'Error' check case sensitivity and add more error checks.
|
171 |
+
if code_output is not None:
|
172 |
+
code_output = "".join(code_output)
|
173 |
+
else:
|
174 |
+
code_output = ""
|
175 |
+
|
176 |
+
if code_output:
|
177 |
+
while 'error' in code_output.lower() or 'exception' in code_output.lower():
|
178 |
+
st.info(
|
179 |
+
"Error in executing code,Trying to fix the code with error")
|
180 |
+
|
181 |
+
# Re-prompt on error.
|
182 |
+
code = st.session_state.bard_coder.get_code()
|
183 |
+
prompt = f"I got error while running the code {code_output}.\nPlease fix the code ``\n`{code}\n``` \nand try again.\nHere is error {code_output}\n\n" + \
|
184 |
+
"Note:The output should only be fixed code and nothing else. No explanation or anything else."
|
185 |
+
|
186 |
+
# Start the bard coder process again.
|
187 |
+
code_output, saved_file, status = auto_bard_execute(
|
188 |
+
prompt, code_file, code_choices, expected_output, exec_type)
|
189 |
+
# Sleep for 5 seconds before re-prompting. Dont get Rate limited.
|
190 |
+
time.sleep(rate_limiter_delay)
|
191 |
+
test_cases_output += 1
|
192 |
+
|
193 |
+
st.info("Code has been fixed for errors")
|
194 |
+
st.code(code_output, language="python")
|
195 |
+
|
196 |
+
# Check for expected output.
|
197 |
+
if code_output and expected_output and code_output.__len__() > 0:
|
198 |
+
|
199 |
+
# While expected output does not contain in code output.
|
200 |
+
while expected_output not in code_output:
|
201 |
+
st.info(
|
202 |
+
f"Expected output {expected_output} not found in code\nOutput: {code_output}")
|
203 |
+
|
204 |
+
# Re-prompt on expected output not found.
|
205 |
+
code = st.session_state.bard_coder.get_code()
|
206 |
+
prompt = f"I got output {code_output}.\nPlease fix the code ``\n`{code}\n``` \nand try again.\nHere is expected output: {code_output}\n\n" + \
|
207 |
+
"Note:The output should only be fixed code and nothing else. No explanation or anything else."
|
208 |
+
|
209 |
+
# start the bard coder process again.
|
210 |
+
code_output, saved_file, status = auto_bard_execute(
|
211 |
+
prompt, code_file, code_choices, expected_output, exec_type)
|
212 |
+
|
213 |
+
# Sleep for N seconds before re-prompting. Dont get Rate limited.
|
214 |
+
time.sleep(rate_limiter_delay)
|
215 |
+
test_cases_output += 1
|
216 |
+
|
217 |
+
st.info("Code has been fixed for expected output")
|
218 |
+
st.info(code_output)
|
219 |
+
else:
|
220 |
+
st.info("Not checking for code expected output")
|
221 |
+
else:
|
222 |
+
st.info("Code output is empty for error")
|
223 |
+
|
224 |
+
# Print output and information.
|
225 |
+
measure_accuracy(test_cases_output)
|
226 |
+
content_file = "response/content.md"
|
227 |
+
show_content(content_file)
|
228 |
+
return code_output, saved_file, status
|
229 |
+
|
230 |
+
|
231 |
+
def find_image_files(file_path):
|
232 |
+
# Create a regular expression for image files
|
233 |
+
image_regex = re.compile(r"\b\w+\.(png|jpg|jpeg|gif|bmp)", re.IGNORECASE)
|
234 |
+
|
235 |
+
# Open the code file
|
236 |
+
with open(file_path) as f:
|
237 |
+
# Read the lines
|
238 |
+
lines = f.readlines()
|
239 |
+
# Loop through the lines
|
240 |
+
for line in lines:
|
241 |
+
# Search for image files in the line
|
242 |
+
match = image_regex.search(line)
|
243 |
+
# If there is a match
|
244 |
+
if match:
|
245 |
+
# Get the image file name
|
246 |
+
image_file = match.group()
|
247 |
+
# Print the image file name
|
248 |
+
return image_file
|
249 |
+
return None
|
250 |
+
|
251 |
+
|
252 |
+
def is_prompt_safe(prompt):
|
253 |
+
if prompt is None:
|
254 |
+
BardCoder.write_log("Prompt is Empty")
|
255 |
+
return False
|
256 |
+
|
257 |
+
BardCoder.write_log("Checking prompt for safety")
|
258 |
+
|
259 |
+
# Extra care for prompt input.
|
260 |
+
prompt_list = [re.sub(r'[^\w\s]', '', re.sub(r'(\*\*|__)(.*?)(\*\*|__)', r'\2', re.sub(
|
261 |
+
r'^\W+|\W+$', '', item))).strip() for item in re.split('\n| ', prompt.lower()) if item.strip() != '']
|
262 |
+
prompt_list = [re.sub(r'\d+', '', i) for i in prompt_list]
|
263 |
+
BardCoder.write_log(f"Prompt list is {prompt_list}")
|
264 |
+
|
265 |
+
# Convert the code to lowercase and split it into a list of words
|
266 |
+
|
267 |
+
# Check if any harmful command is in the list of words
|
268 |
+
for command in harmful_prompts:
|
269 |
+
if command in prompt_list:
|
270 |
+
BardCoder.write_log(f"Prompt is not safe because of illegal command found '{command}'")
|
271 |
+
return False, command
|
272 |
+
BardCoder.write_log(f"Input Prompt is safe")
|
273 |
+
return True, None
|
274 |
+
|
275 |
+
|
276 |
+
def tokenize_source_code(source_code):
|
277 |
+
tokens = []
|
278 |
+
try:
|
279 |
+
for token in tokenize.generate_tokens(io.StringIO(source_code).readline):
|
280 |
+
if token.type not in [tokenize.ENCODING, tokenize.NEWLINE, tokenize.INDENT, tokenize.DEDENT]:
|
281 |
+
if any(char in token.string for char in ['::', '.', '->', '_']) or token.string.isalnum():
|
282 |
+
token_str = re.sub(r'\'|\"', '', token.string)
|
283 |
+
tokens.append(token_str)
|
284 |
+
except tokenize.TokenError:
|
285 |
+
if st.session_state.bard_coder:
|
286 |
+
BardCoder.write_log("Error parsing the tokens")
|
287 |
+
if tokens:
|
288 |
+
tokens = list(([token.lower() for token in tokens]))
|
289 |
+
if st.session_state.bard_coder:
|
290 |
+
BardCoder.write_log(f"Tokenise was called and Tokens length is {tokens.__len__()}")
|
291 |
+
return tokens
|
292 |
+
|
293 |
+
|
294 |
+
def is_code_safe(code):
|
295 |
+
if st.session_state.bard_coder:
|
296 |
+
BardCoder.write_log("Checking code for safety")
|
297 |
+
|
298 |
+
# Combine both lists
|
299 |
+
harmful_code_commands = harmful_commands_python + harmful_commands_cpp
|
300 |
+
|
301 |
+
# Tokenize the code
|
302 |
+
tokens_list = tokenize_source_code(code)
|
303 |
+
|
304 |
+
# Initialize the output dictionary
|
305 |
+
output_dict = []
|
306 |
+
|
307 |
+
# Check if any harmful command is in the list of words
|
308 |
+
for command in harmful_code_commands:
|
309 |
+
for token in tokens_list:
|
310 |
+
if command == token:
|
311 |
+
output_dict.append((False, command, token))
|
312 |
+
|
313 |
+
if output_dict is None or output_dict.__len__() == 0:
|
314 |
+
output_dict = [(True, None, None)]
|
315 |
+
if st.session_state.bard_coder:
|
316 |
+
BardCoder.write_log(f"Output dict is {output_dict}")
|
317 |
+
return output_dict
|
318 |
+
|
319 |
+
|
320 |
+
def load_css(file_name):
|
321 |
+
# Open the file and read the content
|
322 |
+
with open(file_name) as fp:
|
323 |
+
css = fp.read()
|
324 |
+
# Use st.components.v1.html to load the CSS file
|
325 |
+
st.markdown(f'<style>{css}</style>', unsafe_allow_html=True)
|
326 |
+
|
327 |
+
|
328 |
+
def display_logo(logo_file: str, title: str):
|
329 |
+
# create two columns
|
330 |
+
col1, col2 = st.columns(2, gap='large')
|
331 |
+
|
332 |
+
# use the first column for the image
|
333 |
+
col1.image(logo_file, width=370)
|
334 |
+
|
335 |
+
# use the second column for the title
|
336 |
+
col2.title(title)
|
337 |
+
|
338 |
+
|
339 |
+
def dsiplay_buttons(is_prompt_valid: bool):
|
340 |
+
col1, col2, col3 = st.columns(3, gap='large')
|
341 |
+
|
342 |
+
with col1: # use the first column
|
343 |
+
run_button = st.button("Run", key="run-button", use_container_width=True,
|
344 |
+
disabled=not is_prompt_valid) # place the run button as a regular button
|
345 |
+
|
346 |
+
with col2: # use the second column
|
347 |
+
share_button = st.button("Share", key="share-button", use_container_width=True,
|
348 |
+
disabled=not is_prompt_valid) # place the share button as a regular button
|
349 |
+
|
350 |
+
with col3:
|
351 |
+
# place the help button as a regular button
|
352 |
+
help_button = st.button(
|
353 |
+
"Help", key="help-button", use_container_width=True)
|
354 |
+
|
355 |
+
return run_button, share_button, help_button
|
356 |
+
|
357 |
+
|
358 |
+
def init_bard_coder_session(api_key=None, timeout=10):
|
359 |
+
# Initialize the bard coder session
|
360 |
+
bard_coder = BardCoder(api_key=api_key, enable_logs=True, timeout=timeout)
|
361 |
+
return bard_coder
|
362 |
+
|
363 |
+
|
364 |
+
def init_session_state():
|
365 |
+
# Initialize the session state variables
|
366 |
+
if "bard_coder" not in st.session_state:
|
367 |
+
st.session_state.bard_coder = None
|
368 |
+
|
369 |
+
if "api_key_initialized" not in st.session_state:
|
370 |
+
st.session_state.api_key_initialized = False
|
371 |
+
|
372 |
+
if "code_output" not in st.session_state:
|
373 |
+
st.session_state.code_output = ""
|
374 |
+
|
375 |
+
if "messages" not in st.session_state:
|
376 |
+
st.session_state.messages = ""
|
377 |
+
|
378 |
+
if "text_area" not in st.session_state:
|
379 |
+
st.session_state.text_area = ""
|
380 |
+
|
381 |
+
if "file_size" not in st.session_state:
|
382 |
+
st.session_state.file_size = 0
|
383 |
+
|
384 |
+
if "file_char_count" not in st.session_state:
|
385 |
+
st.session_state.file_char_count = 0
|
386 |
+
|
387 |
+
|
388 |
+
if __name__ == "__main__":
|
389 |
+
try:
|
390 |
+
BardCoder.write_log("Starting the streamlit App")
|
391 |
+
|
392 |
+
# Load the CSS file named style.css
|
393 |
+
load_css("styles/style.css")
|
394 |
+
|
395 |
+
# Upload file data variables
|
396 |
+
upload_prompt_data, upload_data, uploaded_file = None, None, None
|
397 |
+
|
398 |
+
# Initialize the session state variables
|
399 |
+
BardCoder.write_log("Initializing the session state variables")
|
400 |
+
init_session_state()
|
401 |
+
BardCoder.write_log("Session state variables initialized")
|
402 |
+
|
403 |
+
# Set the logo and title
|
404 |
+
logo_file = "resources/logo.png"
|
405 |
+
title = "Code Interpreter"
|
406 |
+
display_logo(logo_file, title)
|
407 |
+
BardCoder.write_log("Logo and title set")
|
408 |
+
|
409 |
+
# Use the text_area variable from the session state for input
|
410 |
+
prompt = st.text_area(placeholder="Enter your prompt here", label="Prompt",label_visibility="hidden", height=300, key="text_area_input")
|
411 |
+
|
412 |
+
# check if prompt is changed.
|
413 |
+
if prompt != st.session_state.text_area:
|
414 |
+
BardCoder.write_log(f"Prompt changed from '{st.session_state.text_area}' to '{prompt}'")
|
415 |
+
st.session_state.text_area = prompt
|
416 |
+
|
417 |
+
character_count: int = len(st.session_state.text_area)
|
418 |
+
|
419 |
+
# Status info message. (Char count and file size)
|
420 |
+
status_info_msg = f"Characters:{character_count}/{BARD_FILE_SIZE_LIMIT}"
|
421 |
+
|
422 |
+
if st.session_state.file_size > 0:
|
423 |
+
BardCoder.write_log(f"File Char count is {st.session_state.file_char_count}")
|
424 |
+
character_count += st.session_state.file_char_count
|
425 |
+
# Update the character count for file size.
|
426 |
+
status_info_msg = f"Characters:{character_count}/{BARD_FILE_SIZE_LIMIT}"
|
427 |
+
status_info_msg += " | " + f"File Size is {st.session_state.file_size/1024:.2f}Kb | {st.session_state.file_size/1024/1024:.2f}Mb"
|
428 |
+
|
429 |
+
st.info(status_info_msg)
|
430 |
+
|
431 |
+
# check the Prompt for safety and file size exceeding 4,000 characters.
|
432 |
+
prompt_safe = True
|
433 |
+
if st.session_state.bard_coder:
|
434 |
+
prompt_safe, command = is_prompt_safe(prompt)
|
435 |
+
if not prompt_safe:
|
436 |
+
BardCoder.write_log(f"Error in prompt because of unsafe command found '{command}'")
|
437 |
+
st.error(f"Error in prompt because of illegal command found '{command}'")
|
438 |
+
|
439 |
+
if character_count > BARD_FILE_SIZE_LIMIT or st.session_state.file_char_count > BARD_FILE_SIZE_LIMIT:
|
440 |
+
st.error(f"Error in prompt The file size limit exceeded {BARD_FILE_SIZE_LIMIT} characters")
|
441 |
+
|
442 |
+
# Adding the upload file option
|
443 |
+
uploaded_file = st.file_uploader("Choose a file")
|
444 |
+
if uploaded_file is not None:
|
445 |
+
|
446 |
+
# To read file as bytes:
|
447 |
+
bytes_data = uploaded_file.getvalue()
|
448 |
+
# get the file size
|
449 |
+
st.session_state.file_size = uploaded_file.size
|
450 |
+
|
451 |
+
# To convert to a string based IO:
|
452 |
+
stringio = StringIO(uploaded_file.getvalue().decode("utf-8"))
|
453 |
+
# To read file as string:
|
454 |
+
upload_data = stringio.read()
|
455 |
+
|
456 |
+
# Count the number of characters in the file
|
457 |
+
st.session_state.file_char_count = len(upload_data)
|
458 |
+
|
459 |
+
# write the file to uploads directory
|
460 |
+
with open("uploads/" + uploaded_file.name, "w") as f:
|
461 |
+
f.write(upload_data)
|
462 |
+
|
463 |
+
# Display a success message
|
464 |
+
st.success("File uploaded successfully.")
|
465 |
+
|
466 |
+
# Setting options for the application
|
467 |
+
with st.expander("Options"):
|
468 |
+
code_file = st.text_input("Filename for the generated code (without extension):", value="generated_code")
|
469 |
+
code_choices = st.text_input("Filename for code choices:", value="code_choices")
|
470 |
+
expected_output = st.text_input("Expected output (leave blank if none):")
|
471 |
+
exec_type = st.selectbox("Execution type:", ["single", "multiple"], index=0)
|
472 |
+
timeout_delay = st.number_input("Timeout (in seconds):", value=10)
|
473 |
+
|
474 |
+
with st.expander("Settings"):
|
475 |
+
bard_key_help_text = """
|
476 |
+
How to obtain Google Bard API key.
|
477 |
+
1. Visit bard.google.com and open the console with F12
|
478 |
+
2. Go to Application β Cookies and copy the __Secure-1PSID value
|
479 |
+
3. This is your API key paste it below.
|
480 |
+
"""
|
481 |
+
st.info(bard_key_help_text)
|
482 |
+
bard_api_key = st.text_input(label="API Key", label_visibility="hidden", type="password", placeholder="Enter your bard API key.")
|
483 |
+
|
484 |
+
if bard_api_key and not st.session_state.api_key_initialized:
|
485 |
+
# how to call write_file static method from BardCoder class
|
486 |
+
BardCoder.write_log("Starting init API Key")
|
487 |
+
|
488 |
+
st.session_state.bard_coder = init_bard_coder_session(bard_api_key,timeout_delay)
|
489 |
+
if BardCoder.bard_init:
|
490 |
+
st.session_state.api_key_initialized = True
|
491 |
+
BardCoder.write_log("Bard Coder initialized successfully")
|
492 |
+
st.info("Bard Coder initialized successfully")
|
493 |
+
else:
|
494 |
+
st.session_state.api_key_initialized = False
|
495 |
+
BardCoder.write_log("Error initializing Bard Coder")
|
496 |
+
st.error("Error initializing Bard Coder")
|
497 |
+
|
498 |
+
|
499 |
+
|
500 |
+
# Setting advanced options for the application
|
501 |
+
with st.expander("Advanced"):
|
502 |
+
try:
|
503 |
+
# button to show logs.
|
504 |
+
show_logs = st.button("Show Logs", key="show-logs-button", use_container_width=True)
|
505 |
+
if show_logs:
|
506 |
+
if st.session_state.bard_coder:
|
507 |
+
logs_data = st.session_state.bard_coder.read_file(st.session_state.bard_coder.logs_file)
|
508 |
+
st.code(logs_data, language="python")
|
509 |
+
except Exception as e:
|
510 |
+
st.error(f"Error in showing logs {e}")
|
511 |
+
|
512 |
+
# button to show content.
|
513 |
+
try:
|
514 |
+
show_content_button = st.button("Show Content", key="show-content-button", use_container_width=True)
|
515 |
+
if show_content_button:
|
516 |
+
if st.session_state.bard_coder:
|
517 |
+
content_data = st.session_state.bard_coder.read_file("response/response.md")
|
518 |
+
st.code(content_data, language="python")
|
519 |
+
except Exception as e:
|
520 |
+
st.error(f"Error in showing content {e}")
|
521 |
+
|
522 |
+
# button to show response.
|
523 |
+
try:
|
524 |
+
show_response_button = st.button("Show Response", key="show-response-button", use_container_width=True)
|
525 |
+
if show_response_button:
|
526 |
+
if st.session_state.bard_coder:
|
527 |
+
response_data = st.session_state.bard_coder.read_file("response/response.json")
|
528 |
+
st.code(response_data, language="json")
|
529 |
+
except Exception as e:
|
530 |
+
st.error(f"Error in showing response {e}")
|
531 |
+
|
532 |
+
# Setting the buttons for the application
|
533 |
+
run_button, share_button, help_button = dsiplay_buttons(prompt_safe and st.session_state.file_char_count < BARD_FILE_SIZE_LIMIT)
|
534 |
+
|
535 |
+
# Setting application to run
|
536 |
+
if run_button:
|
537 |
+
# Code to execute when the "Run" button is clicked
|
538 |
+
|
539 |
+
# Check if API Key is empty
|
540 |
+
if bard_api_key is None or bard_api_key == "" or bard_api_key.__len__() == 0:
|
541 |
+
st.error("Error executing code the API key is missing from settings.\nPlease go to settings and add your API key.")
|
542 |
+
BardCoder.write_log("Error executing code the API key is missing from settings.Please go to settings and add your API key.")
|
543 |
+
st.stop()
|
544 |
+
|
545 |
+
# Clear the previous cache.
|
546 |
+
st.info("Running the code interpreter...")
|
547 |
+
subprocess.call(['bash', 'bash_src/clear_cache.sh'])
|
548 |
+
|
549 |
+
# Append the uploaded file data to prompt
|
550 |
+
if upload_data:
|
551 |
+
prompt += "\n" + f"Here is the file called {uploaded_file.name} at location {'uploads/' + uploaded_file.name} data.\n" + \
|
552 |
+
f"```\n{upload_data}\n```"
|
553 |
+
|
554 |
+
# If graph were requested.
|
555 |
+
if 'graph' in prompt.lower():
|
556 |
+
prompt += "\n" + "using Python use Matplotlib save the graph in file called 'graph.png'"
|
557 |
+
|
558 |
+
# if Chart were requested
|
559 |
+
if 'chart' in prompt.lower() or 'plot' in prompt.lower():
|
560 |
+
prompt += "\n" + "using Python use Plotly save the chart in file called 'chart.png'"
|
561 |
+
|
562 |
+
# if Table were requested
|
563 |
+
if 'table' in prompt.lower():
|
564 |
+
prompt += "\n" + "using Python use Pandas save the table in file called 'table.md'"
|
565 |
+
|
566 |
+
# Refine the prompt for harmful commands.
|
567 |
+
if prompt_safe:
|
568 |
+
# Run the auto bard setup process.
|
569 |
+
log_container = st.empty()
|
570 |
+
st.session_state.code_output, saved_file, status = auto_bard_setup(prompt, code_file, code_choices,
|
571 |
+
expected_output, exec_type,timeout_delay)
|
572 |
+
else:
|
573 |
+
st.error(f"Cannot execute the prompt because of illegal command found '{command}'")
|
574 |
+
BardCoder.write_log(f"Cannot execute the prompt: '{prompt}' because of illegal command found '{command}'")
|
575 |
+
st.stop()
|
576 |
+
|
577 |
+
# Check if output is Graph,Chart request.
|
578 |
+
if 'graph' in prompt.lower() or 'chart' in prompt.lower():
|
579 |
+
image_file_graph = find_image_files(saved_file)
|
580 |
+
if image_file_graph:
|
581 |
+
BardCoder.write_log(f"Graph image file is {image_file_graph} and code file is {saved_file}")
|
582 |
+
image = Image.open(image_file_graph)
|
583 |
+
st.image(image, caption='Graph Output')
|
584 |
+
|
585 |
+
# Check if output in Table request.
|
586 |
+
if 'table' in prompt.lower():
|
587 |
+
table_file = "table.md"
|
588 |
+
table_file_data = st.session_state.bard_coder.read_file(
|
589 |
+
table_file)
|
590 |
+
if table_file_data:
|
591 |
+
st.markdown(table_file_data)
|
592 |
+
|
593 |
+
# Adding Share button
|
594 |
+
if share_button:
|
595 |
+
if st.session_state.code_output is None or st.session_state.messages is None:
|
596 |
+
BardCoder.write_log("ShareGPT: Error Please run the code generator first")
|
597 |
+
st.error("Error: Please run the code generator first")
|
598 |
+
else:
|
599 |
+
gpt_data = prompt
|
600 |
+
human_data = ""
|
601 |
+
|
602 |
+
if st.session_state.messages:
|
603 |
+
human_data = "Bard Logs: \n" + st.session_state.messages
|
604 |
+
if st.session_state.code_output:
|
605 |
+
human_data += "\nOutput:\n" + st.session_state.code_output
|
606 |
+
human_data += "\n\n[AutoBard-Coder: Repo](https:#github.com/haseeb-heaven/AutoBard-Coder)"
|
607 |
+
|
608 |
+
if gpt_data.__len__() > 0 and human_data.__len__() > 0:
|
609 |
+
sharegpt_url = sharegpt_get_url(gpt_data, human_data)
|
610 |
+
st.info(f"ShareGPT Url: {sharegpt_url}")
|
611 |
+
else:
|
612 |
+
BardCoder.write_log("ShareGPT: Error Please run the code generator first")
|
613 |
+
st.error("Error: Please run the code generator first")
|
614 |
+
|
615 |
+
# Adding Help button
|
616 |
+
if help_button:
|
617 |
+
content_file = "README.md"
|
618 |
+
if st.session_state.bard_coder:
|
619 |
+
content_data = st.session_state.bard_coder.read_file(content_file)
|
620 |
+
st.markdown(content_data, unsafe_allow_html=True)
|
621 |
+
else:
|
622 |
+
st.error("API key is missing from settings.\nPlease go to settings and add your API key.")
|
623 |
+
BardCoder.write_log("Help: Error API key is missing from settings.")
|
624 |
+
|
625 |
+
except Exception as e:
|
626 |
+
# show_outputf the stack trace
|
627 |
+
stack_trace = traceback.format_exc()
|
628 |
+
st.error("Error: " + str(e))
|
629 |
+
st.error(stack_trace)
|
bash_src/clear_cache.sh
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# Create codes folder if it doesn't exist
|
4 |
+
if [ ! -d "codes" ]; then
|
5 |
+
mkdir codes
|
6 |
+
fi
|
7 |
+
|
8 |
+
# removing only all executables from codes.
|
9 |
+
file -F: codes/* | grep "executable" | cut -d: -f1 | xargs rm
|
10 |
+
|
11 |
+
# clearing response files and logs
|
12 |
+
#truncate -s 0 bardcoder.log
|
13 |
+
truncate -s 0 response/content.md
|
14 |
+
truncate -s 0 response/response.json
|
bash_src/code_runner.sh
ADDED
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
filename=$1
|
4 |
+
debug=0
|
5 |
+
cpp_version="c++17"
|
6 |
+
|
7 |
+
if [[ $filename == *.c ]]; then
|
8 |
+
extension=".c"
|
9 |
+
compiler="gcc"
|
10 |
+
language="c"
|
11 |
+
elif [[ $filename == *.cpp ]]; then
|
12 |
+
extension=".cpp"
|
13 |
+
compiler="g++"
|
14 |
+
language="c++"
|
15 |
+
elif [[ $filename == *.java ]]; then
|
16 |
+
extension=".java"
|
17 |
+
compiler="javac"
|
18 |
+
language="java"
|
19 |
+
elif [[ $filename == *.go ]]; then
|
20 |
+
extension=".go"
|
21 |
+
compiler="go run"
|
22 |
+
language="go"
|
23 |
+
elif [[ $filename == *.cs ]]; then
|
24 |
+
extension=".cs"
|
25 |
+
compiler="csc"
|
26 |
+
language="csharp"
|
27 |
+
elif [[ $filename == *.swift ]]; then
|
28 |
+
extension=".swift"
|
29 |
+
compiler="swift"
|
30 |
+
language="swift"
|
31 |
+
# add for python
|
32 |
+
elif [[ $filename == *.py ]]; then
|
33 |
+
extension=".py"
|
34 |
+
compiler="python3"
|
35 |
+
language="python"
|
36 |
+
elif [[ $filename == *.js ]]; then
|
37 |
+
extension=".js"
|
38 |
+
compiler="node"
|
39 |
+
language="javascript"
|
40 |
+
elif [[ $filename == *.rs ]]; then
|
41 |
+
extension=".rs"
|
42 |
+
compiler="rustc"
|
43 |
+
language="rust"
|
44 |
+
else
|
45 |
+
echo "Error: Unsupported file type"
|
46 |
+
exit 1
|
47 |
+
fi
|
48 |
+
|
49 |
+
if [ $language == "c++" ]; then
|
50 |
+
if [[ $3 == c++* ]]; then
|
51 |
+
version=${3#c++}
|
52 |
+
if [[ $version == 17 || $version == 14 || $version == 11 || $version == 0x ]]; then
|
53 |
+
cpp_version="c++$version"
|
54 |
+
fi
|
55 |
+
fi
|
56 |
+
fi
|
57 |
+
|
58 |
+
if [[ $2 == "--debug" ]]; then
|
59 |
+
debug=1
|
60 |
+
fi
|
61 |
+
|
62 |
+
if [ $debug -eq 1 ]; then
|
63 |
+
if [ $language == "c++" ]; then
|
64 |
+
echo "Compiling $filename with $compiler (C++ $cpp_version)..."
|
65 |
+
else
|
66 |
+
echo "Compiling $filename with $compiler..."
|
67 |
+
fi
|
68 |
+
fi
|
69 |
+
|
70 |
+
if [ $language == "c" ]; then
|
71 |
+
$compiler $filename -o ${filename%.*}
|
72 |
+
elif [ $language == "c++" ]; then
|
73 |
+
$compiler $filename -std=$cpp_version -o ${filename%.*}
|
74 |
+
elif [ $language == "java" ]; then
|
75 |
+
$compiler $filename
|
76 |
+
elif [ $language == "go" ]; then
|
77 |
+
$compiler $filename
|
78 |
+
elif [ $language == "csharp" ]; then
|
79 |
+
$compiler /out:${filename%.*}.exe $filename
|
80 |
+
elif [ $language == "swift" ]; then
|
81 |
+
$compiler $filename
|
82 |
+
elif [ $language == "python" ]; then
|
83 |
+
$compiler $filename
|
84 |
+
elif [ $language == "javascript" ]; then
|
85 |
+
$compiler $filename
|
86 |
+
elif [ $language == "rust" ]; then
|
87 |
+
$compiler $filename
|
88 |
+
else
|
89 |
+
echo "Error: Unsupported file type"
|
90 |
+
exit 1
|
91 |
+
fi
|
92 |
+
|
93 |
+
if [ $? -ne 0 ]; then
|
94 |
+
echo "Compilation failed"
|
95 |
+
exit 1
|
96 |
+
fi
|
97 |
+
|
98 |
+
if [ $debug -eq 1 ]; then
|
99 |
+
echo "Running ${filename%.*}..."
|
100 |
+
fi
|
101 |
+
|
102 |
+
if [ $language == "java" ]; then
|
103 |
+
java ${filename%.*}
|
104 |
+
elif [ $language == "go" ]; then
|
105 |
+
$compiler $filename
|
106 |
+
else
|
107 |
+
./${filename%.*}
|
108 |
+
fi
|
109 |
+
|
110 |
+
if [ $debug -eq 1 ]; then
|
111 |
+
echo "Finished running ${filename%.*}"
|
112 |
+
fi
|
113 |
+
|
lib/__pycache__/blacklist_commands.cpython-311.pyc
ADDED
Binary file (4.06 kB). View file
|
|
lib/bardcoder_lib.py
ADDED
@@ -0,0 +1,440 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Details : BardCoder Library is code genrator for bard. It is used to generate code from bard response.
|
3 |
+
its using Bard API to interact with bard and refine the results for coding purpose.
|
4 |
+
The main purpose of this is to integrate bard with any projects and make code generation easy.
|
5 |
+
Language : Python
|
6 |
+
Author : HeavenHM.
|
7 |
+
License : MIT
|
8 |
+
Date : 21-05-2023
|
9 |
+
"""
|
10 |
+
|
11 |
+
# import libraries
|
12 |
+
import json
|
13 |
+
import logging
|
14 |
+
import os
|
15 |
+
import json
|
16 |
+
from bardapi import Bard
|
17 |
+
import traceback
|
18 |
+
import subprocess
|
19 |
+
import time
|
20 |
+
from os import path
|
21 |
+
import lib.extensions_map as extensions_map
|
22 |
+
from lib.extensions_map import get_file_extesion
|
23 |
+
import inspect
|
24 |
+
|
25 |
+
class BardCoder:
|
26 |
+
global bard
|
27 |
+
global logger
|
28 |
+
bard_init = False
|
29 |
+
logs_enabled = False
|
30 |
+
logs_file = "bardcoder.log"
|
31 |
+
response_id, conversation_id, content, factuality_queries, text_query, code_choices, code_extension = None, None, None, None, None, None, None
|
32 |
+
|
33 |
+
# Initial setup
|
34 |
+
def __init__(self,api_key=None,timeout=10,enable_logs=False):
|
35 |
+
try:
|
36 |
+
BardCoder.write_log("Starting to initialize BardCoder.")
|
37 |
+
# Dont initialize if api key is None.
|
38 |
+
if api_key is None or api_key == "" or '.' not in api_key:
|
39 |
+
BardCoder.write_log("BardCoder API key is missing or is invalid Skipping init")
|
40 |
+
self.bard = None
|
41 |
+
BardCoder.bard_init = False
|
42 |
+
return None
|
43 |
+
|
44 |
+
# Setting up the api key.
|
45 |
+
BardCoder.write_log("Setting up the api key")
|
46 |
+
if api_key and '.' in api_key:
|
47 |
+
self.set_api_key(api_key)
|
48 |
+
|
49 |
+
BardCoder.write_log("Setting up the prompt")
|
50 |
+
# Setting up Bard from BardAPI.
|
51 |
+
self.bard = Bard(timeout=timeout) # Set timeout in seconds
|
52 |
+
|
53 |
+
if not self.bard:
|
54 |
+
BardCoder.write_log("BardCoder not initialized...Skipping init.")
|
55 |
+
self.bard = None
|
56 |
+
BardCoder.bard_init = False
|
57 |
+
return None
|
58 |
+
|
59 |
+
# Enable logs
|
60 |
+
if enable_logs:
|
61 |
+
self.enable_logs()
|
62 |
+
|
63 |
+
BardCoder.write_log("Setting up the logger")
|
64 |
+
# Setups the logging.
|
65 |
+
self.logger = self.setup_logger(self.logs_file)
|
66 |
+
|
67 |
+
BardCoder.bard_init = True
|
68 |
+
BardCoder.write_log("BardCoder initialized successfully.")
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
self.add_log(str(e))
|
72 |
+
stack_trace = traceback.format_exc()
|
73 |
+
self.add_log(stack_trace)
|
74 |
+
self.bard = None
|
75 |
+
|
76 |
+
def write_log(data:str=None):
|
77 |
+
if data is None:
|
78 |
+
raise ValueError("Data cannot be None")
|
79 |
+
with open("bardcoder.log",'a') as f:
|
80 |
+
# Get the name of the calling function
|
81 |
+
caller_name = inspect.stack()[1][3]
|
82 |
+
f.write(f"{time.strftime('%d-%m-%Y %H:%M:%S')} {caller_name}: {data}\n")
|
83 |
+
|
84 |
+
# Set the api key
|
85 |
+
def set_api_key(self, api_key):
|
86 |
+
if api_key:
|
87 |
+
os.environ['_BARD_API_KEY'] = api_key
|
88 |
+
|
89 |
+
# Set the prompt for bard
|
90 |
+
def set_prompt(self, prompt):
|
91 |
+
try:
|
92 |
+
# Get the response from the prompt.
|
93 |
+
response = self.get_response(prompt)
|
94 |
+
if response:
|
95 |
+
data = json.dumps(response, indent=4)
|
96 |
+
|
97 |
+
if data:
|
98 |
+
# Getting the data from the response.
|
99 |
+
json_data = json.loads(data)
|
100 |
+
if json_data:
|
101 |
+
self.content = json_data['content']
|
102 |
+
self.add_log("Content: " + self.content)
|
103 |
+
|
104 |
+
# Saving the response to file.
|
105 |
+
self.add_log("Saving response to file.")
|
106 |
+
self.save_file("response/response.json",json.dumps(response, indent=4))
|
107 |
+
self.save_file("response/content.md", self.content)
|
108 |
+
|
109 |
+
# Getting the content from the response.
|
110 |
+
self.conversation_id = json_data['conversation_id']
|
111 |
+
if self.conversation_id:
|
112 |
+
self.add_log(f"Conversation ID: {self.conversation_id}")
|
113 |
+
|
114 |
+
# Getting the conversation ID from the response.
|
115 |
+
self.response_id = json_data['response_id']
|
116 |
+
if self.response_id:
|
117 |
+
self.add_log(f"Response ID: {self.response_id}")
|
118 |
+
|
119 |
+
# Get the factuality queries from the response.
|
120 |
+
self.factuality_queries = json_data['factualityQueries']
|
121 |
+
if self.factuality_queries:
|
122 |
+
for factualityQuery in self.factuality_queries:
|
123 |
+
self.add_log(f"Factuality Query: {factualityQuery}")
|
124 |
+
# Get the links from the response.
|
125 |
+
links = self.get_links()
|
126 |
+
self.add_log(f"Links: {links}")
|
127 |
+
|
128 |
+
# Get the text query from the response.
|
129 |
+
self.text_query = json_data['textQuery']
|
130 |
+
if self.text_query:
|
131 |
+
self.add_log(f"Text Query: {self.text_query}")
|
132 |
+
|
133 |
+
# Getting the code choices from the response.
|
134 |
+
self.code_choices = json_data['choices']
|
135 |
+
self.add_log(f"Code Choices: {self.code_choices}")
|
136 |
+
if self.code_choices:
|
137 |
+
for code_choice in self.code_choices:
|
138 |
+
self.add_log(f"Code Choice: {code_choice}")
|
139 |
+
|
140 |
+
# Mark end of init. - Success
|
141 |
+
self.add_log("Success.")
|
142 |
+
return True,"Success."
|
143 |
+
else:
|
144 |
+
self.add_log("Json data is empty.")
|
145 |
+
return False,"Json data is empty."
|
146 |
+
else:
|
147 |
+
self.add_log("Data is empty.")
|
148 |
+
return False,"Data is empty."
|
149 |
+
else:
|
150 |
+
self.add_log("Response is empty.\nCheck if the API key is valid.")
|
151 |
+
return False,"Response is empty.\nCheck if the API key is valid."
|
152 |
+
|
153 |
+
except Exception as e:
|
154 |
+
# show stack trace
|
155 |
+
stack_trace = traceback.format_exc()
|
156 |
+
self.add_log(stack_trace)
|
157 |
+
return False,str(e)
|
158 |
+
|
159 |
+
# get the response from bard
|
160 |
+
def get_response(self, prompt: str):
|
161 |
+
if not prompt:
|
162 |
+
self.add_log("Prompt is empty.")
|
163 |
+
return ""
|
164 |
+
if self.bard:
|
165 |
+
self.add_log("Getting response from bard.")
|
166 |
+
response = self.bard.get_answer(prompt)
|
167 |
+
else:
|
168 |
+
self.add_log("Bard is not initialized.")
|
169 |
+
return None
|
170 |
+
|
171 |
+
# get response from bard
|
172 |
+
return response
|
173 |
+
|
174 |
+
# get multiple responses from bard
|
175 |
+
def get_code_choice(self, index):
|
176 |
+
if index < len(self.code_choices):
|
177 |
+
choice_content = self.code_choices[index]['content'][0]
|
178 |
+
start_index = choice_content.find('```') + 3
|
179 |
+
end_index = choice_content.find('```', start_index)
|
180 |
+
if start_index != -1 and end_index != -1:
|
181 |
+
extracted_data = choice_content[start_index:end_index]
|
182 |
+
result = extracted_data.strip()
|
183 |
+
# Remove the code language identifier
|
184 |
+
result = result[result.find('\n') + 1:]
|
185 |
+
return result
|
186 |
+
else:
|
187 |
+
return None
|
188 |
+
else:
|
189 |
+
return None
|
190 |
+
|
191 |
+
# setting the logger
|
192 |
+
def setup_logger(self, filename: str, level=logging.INFO):
|
193 |
+
# Remove existing handlers from the root logger
|
194 |
+
for handler in logging.root.handlers[:]:
|
195 |
+
logging.root.removeHandler(handler)
|
196 |
+
|
197 |
+
# Set up a file handler to write logs to a file
|
198 |
+
file_handler = logging.FileHandler(filename)
|
199 |
+
formatter = logging.Formatter('%(asctime)s - %(message)s', datefmt='%d-%m-%y %H:%M:%S')
|
200 |
+
file_handler.setFormatter(formatter)
|
201 |
+
logging.root.addHandler(file_handler)
|
202 |
+
logging.root.setLevel(level)
|
203 |
+
|
204 |
+
return logging.getLogger(__name__)
|
205 |
+
|
206 |
+
# get the code from bard response
|
207 |
+
def get_code(self):
|
208 |
+
try:
|
209 |
+
if self.content:
|
210 |
+
self.add_log("Getting code from content.")
|
211 |
+
|
212 |
+
data = self.content
|
213 |
+
start_index = data.find("```")
|
214 |
+
if start_index == -1:
|
215 |
+
return None
|
216 |
+
start_index += 3
|
217 |
+
end_index = data.find("```", start_index)
|
218 |
+
if end_index == -1:
|
219 |
+
return None
|
220 |
+
extracted_data = data[start_index:end_index]
|
221 |
+
result = extracted_data.strip()
|
222 |
+
|
223 |
+
# Remove the code language identifier
|
224 |
+
result = result[result.find('\n') + 1:]
|
225 |
+
self.add_log(f"Code: {result}")
|
226 |
+
return result
|
227 |
+
except Exception as e:
|
228 |
+
self.add_log(str(e))
|
229 |
+
stack_trace = traceback.format_exc()
|
230 |
+
self.add_log(stack_trace)
|
231 |
+
|
232 |
+
def save_code(self, filename="code.txt"):
|
233 |
+
code = self.get_code()
|
234 |
+
self.code_extenstion = '.' + self.get_code_extension()
|
235 |
+
if code:
|
236 |
+
code = code.replace("\\n", "\n").replace("\\t", "\t")
|
237 |
+
self.add_log(f"Saving code with filename: {filename} and extension: {self.code_extenstion} and code: {code}")
|
238 |
+
|
239 |
+
# Add extension to filename
|
240 |
+
extension = extensions_map.get_file_extesion(self.code_extenstion) or self.code_extenstion
|
241 |
+
filename = filename + extension
|
242 |
+
|
243 |
+
with open(filename, 'w') as f:
|
244 |
+
f.write(code)
|
245 |
+
self.add_log(f"{filename} saved.")
|
246 |
+
return filename
|
247 |
+
|
248 |
+
def save_code(self, filename="code.txt", code='self.add_log("Hello World")'):
|
249 |
+
self.add_log(f"Saving code with filename: {filename}")
|
250 |
+
extension = self.get_code_extension()
|
251 |
+
if extension:
|
252 |
+
self.code_extenstion = '.' + extension
|
253 |
+
#code = self.get_code()
|
254 |
+
if code:
|
255 |
+
code = code.replace("\\n", "\n").replace("\\t", "\t")
|
256 |
+
self.add_log(f"Saving code with filename: {filename} and extension: {self.code_extenstion} and code: {code}")
|
257 |
+
|
258 |
+
# Add extension to filename
|
259 |
+
extension = extensions_map.get_file_extesion(self.code_extenstion) or self.code_extenstion
|
260 |
+
filename = filename + extension
|
261 |
+
|
262 |
+
with open(filename, 'w') as f:
|
263 |
+
f.write(code)
|
264 |
+
self.add_log(f"{filename} saved.")
|
265 |
+
return filename
|
266 |
+
|
267 |
+
# save multiple codes from bard response
|
268 |
+
def save_code_choices(self, filename):
|
269 |
+
self.add_log(f"Saving code choices with filename: {filename}")
|
270 |
+
extension = self.get_code_extension()
|
271 |
+
if extension:
|
272 |
+
self.code_extension = '.' + extension
|
273 |
+
self.code_extension = extensions_map.get_file_extesion(self.code_extenstion) or self.code_extenstion
|
274 |
+
|
275 |
+
for index, choice in enumerate(self.code_choices):
|
276 |
+
choice_content = self.get_code_choice(index)
|
277 |
+
self.add_log(f"Enumurated Choice content: {choice}")
|
278 |
+
self.save_file("codes/"+filename+'_'+str(index+1) +
|
279 |
+
self.code_extension, choice_content)
|
280 |
+
|
281 |
+
# execute code from bard response using locally installed compilers.
|
282 |
+
# a support for online compilers will be added soon.
|
283 |
+
def execute_code(self, filename):
|
284 |
+
if filename:
|
285 |
+
self.add_log(f"Running {filename}")
|
286 |
+
output = self.run_code_exec(filename)
|
287 |
+
self.add_log(f"Output: {output}")
|
288 |
+
return output
|
289 |
+
return None
|
290 |
+
|
291 |
+
# execute code from bard response using locally installed compilers.
|
292 |
+
def run_code_exec(self, filename: str, debug: bool = False, cpp_version: str = "c++17"):
|
293 |
+
compiler_map = {
|
294 |
+
".c": ("gcc", "c"),
|
295 |
+
".cpp": ("g++", "c++"),
|
296 |
+
".java": ("java", "java"),
|
297 |
+
".go": ("go run", "go"),
|
298 |
+
".cs": ("csc", "csharp"),
|
299 |
+
".swift": ("swift", "swift"),
|
300 |
+
".py": ("python3", "python"),
|
301 |
+
".js": ("node", "javascript"),
|
302 |
+
".rs": ("rustc", "rust")
|
303 |
+
}
|
304 |
+
|
305 |
+
_, extension = os.path.splitext(filename)
|
306 |
+
self.add_log(f"Extension: {extension}")
|
307 |
+
if extension not in compiler_map:
|
308 |
+
self.add_log(f"Extension {extension} not supported.")
|
309 |
+
return
|
310 |
+
|
311 |
+
compiler, language = compiler_map[extension]
|
312 |
+
self.add_log(f"Compiler: {compiler}")
|
313 |
+
|
314 |
+
if language == "c++" and cpp_version.startswith("c++"):
|
315 |
+
version = cpp_version[3:]
|
316 |
+
if version in ["17", "14", "11", "0x"]:
|
317 |
+
cpp_version = f"c++{version}"
|
318 |
+
self.add_log(f":C++ Version: {cpp_version}")
|
319 |
+
|
320 |
+
if debug:
|
321 |
+
if language == "c++":
|
322 |
+
self.add_log(f"Compiling {filename} with {compiler} (C++ {cpp_version})...")
|
323 |
+
else:
|
324 |
+
self.add_log(f"Compiling {filename} with {compiler}...")
|
325 |
+
|
326 |
+
output = ""
|
327 |
+
try:
|
328 |
+
if language == "c":
|
329 |
+
output = subprocess.check_output([compiler, filename, "-o", f"{filename[:-len(extension)]}"], stderr=subprocess.STDOUT).decode('utf-8')
|
330 |
+
elif language == "c++":
|
331 |
+
output = subprocess.check_output([compiler, filename, f"-std={cpp_version}", "-o", f"{filename[:-len(extension)]}"], stderr=subprocess.STDOUT).decode('utf-8')
|
332 |
+
elif language == "java":
|
333 |
+
output = subprocess.check_output([compiler, filename], stderr=subprocess.STDOUT).decode('utf-8')
|
334 |
+
elif language in ["go", "swift", "python", "javascript","java"]:
|
335 |
+
output = subprocess.check_output([compiler, filename], stderr=subprocess.STDOUT).decode('utf-8')
|
336 |
+
elif language == "csharp":
|
337 |
+
output = subprocess.check_output([compiler, f"/out:{filename[:-len(extension)]}.exe", filename], stderr=subprocess.STDOUT).decode('utf-8')
|
338 |
+
elif language == "rust":
|
339 |
+
output = subprocess.check_output([compiler, filename], stderr=subprocess.STDOUT).decode('utf-8')
|
340 |
+
else:
|
341 |
+
self.add_log("Error: Unsupported file type")
|
342 |
+
return
|
343 |
+
self.add_log(f"Output: {output}")
|
344 |
+
except subprocess.CalledProcessError as e:
|
345 |
+
output += e.output.decode('utf-8')
|
346 |
+
self.add_log(f"Error: {output}")
|
347 |
+
|
348 |
+
if debug:
|
349 |
+
self.add_log(f"Running {filename[:-len(extension)]}...")
|
350 |
+
|
351 |
+
# Checking further output for syntax ./path/filename to run the executable
|
352 |
+
try:
|
353 |
+
# run C# with mono command. like this mono ./path/filename.exe
|
354 |
+
if language == "csharp":
|
355 |
+
output_file_exec = f"{filename[:-len(extension)]}.exe"
|
356 |
+
output += subprocess.check_output(['mono',output_file_exec], stderr=subprocess.STDOUT).decode('utf-8')
|
357 |
+
|
358 |
+
else:
|
359 |
+
output_file_exec = f"./{filename[:-len(extension)]}"
|
360 |
+
# checking if file exists output_file_exec
|
361 |
+
if os.path.isfile(output_file_exec):
|
362 |
+
output += subprocess.check_output([output_file_exec], stderr=subprocess.STDOUT).decode('utf-8')
|
363 |
+
except (subprocess.CalledProcessError, Exception) as e:
|
364 |
+
if isinstance(e, subprocess.CalledProcessError):
|
365 |
+
output += '\n' + e.output.decode('utf-8')
|
366 |
+
else:
|
367 |
+
output += '\n' + str(e)
|
368 |
+
self.add_log(f"Error: {output}")
|
369 |
+
|
370 |
+
|
371 |
+
if debug:
|
372 |
+
self.add_log(f"Finished running {filename[:-len(extension)]}")
|
373 |
+
|
374 |
+
self.add_log(f"Output: {output}")
|
375 |
+
return output
|
376 |
+
|
377 |
+
# execute all the code choices from bard response using locally installed compilers.
|
378 |
+
def execute_code_choices(self):
|
379 |
+
self.add_log("Running codes")
|
380 |
+
codes_choices_output = list()
|
381 |
+
for filename in os.listdir('codes'):
|
382 |
+
filepath = path.join('codes', filename)
|
383 |
+
self.add_log(f"Running {filepath}")
|
384 |
+
output = self.execute_code(filepath)
|
385 |
+
if output:
|
386 |
+
codes_choices_output.append(output)
|
387 |
+
time.sleep(5)
|
388 |
+
return codes_choices_output
|
389 |
+
|
390 |
+
# get the code extension from bard response - automatically detects the language from bard response.
|
391 |
+
def get_code_extension(self):
|
392 |
+
try:
|
393 |
+
code_content = self.content
|
394 |
+
if code_content and not code_content in "can't help":
|
395 |
+
self.code_extension = code_content.split('```')[1].split('\n')[0]
|
396 |
+
self.add_log(f"Code extension: {self.code_extension}")
|
397 |
+
return self.code_extension
|
398 |
+
except Exception as e:
|
399 |
+
stack_trace = traceback.format_exc()
|
400 |
+
self.add_log(stack_trace)
|
401 |
+
return None
|
402 |
+
|
403 |
+
# get the links from bard response
|
404 |
+
def get_links(self):
|
405 |
+
data = self.factuality_queries
|
406 |
+
links = []
|
407 |
+
self.add_log("Data: " + str(data))
|
408 |
+
if data is None or len(data) == 0:
|
409 |
+
self.add_log("Data is None.")
|
410 |
+
return links
|
411 |
+
try:
|
412 |
+
for inner_list in data[0]:
|
413 |
+
link = inner_list[2][0]
|
414 |
+
if link:
|
415 |
+
links.append(link)
|
416 |
+
except Exception as e:
|
417 |
+
stack_trace = traceback.format_exc()
|
418 |
+
self.add_log(stack_trace)
|
419 |
+
return links
|
420 |
+
self.add_log("Links: " + str(links))
|
421 |
+
return links
|
422 |
+
|
423 |
+
def save_file(self, filename, data):
|
424 |
+
with open(filename, 'w') as f:
|
425 |
+
f.write(data)
|
426 |
+
|
427 |
+
def read_file(self, filename):
|
428 |
+
with open(filename, 'r') as f:
|
429 |
+
return f.read()
|
430 |
+
|
431 |
+
def add_log(self, log, level=logging.INFO):
|
432 |
+
log_msg = inspect.stack()[1][3] + ": " + log
|
433 |
+
if self.logs_enabled:
|
434 |
+
self.logger.log(level, log_msg)
|
435 |
+
else:
|
436 |
+
self.logger = self.setup_logger(self.logs_file)
|
437 |
+
self.logger.log(level, log_msg)
|
438 |
+
|
439 |
+
def enable_logs(self):
|
440 |
+
self.logs_enabled = True
|
lib/blacklist_commands.py
ADDED
@@ -0,0 +1,369 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
harmful_prompts = [
|
2 |
+
'rename', # Existing commands
|
3 |
+
'delete',
|
4 |
+
'remove',
|
5 |
+
'shutdown',
|
6 |
+
'reboot',
|
7 |
+
'format',
|
8 |
+
'halt',
|
9 |
+
'poweroff',
|
10 |
+
'remove file',
|
11 |
+
'remove folder',
|
12 |
+
'remove directory',
|
13 |
+
'delete file',
|
14 |
+
'delete folder',
|
15 |
+
'delete directory',
|
16 |
+
'rename file',
|
17 |
+
'rename folder',
|
18 |
+
'rename directory',
|
19 |
+
'move file',
|
20 |
+
'move folder',
|
21 |
+
'move directory',
|
22 |
+
'replace file',
|
23 |
+
'replace folder',
|
24 |
+
'replace directory',
|
25 |
+
'exit', # Additional harmful prompts
|
26 |
+
'quit',
|
27 |
+
'vanish',
|
28 |
+
'null',
|
29 |
+
'void',
|
30 |
+
'wipe',
|
31 |
+
'erase',
|
32 |
+
'obliterate',
|
33 |
+
'discard',
|
34 |
+
'purge',
|
35 |
+
'exterminate',
|
36 |
+
'terminate',
|
37 |
+
'expunge',
|
38 |
+
'eliminate',
|
39 |
+
'kill file',
|
40 |
+
'kill folder',
|
41 |
+
'kill directory',
|
42 |
+
'destroy file',
|
43 |
+
'destroy folder',
|
44 |
+
'destroy directory',
|
45 |
+
'nullify file',
|
46 |
+
'nullify folder',
|
47 |
+
'nullify directory',
|
48 |
+
'void file',
|
49 |
+
'void folder',
|
50 |
+
'void directory',
|
51 |
+
'change file',
|
52 |
+
'change folder',
|
53 |
+
'change directory',
|
54 |
+
'shift file',
|
55 |
+
'shift folder',
|
56 |
+
'shift directory',
|
57 |
+
# Standalone commands
|
58 |
+
'remove',
|
59 |
+
'delete',
|
60 |
+
'rename',
|
61 |
+
'move',
|
62 |
+
'replace',
|
63 |
+
'kill',
|
64 |
+
'destroy',
|
65 |
+
'nullify',
|
66 |
+
'void',
|
67 |
+
'change',
|
68 |
+
'shift',
|
69 |
+
# Blacklisted files
|
70 |
+
'bardcode_interpreter.py',
|
71 |
+
'bardcoder.py',
|
72 |
+
'bardcoder_lib.py',
|
73 |
+
'bardcoder',
|
74 |
+
'bardcoder_lib',
|
75 |
+
'bardcode_interpreter',
|
76 |
+
'blacklist_commands.py',
|
77 |
+
'blacklist_commands',
|
78 |
+
'requirements.txt',
|
79 |
+
'clear_cache.sh',
|
80 |
+
'code_runner.sh'
|
81 |
+
]
|
82 |
+
|
83 |
+
harmful_commands_python = [
|
84 |
+
# Existing commands
|
85 |
+
'cut',
|
86 |
+
'dig',
|
87 |
+
'kedit',
|
88 |
+
'ftp',
|
89 |
+
'iwconfig',
|
90 |
+
'pkill',
|
91 |
+
'whois',
|
92 |
+
'scp',
|
93 |
+
'chgrp',
|
94 |
+
'nc',
|
95 |
+
'traceroute',
|
96 |
+
'pgrep',
|
97 |
+
'mv',
|
98 |
+
'move',
|
99 |
+
'replace',
|
100 |
+
'chdir',
|
101 |
+
'rename',
|
102 |
+
'kate',
|
103 |
+
'arp',
|
104 |
+
'route',
|
105 |
+
'host',
|
106 |
+
'curl',
|
107 |
+
'ncat.openbsd',
|
108 |
+
'nmap',
|
109 |
+
'ncat.traditional',
|
110 |
+
'htop',
|
111 |
+
'ls',
|
112 |
+
'netstat',
|
113 |
+
'ping',
|
114 |
+
'sudo',
|
115 |
+
'cd',
|
116 |
+
'mousepad',
|
117 |
+
'wireshark',
|
118 |
+
'wget',
|
119 |
+
'chown',
|
120 |
+
'ps',
|
121 |
+
'tcpdump',
|
122 |
+
'grep',
|
123 |
+
'netcat',
|
124 |
+
'nc.openbsd',
|
125 |
+
'mkdir',
|
126 |
+
'cp',
|
127 |
+
'mac',
|
128 |
+
'nslookup',
|
129 |
+
'sftp',
|
130 |
+
'top',
|
131 |
+
'format',
|
132 |
+
'ifconfig',
|
133 |
+
'nc.traditional',
|
134 |
+
'ip',
|
135 |
+
'nano',
|
136 |
+
'ssh',
|
137 |
+
'chmod',
|
138 |
+
'vim',
|
139 |
+
'kill',
|
140 |
+
'rm',
|
141 |
+
'ss',
|
142 |
+
'restart',
|
143 |
+
'telnet',
|
144 |
+
'kwrite',
|
145 |
+
'cat',
|
146 |
+
'ncat',
|
147 |
+
'rsync',
|
148 |
+
'delete',
|
149 |
+
'deleted',
|
150 |
+
'remove',
|
151 |
+
'shutdown',
|
152 |
+
'reboot',
|
153 |
+
'append',
|
154 |
+
'appended',
|
155 |
+
'remove',
|
156 |
+
'removed',
|
157 |
+
'rmdir',
|
158 |
+
'rmtree',
|
159 |
+
'shutil.rmtree',
|
160 |
+
'subprocess.call',
|
161 |
+
'eval',
|
162 |
+
'exec',
|
163 |
+
'unlink',
|
164 |
+
'pathlib.unlink',
|
165 |
+
'_exit',
|
166 |
+
'abort',
|
167 |
+
'kill',
|
168 |
+
'fork',
|
169 |
+
'execl',
|
170 |
+
'execle',
|
171 |
+
'execlp',
|
172 |
+
'execlpe',
|
173 |
+
'execv',
|
174 |
+
'execve',
|
175 |
+
'execvp',
|
176 |
+
'execvpe',
|
177 |
+
'popen',
|
178 |
+
'popen2',
|
179 |
+
'popen3',
|
180 |
+
'popen4',
|
181 |
+
'startfile',
|
182 |
+
'spawnl',
|
183 |
+
'spawnle',
|
184 |
+
'spawnlp',
|
185 |
+
'spawnlpe',
|
186 |
+
'spawnv',
|
187 |
+
'spawnve',
|
188 |
+
'spawnvp',
|
189 |
+
'spawnvpe',
|
190 |
+
'write(\"\")',
|
191 |
+
'write(\'\')',
|
192 |
+
'write(NULL)',
|
193 |
+
'write(0)',
|
194 |
+
'os.remove',
|
195 |
+
'os.rmdir',
|
196 |
+
'os.removedirs',
|
197 |
+
'os.unlink',
|
198 |
+
'os.rename',
|
199 |
+
'os.renames',
|
200 |
+
'os.system',
|
201 |
+
'os.chdir',
|
202 |
+
'os.mkdir',
|
203 |
+
'os.makedirs',
|
204 |
+
# Blacklisted files
|
205 |
+
'bardcode_interpreter.py',
|
206 |
+
'bardcoder.py',
|
207 |
+
'bardcoder_lib.py',
|
208 |
+
'bardcoder',
|
209 |
+
'bardcoder_lib',
|
210 |
+
'bardcode_interpreter',
|
211 |
+
'blacklist_commands.py',
|
212 |
+
'blacklist_commands',
|
213 |
+
'requirements.txt',
|
214 |
+
'clear_cache.sh',
|
215 |
+
'code_runner.sh'
|
216 |
+
'dd', # Additional shell commands # Can be used to write and backup raw disk images
|
217 |
+
'gksudo', # Graphical sudo
|
218 |
+
'lshw', # Can reveal hardware details
|
219 |
+
'lspci', # Can reveal PCI devices
|
220 |
+
'lsusb', # Can reveal USB devices
|
221 |
+
'dmidecode', # Can reveal system details
|
222 |
+
'hdparm', # Can change hard drive settings
|
223 |
+
'iptables', # Can manipulate network traffic rules
|
224 |
+
'passwd', # Can change user passwords
|
225 |
+
'useradd',
|
226 |
+
'userdel',
|
227 |
+
'adduser',
|
228 |
+
'deluser', # Can manipulate user accounts
|
229 |
+
'gpasswd', # Can change group passwords
|
230 |
+
'groupadd',
|
231 |
+
'groupdel',
|
232 |
+
'addgroup',
|
233 |
+
'delgroup', # Can manipulate group accounts
|
234 |
+
'crontab', # Can schedule tasks
|
235 |
+
'mount',
|
236 |
+
'umount', # Can mount and unmount file systems
|
237 |
+
'nohup', # Can keep processes running after their parent process ends
|
238 |
+
'reboot',
|
239 |
+
'halt',
|
240 |
+
'poweroff', # Can shutdown or reboot the system
|
241 |
+
'su',
|
242 |
+
'sudo', # Can run commands as other users
|
243 |
+
'chsh', # Can change the default shell
|
244 |
+
'chfn', # Can change the user's full name
|
245 |
+
'chage', # Can change password expiry
|
246 |
+
'chpasswd', # Can change passwords
|
247 |
+
'chroot', # Can change the root directory
|
248 |
+
'chrt', # Can change process scheduling
|
249 |
+
'crontab', # Can schedule tasks
|
250 |
+
'date', # Can change the system date and time
|
251 |
+
'dd', # Can write and backup raw disk images
|
252 |
+
'dmesg', # Can show kernel messages
|
253 |
+
'fdisk', # Can manipulate disk partitions
|
254 |
+
'fsck', # Can check and repair file systems
|
255 |
+
'fuser', # Can show which processes are using a file
|
256 |
+
'hdparm', # Can change hard drive settings
|
257 |
+
'kill', # Can send signals to processes
|
258 |
+
'killall', # Can send signals to processes by name
|
259 |
+
'last', # Can show recent logins
|
260 |
+
'lastlog', # Can show recent logins
|
261 |
+
'lsof', # Can show which files are open by which processes
|
262 |
+
'lsusb', # Can show USB devices
|
263 |
+
'lspci', # Can show PCI devices
|
264 |
+
'lsmod', # Can show kernel modules
|
265 |
+
'modinfo', # Can show kernel module details
|
266 |
+
'modprobe', # Can load and unload kernel modules
|
267 |
+
'mount', # Can mount file systems
|
268 |
+
'netstat', # Can show network statistics
|
269 |
+
'nice', # Can change process priority
|
270 |
+
'pstree', # Can show running processes as a tree
|
271 |
+
'pickle', # Can execute arbitrary code during unpickling
|
272 |
+
'os.popen', # Can be used to execute shell commands
|
273 |
+
'os.startfile', # Can be used to start a file with its associated application
|
274 |
+
'os.exec*', # Any function starting with 'os.exec' can be used to execute different programs
|
275 |
+
'os.spawn*', # Any function starting with 'os.spawn' can be used to spawn new process using os-level commands
|
276 |
+
'os.fork', # Can be used to create new processes
|
277 |
+
'os.kill', # Can be used to send signals to processes
|
278 |
+
'socket', # Can be used to create network connections
|
279 |
+
'ctypes', # Can be used to call C functions in Python programs
|
280 |
+
'gc.get_objects', # Can be used to inspect objects currently in memory
|
281 |
+
'os.setuid', # Can be used to set the current process's user id
|
282 |
+
'os.setgid', # Can be used to set the current process's group id
|
283 |
+
'os.chroot', # Can be used to change the root directory of the current process
|
284 |
+
'os.chmod', # Can be used to change file permissions
|
285 |
+
'os.chown', # Can be used to change file ownership
|
286 |
+
'os.chflags', # Can be used to change file flags
|
287 |
+
'os.chroot', # Can be used to change the root directory of the current process
|
288 |
+
'os.seteuid', # Can be used to set the current process's effective user id
|
289 |
+
'os.setegid', # Can be used to set the current process's effective group id
|
290 |
+
'os.setreuid', # Can be used to set the current process's real and effective user id
|
291 |
+
'os.setregid', # Can be used to set the current process's real and effective group id
|
292 |
+
'os.setresuid', # Can be used to set the current process's real, effective, and saved user id
|
293 |
+
]
|
294 |
+
|
295 |
+
harmful_commands_cpp = [
|
296 |
+
"remove", # Existing commands
|
297 |
+
"std::remove",
|
298 |
+
"remove_all",
|
299 |
+
"ilesystem::remove_all",
|
300 |
+
"filesystem::remove",
|
301 |
+
"rename",
|
302 |
+
"std::rename",
|
303 |
+
"filesystem::rename",
|
304 |
+
"std::system",
|
305 |
+
"abort",
|
306 |
+
"std::abort",
|
307 |
+
"exit",
|
308 |
+
"std::exit",
|
309 |
+
"move",
|
310 |
+
"std::move",
|
311 |
+
"filesystem::move",
|
312 |
+
"std::ofstream",
|
313 |
+
"std::quick_exit",
|
314 |
+
"ios::app",
|
315 |
+
"trunc",
|
316 |
+
"ios::trunc",
|
317 |
+
"std::ios::app",
|
318 |
+
"std::ios::trunc",
|
319 |
+
"std::filesystem::remove",
|
320 |
+
"std::_Exit",
|
321 |
+
"fp << NULL",
|
322 |
+
'fp << "" <<',
|
323 |
+
"file << NULL",
|
324 |
+
'file << "" <<',
|
325 |
+
"std::system_clock::now().time_since_epoch().count()",
|
326 |
+
"std::chrono::system_clock::now().time_since_epoch().count()",
|
327 |
+
"std::system(\"rm -rf",
|
328 |
+
"std::system(\"format",
|
329 |
+
"std::system(\"curl",
|
330 |
+
"system", # Additional commands # Executes an shell command
|
331 |
+
"popen", # Can open a process by creating apipe, forking, and invoking the shell
|
332 |
+
"_popen", # Windows version of popen
|
333 |
+
"WinExec", # Can run a command
|
334 |
+
"ShellExecute", # Can run a command
|
335 |
+
"fork", # Can be used to create new processes
|
336 |
+
"execl",
|
337 |
+
"execlp",
|
338 |
+
"execle",
|
339 |
+
"execv",
|
340 |
+
"execvp",
|
341 |
+
"execvpe", # Can be used to execute different programs
|
342 |
+
"spawnl",
|
343 |
+
"spawnlp",
|
344 |
+
"spawnle",
|
345 |
+
"spawnv",
|
346 |
+
"spawnvp",
|
347 |
+
"spawnvpe", # Can be used to spawn new process using os-level commands
|
348 |
+
"kill", # Can be used to send signals to processes
|
349 |
+
"raise", # Can be used to send a signal to the current process
|
350 |
+
"socket", # Can be used to create network connections
|
351 |
+
"setuid", # Can be used to set the current process's user id
|
352 |
+
"setgid", # Can be used to set the current process's group id
|
353 |
+
"chroot", # Can be used to change the root directory of the current process
|
354 |
+
]
|
355 |
+
|
356 |
+
|
357 |
+
# define method to get the list of harmful prompts
|
358 |
+
def get_harmful_prompts():
|
359 |
+
return harmful_prompts
|
360 |
+
|
361 |
+
|
362 |
+
# define method to get the list of harmful commands for python
|
363 |
+
def get_harmful_commands_python():
|
364 |
+
return harmful_commands_python
|
365 |
+
|
366 |
+
|
367 |
+
# define method to get the list of harmful commands for cpp
|
368 |
+
def get_harmful_commands_cpp():
|
369 |
+
return harmful_commands_cpp
|
lib/extensions_map.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Define a dictionary to map language name to file extension
|
2 |
+
extension_map = {
|
3 |
+
".python": ".py",
|
4 |
+
".javascript": ".js",
|
5 |
+
".java": ".java",
|
6 |
+
".c": ".c",
|
7 |
+
".c++": ".cpp",
|
8 |
+
".c#": ".cs",
|
9 |
+
".php": ".php",
|
10 |
+
".ruby": ".rb",
|
11 |
+
".go": ".go",
|
12 |
+
".swift": ".swift",
|
13 |
+
".kotlin": ".kt",
|
14 |
+
".rust": ".rs",
|
15 |
+
".dart": ".dart",
|
16 |
+
".r": ".r",
|
17 |
+
".typescript": ".ts",
|
18 |
+
".scala": ".scala",
|
19 |
+
".perl": ".pl",
|
20 |
+
".haskell": ".hs",
|
21 |
+
".lua": ".lua",
|
22 |
+
".julia": ".jl",
|
23 |
+
".elixir": ".ex",
|
24 |
+
".clojure": ".clj",
|
25 |
+
".erlang": ".erl",
|
26 |
+
".ocaml": ".ml",
|
27 |
+
}
|
28 |
+
|
29 |
+
streamlit_code_langs = {
|
30 |
+
".py": "python",
|
31 |
+
".js": "javascript",
|
32 |
+
".html": "html",
|
33 |
+
".css": "css",
|
34 |
+
".java": "java",
|
35 |
+
".cpp": "c++",
|
36 |
+
".cs": "csharp",
|
37 |
+
".r": "r",
|
38 |
+
".sql": "sql"
|
39 |
+
}
|
40 |
+
|
41 |
+
def get_streamlit_code_lang(language):
|
42 |
+
# make language name small letters
|
43 |
+
language = language.lower()
|
44 |
+
# get file extension from ext_map
|
45 |
+
file_extension = streamlit_code_langs.get(language)
|
46 |
+
# return file extension
|
47 |
+
#print(f"get_file_extesion: Language: {language} File Extension: {file_extension}")
|
48 |
+
return file_extension
|
49 |
+
|
50 |
+
# Method to get file extension from language name
|
51 |
+
def get_file_extesion(language):
|
52 |
+
# make language name small letters
|
53 |
+
language = language.lower()
|
54 |
+
# get file extension from ext_map
|
55 |
+
file_extension = extension_map.get(language)
|
56 |
+
# return file extension
|
57 |
+
#print(f"get_file_extesion: Language: {language} File Extension: {file_extension}")
|
58 |
+
return file_extension
|
lib/sharegpt_api.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
Description: This file contains methods to post conversation data to ShareGPT API and get ShareGPT URL.
|
3 |
+
Link : https://github.com/domeccleston/sharegpt#rest-api
|
4 |
+
Author of ShareGPT : Dome Eccleston
|
5 |
+
"""
|
6 |
+
import json
|
7 |
+
import requests
|
8 |
+
|
9 |
+
url = "https://sharegpt.com/api/conversations"
|
10 |
+
|
11 |
+
# Method to post conversation data to ShareGPT API
|
12 |
+
def sharegpt_post_conversation(conversation_data):
|
13 |
+
headers = {"Content-Type": "application/json"}
|
14 |
+
response = requests.post(url, headers=headers, data=json.dumps(conversation_data))
|
15 |
+
response_data = response.json()
|
16 |
+
id = response_data["id"]
|
17 |
+
sharegpt_url = f"https://shareg.pt/{id}"
|
18 |
+
return sharegpt_url
|
19 |
+
|
20 |
+
# Method to get ShareGPT URL
|
21 |
+
def sharegpt_get_url(gpt_data="", human_data=""):
|
22 |
+
conversation_data = {
|
23 |
+
"items": [
|
24 |
+
{"from": "gpt", "value": gpt_data},
|
25 |
+
{"from": "human", "value": human_data},
|
26 |
+
]
|
27 |
+
}
|
28 |
+
sharegpt_url = sharegpt_post_conversation(conversation_data)
|
29 |
+
return sharegpt_url
|
requirements.txt
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
bardapi
|
3 |
+
pillow
|
4 |
+
matplotlib
|
5 |
+
pandas
|
6 |
+
plotly
|
7 |
+
requests
|
8 |
+
keras
|
9 |
+
scikit-learn
|
10 |
+
tabulate
|
11 |
+
librosa
|
12 |
+
scipy
|
resources/upload_data_ui.png
ADDED
uploads/.DS_Store
ADDED
Binary file (6.15 kB). View file
|
|