Yangyang1205 commited on
Commit
c0a0dd2
·
verified ·
1 Parent(s): 5524328

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -15
app.py CHANGED
@@ -13,7 +13,7 @@ def load_model():
13
  global model_loaded, load_error_msg
14
  print(f"🚀 正在启动... 准备加载模型: {MODEL_ID}")
15
  try:
16
- # 1. 强制修正 Config (保持之前的修复)
17
  config = AutoConfig.from_pretrained(MODEL_ID)
18
  config.tie_word_embeddings = True
19
 
@@ -58,18 +58,14 @@ def generate_text(prompt, max_len, temp):
58
  # 解码全部内容
59
  full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
60
 
61
- # --- ✨ 优化显示逻辑 ✨ ---
62
- # 1. 只提取【新增】的内容,不要把原来的 Prompt 再显示一遍
63
- # 这样右边的框里就只有 "Paris.",看起来更清爽
64
  new_text = full_response[len(prompt):]
65
 
66
- # 2. 尝试去掉最后那个没说完的话 (如果有换行符)
67
- # 如果模型写了 "Paris.\nThe capital of US is", 我们只保留 "Paris."
68
  if "\n" in new_text.strip():
69
- # 取第一行非空的内容
70
  lines = [line for line in new_text.split('\n') if line.strip()]
71
  if lines:
72
- return lines[0] # 只返回第一行
73
 
74
  return new_text
75
 
@@ -77,7 +73,8 @@ def generate_text(prompt, max_len, temp):
77
  return str(e)
78
 
79
  # --- ✨ 全新 UI 布局 (Blocks) ✨ ---
80
- with gr.Blocks(theme="soft", title="MobileLLM 80M Demo") as demo:
 
81
 
82
  # 标题区域
83
  gr.Markdown(
@@ -93,24 +90,24 @@ with gr.Blocks(theme="soft", title="MobileLLM 80M Demo") as demo:
93
  with gr.Column():
94
  input_box = gr.Textbox(
95
  label="输入 Prompt (提示词)",
96
- lines=8, # 高度设大一点
97
  placeholder="在这里输入排比句...",
98
  value="The capital of China is Beijing.\nThe capital of Japan is Tokyo.\nThe capital of Germany is Berlin.\nThe capital of France is"
99
  )
100
 
101
- # 高级参数折叠区
102
- with gr.Accordion("⚙️ 高级参数 (点我展开)", open=False):
103
  slider_len = gr.Slider(minimum=1, maximum=100, value=20, label="生成长度", step=1)
104
  slider_temp = gr.Slider(minimum=0.1, maximum=1.0, value=0.6, label="温度 (创造力)", step=0.1)
105
 
106
- submit_btn = gr.Button("🚀 开始生成", variant="primary", scale=1)
107
 
108
  # 右边:输出区
109
  with gr.Column():
110
  output_box = gr.Textbox(
111
  label="模型续写结果",
112
- lines=8, # 高度设大一点,防止显示不全
113
- interactive=False # 输出框不可编辑
114
  )
115
 
116
  # 绑定点击事件
 
13
  global model_loaded, load_error_msg
14
  print(f"🚀 正在启动... 准备加载模型: {MODEL_ID}")
15
  try:
16
+ # 1. 强制修正 Config
17
  config = AutoConfig.from_pretrained(MODEL_ID)
18
  config.tie_word_embeddings = True
19
 
 
58
  # 解码全部内容
59
  full_response = tokenizer.decode(outputs[0], skip_special_tokens=True)
60
 
61
+ # 结果清洗:去掉输入部分
 
 
62
  new_text = full_response[len(prompt):]
63
 
64
+ # 去掉的句
 
65
  if "\n" in new_text.strip():
 
66
  lines = [line for line in new_text.split('\n') if line.strip()]
67
  if lines:
68
+ return lines[0]
69
 
70
  return new_text
71
 
 
73
  return str(e)
74
 
75
  # --- ✨ 全新 UI 布局 (Blocks) ✨ ---
76
+ # ⚠️ 修复点:去掉了 theme title 参数,防止报错
77
+ with gr.Blocks() as demo:
78
 
79
  # 标题区域
80
  gr.Markdown(
 
90
  with gr.Column():
91
  input_box = gr.Textbox(
92
  label="输入 Prompt (提示词)",
93
+ lines=10,
94
  placeholder="在这里输入排比句...",
95
  value="The capital of China is Beijing.\nThe capital of Japan is Tokyo.\nThe capital of Germany is Berlin.\nThe capital of France is"
96
  )
97
 
98
+ # 高级参数
99
+ with gr.Accordion("⚙️ 高级参数", open=False):
100
  slider_len = gr.Slider(minimum=1, maximum=100, value=20, label="生成长度", step=1)
101
  slider_temp = gr.Slider(minimum=0.1, maximum=1.0, value=0.6, label="温度 (创造力)", step=0.1)
102
 
103
+ submit_btn = gr.Button("🚀 开始生成", variant="primary")
104
 
105
  # 右边:输出区
106
  with gr.Column():
107
  output_box = gr.Textbox(
108
  label="模型续写结果",
109
+ lines=10,
110
+ interactive=False
111
  )
112
 
113
  # 绑定点击事件