中秋
中秋作为中国四大传统节日之一,又称祭月节、月光诞、月夕、秋节、仲秋节、拜月节、月娘节、月亮节、团圆节等,是中国民间的传统节日。中秋节源自天象崇拜,由上古时代秋夕祭月演变而来。中秋节自古便有祭月、赏月、吃月饼、看花灯、赏桂花、饮桂花酒等民俗,流传至今,经久不息。
文心大模型又能够和中秋一起产生什么样的火花呢?
让我们一起去看看吧~~~
文心大模型使用
可以参考:『抽丝剥茧』手把手快速上带你开启文心大模型的奇妙冒险
安装wenxin_api
!pip install wenxin_api
利用ERNIE-ViLG文生图模型生成图片
生成有关中秋的图片
在"text"中输入关键词即可(例如:中秋佳节、月亮、中秋吃月饼赏月、月饼、嫦娥奔月等)
稍等片刻即可
import wenxin_api from wenxin_api.tasks.text_to_image import TextToImage wenxin_api.ak = "You API Key" # 填写自己的API Key wenxin_api.sk = "You Secret Key" # 填写自己的Secret Key input_dict = { "text": "一家人一起过中秋吃月饼赏月", # 文字描述内容 "style": "水彩" # 生成风格 } photorst = TextToImage.create(**input_dict) print(photorst)
2022-09-04 20:26:42,725 - model is painting now!, taskId: 1518540, waiting: 3m 2022-09-04 20:27:03,390 - model is painting now!, taskId: 1518540, waiting: 1m {'imgUrls': ['https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f0639796ex', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f0639796i4', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f06397965q', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f063979630', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f0639796v9', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f0639796a2']}
下载生成的图片
import requests img_names = [] for i in photorst['imgUrls']: img = requests.get(i) img_name = i.split('/')[-1]+'.png' img_names.append(img_name) with open(img_name, 'wb')as f: f.write(img.content) print("下载完成")
2022-09-04 20:26:42,725 - model is painting now!, taskId: 1518540, waiting: 3m 2022-09-04 20:27:03,390 - model is painting now!, taskId: 1518540, waiting: 1m {'imgUrls': ['https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f0639796ex', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f0639796i4', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f06397965q', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f063979630', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f0639796v9', 'https://wenxin.baidu.com/younger/file/ERNIE-ViLG/cad0bb72920308d8b5637355f0639796a2']}
下载生成的图片
import requests img_names = [] for i in photorst['imgUrls']: img = requests.get(i) img_name = i.split('/')[-1]+'.png' img_names.append(img_name) with open(img_name, 'wb')as f: f.write(img.content) print("下载完成")
下载完成
使用ERNIE 3.0模型,生成有关中秋的佳句
在"text"中输入有关的关键词即可
import wenxin_api from wenxin_api.tasks.composition import Composition wenxin_api.ak = "You API Key" # 填写自己的API Key wenxin_api.sk = "You Secret Key" # 填写自己的Secret Key input_dict = { "text": "有关赞赏中秋的佳句:", # 题目/内容 "seq_len": 100, "topp": 0.5, "penalty_score": 1.2, "min_dec_len": 2, "min_dec_penalty_text": "。?:![<S>]", "is_unidirectional": 0, "task_prompt": "qa", "mask_type": "paragraph" } rst = Composition.create(**input_dict) print(rst) content = rst['result']
2022-09-04 23:03:55,861 - model 1: starts writing {'result': '“海上生明月,天涯共此时。”今夜月明人尽望,不知秋思落谁家?举杯邀明月,对影成三人。'}
图片和文字合成
import random from PIL import ImageFont from PIL import Image from PIL import ImageDraw # 初始化参数 x = 200 y = 20 css = ['./HGXC_CNKI.TTF', './HGXK_CNKI.TTF', './HGXS_CNKI.TTF'] word_size = 50 # 文字大小 word_css = random.choice(css) strs = content font = ImageFont.truetype(word_css,word_size) imageFile = './3.jpg'#random.choice(img_names) im1=Image.open(imageFile) draw = ImageDraw.Draw(im1) print(font.getsize(strs)) im1=Image.open(imageFile) draw = ImageDraw.Draw(im1) right = 0 #往右位移量 down = 0 #往下位移量 w = 800 #文字宽度(默认值) h = 800 #文字高度(默认值) row_hight = 0 #行高设置(文字行距) word_dir = 0; #文字间距 print(strs) for k,s2 in enumerate(strs): if k == 0: w,h = font.getsize(s2) # 获取第一个文字的宽和高 if s2 in [',', '。', '!', ';']: # 换行识别 right = right + w + row_hight down = 0 continue else : down = down+h + word_dir draw.text((x+right, y+down),s2,(255,255,0),font=font) new_filename = "new_mid_autumn.jpg" im1.save(new_filename) del draw im1.close()
成品展示
快来试试你们的中秋佳作吧!
记得留言让大家一起欣赏哦~