用【文心一言】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...

相关推荐
未来智慧谷18 分钟前
微软医疗AI诊断系统发布 多智能体协作实现疑难病例分析
人工智能·microsoft·医疗ai
野生技术架构师19 分钟前
简述MCP的原理-AI时代的USB接口
人工智能·microsoft
Allen_LVyingbo30 分钟前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
jndingxin32 分钟前
OpenCV中超分辨率(Super Resolution)模块类cv::dnn_superres::DnnSuperResImpl
人工智能·opencv·dnn
NAGNIP35 分钟前
一文搞懂FlashAttention怎么提升速度的?
人工智能·算法
智能砖头38 分钟前
LangChain 与 LlamaIndex 深度对比与选型指南
人工智能·python
大师兄带你刨AI39 分钟前
「AI产业」| 《中国信通院&华为:智能体技术和应用研究报告》
大数据·人工智能
老周聊大模型1 小时前
《ChatGLM/Llama调优实战:从指令微调到RLHF的工业级对齐方案》
人工智能·程序员·架构
ResponsibilityAmbiti1 小时前
AI 发展 && MCP
人工智能·llm·aigc
zkmall1 小时前
ZKmall模块商城批发电商平台搭建方案,多商户支持 + 订单管理功能全覆盖
大数据·人工智能