关于chatgpt的图像生成及语言翻译模块简单使用
图像生成
显示一张图片
import openai
from IPython.display import Image
openai.api_key = "你的api"
def chat_gpt_img(prompt):# 你的问题prompt = prompt# 调用 ChatGPT 接口
response = openai.Image.create(
prompt=prompt,
n=1,
size="256x256"
)
image_url = response['data'][0]['url']
return image_url
describe=input('请描述你想得到图片:')
Image(url="{}".format(chat_gpt_img(describe)))
请描述你想得到图片:小姐姐
display(Image(url="{}".format(chat_gpt_img(describe))))
显示多个
import openai
from IPython.display import Image
openai.api_key = "你的api"
def chat_gpt_img(prompt):# 你的问题prompt = prompt# 调用 ChatGPT 接口
response = openai.Image.create(
prompt=prompt,
n=2, #1-10张
size="256x256" #256x256、512x512 或 1024x1024 像素
)
image_url_list = response['data']
#print(len(response['data']))
#print(response)
return image_url_list
describe=input('请描述你想得到图片:')
image_url_list=chat_gpt_img(describe)
img_num=len(image_url_list)
# print(len(image_url_list))
# print(image_url_list)
# for i in range(0,img_num):
# Image(url="{}".format(image_url_list[i]['url']))
for i in range(img_num):
#print(image_url_list[i]['url'])
img_url=image_url_list[i]['url']
display(Image(url="{}".format(img_url)))
#print(image_url_list[0]['url'])
# for i in range(len(image_url_list)):
# Image(url="{}".format(img_url[i]['url']))
请描述你想得到图片:美女
Ps:官方还有图片编辑和图片变化功能,但是输入图片都要求是正方形??????感觉很鸡肋,未来优化了再补充
翻译模块
import openai
openai.api_key = "你的api"
# response = openai.Completion.create(
# model="text-davinci-003",
# prompt="Translate this into 1. Chinese, 2.English",
# temperature=0.3,
# max_tokens=100,
# top_p=1.0,
# frequency_penalty=0.0,
# presence_penalty=0.0
# )
def chat_gpt_translate(input_prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt="Translate this into 1. Chinese, 2.English"+input_prompt,
temperature=0.3,
max_tokens=100,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
)
print(response['choices'][0].text)
chat_gpt_translate(input('请输入你要翻译的内容:'))
请输入你要翻译的内容:小鸟在飞
1. 小鸟在飞。
2. The bird is flying.