imactool/hyperf-stable-diffusion

1.1.1 2023-06-26 02:16 UTC

This package is auto-updated.

Last update: 2024-09-26 05:06:42 UTC


README

基于 rulilg/laravel-stable-diffusion 包直接平移并适配了 hyperf 框架。

我对它进行了一些改造,大部分功能保持了相同。在这里感谢一下 RuliLG ,实现了如此强大好用的 stable-diffusion 组件。

基于 Replicate API 和 stablediffusionapi 的 Stable Diffusion 实现。

  • 🎨 内置提示助手,以创建更好的图像
  • 🚀 将结果存储在您的数据库中
  • 🎇 在同一 API 调用中生成多个图像
  • 💯 支持文本到图像和图像到图像

鸣谢:原作:RuliLG,特此鸣谢!

安装

composer require imactool/hyperf-stable-diffusion

注意:表新增了platform平台字段。-- 后续会使用迁移增加,目前不考虑 :)

发布配置(包含配置文件和迁移文件)

php bin/hyperf.php vendor:publish imactool/hyperf-stable-diffusion
php bin/hyperf.php migrate

至此,配置完成。

return [
    'url' => env('REPLICATE_URL', 'https://api.replicate.com/v1/predictions'),
    'token' => env('REPLICATE_TOKEN'),
    'version' => env('REPLICATE_STABLEDIFFUSION_VERSION', 'db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf'),
];

然后在 .env 里增加 ReplicatetokenREPLICATE_TOKEN即可。

使用

文字生成图片(Text to Image)

use Imactool\HyperfStableDiffusion\Prompt;
use Imactool\HyperfStableDiffusion\Replicate;

   $result = Replicate::make()->withPrompt(
            Prompt::make()
                ->with('a panda sitting on the streets of New York after a long day of walking')
                ->photograph()
                ->resolution4k()
                ->trendingOnArtStation()
                ->highlyDetailed()
                ->dramaticLighting()
                ->octaneRender()
        )->inputParams('num_outputs', 1)
        ->generate(); //根据不同的模型来传递生成的张数

图片生成图片(Image to Image)

use Imactool\HyperfStableDiffusion\Prompt;
use Imactool\HyperfStableDiffusion\Replicate;
use Intervention\Image\ImageManager;

//这里使用了 intervention/image 扩展来处理图片文件,你也可以更换为其他的
 $sourceImg =  (string) (new ImageManager(['driver' => 'imagick']))->make('path/image/source.png')->encode('data-url');

$prompt = 'Petite 21-year-old Caucasian female gamer streaming from her bedroom with pastel pink pigtails and gaming gear. Dynamic and engaging image inspired by colorful LED lights and the energy of Twitch culture, in 1920x1080 resolution.';
$result = Replicate::make()
    ->converVersion('a991dcab77024471af6a89ef758d98d1a54c5a25fc52a06ccfd7754b7ad04b35')
    ->withPrompt(
        Prompt::make()
            ->with($prompt)
    )
    ->inputParams('image',$sourceImg)
    ->inputParams('negative_prompt', 'disfigured, kitsch, ugly, oversaturated, greain, low-res, Deformed, blurry, bad anatomy, disfigured, poorly drawn face, mutation, mutated, extra limb, ugly, poorly drawn hands, missing limb, blurry, floating limbs, disconnected limbs, malformed hands, blur, out of focus, long neck, long body, ugly, disgusting, poorly drawn, childish, mutilated, mangled, old, surreal, calligraphy, sign, writing, watermark, text, body out of frame, extra legs, extra arms, extra feet, out of frame, poorly drawn feet, cross-eye, blurry, bad anatomy')
    ->inputParams('strength', 0.5)
    ->inputParams('upscale', 2)
    ->inputParams('num_inference_steps', 25)
    ->inputParams('guidance_scale', 7.5)
    ->inputParams('scheduler', 'EulerAncestralDiscrete')
    ->inputParams('num_outputs', 1)
    ->generate();

查询结果

use Imactool\HyperfStableDiffusion\Replicate;
 $freshResults = Replicate::get($replicate_id);

生成提示

已内置几种样式

此外,您可以使用以下方法添加自定义样式

  • as(string $canvas): 在开头添加一个字符串(例如“一张照片”)
  • paintingStyle(string $style): 添加绘画风格(例如真实,超现实主义等)
  • by(string $author): 指示系统以特定作者的风格绘制
  • effect(string $effect): 为提示添加最终效果。您可以添加任意多个。

有关如何为 Stable Diffusion 构建提示的更多信息,请点击此链接.

基于 stablediffusionapi 平台 https://stablediffusionapi.com/docs/

或者 Postman 集合

文字生成图片(Text to Image)

use Imactool\HyperfStableDiffusion\StableDiffusion;

 $res = StableDiffusion::make()
                    ->useDreamboothApiV4()
                    ->withPayload('key', '')
                    ->withPayload('model_id', 'anything-v4')
                    ->withPayload('prompt', 'ultra realistic close up portrait ((beautiful pale cyberpunk female with heavy black eyeliner)), blue eyes, shaved side haircut, hyper detail, cinematic lighting, magic neon, dark red city, Canon EOS R3, nikon, f/1.4, ISO 200, 1/160s, 8K, RAW, unedited, symmetrical balance, in-frame, 8K')
                    ->withPayload('negative_prompt', 'painting, extra fingers, mutated hands, poorly drawn hands, poorly drawn face, deformed, ugly, blurry, bad anatomy, bad proportions, extra limbs, cloned face, skinny, glitchy, double torso, extra arms, extra hands, mangled fingers, missing lips, ugly face, distorted face, extra legs, anime')
                    ->withPayload('width', '512')
                    ->withPayload('height', '512')
                    ->withPayload('samples', '1')
                    ->withPayload('num_inference_steps', '30')
                    ->withPayload('seed', null)
                    ->withPayload('guidance_scale', '7.5')
                    ->withPayload('webhook', null)
                    ->withPayload('track_id', null)
                    ->text2img();

                var_dump( $res);

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件.