作业一
改进代码,为文生图功能加上风格选项,并在页面上加-个可指定图片风格的选项框。
实现
streamlit在功能中支持单选框,类似html语言,利用如下代码生成单选框内容,并设置动漫为默认风格
image_style = st.radio(
"请选择生成风格:",
('动漫', '简笔', '油画', '水墨'), # 暂支持四种风格,动漫为默认
index=0
)
然后再接收风格选项信息,并和文生图prompt进行拼接
style = f"{image_style}风格。" # 二次元风格。
image_prompt = style + image_prompt.strip()
效果
然后我们利用简单的示例进行测试,可以看出生成了不同风格的图片
作业二
实现role-play对话数据生成工具,要求包含下列功能:
1.基于一段文本(自己找一段文本,复制到提示词就可以了,比如你可以从小说中选取一部分文本,注意文本要用markdown格式)生成角色人设,可借助ChatGLM实现。
2.给定两个角色的人设,调用CharacterGLM交替生成他们的回复。
3.将生成的对话数据保存到文件中。
4.(可选)设计图形界面,通过点击图形界面上的按钮执行对话数据生成,并展示对话数据。
实现
通过代码包中api文件,调用get_characterglm_response实现多轮对话,并在output_stream_response中实现保存对话至文件的功能,这里我们指定为json文件格式
def output_stream_response(response_stream: Iterator[str], placeholder):
content = ""
conversation_history = []
for content in itertools.accumulate(response_stream): # 累积流式响应的字符
placeholder.markdown(content)
# Add the user's input and the assistant's response to the conversation history
user_input = st.session_state["history"][-1]["content"]
response_data = {
"user_input": user_input,
"assistant_response": content
}
conversation_history.append(response_data)
# Save the entire conversation history to a JSON file
filename = "conversation_history.json"
with open(filename, "a", encoding="utf-8") as f:
json.dump(conversation_history, f, ensure_ascii=False, indent=2)
return content