kopia lustrzana https://github.com/hc-psy/blender-gpt
[fix] os and panel
rodzic
abbba42a5a
commit
3e82d05f37
12
gpt_gpt.py
12
gpt_gpt.py
|
@ -13,7 +13,11 @@ def post_process(final_txt):
|
|||
return final_txt
|
||||
|
||||
|
||||
def chatgpt(context):
|
||||
def chatgpt(context, api_key=''):
|
||||
|
||||
if not api_key:
|
||||
raise Exception("Please provide an OpenAI API key")
|
||||
|
||||
scene = context.scene
|
||||
|
||||
# sysprompt preparation
|
||||
|
@ -34,15 +38,15 @@ def chatgpt(context):
|
|||
messages.append({"role": "user", "content": formatted_message})
|
||||
|
||||
# send message to GPT
|
||||
client = OpenAI()
|
||||
response = client.ChatCompletion.create(
|
||||
client = OpenAI(api_key=api_key)
|
||||
response = client.chat.completions.create(
|
||||
model=scene.model,
|
||||
messages=messages,
|
||||
temperature=scene.creativity,
|
||||
)
|
||||
|
||||
try:
|
||||
final_txt = response['choices'][0]['message']['content']
|
||||
final_txt = response.choices[0].message.content
|
||||
return post_process(final_txt)
|
||||
except IndexError:
|
||||
return ''
|
||||
|
|
|
@ -89,10 +89,10 @@ class BLENDERGPT_OT_SEND_MSG(Operator):
|
|||
|
||||
# openai api key
|
||||
prf = context.preferences
|
||||
openai.api_key = prf.addons["blender-gpt"].preferences.openai_key
|
||||
prf_openai_api_key = prf.addons["blender-gpt"].preferences.openai_key
|
||||
lan = prf.addons["blender-gpt"].preferences.language
|
||||
|
||||
if not openai.api_key:
|
||||
if not prf_openai_api_key:
|
||||
self.report({'ERROR'}, UI['error_no_api_key'][lan])
|
||||
return {'CANCELLED'}
|
||||
|
||||
|
@ -106,7 +106,7 @@ class BLENDERGPT_OT_SEND_MSG(Operator):
|
|||
return {'CANCELLED'}
|
||||
|
||||
try:
|
||||
code_exe_blender = chatgpt(context)
|
||||
code_exe_blender = chatgpt(context, api_key=prf_openai_api_key)
|
||||
except Exception as e:
|
||||
self.report({'ERROR'}, f"Error: {e}")
|
||||
scene.on_finish = False
|
||||
|
|
17
gpt_pkg.py
17
gpt_pkg.py
|
@ -22,13 +22,10 @@ def python_exec():
|
|||
if isWindows():
|
||||
return os.path.join(sys.prefix, 'bin', 'python.exe')
|
||||
elif isMacOS():
|
||||
|
||||
try:
|
||||
# 2.92 and older
|
||||
path = bpy.app.binary_path_python
|
||||
except AttributeError:
|
||||
# 2.93 and later
|
||||
import sys
|
||||
path = sys.executable
|
||||
return os.path.abspath(path)
|
||||
elif isLinux():
|
||||
|
@ -39,17 +36,19 @@ def python_exec():
|
|||
|
||||
|
||||
def installModule(packageName):
|
||||
python_exe = python_exec()
|
||||
|
||||
try:
|
||||
subprocess.call([python_exe, "import ", packageName])
|
||||
except:
|
||||
python_exe = python_exec()
|
||||
# upgrade pip
|
||||
subprocess.check_call([python_exe, "-c", "import " + packageName])
|
||||
print(f"{packageName} already installed")
|
||||
except subprocess.CalledProcessError:
|
||||
# upgrade pip
|
||||
subprocess.call([python_exe, "-m", "ensurepip"])
|
||||
subprocess.call(
|
||||
[python_exe, "-m", "pip", "install", "--upgrade", "pip"])
|
||||
# install required packages
|
||||
subprocess.call([python_exe, "-m", "pip", "install", packageName])
|
||||
# install required packages and upgrade
|
||||
subprocess.call([python_exe, "-m", "pip", "install",
|
||||
"--upgrade", packageName])
|
||||
|
||||
|
||||
installModule('openai')
|
||||
|
|
10
gpt_pnl.py
10
gpt_pnl.py
|
@ -20,7 +20,7 @@ class BLENDERGPT_PT_PANEL(Panel):
|
|||
column = layout.column(align=True)
|
||||
|
||||
# language youre using
|
||||
column.label(text=UI['label_language'][lan])
|
||||
column.label(text=UI['language'][lan])
|
||||
|
||||
column.separator()
|
||||
|
||||
|
@ -92,10 +92,10 @@ def model_props_generator():
|
|||
name=UI['label_model'][lan],
|
||||
description=UI['label_model_description'][lan],
|
||||
items=[
|
||||
("gpt-3.5-turbo", UI['model_options']['gpt3.5']
|
||||
[lan], UI['model_options']['gpt3.5'][lan]),
|
||||
("gpt-4", UI['model_options']['gpt4']
|
||||
[lan], UI['model_options']['gpt4'][lan]),
|
||||
("gpt-3.5-turbo", UI['model_options'][lan]
|
||||
['gpt3.5'], UI['model_options'][lan]['gpt3.5']),
|
||||
("gpt-4", UI['model_options'][lan]['gpt4'],
|
||||
UI['model_options'][lan]['gpt4']),
|
||||
],
|
||||
default="gpt-3.5-turbo",
|
||||
)
|
||||
|
|
32
gpt_prf.py
32
gpt_prf.py
|
@ -3,6 +3,7 @@ from bpy import props
|
|||
from bpy.types import AddonPreferences
|
||||
from .gpt_cst import UI
|
||||
|
||||
|
||||
class BLENDERGPT_AddonPreferences(AddonPreferences):
|
||||
bl_idname = "blender-gpt"
|
||||
|
||||
|
@ -24,49 +25,49 @@ class BLENDERGPT_AddonPreferences(AddonPreferences):
|
|||
name="Language",
|
||||
items=languages,
|
||||
default='en',
|
||||
description="Select your preferred language"
|
||||
update=update_language,
|
||||
description="Select your preferred language",
|
||||
update=lambda self, context: self.update_language(context)
|
||||
)
|
||||
|
||||
def draw(self, context):
|
||||
layout = self.layout
|
||||
layout.prop(self, "openai_key")
|
||||
layout.prop(self, "language", text="Language")
|
||||
|
||||
|
||||
def update_language(self, context):
|
||||
prefs = context.preferences.addons['blender-gpt'].preferences
|
||||
lan = prefs.language
|
||||
|
||||
# model
|
||||
current_model = getattr(context.scene, "model", "gpt-3.5-turbo")
|
||||
|
||||
|
||||
bpy.types.Scene.model = bpy.props.EnumProperty(
|
||||
name=UI['label_model'][lan],
|
||||
description=UI['label_model_description'][lan],
|
||||
items=[
|
||||
("gpt-3.5-turbo", UI['model_options']['gpt3.5']
|
||||
[lan], UI['model_options']['gpt3.5'][lan]),
|
||||
("gpt-4", UI['model_options']['gpt4']
|
||||
[lan], UI['model_options']['gpt4'][lan]),
|
||||
("gpt-3.5-turbo", UI['model_options'][lan]
|
||||
['gpt3.5'], UI['model_options'][lan]['gpt3.5']),
|
||||
("gpt-4", UI['model_options'][lan]['gpt4'],
|
||||
UI['model_options'][lan]['gpt4']),
|
||||
],
|
||||
default=current_model,
|
||||
)
|
||||
setattr(context.scene, "model", current_model)
|
||||
|
||||
|
||||
# prompt_input
|
||||
current_prompt_input = getattr(context.scene, "prompt_input", "")
|
||||
|
||||
|
||||
bpy.types.Scene.prompt_input = bpy.props.StringProperty(
|
||||
name=UI['command'][lan],
|
||||
description=UI['command_instruction'][lan],
|
||||
default=current_prompt_input,
|
||||
)
|
||||
|
||||
|
||||
setattr(context.scene, "prompt_input", current_prompt_input)
|
||||
|
||||
|
||||
# creativity
|
||||
current_creativity = getattr(context.scene, "creativity", 0)
|
||||
|
||||
|
||||
bpy.types.Scene.creativity = bpy.props.FloatProperty(
|
||||
name=UI['creativity'][lan],
|
||||
description=UI['creativity'][lan],
|
||||
|
@ -74,8 +75,5 @@ class BLENDERGPT_AddonPreferences(AddonPreferences):
|
|||
min=0,
|
||||
max=1,
|
||||
)
|
||||
|
||||
|
||||
setattr(context.scene, "creativity", current_creativity)
|
||||
|
||||
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue