过去的这个周末,我一直在把玩 Stable Diffusion。之前用过 stable diffusion(SD)的 WebUI,也玩过 invokeAI,但作为一个非技术英语英语不那么灵光的工程师,形容女子漂亮绞尽脑汁也就止步于 delicate,衣着华美 gorgeous 顶天,颜色丰富决计想不到还有个 iridescent。所以,同样的工具,别人用出来是王者的效果,我自己则妥妥青铜。AI 作画对我而言,属于玩一会就会因为各种垃圾画作的产生而失去兴趣的东西。我想,要是能用中文写 prompt,然后自动转换成像样的,符合 SD,能生成精美画作的英文描述,那该多好啊?
这问题搁以前,是非常有挑战的事情。但如今有了 GPT,也就是个调用 API 的难度。我可以告诉 GPT3.5,把我提供的 prompt 翻译成英文并以其为蓝本,写个100字的小作文。于是乎我的 prompt:
一个安详的小村庄,蓝天白云绿草,满树的花,绝美
GPT3.5 给我翻译后并用英文扩展出来的描述:
Nestled in the heart of the countryside lies a serene and tranquil village, where the sky is a brilliant shade of blue and the clouds float lazily by. The lush green grass stretches out as far as the eye can see, and the trees are adorned with a plethora of colorful flowers, creating a breathtakingly beautiful scene. The air is filled with the sweet fragrance of nature, and the gentle breeze carries with it the sounds of chirping birds and rustling leaves. It is a place where time seems to stand still, and one can't help but feel a sense of peace and contentment in this idyllic setting.digital painting, hyperrealistic, fantasy, surrealist, artstation, ((insanely detailed and intricate)), (sci-fi, stunningly beautiful), dystopian, (sharp focus), ((cinematic lighting)), depth of field, iridescent golden
SD 生成的图片:

再比如:
不知火舞穿着红色的旗袍,站在樱花树下撑着绿色的小伞,对着镜头微笑
GPT 3.5 准确地理解这姐姐是拳皇中的人物:
Mai Shiranui, the iconic character from the video game series "The King of Fighters," is elegantly dressed in a vibrant red qipao, standing gracefully beneath a blooming cherry blossom tree. She holds a small green umbrella, adding a touch of whimsy to her already charming demeanor. Her smile is warm and inviting, drawing the viewer in with its genuine nature. The contrast between the bold red of her dress and the delicate pink of the cherry blossoms creates a stunning visual display. Mai Shiranui exudes confidence and beauty, making her a captivating subject for any artist or admirer.digital painting, hyperrealistic, fantasy, surrealist, artstation, ((insanely detailed and intricate)), (sci-fi, stunningly beautiful), dystopian, (sharp focus), ((cinematic lighting)), depth of field, iridescent golden, extremely long black ponytail hair, black eyes, hourglass body, long slim legs, elegant, character portrait, ultra realistic, ultra detailed face and eyes, realistic skin, realistic hair, realistic eyes, realistic face, realistic body, realistic hands, realistic feet
SD 生成的图片(注意,SD 并未如实表达绿色小伞):

有了这个思路,我用 streamlit 和 diffusers 构建了一个简单的 Web UI,允许我输入中英文 prompt,然后调用 GPT,将其拓展成小作文,并附带一些额外的参数,传给 diffusers 作图。其中对于人物我还特定添加了细节的参数,比如眼睛的颜色,头发的样式等。有点给芭比娃娃打扮的感觉:

后来我又稍作修改,允许载入不同的 model,如下是载入 prompthero/openjourney 绘制铁甲霸王龙:

大图(使用 realESRGAN 4 倍放大):


这个在几年前无法想象的绘图工具,如今只需200行代码就能完成全部的 UI,后端逻辑(包括调用 GPT API 和 SD 模型处理),图像放大等功能。核心的绘制图片的代码:
upscaler = Upscalerimg = upscaler.run(image, 1.0) img.save(image_path)
return{ 'image': img, 'path': image_path, 'prompt': prompt, 'width': width, 'height': height, 'steps': steps, 'scale': scale, }
在撰写的过程中,我还故意尝试使用 cursor IDE(GPT-4)来生成代码,最终大概有 30-40% 的代码(主要是 streamlit)是 cursor 帮我生成的。然而生成图片的代码,以及 upscale 的代码,cursor 还无法帮我生成,感觉它还没有学习 diffusers 和realESRGAN 这两个库。比如我让它用 diffusers 帮我根据 prompt 生成图片:
prompt = "A beautiful sunset over the ocean"image = diffusers.generate(prompt)image.save( "sunset.png")
它会瞎调用不存在的 API。不过,cursor 对 streamlit 就很熟。我让它生成的 streamlit 代码,基本都满足了我的需求。
结语
这两天带着小宝小贝没少玩我做的小工具,加上我各种测试生成的图片,前前后后有个两百张高质量的图片。我没有专门的 GPU,只能拿着 m1max 凑合跑,在网上找了些 serverless GPU 的方案,包括 replicate,banana.dev,都遇到不少问题。由此,我对 serverless GPU 的实现逻辑也有一些纸上谈兵的粗浅想法,后续也许会写文章描述一下。两天下来,GPT API 的花销仅仅增加了 4 美分,却大大提高了我生成图片的效率和质量。如果你还没用 GPT API 做点事情,我强烈建议你试试,python,JS,Rust 的 SDK 质量都还可以。
有人说:后 AIGC 时代,程序员将会失业。我却不这么认为。由于编程的门槛大大降低,AIGC 时代会激活更多 ”程序开发“ 的工种,就像高级语言时代终结的不是程序员,反而开拓了程序员的市场;web 开发时代终结的也不是程序员,它让非科班出身的开发者也成为程序员;AIGC 时代,传统意义上的程序员会小众化,但更多的 “程序开发” 的需求会带来一个全民程序员的时代。那些被淘汰的程序员,也仅仅是因为无法适应新的开发模式而被淘汰,就像汽车的平民时代到来后,只有那些不转型的,仅仅会为单位开车的小轿车司机,才会被淹没在历史之中。
扯远了,关于这个话题,我计划 4/16 号在望京的 meetup 多聊聊。嗯,是的,在阔别祖国 3 年多,我终于可以短暂地回去一趟。具体细节,过几天会另行通知。
再放几张高清大图,请自取:

未来世界的女机器人

从油画中走出来的拿破仑

穿着高定礼服,佩戴精致首饰的黑发公主

机器人写代码

杀人鲸跃出水面

站在天坛前面的小女孩(SD给了个科幻版的天坛)

航拍宜居的地外星球

完成任务的机甲战士

拥有东方血统的水边的阿狄丽娜