Yoyo2004 commited on
Commit
1e4ff71
·
verified ·
1 Parent(s): 586ae2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -25
app.py CHANGED
@@ -203,10 +203,7 @@ body, .gradio-container {
203
 
204
  def paginate_story(story_data, chars_per_page=450, first_page_ratio=0.8):
205
  """
206
- 分章 -> 分页。
207
- - 首章首页可用字符数 = chars_per_page * first_page_ratio(因为有大标题和空白)
208
- - 之后的每一页都用 chars_per_page,尽量“填满”固定高度。
209
- 返回: (flat_pages, chapter_start_indices)
210
  """
211
  if not story_data:
212
  return [], []
@@ -226,7 +223,7 @@ def paginate_story(story_data, chars_per_page=450, first_page_ratio=0.8):
226
  current_char_count = 0
227
  is_start = True
228
 
229
- # 首页稍微少一点,为标题留空间
230
  limit = int(chars_per_page * first_page_ratio)
231
 
232
  for p in paragraphs:
@@ -234,7 +231,7 @@ def paginate_story(story_data, chars_per_page=450, first_page_ratio=0.8):
234
  if not p:
235
  continue
236
 
237
- # 如果当前段落加进去会超过限制,并且当前页已有内容 -> 换页
238
  if current_char_count + len(p) > limit and current_page_content:
239
  flat_pages.append({
240
  "title": title if is_start else "",
@@ -244,16 +241,14 @@ def paginate_story(story_data, chars_per_page=450, first_page_ratio=0.8):
244
  })
245
  current_global_page_index += 1
246
 
247
- # 新的一页
248
  current_page_content = [p]
249
  current_char_count = len(p)
250
  is_start = False
251
- limit = chars_per_page # 之后的页按照标准容量
252
  else:
253
  current_page_content.append(p)
254
  current_char_count += len(p)
255
 
256
- # 收尾
257
  if current_page_content:
258
  flat_pages.append({
259
  "title": title if is_start else "",
@@ -267,7 +262,7 @@ def paginate_story(story_data, chars_per_page=450, first_page_ratio=0.8):
267
 
268
 
269
  def render_book_page(flat_pages, page_index):
270
- # 1. 空状态
271
  if not flat_pages or len(flat_pages) == 0:
272
  return """
273
  <div class='book-page-container empty-state'>
@@ -280,7 +275,7 @@ def render_book_page(flat_pages, page_index):
280
  </div>
281
  """
282
 
283
- # 2. 渲染正常页面
284
  total_pages = len(flat_pages)
285
  page_index = max(0, min(page_index, total_pages - 1))
286
  page_data = flat_pages[page_index]
@@ -346,8 +341,7 @@ def build_full_text(story_data):
346
 
347
  def build_full_text_html(full_text: str) -> str:
348
  """
349
- 生成带「一键复制」按钮的 HTML。
350
- 使用 textarea + JS 调用 navigator.clipboard.writeText。
351
  """
352
  escaped = py_html.escape(full_text or "")
353
  return f"""
@@ -395,14 +389,14 @@ def bridge_to_backend(premise):
395
  personas_html = result[3]
396
  raw_story_list = result[4]
397
 
398
- # 分页 + 计算章节索引(让页面尽量填满)
399
  flat_pages, chap_indices = paginate_story(
400
  raw_story_list,
401
- chars_per_page=1200,
402
  first_page_ratio=0.8,
403
  )
404
 
405
- # 一键复制全文的文本 + HTML
406
  full_text = build_full_text(raw_story_list)
407
  full_text_html = build_full_text_html(full_text)
408
 
@@ -418,17 +412,13 @@ def bridge_to_backend(premise):
418
  # ==========================================
419
  # 4. 前端 UI 布局
420
  # ==========================================
421
- with gr.Blocks(
422
- theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"),
423
- css=custom_css,
424
- title="LongStory Agent"
425
- ) as demo:
426
-
427
  # --- 状态管理 ---
428
  story_pages_state = gr.State([]) # 所有的页面 List[Dict]
429
  chapter_indices_state = gr.State([]) # 每一章起始页的 index List[int]
430
  current_page_state = gr.State(0) # 当前页面
431
 
 
432
  # --- 顶部标题 ---
433
  with gr.Row(elem_classes=["header-box"]):
434
  gr.HTML("""
@@ -463,9 +453,12 @@ with gr.Blocks(
463
  gr.HTML('<div class="examples-container"><div class="examples-label">⚡ 快速开始 (Quick Inspirations)</div></div>')
464
 
465
  example_prompts = [
466
- ["高中时互相看不顺眼的死对头,十年后在公司并购案谈判桌上重逢,一个是冷血收购方,一个是绝境求生的CEO。"],
467
- ["天生'废灵根'的宗门弃徒,在被逐出师门当晚,意外捡到一个能听见万物心声的黑色小鼎。"],
468
- ["一觉醒来,人类全部消失,只剩下我和我养的猫。第三天,猫开口对我说话了。"]
 
 
 
469
  ]
470
 
471
  with gr.Column(elem_classes=["examples-table"]):
 
203
 
204
  def paginate_story(story_data, chars_per_page=450, first_page_ratio=0.8):
205
  """
206
+ 分章 -> 分页
 
 
 
207
  """
208
  if not story_data:
209
  return [], []
 
223
  current_char_count = 0
224
  is_start = True
225
 
226
+ # 首页为标题预留空间
227
  limit = int(chars_per_page * first_page_ratio)
228
 
229
  for p in paragraphs:
 
231
  if not p:
232
  continue
233
 
234
+ # 换页
235
  if current_char_count + len(p) > limit and current_page_content:
236
  flat_pages.append({
237
  "title": title if is_start else "",
 
241
  })
242
  current_global_page_index += 1
243
 
 
244
  current_page_content = [p]
245
  current_char_count = len(p)
246
  is_start = False
247
+ limit = chars_per_page
248
  else:
249
  current_page_content.append(p)
250
  current_char_count += len(p)
251
 
 
252
  if current_page_content:
253
  flat_pages.append({
254
  "title": title if is_start else "",
 
262
 
263
 
264
  def render_book_page(flat_pages, page_index):
265
+ # 空状态
266
  if not flat_pages or len(flat_pages) == 0:
267
  return """
268
  <div class='book-page-container empty-state'>
 
275
  </div>
276
  """
277
 
278
+ # 正常渲染页面
279
  total_pages = len(flat_pages)
280
  page_index = max(0, min(page_index, total_pages - 1))
281
  page_data = flat_pages[page_index]
 
341
 
342
  def build_full_text_html(full_text: str) -> str:
343
  """
344
+ 生成「一键复制」按钮
 
345
  """
346
  escaped = py_html.escape(full_text or "")
347
  return f"""
 
389
  personas_html = result[3]
390
  raw_story_list = result[4]
391
 
392
+ # 分页 + 计算章节索引
393
  flat_pages, chap_indices = paginate_story(
394
  raw_story_list,
395
+ chars_per_page=500,
396
  first_page_ratio=0.8,
397
  )
398
 
399
+ # 一键复制全文文本
400
  full_text = build_full_text(raw_story_list)
401
  full_text_html = build_full_text_html(full_text)
402
 
 
412
  # ==========================================
413
  # 4. 前端 UI 布局
414
  # ==========================================
415
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="indigo", neutral_hue="slate"), css=custom_css, title="LongStory Agent") as demo:
 
 
 
 
 
416
  # --- 状态管理 ---
417
  story_pages_state = gr.State([]) # 所有的页面 List[Dict]
418
  chapter_indices_state = gr.State([]) # 每一章起始页的 index List[int]
419
  current_page_state = gr.State(0) # 当前页面
420
 
421
+
422
  # --- 顶部标题 ---
423
  with gr.Row(elem_classes=["header-box"]):
424
  gr.HTML("""
 
453
  gr.HTML('<div class="examples-container"><div class="examples-label">⚡ 快速开始 (Quick Inspirations)</div></div>')
454
 
455
  example_prompts = [
456
+ # 青春校园
457
+ ["高二分班那天,全校第一的高���学霸竟然主动申请坐到了全是“差生”的最后一排,成了我的同桌,还递给我一本写满笔记的物理书。"],
458
+ # 玄幻修仙
459
+ ["宗门大比前夕,一直被嘲笑为“杂役弟子”的主角,在后山禁地里误吞了一颗会呼吸的金色龙蛋,觉醒了上古血脉。"],
460
+ # 都市爱情
461
+ ["为了应付家里催婚,我和刚认识不到两小时的陌生人领了证,婚后第一天上班,却发现他竟然是公司新来的那个传闻中冷面无情的顶头上司。"]
462
  ]
463
 
464
  with gr.Column(elem_classes=["examples-table"]):