2026-03-11 21:45:46 +08:00
|
|
|
|
import json
|
|
|
|
|
|
import logging
|
|
|
|
|
|
import uuid
|
|
|
|
|
|
from google.oauth2 import service_account
|
|
|
|
|
|
from langchain_core.tools import tool
|
|
|
|
|
|
from google import genai
|
|
|
|
|
|
from google.genai.types import GenerateContentConfig, Modality
|
|
|
|
|
|
|
|
|
|
|
|
from minio import Minio
|
|
|
|
|
|
|
|
|
|
|
|
from src.core.config import settings
|
|
|
|
|
|
from src.server.utils.new_oss_client import oss_upload_image
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
# 初始化全局凭证和客户端
|
|
|
|
|
|
creds = service_account.Credentials.from_service_account_file(
|
|
|
|
|
|
settings.GOOGLE_GENAI_USE_VERTEXAI,
|
|
|
|
|
|
scopes=["https://www.googleapis.com/auth/cloud-platform"],
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
minio_client = Minio(settings.MINIO_URL, access_key=settings.MINIO_ACCESS, secret_key=settings.MINIO_SECRET, secure=settings.MINIO_SECURE)
|
|
|
|
|
|
client = genai.Client(
|
|
|
|
|
|
credentials=creds,
|
|
|
|
|
|
project=settings.GOOGLE_CLOUD_PROJECT,
|
|
|
|
|
|
location=settings.GOOGLE_CLOUD_LOCATION,
|
|
|
|
|
|
vertexai=True
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@tool
|
|
|
|
|
|
async def generate_furniture(prompt: str) -> str:
|
|
|
|
|
|
"""
|
|
|
|
|
|
使用 Gemini 图像生成模型根据详细的英文提示词生成家具设计草图。
|
|
|
|
|
|
"""
|
|
|
|
|
|
print(f"\n[系统日志] 正在调用 Nano Banana (Gemini Image Gen) ...")
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
response = client.models.generate_content(
|
|
|
|
|
|
model="gemini-2.5-flash-image",
|
|
|
|
|
|
contents=(f"Generate a professional furniture design sketch: {prompt}"),
|
|
|
|
|
|
config=GenerateContentConfig(
|
|
|
|
|
|
response_modalities=[Modality.TEXT, Modality.IMAGE],
|
|
|
|
|
|
),
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
image_bytes = None
|
|
|
|
|
|
for part in response.candidates[0].content.parts:
|
|
|
|
|
|
if part.inline_data:
|
|
|
|
|
|
image_bytes = part.inline_data.data
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
|
|
if not image_bytes:
|
|
|
|
|
|
return "未能生成图像数据。"
|
|
|
|
|
|
object_name = f"furniture/sketches/{uuid.uuid4()}.png"
|
|
|
|
|
|
bucket = "fida-test" # 替换为你的 bucket 名称
|
|
|
|
|
|
# 3. 调用你的上传函数
|
|
|
|
|
|
upload_res = oss_upload_image(
|
|
|
|
|
|
oss_client=minio_client,
|
|
|
|
|
|
bucket=bucket,
|
|
|
|
|
|
object_name=object_name,
|
|
|
|
|
|
image_bytes=image_bytes
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if upload_res:
|
|
|
|
|
|
# 4. 构造访问链接 (如果是私有 bucket,需使用 presigned_get_object)
|
|
|
|
|
|
# 这里简单示例为直接访问地址
|
|
|
|
|
|
image_url = f"{bucket}/{object_name}"
|
2026-03-12 14:31:14 +08:00
|
|
|
|
return image_url
|
2026-03-11 21:45:46 +08:00
|
|
|
|
else:
|
2026-03-12 14:31:14 +08:00
|
|
|
|
return "图片生成成功,但上传至存储服务器失败。"
|
2026-03-11 21:45:46 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.warning(e)
|
2026-03-12 14:31:14 +08:00
|
|
|
|
return "绘图流程异常"
|