DavidWill commited on
Commit
0b8a894
·
verified ·
1 Parent(s): a386dd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -31
app.py CHANGED
@@ -1,23 +1,20 @@
1
 
2
- # app.py —— 完整修正版(現代中文故事生成器)
3
  from transformers import pipeline, set_seed
4
  import gradio as gr
5
  import torch
6
  import re
7
 
8
- # 🎯 使用現代中文訓練的模型(CLUE 語料,含新聞/百科/論壇)
9
  MODEL_NAME = "uer/gpt2-chinese-cluecorpussmall"
10
 
11
- # 安全載入模型
12
  generator = pipeline(
13
  "text-generation",
14
  model=MODEL_NAME,
15
- device=-1, # 強制使用 CPU
16
  torch_dtype=torch.float32
17
  )
18
 
19
  def is_mostly_chinese(text, threshold=0.7):
20
- """檢查文字是否至少 70% 是中文字或中文標點"""
21
  if not text.strip():
22
  return False
23
  total = len(text)
@@ -26,24 +23,24 @@ def is_mostly_chinese(text, threshold=0.7):
26
  )
27
  return (chinese_count / total) >= threshold if total > 0 else False
28
 
29
- def generate_story(starter, max_length=80, temperature=0.9):
30
  if not starter.strip():
31
  return "請輸入一個故事開頭喔!"
32
 
33
- # ✅ 關鍵:用「現代童話」風格引導,明確要求「迪士尼風格」
34
  prompt = f"""請用現代繁體中文寫一個迪士尼風格的童話故事。
35
  開頭:「{starter}」
36
- 要求:60字以內,有魔法、有動物朋友、結尾溫馨。
37
  故事:"""
38
 
39
  try:
40
- set_seed(42) # 固定種子,讓結果較穩定
41
  outputs = generator(
42
  prompt,
43
- max_new_tokens=min(int(max_length), 300),
44
  temperature=float(temperature),
45
  do_sample=True,
46
- top_k=60, # 提高創意
47
  top_p=0.9,
48
  repetition_penalty=1.2,
49
  pad_token_id=generator.tokenizer.eos_token_id
@@ -51,59 +48,52 @@ def generate_story(starter, max_length=80, temperature=0.9):
51
 
52
  full_text = outputs[0]['generated_text']
53
 
54
- # 提取「故事:」之後的內容
55
  if "故事:" in full_text:
56
  story = full_text.split("故事:", 1)[-1].strip()
57
  else:
58
  story = full_text.replace(prompt, "").strip()
59
 
60
- # 清理多餘空格與符號
61
  story = re.sub(r'\s+', ' ', story).strip()
62
  story = re.sub(r'[「」]+', '', story)
63
- story = story.split('\n')[0] # 只取第一句,避免後面亂加
64
 
65
- # 防呆:內容太短或非中文
66
- if len(story) < 10 or not is_mostly_chinese(story):
67
- return "AI 今天靈感枯竭... 試試換個開頭或調高『創意溫度』!"
68
 
69
  return story
70
 
71
  except Exception as e:
72
- return f"❌ 錯誤:{str(e)}\n建議降低『最大長度』或重試"
73
 
74
- # 建立 Gradio 介面
75
  demo = gr.Interface(
76
  fn=generate_story,
77
  inputs=[
78
  gr.Textbox(
79
  lines=2,
80
- placeholder="例如:迪士尼公主的魔法口紅不見了...",
81
  label="📖 輸入你的故事開頭"
82
  ),
83
- gr.Slider(40, 300, value=70, step=10, label="📏 故事長度"),
84
  gr.Slider(0.8, 1.3, value=1.0, step=0.1, label="🎨 創意溫度")
85
  ],
86
  outputs=gr.Textbox(
87
  label="📜 AI 生成的童話故事",
88
- lines=8,
89
- max_lines=20,
90
- placeholder="魔法故事即將誕生...",
91
  autoscroll=True
92
  ),
93
- title="🐉 現代中文 AI 說書人",
94
- description="✨ 專為「迪士尼」「現代詞彙」優化!輸入開頭,AI 幫你寫出溫馨奇幻故事~",
95
  examples=[
96
- ["迪士尼公主的魔法口紅不見了...", 70, 1.0],
97
- ["皮卡丘開了一家深夜拉麵店...", 80, 1.1],
98
- ["我的手機殼裡住著一隻小精靈...", 75, 0.9]
99
  ],
100
  theme=gr.themes.Soft()
101
  )
102
 
103
- # 可選:加大字體
104
  demo.css = """
105
  .output-text { font-size: 18px !important; line-height: 1.8; }
106
- .gr-button-primary { background: #ff6b6b !important; }
107
  """
108
 
109
  if __name__ == "__main__":
 
1
 
2
+
3
  from transformers import pipeline, set_seed
4
  import gradio as gr
5
  import torch
6
  import re
7
 
 
8
  MODEL_NAME = "uer/gpt2-chinese-cluecorpussmall"
9
 
 
10
  generator = pipeline(
11
  "text-generation",
12
  model=MODEL_NAME,
13
+ device=-1,
14
  torch_dtype=torch.float32
15
  )
16
 
17
  def is_mostly_chinese(text, threshold=0.7):
 
18
  if not text.strip():
19
  return False
20
  total = len(text)
 
23
  )
24
  return (chinese_count / total) >= threshold if total > 0 else False
25
 
26
+ def generate_story(starter, max_length=100, temperature=0.9):
27
  if not starter.strip():
28
  return "請輸入一個故事開頭喔!"
29
 
30
+ # ✅ 要求更長、更豐富的故事
31
  prompt = f"""請用現代繁體中文寫一個迪士尼風格的童話故事。
32
  開頭:「{starter}」
33
+ 要求:150字以內,要有魔法、動物朋友、情節轉折、溫馨結局。
34
  故事:"""
35
 
36
  try:
37
+ set_seed(42)
38
  outputs = generator(
39
  prompt,
40
+ max_new_tokens=int(max_length), # ✅ 完全跟隨滑桿值
41
  temperature=float(temperature),
42
  do_sample=True,
43
+ top_k=60,
44
  top_p=0.9,
45
  repetition_penalty=1.2,
46
  pad_token_id=generator.tokenizer.eos_token_id
 
48
 
49
  full_text = outputs[0]['generated_text']
50
 
 
51
  if "故事:" in full_text:
52
  story = full_text.split("故事:", 1)[-1].strip()
53
  else:
54
  story = full_text.replace(prompt, "").strip()
55
 
 
56
  story = re.sub(r'\s+', ' ', story).strip()
57
  story = re.sub(r'[「」]+', '', story)
 
58
 
59
+ if len(story) < 20 or not is_mostly_chinese(story): # ✅ 放寬最小字數
60
+ return "AI 今天文思泉湧... 試試調高『創意溫度』或再按一次!"
 
61
 
62
  return story
63
 
64
  except Exception as e:
65
+ return f"❌ 錯誤:{str(e)}\n建議降低長度或重試"
66
 
 
67
  demo = gr.Interface(
68
  fn=generate_story,
69
  inputs=[
70
  gr.Textbox(
71
  lines=2,
72
+ placeholder="例如:小矮人發現了一張藏寶圖,指向雲端的糖果城堡...",
73
  label="📖 輸入你的故事開頭"
74
  ),
75
+ gr.Slider(50, 200, value=120, step=10, label="📏 故事長度(token數)"), # ✅ 最高 200
76
  gr.Slider(0.8, 1.3, value=1.0, step=0.1, label="🎨 創意溫度")
77
  ],
78
  outputs=gr.Textbox(
79
  label="📜 AI 生成的童話故事",
80
+ lines=12, # ✅ 增加顯示行數
81
+ max_lines=30, # ✅ 增加最大行數
82
+ placeholder="長篇魔法故事即將誕生...",
83
  autoscroll=True
84
  ),
85
+ title="🐉 長篇中文 AI 說書人",
86
+ description="✨ 支援更長故事!輸入開頭,AI 幫你寫出情節豐富的奇幻冒險~",
87
  examples=[
88
+ ["小矮人發現了一張藏寶圖,指向雲端的糖果城堡...", 150, 1.1],
89
+ ["會說話的雲朵邀請我去參加天空動物園的開幕典禮...", 180, 1.2],
90
+ ["我的鬧鐘其實是時光機,每天早上帶我去不同年代...", 160, 1.0]
91
  ],
92
  theme=gr.themes.Soft()
93
  )
94
 
 
95
  demo.css = """
96
  .output-text { font-size: 18px !important; line-height: 1.8; }
 
97
  """
98
 
99
  if __name__ == "__main__":