当前位置:   article > 正文

AI 绘画平台难开发,难变现?试试 Stable Diffusion API Serverless 版解决方案_ai 绘画平台难开发,难变现

ai 绘画平台难开发,难变现

Stable Diffusion 模型,已经成为 AI 行业从传统深度学习时代走向 AIGC 时代的标志性里程碑。越来越多的开发者借助 stable-diffusion-webui(以下简称 SDWebUI)能力进行 AI 绘画领域创业或者业务上新,获得高流量及商业价值,但是面对多客户、高并发的复杂场景,使用原生 Stable Diffusion API 会面临以下挑战:

1. 显卡资源昂贵且难以购买,GPU 卡池管理技术门槛高:高性能的 GPU 资源不仅价格昂贵,而且往往难以大规模采购。此外,GPU 卡池的有效管理和维护需要复杂的技术支持,也带来了额外的挑战。

2. 难以应对高并发:原生的 Stable Diffusion API 采用单实例推理模式,其并发处理能力有限。在面对高并发场景时,尤其是并发请求具有大的波动性时,资源配置难以精确预测,从而可能导致系统错误和业务中断。

3. 多模型切换难度大:当不同模型的请求在高并发条件下同时发送到同一实例时,频繁的模型切换成为一个显著的瓶颈。这种切换不仅消耗巨大,而且影响了推理效率,使得多模型部署在实际应用中变得复杂和低效。

为了帮助用户高效率、低成本应对企业级复杂场景,函数计算团队正式推出 Stable Diffusion API Serverless 版解决方案,通过使用该方案,用户可以充分利用 Stable Diffusion +Serverless 技术优势快速开发上线 AI 绘画应用,期待为广大开发者 AI 绘画创业及变现提供思路。

基于 Stable Diffusion API Serverless 版解决方案搭建的,部署 AI 艺术字应用活动火热进行中!生成姓氏头像赢新春好礼,欢迎点击 “阅读原文”体验。
活动链接: https://developer.aliyun.com/topic/fcnewyear

01 应用场景

Stable Diffusion API Serverless 版本提供了多种可能性,以适应不同的应用场景和业务需求。

1. 个性化内容创作:利用 Stable Diffusion API,内容创作者和数字艺术家可以快速生成个性化的艺术作品和图像。例如,艺术家可以根据用户的描述或关键词,即时创作出独特的艺术风格图像,实现个性化的艺术创作和用户互动。

2. 广告和市场营销:在广告和市场营销领域,Stable Diffusion API 可以用来生成吸引人的视觉内容,从而增强广告效果。例如,企业可以根据产品特性和目标受众的偏好,快速创建多样化的广告图像,以提升品牌形象和市场吸引力。

3. 游戏和娱乐产业:在游戏和娱乐产业中,Stable Diffusion API 可以被用来增强用户体验,通过生成独特的游戏背景、角色和元素来丰富游戏世界。例如,游戏开发者可以使用 API 来设计独特的游戏环境和角色,为玩家提供更丰富和个性化的游戏体验。

02 方案优势

Stable Diffusion API Serverless 版本在多方面提供了显著的优势,特别是在简化部署、成本效率、推理效率、资源管理、并发处理和用户体验上。以下是这些优势的具体体现:

1. 上手简单,快速部署:借助阿里云 Serverless 应用中心,用户可以实现快速部署,大幅简化传统 Stable Diffusion API 的复杂部署流程。这使得开发者能够快速上手并专注于应用的开发和创新。

2. 计费灵活,成本效益显著:Serverless 版本提供按需计费模式,用户仅需为实际使用的资源付费,无需预先投资昂贵的硬件。这种灵活的计费方式大幅降低了总体成本,尤其适合资源需求波动的场景。

3. 优化的模型管理,提升推理效率:通过优化多模型的管理和部署,Serverless 版本有效提高了推理效率。减少模型切换和加载的频率,确保了快速、稳定的推理性能。

4. 自动扩缩容,高效资源管理:利用自动扩缩容机制,Serverless 版本根据实时需求灵活调整资源使用,避免了资源浪费并保障了服务的连续性。

5. 异步处理和排队机制,优化并发处理:Serverless 版本通过引入异步处理和高效排队机制,克服了高并发场景下的挑战,保证了服务的高可用性和响应速度。

总之,Stable Diffusion API Serverless 版本集成了阿里云 Serverless 技术的核心优势,提供了一种高效、成本有效且用户友好的解决方案,为开发者在 AI 绘画和其他 AI 应用领域的创新和商业化提供支持。

方案架构图

名词解释:

  • admin,提供模型管理,包括模型上传+删除等
  • webui,提供界面化的调试功能,主要是模型和参数调整、插件安装等,达到更好出图效果
  • proxy,API 的前端服务,提供非推理之外的功能,主要包括结果、进度查询等
  • control,控制推理实例最大并发实例数。通过控制 control 的并发度,控制下游多函数推理服务的实例数
  • agent+sd-api,推理服务
    • 一个模型(checkpoints)创建一个函数,每个函数支持弹多个实例,总的实例数通过 control 并发度进行控制。
    • 之所以一个模型(checkpoints)创建一个函数,是为了避免多模型并发推理使用场景下的模型间的频繁切换带来的开销,提升推理效率,进而达到给用户降本的效果
    • sd_ 开头的都是动态创建函数

注意事项:

  • 使用过程中会动态创建 sdapi 函数,每个 checkpoint 对应一个函数,并且会在 ots 中 function 表中记录对应的函数详情。如果想删除动态创建的函数,请清理对应 ots 中 function 表的函数记录,避免后续调用出问题
  • 异步推理结果会默认存放到 oss 中,存放到 image/default 路径下

03 部署 Stable Diffusion API Serverless 版

准备工作

开通云产品:

  • 函数计算 FC:用于提供 CPU+GPU 算力
  • 对象存储 OSS:用于保存输出图片结果;同时存储请求中的中转图片,便面直接传递 base64 导致超出请求的 body 限制
  • 表格存储 Tablestore:用于存储推理结果、函数信息等
  • 文件存储 NAS:用于多节点共享存储空间

部署 Stable Diffusion Serverless API

1. 进入函数计算 FC 3.0 控制台,点击左侧“应用”进行应用中心。如果老用户曾经创建过应用,点击“创建应用”也可直达应用中心

2. 点击“人工智能”分类,选择“fc-stable-diffusion-v3”模版,点击“立即创建”

3. 确定详细参数进行应用创建,您可以重点注意三个信息的填写,其他使用默认值即可。

  • 地域:选择距离您较近的地区,如果后续有更多出图需要,可以考虑选择海外地区,以方便 hugging face 等网站的连接
  • 命名空间:如果您已经部署多个 SD,请在这里进行区分,新用户可使用默认值
  • 绘图类型:选择艺术字

4. 首次使用需要额外的权限,可以根据提示“前往授权”

5. 点击“创建应用”,勾选了解内容,点击“同意并继续部署”,等待大约 1 分钟

6. 生成 WebUI 域名(注意保护此链接不外传以免耗费您账户的费用),不要点击链接,直接切换到右侧“Serverless API”

7. 点击“Serverless API”点击“初始化 Serverless API”,再次确认已经开通“FC、OSS、OTS”三款产品,勾选“已阅读”点击“下一步”

8. 进行“角色名”的授权,勾选“启用 Serverless API”,OTS 实例创建方式可以默认“自动创建”,确认后等待大约 30 秒

9. 创建好 Serverless API,就可以参考下面 API 定义开始进行测试生产使用

04 Stable Diffusion API Serverless 版支持的 API 详情

API 接口主要分两类:

  • 非推理类接口,用于结果和进度查询、模型管理、应用重启等
  • 推理类接口,文生图、图生图、图片放大等

具体可以参考: https://github.com/devsapp/serverless-stable-diffusion-api/blob/main/api/api.yaml

1. 模型相关 API

模型的注册、更新和删除都通过 admin 界面化操作即可

1.1 获取模型列表 API

GET /modelsresponse:

  1. [
  2. {
  3. "type": "stableDiffusion",
  4. "name": "model_v1",
  5. "ossPath": "/path/to/oss/model_v1",
  6. "etag": "3f786850e387550fdab836ed7e6dc881de23001b",
  7. "status": "loaded", // registering|loading|loaded|unloaded|deleted
  8. "registeredTime": "2023-01-01T12:00:00Z",
  9. "lastModificationTime": "2023-01-10T12:00:00Z"
  10. }
  11. ]

2. 推理相关 API

支持文生图和图生图

支持同步模式和异步模式两种,默认同步模式

  • 同步模式:header 中添加 {"Request-Type":"sync"},不添加默认为同步模式
  • 异步模式:header 中添加 {"Request-Type":"async"}

推理结果会同步存储到 oss 和 ots 中,可以通过 /tasks/{taskId}/result 接口获取推理结果(oss 图片地址、推理参数等)

2.1 txt2img

POST /txt2img

request:其中 stable_diffusion_model, sd_vae 新加字段,其他保持跟原生 webui:txt2img 保持一致

其中 controlnet 中图片支持两种格式:

  • 图片的 base64 编码:备注:base64 请求存在超过 FC 异步请求 body 上限可能,如果超过上限请使用 oss 方式
  • oss 上图片路径,支持 png/jpg/jpeg
  1. {
  2. "stable_diffusion_model": "diffusion_v1", // sd主模型
  3. "sd_vae": "vae_v1", // vae模型
  4. "enable_hr": true,
  5. "denoising_strength": 0.5,
  6. "firstphase_width": 640,
  7. "firstphase_height": 480,
  8. "hr_scale": 2,
  9. "hr_upscaler": "upscale_method_v1",
  10. "hr_second_pass_steps": 10,
  11. "hr_resize_x": 1280,
  12. "hr_resize_y": 960,
  13. "hr_sampler_name": "sampler_v1",
  14. "hr_prompt": "High resolution prompt",
  15. "hr_negative_prompt": "Negative high resolution prompt",
  16. "prompt": "Mountain landscape during sunset",
  17. "styles": [
  18. "style1",
  19. "style2"
  20. ],
  21. "seed": 123456,
  22. "subseed": 789,
  23. "subseed_strength": 5,
  24. "seed_resize_from_h": 480,
  25. "seed_resize_from_w": 640,
  26. "sampler_name": "sampler_v2",
  27. "batch_size": 32,
  28. "n_iter": 1000,
  29. "steps": 100,
  30. "cfg_scale": 1,
  31. "width": 640,
  32. "height": 480,
  33. "restore_faces": true,
  34. "tiling": false,
  35. "do_not_save_samples": false,
  36. "do_not_save_grid": false,
  37. "negative_prompt": "Avoid mountains",
  38. "eta": 5,
  39. "s_min_uncond": 1,
  40. "s_churn": 3,
  41. "s_tmax": 10,
  42. "s_tmin": 1,
  43. "s_noise": 2,
  44. "override_settings": {
  45. "settingKey": "settingValue"
  46. },
  47. "override_settings_restore_afterwards": true,
  48. "script_args": [
  49. {
  50. "argKey": "argValue"
  51. }
  52. ],
  53. "sampler_index": "index_v1",
  54. "script_name": "script_v1",
  55. "send_images": true,
  56. "save_images": true,
  57. "alwayson_scripts": {
  58. "controlnet": {
  59. "args": [
  60. {
  61. "image":"base64srcimg|image/default/xxxx.png", //支持传输base64和oss对应图片path(png/jpg/jpeg)
  62. "enabled":True,
  63. "module":"canny",
  64. "model":"control_v11p_sd15_scribble",
  65. "weight":1,
  66. "resize_mode":"Crop and Resize",
  67. "low_vram":False,
  68. "processor_res":512,
  69. "threshold_a":100,
  70. "threshold_b":200,
  71. "guidance_start":0,
  72. "guidance_end":1,
  73. "pixel_perfect":True,
  74. "control_mode":"Balanced",
  75. "input_mode":"simple",
  76. "batch_images":"",
  77. "output_dir":"",
  78. "loopback":False
  79. }
  80. ]
  81. }
  82. }
  83. }

response:

  1. {
  2. "status":"succeeded", // 推理任务状态
  3. "taskId":"1HmyrbhBJD", // 推理任务id, 后续结果获取,进度查询,取消推理都依赖于该id
  4. "ossUrl" :["xxxxx"] // 同步模式下返回的oss上图片临时地址(有一定时效性)
  5. }

2.2 img2img

POST /img2img

request:其中 stable_diffusion_model,sd_vae 新加字段,其他保持跟原生 webui:img2img 保持一致

其中 controlnet 和 init_images 中图片支持两种格式:

  • 图片的 base64 编码:备注:base64 请求存在超过 FC 异步请求 body 上限可能,如果超过上限请使用 oss 方式
  • oss 上图片路径,支持 png/jpg/jpeg
  1. {
  2. "stable_diffusion_model": "diffusion_v2", // sd主模型
  3. "sd_vae": "vae_v2", // vae模型
  4. "init_images": [ //支持base64、oss图片地址两种格式
  5. "Base64SrcImg|ossPath",
  6. "Base64SrcImg|ossPath"
  7. ],
  8. "resize_mode": 1,
  9. "denoising_strength": 0.8,
  10. "image_cfg_scale": 2,
  11. "mask": "mask_path",
  12. "mask_blur": 3,
  13. "mask_blur_x": 2,
  14. "mask_blur_y": 2,
  15. "inpainting_fill": 4,
  16. "inpaint_full_res": true,
  17. "inpaint_full_res_padding": 2,
  18. "inpainting_mask_invert": 0,
  19. "initial_noise_multiplier": 5,
  20. "prompt": "Forest landscape",
  21. "styles": [
  22. "styleA",
  23. "styleB"
  24. ],
  25. "seed": 654321,
  26. "subseed": 987,
  27. "subseed_strength": 6,
  28. "seed_resize_from_h": 480,
  29. "seed_resize_from_w": 640,
  30. "sampler_name": "sampler_v3",
  31. "batch_size": 64,
  32. "n_iter": 500,
  33. "steps": 50,
  34. "cfg_scale": 2,
  35. "width": 1280,
  36. "height": 960,
  37. "restore_faces": false,
  38. "tiling": true,
  39. "do_not_save_samples": false,
  40. "do_not_save_grid": true,
  41. "negative_prompt": "Avoid forests",
  42. "eta": 6,
  43. "s_min_uncond": 2,
  44. "s_churn": 4,
  45. "s_tmax": 11,
  46. "s_tmin": 2,
  47. "s_noise": 3,
  48. "override_settings": {
  49. "settingKeyV2": "settingValueV2"
  50. },
  51. "override_settings_restore_afterwards": false,
  52. "script_args": [
  53. "arg1",
  54. "arg2"
  55. ],
  56. "sampler_index": "index_v2",
  57. "include_init_images": false,
  58. "script_name": "script_v2",
  59. "send_images": false,
  60. "save_images": true,
  61. "alwayson_scripts": {
  62. "controlnet": {
  63. "args": [
  64. {
  65. "image":"base64srcimg|ossPath", //支持base64、oss图片地址两种格式
  66. "enabled":True,
  67. "module":"canny",
  68. "model":"control_v11p_sd15_scribble",
  69. "weight":1,
  70. "resize_mode":"Crop and Resize",
  71. "low_vram":False,
  72. "processor_res":512,
  73. "threshold_a":100,
  74. "threshold_b":200,
  75. "guidance_start":0,
  76. "guidance_end":1,
  77. "pixel_perfect":True,
  78. "control_mode":"Balanced",
  79. "input_mode":"simple",
  80. "batch_images":"",
  81. "output_dir":"",
  82. "loopback":False
  83. }
  84. ]
  85. }
  86. }
  87. }

response:

  1. {
  2. "status":"succeeded", // 推理任务状态
  3. "taskId":"1HmyrbhBJD", // 推理任务id, 后续结果获取,进度查询,取消推理都依赖于该id
  4. "ossUrl" :["xxxxx"] // 同步模式下返回的oss上图片临时地址(有一定时效性)
  5. }

3. 图片处理 API

3.1 图片放大

图片放大,支持单张图片处理,暂不支持批量处理。respone 返回 taskId,调用获取结果接口获取图片地址即可

其中 image 支持两种格式:

  • 图片的 base64
  • oss 上图片路径,支持 png/jpg/jpeg

POST /extra_images

request:

  1. {
  2. "resize_mode": 0,
  3. "show_extras_results": True,
  4. "gfpgan_visibility": 0,
  5. "codeformer_visibility": 0,
  6. "codeformer_weight": 0,
  7. "upscaling_resize": 4,
  8. "upscaling_crop": True,
  9. "upscaler_1": "Lanczos",
  10. "upscaler_2": "None",
  11. "extras_upscaler_2_visibility": 0,
  12. "upscale_first": False,
  13. "image":self.file_to_base64(),
  14. "image" : "base64|ossPath" //支持传输base64和oss对应图片path(png/jpg/jpeg)
  15. }

response:

  1. {
  2. "status":"succeeded",
  3. "taskId":"TovRrc0Jnr",
  4. "ossUrl" :["xxxxx"] // 同步模式下返回的oss上图片临时地址(有一定时效性)
  5. }

4. 结果相关 API

4.1 获取结果

通过 taskid 获取推理结果

GET /tasks/{taskId}/result

response:

  1. {
  2. "images":[
  3. "images/admin/Xldf9m80im_1.png" // images 推理结果, oss图片path
  4. ],
  5. "ossUrl" :["xxxxx"], oss上图片临时地址(有一定时效性)
  6. "info":{ // info 推理过程中产生信息
  7. "all_negative_prompts":[
  8. ""
  9. ],
  10. "all_prompts":[
  11. "cute beautiful blonde, very detailed, 21 years old, inoccent face, natural wave hair, blue eyes, high-res, masterpiece, best quality,intricate details, highly detailed,sharp focus, detailed skin,realistic skin texture,texture, detailed eyes lora:shuimobysimV3:1\u003e"
  12. ],
  13. "all_seeds":[
  14. 3896184641
  15. ],
  16. "all_subseeds":[
  17. 6579621
  18. ],
  19. "alwayson_scripts":"",
  20. "batch_size":1,
  21. "cfg_scale":7,
  22. "clip_skip":1,
  23. "denoising_strength":0,
  24. "do_not_save_grid":false,
  25. "do_not_save_samples":false,
  26. "enable_hr":false,
  27. "eta":null,
  28. "extra_generation_params":{
  29. },
  30. "face_restoration_model":null,
  31. "firstphase_height":0,
  32. "firstphase_width":0,
  33. "height":512,
  34. "hr_negative_prompt":"",
  35. "hr_prompt":"",
  36. "hr_resize_x":0,
  37. "hr_resize_y":0,
  38. "hr_sampler_name":null,
  39. "hr_scale":2,
  40. "hr_second_pass_steps":0,
  41. "hr_upscaler":null,
  42. "index_of_first_image":0,
  43. "infotexts":[
  44. "cute beautiful blonde, very detailed, 21 years old, inoccent face, natural wave hair, blue eyes, high-res, masterpiece, best quality,intricate details, highly detailed,sharp focus, detailed skin,realistic skin texture,texture, detailed eyes lora:shuimobysimV3:1\u003e\nSteps: 50, Sampler: Euler, CFG scale: 7.0, Seed: 3896184641, Size: 512x512, Model hash: 18ed2b6c48, Model: xxmix9realistic_v40, Denoising strength: 0, Version: v1.5.1"
  45. ],
  46. "is_using_inpainting_conditioning":false,
  47. "job_timestamp":"20230828073155",
  48. "n_iter":1,
  49. "negative_prompt":"",
  50. "override_settings":{
  51. },
  52. "override_settings_restore_afterwards":true,
  53. "prompt":"cute beautiful blonde, very detailed, 21 years old, inoccent face, natural wave hair, blue eyes, high-res, masterpiece, best quality,intricate details, highly detailed,sharp focus, detailed skin,realistic skin texture,texture, detailed eyes lora:shuimobysimV3:1\u003e",
  54. "restore_faces":false,
  55. "s_churn":0,
  56. "s_min_uncond":0,
  57. "s_noise":1,
  58. "s_tmax":null,
  59. "s_tmin":0,
  60. "sampler_index":"Euler",
  61. "sampler_name":"Euler",
  62. "save_images":false,
  63. "script_args":[
  64. ],
  65. "script_name":null,
  66. "sd_model_hash":"18ed2b6c48",
  67. "seed":3896184641,
  68. "seed_resize_from_h":-1,
  69. "seed_resize_from_w":-1,
  70. "send_images":true,
  71. "steps":50,
  72. "styles":[
  73. ],
  74. "subseed":6579621,
  75. "subseed_strength":0,
  76. "tiling":false,
  77. "width":512
  78. },
  79. "parameters":{ // parameters实际推理过程中的参数
  80. "all_negative_prompts":[
  81. ""
  82. ],
  83. "all_prompts":[
  84. "cute beautiful blonde, very detailed, 21 years old, inoccent face, natural wave hair, blue eyes, high-res, masterpiece, best quality,intricate details, highly detailed,sharp focus, detailed skin,realistic skin texture,texture, detailed eyes lora:shuimobysimV3:1\u003e"
  85. ],
  86. "all_seeds":[
  87. 3896184641
  88. ],
  89. "all_subseeds":[
  90. 6579621
  91. ],
  92. "alwayson_scripts":"",
  93. "batch_size":1,
  94. "cfg_scale":7,
  95. "clip_skip":1,
  96. "denoising_strength":0,
  97. "do_not_save_grid":false,
  98. "do_not_save_samples":false,
  99. "enable_hr":false,
  100. "eta":null,
  101. "extra_generation_params":{
  102. },
  103. "face_restoration_model":null,
  104. "firstphase_height":0,
  105. "firstphase_width":0,
  106. "height":512,
  107. "hr_negative_prompt":"",
  108. "hr_prompt":"",
  109. "hr_resize_x":0,
  110. "hr_resize_y":0,
  111. "hr_sampler_name":null,
  112. "hr_scale":2,
  113. "hr_second_pass_steps":0,
  114. "hr_upscaler":null,
  115. "index_of_first_image":0,
  116. "infotexts":[
  117. "cute beautiful blonde, very detailed, 21 years old, inoccent face, natural wave hair, blue eyes, high-res, masterpiece, best quality,intricate details, highly detailed,sharp focus, detailed skin,realistic skin texture,texture, detailed eyes lora:shuimobysimV3:1\u003e\nSteps: 50, Sampler: Euler, CFG scale: 7.0, Seed: 3896184641, Size: 512x512, Model hash: 18ed2b6c48, Model: xxmix9realistic_v40, Denoising strength: 0, Version: v1.5.1"
  118. ],
  119. "is_using_inpainting_conditioning":false,
  120. "job_timestamp":"20230828073155",
  121. "n_iter":1,
  122. "negative_prompt":"",
  123. "override_settings":{
  124. },
  125. "override_settings_restore_afterwards":true,
  126. "prompt":"cute beautiful blonde, very detailed, 21 years old, inoccent face, natural wave hair, blue eyes, high-res, masterpiece, best quality,intricate details, highly detailed,sharp focus, detailed skin,realistic skin texture,texture, detailed eyes lora:shuimobysimV3:1\u003e",
  127. "restore_faces":false,
  128. "s_churn":0,
  129. "s_min_uncond":0,
  130. "s_noise":1,
  131. "s_tmax":null,
  132. "s_tmin":0,
  133. "sampler_index":"Euler",
  134. "sampler_name":"Euler",
  135. "save_images":false,
  136. "script_args":[
  137. ],
  138. "script_name":null,
  139. "sd_model_hash":"18ed2b6c48",
  140. "seed":3896184641,
  141. "seed_resize_from_h":-1,
  142. "seed_resize_from_w":-1,
  143. "send_images":true,
  144. "steps":50,
  145. "styles":[
  146. ],
  147. "subseed":6579621,
  148. "subseed_strength":0,
  149. "tiling":false,
  150. "width":512
  151. },
  152. "taskId":"Xldf9m80im"
  153. }

4.2 查询进度

推理进度查询

GET /tasks/{taskId}/progress,同 webui 中的 progress

response:

  1. {
  2. "currentImage":"",
  3. "etaRelative":0.10594336,
  4. "progress":0.99,
  5. "state":{
  6. "interrupted":false,
  7. "job":"scripts_txt2img",
  8. "job_count":1,
  9. "job_no":0,
  10. "job_timestamp":"20230828073155",
  11. "sampling_step":49,
  12. "sampling_steps":50,
  13. "skipped":false
  14. },
  15. "taskId":"Xldf9m80im"
  16. }

4.3 取消推理

取消对应任务

POST /tasks/{taskId}/cancellation

5. 动态资源相关 API

5.1 获取动态创建 sd 函数

获取动态创建的 sdapi 函数

GET /list/sdapi/fucntions

response:

  1. {
  2. "functions":[
  3. {
  4. "functionName":"sd_739f6de96fdbb66704296cd11ab3f96c182fde7f2cbbb127185b184a43414dea",
  5. "model":"chilloutmix_NiPrunedFp16Fix.safetensors"
  6. }
  7. ],
  8. "status":"success"
  9. }

5.2 更新动态创建 sd 资源

批量更新动态创建 sd 函数资源,比如镜像、环境变量、cpu、显存等。其中 models 不指定代表更新所有动态创建 sd 函数资源

POST /batch_update_sd_resource

request:

  1. {
  2. "models": ["chilloutmix_NiPrunedFp16Fix.safetensors"],
  3. "cpu": 4,
  4. "memorySize": 16384, // MB
  5. "image": "xxx",
  6. "extraArgs": "--api --nowebui --no-hashing",
  7. "instanceType": "fc.gpu.ampere.1",
  8. "gpuMemorySize": 16384, // MB
  9. "timeout": 60, //s
  10. "env": {},
  11. "vpcConfig":{
  12. "securityGroupId":"xxx",
  13. "vSwitchIds":[
  14. "xxx"
  15. ],
  16. "vpcId":"xxx"
  17. },
  18. "nasConfig": {
  19. "groupId" : 123,
  20. "mountPoints": [
  21. {
  22. "enableTLS": true|false,
  23. "mountDir": "xxx",
  24. "serverAddr": "dddd"
  25. }
  26. ],
  27. "userId": 123
  28. },
  29. "mountPoints":[
  30. {
  31. "bucketName": "xxx",
  32. "bucketPath": "xxx",
  33. "endpoint": "xxx",
  34. "mountDir": "ddd",
  35. "readOnly": true|false
  36. }
  37. ]
  38. }

response:

  1. {
  2. "status": "success|fail",
  3. "failFuncList": ["xxx"], // 失败的函数列表
  4. "errMsg": ["xxxx"] //错误信息
  5. }

5.3 批量删除动态创建函数接口

批量删除动态创建的函数,入参函数列表

POST /del/sd/functions

request:

  1. {
  2. "functions":["xxxxx"]
  3. }
  4. responese
  5. // status_code=200
  6. {
  7. "status":"success"
  8. }
  9. // status_code=500
  10. {
  11. "fails":[
  12. {
  13. "err":"xxxx",
  14. "functionName":"xxxx"
  15. }
  16. ],
  17. "status":"fail"
  18. }

6. 其他接口

  • 原生 webui-api 接口,除了上面支持的功能接口,剩下的 api 接口
  • 插件自定义的 api 接口

其中支持同步、异步模式+任务模式:

  • 同步/异步,header 中设置 Request-Type,其中值 sync 为同步模式,async 为异步模式,不设置该值默认为同步模式,异步获取结果需要从上面的获取结果接口(tasks/{taskId}/result)获取最终的结果
  • 任务模式,header 中设置 Task-Flag,会将结果保存到 ots 进行持久化,同样支持同步+异步

05 最佳实践

为了方便大家直观体验一下该解决方案成效,基于函数计算团队开发者的基于 Stable Diffusion Serverless API 解决方案搭建的 AI 文字生成应用,作为一个实验 demo 开放体验,期待为广大开发者 AI 绘画创业及变现提供一些有益思考。直接参加体验活动,送好礼!

活动链接: https://developer.aliyun.com/topic/fcnewyear

部署成功的 AI 绘画应用:

眯着眼睛看看是什么字?

开源代码 github :  https://github.com/devsapp/serverless-stable-diffusion-api

可以自己基于开源代码加工开发

作者:王佳、江昱、筱姜

原文链接

本文为阿里云原创内容,未经允许不得转载。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/777693
推荐阅读
相关标签
  

闽ICP备14008679号