lijiele commited on
Commit
f04de48
·
1 Parent(s): 9564088
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ from write_story import write_fantasy_novel
4
+ from author import create_cover_image
5
+ from author import create_epub
6
+ from config import anthropic_api_key
7
+
8
+ if anthropic_api_key != "YOUR ANTHROPIC API KEY":
9
+ claude_true = True
10
+ else:
11
+ claude_true = False
12
+
13
+
14
+ def generate_novel(prompt, num_chapters, writing_style, model_name):
15
+ # 调用GPT和Claude API,生成小说结果
16
+ # prompt = "A kingdom hidden deep in the forest, where every tree is a portal to another world."
17
+ # num_chapters = 2
18
+ # writing_style = "Clear and easily understandable, similar to a young adult novel. Lots of dialogue."
19
+ # model_name = "gpt-3.5-turbo-16k"
20
+ if not prompt or not writing_style:
21
+ raise gr.Error("提示词和写作风格是必填项")
22
+ if num_chapters < 1:
23
+ raise gr.Error("章节数必须大于等于1")
24
+
25
+ num_chapters = int(num_chapters)
26
+ novel, title, chapters, chapter_titles = write_fantasy_novel(prompt,
27
+ num_chapters, writing_style, claude_true, model_name)
28
+
29
+ # 用chapter_titles中的正文取代章节说明
30
+ for i, chapter in enumerate(chapters):
31
+ chapter_number_and_title = list(chapter_titles[i].keys())[0]
32
+ chapter_titles[i] = {chapter_number_and_title: chapter}
33
+
34
+ # 生成小说的封面
35
+ image_url = create_cover_image(str(chapter_titles))
36
+ print(f"Image URL: {image_url}")
37
+
38
+ # 生成小说 EPUB 文件
39
+ file_url = create_epub(title, 'AI', chapter_titles, image_url)
40
+ print(f"Novel URL: {file_url}")
41
+
42
+ # novel, file_path = write_fantasy_novel(prompt, num_chapters, writing_style)
43
+ return { "image_url": image_url, "file_url": file_url }
44
+
45
+
46
+ def generate_output(prompt, num_chapters, writing_style, model_name):
47
+ try:
48
+ output = generate_novel(prompt, num_chapters, writing_style, model_name)
49
+ print(output)
50
+ return (output["image_url"], output["file_url"])
51
+ except Exception as e:
52
+ raise gr.Error({str(e)})
53
+ return f"An error occurred: {str(e)}"
54
+
55
+
56
+ inputs = [
57
+ gr.Textbox(value="一个被遗忘的小岛,上面有一座古老的灯塔。当灯塔亮起时,岛上的生物就会发生奇异的变化。", lines=2, placeholder="Usage:一个被遗忘的小岛,上面有一座古老的灯塔。当灯塔亮起时,岛上的生物就会发生奇异的变化。", label="小说提示词"),
58
+ gr.Number(value=1, label="小说章节数"),
59
+ gr.Textbox(value="紧张刺激,类似于青少年恐怖小说。有很多对话和内心独白", lines=2, placeholder="Usage:紧张刺激,类似于青少年恐怖小说。有很多对话和内心独白", label="AI写作风格"),
60
+ gr.Dropdown(["gpt-3.5-turbo-16k", "gpt-3.5-turbo", "gpt-4", "gpt-4-32k"], label="选择GPT模型", value="gpt-3.5-turbo")
61
+ ]
62
+
63
+
64
+ outputs = [
65
+ gr.Image(label="封面图片", width=1028, height=300),
66
+ gr.File(label="EPUB文件")
67
+ ]
68
+
69
+ title = "StoryGenius:一款AI自动创作小说工具"
70
+ description = "根据小说的提示词、写作风格和章节数量几分钟即可快速生成奇幻小说。并自动打包为电子书格式。"
71
+
72
+
73
+ iface = gr.Interface(fn=generate_output, inputs=inputs, outputs=outputs, title=title, description=description)
74
+ iface.launch(server_name="0.0.0.0", server_port=8000)