characters / app.py
gugebing's picture
Create app.py
e8473ef
import gradio as gr
import os
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_response(character_name, gender, personality, experience, hobby, catchphrase):
# 构造角色扮演的 Prompt
prompt = f"请你扮演一名叫{character_name}{gender}生回答我的所有问题,你的性格是{personality},你有着{experience},喜欢{hobby},你常说的话是{catchphrase}。"
# 使用 OpenAI GPT-3 API 生成回复
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
# 返回回复的文本
return response.choices[0].text.strip()
# 定义输入组件
input_components = [
gr.inputs.Textbox(label="角色名字"),
gr.inputs.Radio(["男性", "女性", "其他"], label="性别"),
gr.inputs.Dropdown(["开朗", "内向", "活泼", "安静", "冷静", "热情", "严肃", "幽默", "善良", "狡猾", "无情", "自大", "自卑", "无聊", "兴奋", "紧张", "悲伤", "愤怒"], label="性格"),
gr.inputs.Textbox(label="经历"),
gr.inputs.Textbox(label="爱好"),
gr.inputs.Textbox(label="口头禅"),
]
# 定义输出组件
output_component = gr.outputs.Textbox()
# 创建 Gradio 应用
app = gr.Interface(
generate_response,
inputs=input_components,
outputs=output_component,
title="角色扮演聊天机器人",
description="输入角色的关键信息,然后与聊天机器人进行交互。",
theme="default",
layout="vertical",
allow_screenshot=True,
)
# 运行应用
if __name__ == "__main__":
app.launch()