-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathgpt.py
More file actions
118 lines (87 loc) · 4.44 KB
/
gpt.py
File metadata and controls
118 lines (87 loc) · 4.44 KB
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
import FreeCAD as App
import FreeCADGui as Gui
import Part
from FreeCAD import Base
from PySide2 import QtWidgets, QtGui, QtCore
from gpt4_integration import generate_chat_completion
def process_command(command, conversation_history):
messages = [{"role": "system", "content": "You are a FreeCAD scripter. You will output and execute the Python code for the shape the user inputs"}]
messages.extend(conversation_history)
messages.append({"role": "user", "content": command})
response_text = generate_chat_completion(messages, max_tokens=4000)
return response_text
def ensure_active_document():
if not App.ActiveDocument:
App.newDocument("Unnamed")
class GPTCommandDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(GPTCommandDialog, self).__init__(parent)
self.setWindowModality(QtCore.Qt.NonModal) # Set the dialog to be non-modal
self.init_ui()
self.conversation_history = []
def init_ui(self):
self.setWindowTitle("GPT4FreeCAD Input")
self.resize(600, 400)
self.setWindowFlags(self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint | QtCore.Qt.WindowMinimizeButtonHint) # Remove question mark and add minimize button
self.verticalLayout = QtWidgets.QVBoxLayout(self)
self.scroll_area = QtWidgets.QScrollArea()
self.scroll_area.setWidgetResizable(True)
self.verticalLayout.addWidget(self.scroll_area)
self.scroll_widget = QtWidgets.QWidget()
self.scroll_area.setWidget(self.scroll_widget)
self.scroll_layout = QtWidgets.QVBoxLayout(self.scroll_widget)
self.label = QtWidgets.QLabel("Describe your part:")
self.verticalLayout.addWidget(self.label)
self.command_input = QtWidgets.QLineEdit()
self.verticalLayout.addWidget(self.command_input)
self.execute_button = QtWidgets.QPushButton("Execute")
self.execute_button.clicked.connect(self.execute_command)
self.verticalLayout.addWidget(self.execute_button)
self.undo_button = QtWidgets.QPushButton("Undo")
self.undo_button.clicked.connect(self.undo_last_command)
self.verticalLayout.addWidget(self.undo_button)
def execute_command(self):
command = self.command_input.text()
if not command:
App.Console.PrintError("No command found.\n")
return
try:
response_text = process_command(command, self.conversation_history)
self.conversation_history.append({"role": "user", "content": command})
# Display user input in the scrollable area
user_label = QtWidgets.QLabel(f"Input: {command}")
user_label.setFont(QtGui.QFont("Arial", 9, QtGui.QFont.Bold))
self.scroll_layout.addWidget(user_label)
# Scroll to the bottom of the scrollable area
self.scroll_area.verticalScrollBar().setValue(self.scroll_area.verticalScrollBar().maximum())
self.command_input.clear()
ensure_active_document()
# Check if the response contains code
if "```python" in response_text and "\n```" in response_text:
# Split the response into description and code parts
_, code = response_text.split("```python\n", 1)
code, _ = code.split("\n```", 1)
# Print the code in the console
App.Console.PrintMessage(f"{code}\n")
# Execute the generated code in the Python environment
exec(code, {"App": App, "Part": Part, "Base": Base})
except Exception as e:
App.Console.PrintError(f"Error: {str(e)}\n")
def undo_last_command(self):
if App.ActiveDocument is not None:
if App.ActiveDocument.UndoCount > 0:
App.ActiveDocument.undo()
App.Console.PrintMessage(f"Undid the last command.\n")
else:
App.Console.PrintError(f"No actions to undo.\n")
def show_gpt_command_dialog():
dialog = GPTCommandDialog(Gui.getMainWindow())
dialog.show()
from PySide2.QtCore import QCoreApplication, QMetaObject, QTimer
def delayed_show_dialog():
dialog = GPTCommandDialog(Gui.getMainWindow())
dialog.show()
timer = QTimer()
timer.setSingleShot(True)
timer.timeout.connect(delayed_show_dialog)
timer.start(0)