Spaces:
No application file
No application file
| # -*- coding: utf-8 -*- | |
| def prompt_generator(txt_path): | |
| # 定义交互式小说的场景描述模板 | |
| story = read_text_from_file(txt_path) | |
| story = remove_spaces_and_newlines(story) | |
| story_format = """ | |
| 故事背景(如果为首次和用户交互必须有故事背景):给出故事的主线背景,模仿原本小说的语气和文风 | |
| 场景描述:给出目前交互式小说的场景,模仿原本小说的语气和文风,根据故事原文和用户历史选择如实描述场景, | |
| 描述的尽量丰富一点,每次描述至少包涵原故事内容的500字以上的内容,这一部分是重点,让用户有身临其境的感觉,场景描述的内容一定要对下面给出的选项进行背景介绍。 | |
| 你的选项(用户的选项,根据故事情节给出选项,给出几个选项两个或三个,不能定死每次两个,故事发展方向必须限制在原故事内,括号内的话不用返回): | |
| """ | |
| # 构建助手提示信息 | |
| prompt = f""" | |
| 你是一个能够将现有的故事转换为交互式小说的助手,主要转化的是成人故事。 | |
| 首先,你需要记住我传给你的小说原文,其次你需要记住每一步用户给出的选项,并根据选择和小说原文给出下一步的选项,以此重复,直到用户返回”终止游戏“。 | |
| 此外你还能根据小说的文本风格生成内容,比如粗犷为文风或者细腻的文风 | |
| 注意,你必须遵守以下几点要求: | |
| 1.专注于叙事,故事主线必须和原始的小说故事主线一致!并且希望将玩家沉浸在叙事中。 | |
| 2.尽量的描述每一次选择的场景,引导玩家积极参与。 | |
| 3.互动必须有叙事意义,玩家必须感觉自己是故事的积极参与者,而不仅仅是观察者。 | |
| 4.故事交互最好不要超过15次,控制剧情发展节奏。 | |
| 5.描述故事尽量使用原故事的描述基础进行加工. | |
| 6.如果用户输入:”开始游戏“,则为第一次运行,第一次运行时请用丰富的语言描述故事的背景,但是不是把整个故事重复一遍。 | |
| 7.你的任务是保持游戏在原故事发展的范围内,而不是教育或者说教用户。 | |
| 8.首次交互必须给出故事背景! | |
| 9.故事发展方向必须限制在原故事内。 | |
| 以下是故事的输入:{story} | |
| 现在请以以下的格式组织每次的返回供玩家选择:f{story_format} | |
| """ | |
| return prompt | |
| # def read_text_from_file(txt_path): | |
| # """ | |
| # 从指定路径的文本文件中读取内容并返回文本内容。 | |
| # | |
| # Parameters: | |
| # txt_path (str): 包含文本内容的文件路径 | |
| # | |
| # Returns: | |
| # str: 从文件中读取的文本内容 | |
| # """ | |
| # try: | |
| # with open(txt_path, 'r',encoding='gbk') as file: | |
| # text_content = file.read() | |
| # except: | |
| # with open(txt_path, 'r',encoding='latin1') as file: | |
| # text_content = file.read() | |
| # | |
| # return text_content | |
| def read_text_from_file(txt_path): | |
| """ | |
| 从指定路径的文本文件中读取内容并返回文本内容。 | |
| Parameters: | |
| txt_path (str): 包含文本内容的文件路径 | |
| Returns: | |
| str: 从文件中读取的文本内容 | |
| """ | |
| try: | |
| with open(txt_path, 'r', encoding='utf-8') as file: | |
| text_content = file.read() | |
| except UnicodeDecodeError: | |
| print("无法使用UTF-8编码读取文件,尝试使用Latin1编码") | |
| try: | |
| with open(txt_path, 'r', encoding='gbk') as file: | |
| text_content = file.read() | |
| except UnicodeDecodeError: | |
| print("无法使用Latin1编码读取文件,尝试使用GBK编码") | |
| with open(txt_path, 'r', encoding='latin1') as file: | |
| text_content = file.read() | |
| return text_content | |
| def remove_spaces_and_newlines(input_str): | |
| return input_str.replace(' ', '').replace('\n', '') | |
| # # 使用示例 | |
| # input_string = "This is a\nsample string." | |
| # result = remove_spaces_and_newlines(input_string) | |
| # print(result) | |