用【文心一言】DIY自己的中秋明信片

一、我的中秋明信片

项目地址:用【文心一言】DIY自己的中秋明信片 aistudio.baidu.com/projectdeta...

1.思路

  • sd生成中秋图片
  • 文心一言生成中秋祝福
  • 合并中秋图、祝福语

2.效果图

二、环境设置

  • ppdiffusers
  • aistudio
  • gradio
  • langchain
  • llama-index
  • omegaconf
  • nltk
python 复制代码
%%capture
!pip install -r requirements.txt

三、素材生成

1.prompt翻译

python 复制代码
from tools import translate_prompt, magic_prompt
translated = translate_prompt("广州的高楼大厦,夜晚,中秋节,月饼, 月亮,孤独的一个人")
translated
swift 复制代码
'"Guangzhou\'s tall buildings at night, Mid-Autumn Festival, Mooncake, Moon, lonely person"'

2.文生图

python 复制代码
import sd

# 中秋,4k,光线追踪
prompt = translated+ ", 4K, ray tracing"
image = sd.imagine(prompt)
image
shell 复制代码
100%|██████████| 25/25 [00:06<00:00,  3.62it/s]
python 复制代码
# 保存处理后的图片  
image.save('background.jpg')
python 复制代码
image.size
scss 复制代码
(1200, 800)

3.中秋祝福语生成

python 复制代码
import aistudio

send_messages = [{
    "role": "user",
    "content": "我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"
}]

chat = aistudio.chat.create(
    messages=send_messages
)
bot_message = chat.result
bot_message
arduino 复制代码
'月明故乡思,\n乡愁在心间。\n亲人在眼前,\n祝福寄千里。'

4.过滤标点符号

python 复制代码
# 过滤标点
import re  
  
  
# 使用正则表达式删除句号和逗号  
bot_message = re.sub(r',|\。', '', bot_message)  
  
print(bot_message)
复制代码
月明故乡思
乡愁在心间
亲人在眼前
祝福寄千里

四、明信片合成

python 复制代码
from PIL import Image, ImageDraw, ImageFont


def combine_text_with_image(text, image_path, output_path,font_size, x, y):
    # Load the image
    image = Image.open(image_path)

    # Create a draw object
    draw = ImageDraw.Draw(image)

    # Load the font
    font = ImageFont.truetype("叶根友毛笔行书.ttf", font_size)  # You can change the font to any other font you have

    # Calculate the text size
    _, _, text_width, text_height = draw.textbbox((0, 0), text=text, font=font)
    # Set the position of the texts
    x = x
    y = y

    # Add the text to the image
    draw.text((x, y), text, font=font, fill="white")

    # Save the combined image
    image.save(output_path)
python 复制代码
combine_text_with_image(bot_message, "background.jpg", "combined_image.jpg", font_size=60,x=10, y=10)
combine_text_with_image('海上生明月,天涯共此时\n我在星河社区祝福大家中秋快乐!', "combined_image.jpg", "combined_image2.jpg",font_size=40, x=600, y=700)

五、gradio 部署

1.创建gradio部署文件

好多第一次使用的经常问到怎么部署?那截图来了

然后就会创建出事代码

python 复制代码
#该应用创建工具共包含三个区域,顶部工具栏,左侧代码区,右侧交互效果区,其中右侧交互效果是通过左侧代码生成的,存在对照关系。
#顶部工具栏:运行、保存、新开浏览器打开、实时预览开关,针对运行和在浏览器打开选项进行重要说明:
#[运行]:交互效果并非实时更新,代码变更后,需点击运行按钮获得最新交互效果。
#[在浏览器打开]:新建页面查看交互效果。
#以下为应用创建工具的示例代码

import gradio as gr

def quickstart(name):
    return "欢迎使用BML CodeLab应用创建工具Gradio, " + name + "!!!"
demo = gr.Interface(fn=quickstart, inputs="text", outputs="text")

demo.launch()

2.撰写部署文件

python 复制代码
# 1.安装依赖
import os
os.system("python -m pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/")

import gradio as gr
import sd
from PIL import Image, ImageDraw, ImageFont
from tools import translate_prompt, magic_prompt
import sd
import aistudio
# 过滤标点
import re  


# 2.图片生成
def generate_pic(prompt):
    # prompt翻译
    translated = translate_prompt(prompt)
    # 4k,光线追踪
    prompt = translated+ ", 4K, ray tracing"
    image = sd.imagine(prompt)
    # 保存处理后的图片  
    image.save('background.jpg')
    return 'background.jpg'

# 3.祝福语生成
def generate_5jueju(prompt):
    send_messages = [{
        "role": "user",
        "content": "我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"
    }]

    chat = aistudio.chat.create(
        messages=send_messages
    )
    bot_message = chat.result
    return bot_message

# 4.合并图片及文字
def combine_text_with_image(text, image_path, output_path,font_size, x, y):
    # Load the image
    image = Image.open(image_path)

    # Create a draw object
    draw = ImageDraw.Draw(image)

    # Load the font
    font = ImageFont.truetype("叶根友毛笔行书.ttf", font_size)  # You can change the font to any other font you have

    # Calculate the text size
    _, _, text_width, text_height = draw.textbbox((0, 0), text=text, font=font)
    # Set the position of the texts
    x = x
    y = y
    # Add the text to the image
    draw.text((x, y), text, font=font, fill="white")
    # Save the combined image
    image.save(output_path)

# 5.合并文字和图片
def generate(prompt1, prompt2):
    pic=generate_pic(prompt1)
    jueju5=generate_5jueju(prompt2)   
    # 使用正则表达式删除句号和逗号  
    jueju5 = re.sub(r',|\。', '', jueju5)
    combine_text_with_image(jueju5, "background.jpg", "combined_image.jpg", font_size=60,x=10, y=10)
    combine_text_with_image('海上生明月,天涯共此时\n我在星河社区祝福大家中秋快乐!', "combined_image.jpg", "combined_image2.jpg",font_size=40, x=600, y=700)
    return "combined_image2.jpg"

examples =  [
            ["北京的写字间,打工人在加班,桌上阅兵,窗外明亮的月亮","我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"],
            ["广州的高楼大厦,夜晚,中秋节,月饼, 月亮,孤独的一个人","我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"],
            ["关中平原,一望无际的玉米地,农家小院丰盛的晚餐,阅兵,月亮","我的家乡在陕西,中秋节特别思念家乡的亲人,请帮我生成中秋节日祝福五言绝句"]
            ]

# 6.gradio部署
demo=gr.Interface(
    fn=generate,
    inputs=[gr.inputs.Textbox(lines=5, label="场景描述输入"), 
            gr.inputs.Textbox(lines=5, label="诗词描述输入"),],
    outputs=gr.Image(label="明信片输出"),
    examples=examples
)

# 7.启动项目
demo.launch()

六、项目地址

用【文心一言】DIY自己的中秋明信片 aistudio.baidu.com/projectdeta...

相关推荐
wei_shuo1 小时前
OB Cloud 云数据库V4.3:SQL +AI全新体验
数据库·人工智能·sql
努力的搬砖人.1 小时前
AI生成视频推荐
人工智能
想要成为计算机高手2 小时前
Helix:一种用于通用人形控制的视觉语言行动模型
人工智能·计算机视觉·自然语言处理·大模型·vla
Mory_Herbert2 小时前
5.1 神经网络: 层和块
人工智能·深度学习·神经网络
Evand J3 小时前
MATLAB程序演示与编程思路,相对导航,四个小车的形式,使用集中式扩展卡尔曼滤波(fullyCN-EKF)
人工智能·算法
知来者逆4 小时前
在与大语言模型交互中的礼貌现象:技术影响、社会行为与文化意义的多维度探讨
人工智能·深度学习·语言模型·自然语言处理·llm
xwz小王子7 小时前
Taccel:一个高性能的GPU加速视触觉机器人模拟平台
人工智能·机器人
深空数字孪生8 小时前
AI时代的数据可视化:未来已来
人工智能·信息可视化
Icoolkj8 小时前
探秘 Canva AI 图像生成器:重塑设计创作新范式
人工智能
魔障阿Q8 小时前
windows使用bat脚本激活conda环境
人工智能·windows·python·深度学习·conda