-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathapp.py
277 lines (248 loc) · 7.82 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import random
import threading
import time
import gradio as gr
from AIGN import AIGN
from LLM import chatLLM
STREAM_INTERVAL = 0.
def make_middle_chat():
carrier = threading.Event()
carrier.history = []
def middle_chat(messages, temperature=None, top_p=None):
nonlocal carrier
carrier.history.append([None, ""])
if len(carrier.history) > 20:
carrier.history = carrier.history[-16:]
try:
for resp in chatLLM(
messages, temperature=temperature, top_p=top_p, stream=True
):
output_text = resp["content"]
total_tokens = resp["total_tokens"]
carrier.history[-1][1] = f"total_tokens: {total_tokens}\n{output_text}"
return {
"content": output_text,
"total_tokens": total_tokens,
}
except Exception as e:
carrier.history[-1][1] = f"Error: {e}"
raise e
return carrier, middle_chat
def gen_ouline_button_clicked(aign, user_idea, history):
aign.user_idea = user_idea
carrier, middle_chat = make_middle_chat()
carrier.history = history
aign.novel_outline_writer.chatLLM = middle_chat
gen_ouline_thread = threading.Thread(target=aign.genNovelOutline)
gen_ouline_thread.start()
while gen_ouline_thread.is_alive():
yield [
aign,
carrier.history,
aign.novel_outline,
gr.Button(visible=False),
]
time.sleep(STREAM_INTERVAL)
yield [
aign,
carrier.history,
aign.novel_outline,
gr.Button(visible=False),
]
def gen_beginning_button_clicked(
aign, history, novel_outline, user_requriments, embellishment_idea
):
aign.novel_outline = novel_outline
aign.user_requriments = user_requriments
aign.embellishment_idea = embellishment_idea
carrier, middle_chat = make_middle_chat()
carrier.history = history
aign.novel_beginning_writer.chatLLM = middle_chat
aign.novel_embellisher.chatLLM = middle_chat
gen_beginning_thread = threading.Thread(target=aign.genBeginning)
gen_beginning_thread.start()
while gen_beginning_thread.is_alive():
yield [
aign,
carrier.history,
aign.writing_plan,
aign.temp_setting,
aign.novel_content,
gr.Button(visible=False),
]
time.sleep(STREAM_INTERVAL)
yield [
aign,
carrier.history,
aign.writing_plan,
aign.temp_setting,
aign.novel_content,
gr.Button(visible=False),
]
def gen_next_paragraph_button_clicked(
aign,
history,
user_idea,
novel_outline,
writing_memory,
temp_setting,
writing_plan,
user_requriments,
embellishment_idea,
):
aign.user_idea = user_idea
aign.novel_outline = novel_outline
aign.writing_memory = writing_memory
aign.temp_setting = temp_setting
aign.writing_plan = writing_plan
aign.user_requriments = user_requriments
aign.embellishment_idea = embellishment_idea
carrier, middle_chat = make_middle_chat()
carrier.history = history
aign.novel_writer.chatLLM = middle_chat
aign.novel_embellisher.chatLLM = middle_chat
aign.memory_maker.chatLLM = middle_chat
gen_next_paragraph_thread = threading.Thread(target=aign.genNextParagraph)
gen_next_paragraph_thread.start()
while gen_next_paragraph_thread.is_alive():
yield [
aign,
carrier.history,
aign.writing_plan,
aign.temp_setting,
aign.writing_memory,
aign.novel_content,
gr.Button(visible=False),
]
time.sleep(STREAM_INTERVAL)
yield [
aign,
carrier.history,
aign.writing_plan,
aign.temp_setting,
aign.writing_memory,
aign.novel_content,
gr.Button(visible=False),
]
css = """
#row1 {
min-width: 200px;
max-height: 700px;
overflow: auto;
}
#row2 {
min-width: 300px;
max-height: 700px;
overflow: auto;
}
#row3 {
min-width: 200px;
max-height: 700px;
overflow: auto;
}
"""
with gr.Blocks(css=css) as demo:
aign = gr.State(AIGN(chatLLM))
gr.Markdown("## AI 写小说 Demo")
with gr.Row():
with gr.Column(scale=0, elem_id="row1"):
with gr.Tab("开始"):
gr.Markdown("生成大纲->大纲标签->生成开头->状态标签->生成下一段")
user_idea_text = gr.Textbox(
"主角独自一人在异世界冒险,它爆种时会大喊一句:原神,启动!!!",
label="想法",
lines=4,
interactive=True,
)
user_requriments_text = gr.Textbox(
"",
label="写作要求",
lines=4,
interactive=True,
)
embellishment_idea_text = gr.Textbox(
"",
label="润色要求",
lines=4,
interactive=True,
)
gen_ouline_button = gr.Button("生成大纲")
with gr.Tab("大纲"):
novel_outline_text = gr.Textbox(
label="大纲", lines=24, interactive=True
)
gen_beginning_button = gr.Button("生成开头")
with gr.Tab("状态"):
writing_memory_text = gr.Textbox(
label="记忆",
lines=6,
interactive=True,
max_lines=8,
)
writing_plan_text = gr.Textbox(label="计划", lines=6, interactive=True)
temp_setting_text = gr.Textbox(
label="临时设定", lines=5, interactive=True
)
# TODO
# gen_next_paragraph_button = gr.Button("撤销生成")
gen_next_paragraph_button = gr.Button("生成下一段")
# TODO
# auto_gen_next_checkbox = gr.Checkbox(
# label="自动生成下一段", checked=False, interactive=True
# )
with gr.Column(scale=3, elem_id="row2"):
chatBox = gr.Chatbot(height=f"80vh", label="输出")
with gr.Column(scale=0, elem_id="row3"):
novel_content_text = gr.Textbox(
label="小说正文", lines=32, interactive=True, show_copy_button=True
)
# TODO
# download_novel_button = gr.Button("下载小说")
gr.Markdown("github: https://github.com/cjyyx/AI_Gen_Novel")
gen_ouline_button.click(
gen_ouline_button_clicked,
[aign, user_idea_text, chatBox],
[aign, chatBox, novel_outline_text, gen_ouline_button],
)
gen_beginning_button.click(
gen_beginning_button_clicked,
[
aign,
chatBox,
novel_outline_text,
user_requriments_text,
embellishment_idea_text,
],
[
aign,
chatBox,
writing_plan_text,
temp_setting_text,
novel_content_text,
gen_beginning_button,
],
)
gen_next_paragraph_button.click(
gen_next_paragraph_button_clicked,
[
aign,
chatBox,
user_idea_text,
novel_outline_text,
writing_memory_text,
temp_setting_text,
writing_plan_text,
user_requriments_text,
embellishment_idea_text,
],
[
aign,
chatBox,
writing_plan_text,
temp_setting_text,
writing_memory_text,
novel_content_text,
],
)
demo.queue()
demo.launch()