增加get_ai_settings_from_jingrow函数,midjourney增加支持使用自定义api出图
This commit is contained in:
parent
48cd1bb7da
commit
e78c6cf9dc
@ -21,10 +21,11 @@ import sys
|
||||
import os
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
from config import Config
|
||||
from utils.auth import get_jingrow_cloud_api_headers, get_jingrow_cloud_api_url
|
||||
from utils.auth import get_jingrow_cloud_api_headers, get_jingrow_cloud_api_url, get_session_api_headers
|
||||
from utils.jingrow_api import get_ai_settings_from_jingrow
|
||||
|
||||
# 定义配置变量(jflow适配版本)
|
||||
api_url = "https://api.midjourney.com" # 默认值,可以从Config中获取
|
||||
# 默认配置变量
|
||||
api_url = "https://api.midjourney.com"
|
||||
application_id = ""
|
||||
data_id = ""
|
||||
data_version = ""
|
||||
@ -449,8 +450,54 @@ def process_midjourney_node_task(prompt: str, config: Dict = None) -> Dict:
|
||||
return {"success": False, "message": "提示词不能为空"}
|
||||
|
||||
config = config or {}
|
||||
# 从全局配置开始,然后用传入的配置覆盖
|
||||
# 重新获取AI配置(确保使用最新配置)
|
||||
current_ai_config = get_ai_settings_from_jingrow()
|
||||
print(f"[DEBUG] current_ai_config: {current_ai_config}")
|
||||
print(f"[DEBUG] current_ai_config type: {type(current_ai_config)}")
|
||||
if current_ai_config is None:
|
||||
return {"success": False, "message": "获取AI Settings配置失败,请检查网络连接和登录状态"}
|
||||
|
||||
print(f"[DEBUG] 配置获取成功,包含字段: {list(current_ai_config.keys())}")
|
||||
print(f"[DEBUG] 开始更新全局配置变量...")
|
||||
jingrow_midjourney = current_ai_config.get('jingrow_midjourney', True)
|
||||
print(f"[DEBUG] jingrow_midjourney: {jingrow_midjourney}")
|
||||
|
||||
# 更新全局配置变量
|
||||
global api_url, application_id, data_id, data_version, session_id, midjourney_suffix, add_title_to_image_name, http_proxy, https_proxy, midjourney_options
|
||||
print(f"[DEBUG] 更新api_url...")
|
||||
api_url = current_ai_config.get('midjourney_api_url') or "https://api.midjourney.com"
|
||||
print(f"[DEBUG] 更新application_id...")
|
||||
application_id = current_ai_config.get('midjourney_application_id') or ""
|
||||
print(f"[DEBUG] 更新data_id...")
|
||||
data_id = current_ai_config.get('midjourney_data_id') or ""
|
||||
print(f"[DEBUG] 更新data_version...")
|
||||
data_version = current_ai_config.get('midjourney_data_version') or ""
|
||||
print(f"[DEBUG] 更新session_id...")
|
||||
session_id = current_ai_config.get('midjourney_session_id') or ""
|
||||
print(f"[DEBUG] 更新midjourney_suffix...")
|
||||
midjourney_suffix = current_ai_config.get('midjourney_suffix') or "mj"
|
||||
print(f"[DEBUG] 更新add_title_to_image_name...")
|
||||
add_title_to_image_name = current_ai_config.get('add_title_to_image_name') or 0
|
||||
print(f"[DEBUG] 更新http_proxy...")
|
||||
http_proxy = current_ai_config.get('http_proxy') or ""
|
||||
print(f"[DEBUG] 更新https_proxy...")
|
||||
https_proxy = current_ai_config.get('https_proxy') or ""
|
||||
|
||||
# 获取Midjourney选项配置
|
||||
print(f"[DEBUG] 更新midjourney_options...")
|
||||
midjourney_options_str = current_ai_config.get('midjourney_options')
|
||||
if midjourney_options_str:
|
||||
try:
|
||||
midjourney_options = json.loads(midjourney_options_str)
|
||||
except:
|
||||
midjourney_options = {"ar": "1:1", "v": "6.1"}
|
||||
else:
|
||||
midjourney_options = {"ar": "1:1", "v": "6.1"}
|
||||
print(f"[DEBUG] 全局配置更新完成")
|
||||
|
||||
# 从全局配置获取midjourney_options
|
||||
options = midjourney_options.copy()
|
||||
|
||||
if "options" in config:
|
||||
options.update(config.get("options", {}))
|
||||
|
||||
@ -460,9 +507,6 @@ def process_midjourney_node_task(prompt: str, config: Dict = None) -> Dict:
|
||||
if "seed" not in options or options['seed'] == "":
|
||||
options['seed'] = random.randint(0, 4294967295)
|
||||
|
||||
# 使用JAPI(jflow适配版本)
|
||||
jingrow_midjourney = True # jflow默认使用JAPI
|
||||
|
||||
# 根据配置选择使用JAPI还是Discord API
|
||||
if jingrow_midjourney:
|
||||
# 使用JAPI
|
||||
@ -498,8 +542,89 @@ def process_midjourney_node_task(prompt: str, config: Dict = None) -> Dict:
|
||||
return {"success": True, "data": images_data}
|
||||
|
||||
else:
|
||||
# jflow版本只支持JAPI,不支持Discord API
|
||||
return {"success": False, "message": "jflow版本只支持JAPI模式"}
|
||||
# 使用Discord API
|
||||
discord_channel_id = current_ai_config.get('discord_channel_id')
|
||||
discord_user_token = current_ai_config.get('discord_user_token')
|
||||
|
||||
if not discord_channel_id or not discord_user_token:
|
||||
return {"success": False, "message": "Discord配置不完整,请在AI设置中配置discord_channel_id和discord_user_token"}
|
||||
|
||||
# 初始化Midjourney客户端
|
||||
midjourney = Midjourney(discord_channel_id, discord_user_token)
|
||||
|
||||
# 处理sources参数(参考图片)
|
||||
print(f"[DEBUG] 处理sources参数,config: {config}")
|
||||
sources = config.get("sources", {})
|
||||
print(f"[DEBUG] sources: {sources}")
|
||||
|
||||
# 处理负面提示词
|
||||
print(f"[DEBUG] 处理负面提示词,config: {config}")
|
||||
if config.get("negative_prompt"):
|
||||
prompt = f"{prompt} --no {config.get('negative_prompt')}"
|
||||
|
||||
# 生成图像并返回生成的消息ID
|
||||
print(f"[DEBUG] 调用midjourney.generate...")
|
||||
result = midjourney.generate(prompt, options, sources, None, None)
|
||||
print(f"[DEBUG] generate结果: {result}")
|
||||
print(f"[DEBUG] result类型: {type(result)}")
|
||||
|
||||
if not result:
|
||||
return {"success": False, "message": "Midjourney生成失败"}
|
||||
|
||||
imagine_message_id = result.get('imagine_message_id')
|
||||
raw_message = result.get('raw_message')
|
||||
seed = result.get('seed')
|
||||
print(f"[DEBUG] imagine_message_id: {imagine_message_id}")
|
||||
print(f"[DEBUG] raw_message: {raw_message}")
|
||||
print(f"[DEBUG] seed: {seed}")
|
||||
|
||||
# 确保生成的消息包含正确的seed
|
||||
if f"--seed {seed}" not in raw_message['content']:
|
||||
return {"success": False, "message": "Seed不匹配!该消息不属于当前任务。"}
|
||||
|
||||
# 设置代理用于下载Discord图片
|
||||
proxies = {}
|
||||
if http_proxy and http_proxy.strip():
|
||||
proxies['http'] = http_proxy
|
||||
if https_proxy and https_proxy.strip():
|
||||
proxies['https'] = https_proxy
|
||||
|
||||
# 从原始消息中获取合并图像URL
|
||||
original_image_url = None
|
||||
if 'attachments' in raw_message and len(raw_message['attachments']) > 0:
|
||||
original_image_url = raw_message['attachments'][0]['url']
|
||||
|
||||
if not original_image_url:
|
||||
return {"success": False, "message": "无法获取原始图像URL"}
|
||||
|
||||
# 下载原始合并图像
|
||||
try:
|
||||
if proxies:
|
||||
response = requests.get(original_image_url, proxies=proxies, timeout=30)
|
||||
else:
|
||||
response = requests.get(original_image_url, timeout=30)
|
||||
|
||||
if response.status_code != 200:
|
||||
raise Exception(f"下载原始图像失败: 状态码 {response.status_code}")
|
||||
|
||||
original_image_data = response.content
|
||||
except Exception as e:
|
||||
raise Exception(f"下载原始图像失败: {str(e)}")
|
||||
|
||||
# 分割原始图像为四个象限
|
||||
image_quadrants = split_image_into_four(original_image_data)
|
||||
if not image_quadrants:
|
||||
raise Exception("分割图像失败")
|
||||
|
||||
# 准备返回的图像数据
|
||||
images_data = []
|
||||
for i, quadrant_data in enumerate(image_quadrants):
|
||||
images_data.append({
|
||||
"image_data": quadrant_data,
|
||||
"image_url": None # Discord模式下没有URL
|
||||
})
|
||||
|
||||
return {"success": True, "data": images_data}
|
||||
|
||||
except Exception as e:
|
||||
return {"success": False, "message": f"处理Midjourney任务异常:{str(e)}"}
|
||||
@ -165,3 +165,37 @@ def get_field_value_from_jingrow(pagetype, name, fieldname):
|
||||
except Exception as e:
|
||||
log_error(f"获取字段值异常: {str(e)}")
|
||||
return None
|
||||
|
||||
def get_ai_settings_from_jingrow():
|
||||
|
||||
try:
|
||||
# 使用新的API端点
|
||||
api_url = f"{Config.JINGROW_SERVER_URL}/api/action/jingrow.ai.utils.jflow.get_ai_settings"
|
||||
headers = get_session_api_headers()
|
||||
|
||||
response = requests.post(api_url, headers=headers, timeout=10)
|
||||
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
|
||||
# 检查session是否过期
|
||||
if result.get('session_expired'):
|
||||
log_error("Session已过期,请重新登录")
|
||||
return None
|
||||
|
||||
# 处理Jingrow API的响应格式
|
||||
if 'message' in result:
|
||||
result = result['message']
|
||||
|
||||
if result.get('success'):
|
||||
return result.get('config', {})
|
||||
else:
|
||||
log_error(f"获取AI Settings失败: {result.get('error', '未知错误')}")
|
||||
return None
|
||||
else:
|
||||
log_error(f"获取AI Settings失败: HTTP {response.status_code}")
|
||||
return None
|
||||
|
||||
except Exception as e:
|
||||
log_error(f"获取AI Settings配置异常: {str(e)}")
|
||||
return None
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user