2024-11-21 19:38:09 +08:00
|
|
|
|
import json
|
|
|
|
|
import types
|
|
|
|
|
from tencentcloud.common import credential
|
|
|
|
|
from tencentcloud.common.profile.client_profile import ClientProfile
|
|
|
|
|
from tencentcloud.common.profile.http_profile import HttpProfile
|
2024-11-30 03:40:10 +08:00
|
|
|
|
from tencentcloud.common.exception.tencent_cloud_sdk_exception import (
|
|
|
|
|
TencentCloudSDKException,
|
|
|
|
|
)
|
2024-11-21 19:38:09 +08:00
|
|
|
|
from tencentcloud.hunyuan.v20230901 import hunyuan_client, models
|
|
|
|
|
from .config import config
|
2024-11-30 03:40:10 +08:00
|
|
|
|
|
|
|
|
|
|
2024-11-21 19:38:09 +08:00
|
|
|
|
def generate_image(prompt: str):
|
2024-11-30 03:40:10 +08:00
|
|
|
|
cred = credential.Credential(
|
|
|
|
|
config.marshoai_tencent_secretid, config.marshoai_tencent_secretkey
|
|
|
|
|
)
|
2024-11-21 19:38:09 +08:00
|
|
|
|
# 实例化一个http选项,可选的,没有特殊需求可以跳过
|
|
|
|
|
httpProfile = HttpProfile()
|
|
|
|
|
httpProfile.endpoint = "hunyuan.tencentcloudapi.com"
|
|
|
|
|
|
|
|
|
|
# 实例化一个client选项,可选的,没有特殊需求可以跳过
|
|
|
|
|
clientProfile = ClientProfile()
|
|
|
|
|
clientProfile.httpProfile = httpProfile
|
|
|
|
|
client = hunyuan_client.HunyuanClient(cred, "ap-guangzhou", clientProfile)
|
|
|
|
|
|
|
|
|
|
req = models.TextToImageLiteRequest()
|
2024-11-30 03:40:10 +08:00
|
|
|
|
params = {"Prompt": prompt, "RspImgType": "url", "Resolution": "1080:1920"}
|
2024-11-21 19:38:09 +08:00
|
|
|
|
req.from_json_string(json.dumps(params))
|
|
|
|
|
|
|
|
|
|
# 返回的resp是一个TextToImageLiteResponse的实例,与请求对象对应
|
|
|
|
|
resp = client.TextToImageLite(req)
|
|
|
|
|
# 输出json格式的字符串回包
|
|
|
|
|
return resp.to_json_string()
|