-
Notifications
You must be signed in to change notification settings - Fork 8
/
functional.py
159 lines (141 loc) · 5.24 KB
/
functional.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import requests
import processing
import io
import contextlib
import platform
import traceback
import qgis.utils
import tempfile
import os
import subprocess
import sys
from qgis.PyQt.QtCore import QThreadPool
from PyQt5.QtCore import QThread, pyqtSignal
from qgis.utils import *
import sqlite3
# code containerization
def containerize_code(code_string):
code_string ="""from qgis.core import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import processing
from qgis.utils import *
import tempfile
""" +code_string +"""# refresh the canvas
iface.mapCanvas().refresh()"""
# From Engshell
try:
output_buffer = io.StringIO()
with contextlib.redirect_stdout(output_buffer):
exec(code_string, globals())
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
tb = traceback.extract_tb(exc_traceback)
filename, line, func, text = tb[-1]
error_msg = f"{exc_type.__name__}: {str(e)}"
return False, f'Error: {error_msg}. Getting the error from function: {func} (line: {line})'
code_printout = output_buffer.getvalue()
return True, code_printout
# Get completion from OpenAI API
def get_completion(prompt,api_key,temprature=0.0):
# Replace MODEL_ID with the ID of the OpenAI model you want to use
model_id = 'text-davinci-003'
max_tockens = 1000
# Define the parameters for the API request
data = {
'model': model_id,
'prompt': prompt,
"max_tokens": max_tockens,
"temperature": temprature,
}
# Define the headers for the API request
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {api_key}',
}
try:
# Send the API request and get the response
response = requests.post('https://api.openai.com/v1/completions', json=data, headers=headers)
#print(response)
if response.status_code==200:
# Parse the response to get the text completion
completion = response.json()['choices'][0]['text']
else:
completion ='code_error'
except:
completion='connection_error'
return completion
#Get completion thread
class RequestWorker(QThread):
# Define a custom signal to emit when the request is finished
finished_signal = pyqtSignal(str)
def __init__(self, prompt,api_key,temprature=0.0):
super().__init__()
self.prompt=prompt
self.api_key =api_key
self.temprature=temprature
def run(self):
completion =get_completion(self.prompt, self.api_key,self.temprature)
self.finished_signal.emit(completion)
# Run code in a thread
class CodeRunner(QThread):
finished_signal = pyqtSignal(str)
def __init__(self, code_string):
super().__init__()
self.code_string = code_string
def run(self):
success, code_printout = containerize_code(self.code_string)
# emit binary success and code printout
self.finished_signal.emit(str(success) + '|||' + code_printout)
#Sqlite 3 database class
class Database:
def __init__(self, db_path):
self.db_path = db_path
self.conn = None
self.cursor = None
self.connect()
def connect(self):
self.conn = sqlite3.connect(self.db_path)
self.cursor = self.conn.cursor()
def execute(self, query, params=None):
if params:
self.cursor.execute(query, params)
else:
self.cursor.execute(query)
self.conn.commit()
def fetchall(self):
return self.cursor.fetchall()
def fetchone(self):
return self.cursor.fetchone()
def close(self):
self.conn.close()
def setSettingsValue(self, key, value):
self.execute('INSERT OR REPLACE INTO settings VALUES ((SELECT id FROM settings WHERE key = ?), ?, ?)', (key, key, value))
self.conn.commit()
def getSettingsValue(self, key):
self.execute('SELECT value FROM settings WHERE key = ?', (key,))
return self.fetchone()[0]
def getHistory(self):
self.execute('SELECT * FROM history')
return self.fetchall()
def addHistory(self,command, code, datetime, success, printout):
#print(command, code, datetime, success, printout)
#auto increment id is the last id + 1
self.execute('INSERT INTO history VALUES ((SELECT id FROM history ORDER BY id DESC LIMIT 1)+1, ?, ?, ?, ?, ?)', (command, code, datetime, success, printout))
self.conn.commit()
def deleteHistory(self, id):
self.execute('DELETE FROM history WHERE id = ?', (id,))
self.conn.commit()
def deleteAllHistory(self):
self.execute('DELETE FROM history')
self.conn.commit()
def createTable(self, table_name, columns):
self.execute(f'CREATE TABLE {table_name} ({columns})')
def createAllTables(self):
#settings table id,key,value
self.createTable('settings', 'id INTEGER PRIMARY KEY, key TEXT, value TEXT')
#history table id,command,code,datetime,success,printout
self.createTable('history', 'id INTEGER PRIMARY KEY, command TEXT, code TEXT, datetime TEXT, success TEXT, printout TEXT')
#commit changes
self.conn.commit()