【通义实验室】开源【文本生成图片】大模型

文本生成图片效果

文本为一首古诗:孤帆远影碧空尽,唯见长江天际流。 不同风格生成的图片

模型地址

中文StableDiffusion-通用领域

初始化pipeline

python 复制代码
task = Tasks.text_to_image_synthesis
model_id = 'damo/multi-modal_chinese_stable_diffusion_v1.0'
pipe = pipeline(task=task, model=model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)

生成图片

python 复制代码
# 反向提示词
negative_prompt = (
        "blood, gore, violence, murder, kill, dead, corpse, "
        "horrible, frightening, scary, monster, ghost, skeleton, zombie, "
        "sex, nudity, pornography, adult, erotic, mature, "
        "drugs, alcohol, smoking, tobacco, illegal, "
        "dark, night, storm, thunder, lightning, apocalypse, disaster, "
        "gun, knife, sword, bomb, explosion, firearm, "
        "mean, angry, sadistic, hostile, aggressive, bullying, "
        "dangerous, unsafe, hazardous, poison, toxic, pollution"
    )
output = pipe(
        {
            'text': '孤帆远影碧空尽,唯见长江天际流。中国画',
            'num_inference_steps': 120,
            'guidance_scale': 11,
            'negative_prompt': negative_prompt
        }
    )
cv2.imwrite('result1.png', output['output_imgs'][0])
# 输出为opencv numpy格式,转为PIL.Image
img = output['output_imgs'][0]
img = Image.fromarray(img[:,:,::-1])
img.save('result1.png')

封装为http接口的完整代码

python 复制代码
from flask import Flask, request, send_file
import io
import torch
import cv2
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from PIL import Image

app = Flask(__name__)

# 初始化pipeline
task = Tasks.text_to_image_synthesis
model_id = 'damo/multi-modal_chinese_stable_diffusion_v1.0'
pipe = pipeline(task=task, model=model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)

@app.route('/generate', methods=['POST'])
def generate_image():
    data = request.json
    text = data.get('text', '')
    guidance_scale = data.get('guidance_scale', 9)

    if not text:
        return {'error': 'No text provided'}, 400

    negative_prompt = (
        "blood, gore, violence, murder, kill, dead, corpse, "
        "horrible, frightening, scary, monster, ghost, skeleton, zombie, "
        "sex, nudity, pornography, adult, erotic, mature, "
        "drugs, alcohol, smoking, tobacco, illegal, "
        "dark, night, storm, thunder, lightning, apocalypse, disaster, "
        "gun, knife, sword, bomb, explosion, firearm, "
        "mean, angry, sadistic, hostile, aggressive, bullying, "
        "dangerous, unsafe, hazardous, poison, toxic, pollution"
    )

    output = pipe(
        {
            'text': text,
            'num_inference_steps': 120,
            'guidance_scale': guidance_scale,
            'negative_prompt': negative_prompt
        }
    )

    img = output['output_imgs'][0]
    img = Image.fromarray(img[:, :, ::-1])  # Convert BGR to RGB

    # Save image to bytes
    img_byte_arr = io.BytesIO()
    img.save(img_byte_arr, format='PNG')
    img_byte_arr.seek(0)

    return send_file(img_byte_arr, mimetype='image/png')


if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0', port=5000)

在python环境下运行代码

第一次运行会下载大模型文件,需要等待一段时间 启动成功会有如下提示

csharp 复制代码
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://10.10.10.132:5000

使用postman测试

源码下载地址

相关推荐
霍格沃兹测试学院-小舟畅学4 小时前
AI Agent 运行时架构:Harness、Loop 与任务调度机制
人工智能·架构
海盗12344 小时前
微软技术日报 2026-10-14:十月补丁 1411 个漏洞,.NET 11 交出 RC2
人工智能·microsoft·机器人·aigc·.net
yunfanleo4 小时前
模型微调(Fine-tuning)详解:原理、Full/LoRA/QLoRA 三种方案对比与 PEFT 工程实践
人工智能·深度学习·机器学习·大模型·人工智能python
晨间晓4 小时前
图片拍歪了怎么办?用华为云码道开发 OpenCV 自动纠偏工具
人工智能·opencv·华为云
语音之家4 小时前
多模态模型的“思考”方式:视觉、通用音频与语音的 CoT 技术路线
人工智能·计算机视觉·语言模型·visual cot
旋生万物4 小时前
分形自相似:微服务、缓存、Prompt 模板为什么长一样
人工智能·缓存·微服务·云原生·prompt
虎鲸不是鱼4 小时前
【虎鲸进化论 02】三阶跃迁:Tab 补全 → Vibe Coding → Agent,人在每一层退到哪里
大数据·人工智能·大模型·llm·自进化agent
Yolanda_20224 小时前
Claudecode报错402 insufficient balance(1008)
人工智能·神经网络·chatgpt
武天旭4 小时前
AI安全--AI大模型的漏洞攻防与数据投毒风险
人工智能·安全
严谨的麻辣烫4 小时前
为什么 API 接口偶尔报错,重试一次又恢复了?从 HTTP 连接池到 Keep-Alive 排查
python·网络协议·tcp/ip·http·性能优化