DYDYLAN commited on
Commit
7234f68
·
verified ·
1 Parent(s): 3e0ae5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -44
app.py CHANGED
@@ -8,14 +8,14 @@ from pypdf import PdfReader
8
  # 1. 加载模型(CPU Basic 友好)
9
  # =========================================================
10
 
11
- # 图像 → 文本:BLIP,比 vit-gpt2 准,不依赖 sentencepiece
12
  vision_pipe = pipeline(
13
  "image-to-text",
14
  model="salesforce/blip-image-captioning-base",
15
  device=-1 # CPU
16
  )
17
 
18
- # 文本生成:用 flan-t5-small(更快)统一做描述/解释/翻译
19
  text_pipe = pipeline(
20
  "text2text-generation",
21
  model="google/flan-t5-small",
@@ -25,20 +25,14 @@ text_pipe = pipeline(
25
 
26
 
27
  # =========================================================
28
- # 2. 一些小工具函数
29
  # =========================================================
30
 
31
  def dedup_sentences(text: str) -> str:
32
- """
33
- 简单去重复:
34
- - 按句号/问号/感叹号拆分
35
- - 去掉重复句子(忽略大小写和多余空格)
36
- - 防止 caption / summary 疯狂复读同一句话
37
- """
38
  if not text:
39
  return text
40
 
41
- # 先粗略按中英文句号拆分
42
  parts = re.split(r'(?<=[。!?!?\.])\s+', text.strip())
43
  seen = set()
44
  result = []
@@ -54,22 +48,22 @@ def dedup_sentences(text: str) -> str:
54
  result.append(s_clean)
55
 
56
  out = ' '.join(result)
57
- # 太长就截断一点,避免刷屏
58
  if len(out) > 1200:
59
  out = out[:1200]
60
  return out
61
 
62
 
63
- def extract_pdf_context(pdf_file, keywords: str, max_chars: int = 1200) -> str:
64
  """
65
  从上传的 PDF 中抽取和关键词最相关的几段文字,
66
  再作为 figure 的上下文摘要使用。
 
67
  """
68
- if pdf_file is None:
69
  return ""
70
 
71
  try:
72
- reader = PdfReader(pdf_file.name)
73
  pages_text = []
74
  for page in reader.pages:
75
  txt = page.extract_text() or ""
@@ -81,14 +75,9 @@ def extract_pdf_context(pdf_file, keywords: str, max_chars: int = 1200) -> str:
81
  if not full_text.strip():
82
  return ""
83
 
84
- # 简单按空行拆分段落
85
  paragraphs = [p.strip() for p in re.split(r'\n\s*\n', full_text) if p.strip()]
86
 
87
- # 关键词打分(非常朴素,但够用)
88
  kw_list = [k.strip().lower() for k in keywords.split(",") if k.strip()]
89
- if not kw_list:
90
- kw_list = []
91
-
92
  scored = []
93
  for p in paragraphs:
94
  pl = p.lower()
@@ -96,13 +85,11 @@ def extract_pdf_context(pdf_file, keywords: str, max_chars: int = 1200) -> str:
96
  for kw in kw_list:
97
  if kw in pl:
98
  score += 1
99
- # 略微偏向包含 "fig" 的段落
100
  if "fig" in pl or "figure" in pl:
101
  score += 1
102
  if score > 0:
103
  scored.append((score, p))
104
 
105
- # 如果没匹配到关键词,就拿前几段凑合
106
  if not scored:
107
  top_text = "\n\n".join(paragraphs[:3])
108
  else:
@@ -110,11 +97,9 @@ def extract_pdf_context(pdf_file, keywords: str, max_chars: int = 1200) -> str:
110
  top_paras = [p for _, p in scored[:3]]
111
  top_text = "\n\n".join(top_paras)
112
 
113
- # 控制长度
114
  if len(top_text) > max_chars:
115
  top_text = top_text[:max_chars]
116
 
117
- # 用 T5 生成一个 2–3 句的简短 summary
118
  prompt = (
119
  "You are summarizing background for one figure in a scientific paper.\n\n"
120
  f"Keywords: {', '.join(kw_list) if kw_list else 'N/A'}\n\n"
@@ -128,10 +113,7 @@ def extract_pdf_context(pdf_file, keywords: str, max_chars: int = 1200) -> str:
128
 
129
 
130
  def enhance_image_simple(image: Image.Image) -> Image.Image:
131
- """
132
- 简单地提升亮度和对比度,用来生成“增强版图表”。
133
- 不用 Diffusion,轻量、可在 CPU Basic 上跑。
134
- """
135
  if image is None:
136
  return None
137
  img = image.convert("RGB")
@@ -141,25 +123,23 @@ def enhance_image_simple(image: Image.Image) -> Image.Image:
141
 
142
 
143
  # =========================================================
144
- # 3. 主函数:一次点击完成所有步骤
145
- # Step 2:1 次 small T5
146
- # Step 3 + Caption + Summary:合并成 1 次 small T5
147
  # =========================================================
148
 
149
- def analyze_and_explain(image, keywords, language, pdf_file):
150
  if image is None:
151
  return None, "", "", None, "", ""
152
 
153
- # ---- Step 1: Vision 模型粗略描述图像 ----
154
  try:
155
  raw_caption = vision_pipe(image)[0]["generated_text"]
156
  except Exception:
157
  raw_caption = ""
158
 
159
- # ---- PDF 中抽取上下文(可选) ----
160
- context_summary = extract_pdf_context(pdf_file, keywords or "")
161
 
162
- # ---- Step 2: 自动图表描述(图表 QA + 论文上下文 ----
163
  step2_prompt = (
164
  "You are helping to describe one bar chart in a scientific paper.\n\n"
165
  f"Rough visual caption from an image model: {raw_caption}\n"
@@ -181,12 +161,10 @@ def analyze_and_explain(image, keywords, language, pdf_file):
181
  )
182
  step2_en = text_pipe(step2_prompt)[0]["generated_text"].strip()
183
  step2_en = dedup_sentences(step2_en)
184
-
185
- # 保底:强制以 A bar chart showing 开头
186
  if not step2_en.lower().startswith("a bar chart showing"):
187
  step2_en = "A bar chart showing " + step2_en.lstrip()
188
 
189
- # ---- Step 3 + Step 5:生成 Explanation + Caption + Summary ----
190
  context_part = (
191
  f"Short paper context: {context_summary}\n\n"
192
  if context_summary else ""
@@ -217,7 +195,6 @@ def analyze_and_explain(image, keywords, language, pdf_file):
217
 
218
  multi_out = text_pipe(multi_prompt)[0]["generated_text"]
219
 
220
- # 解析三段
221
  expl_part = ""
222
  cap_part = ""
223
  sum_part = ""
@@ -241,10 +218,10 @@ def analyze_and_explain(image, keywords, language, pdf_file):
241
  caption_en = dedup_sentences(cap_part)
242
  summary_en = dedup_sentences(sum_part)
243
 
244
- # ---- Step 4: 简单增强后的图表 ----
245
  enhanced_img = enhance_image_simple(image)
246
 
247
- # ---- 如果选择中文,则翻译三段文本 ----
248
  if language == "中文":
249
  def translate_to_zh(txt):
250
  if not txt:
@@ -279,7 +256,7 @@ with gr.Blocks() as demo:
279
  gr.Markdown("## ChartSmith – AI 论文图表生成助手 (v2)")
280
 
281
  with gr.Row():
282
- # 左侧:输入
283
  with gr.Column():
284
  img_in = gr.Image(
285
  type="pil",
@@ -296,11 +273,11 @@ with gr.Blocks() as demo:
296
  )
297
  pdf_in = gr.File(
298
  label="上传论文 PDF(可选,用于结合上下文解释图表)",
299
- type="file"
300
  )
301
  run_btn = gr.Button("分析并美化图表", variant="primary")
302
 
303
- # 右侧:输出
304
  with gr.Column():
305
  orig_img = gr.Image(label="原始图表预览", interactive=False)
306
 
 
8
  # 1. 加载模型(CPU Basic 友好)
9
  # =========================================================
10
 
11
+ # 图像 → 文本:BLIP,比 vit-gpt2 稳定
12
  vision_pipe = pipeline(
13
  "image-to-text",
14
  model="salesforce/blip-image-captioning-base",
15
  device=-1 # CPU
16
  )
17
 
18
+ # 文本生成:small T5用来做描述/解释/翻译
19
  text_pipe = pipeline(
20
  "text2text-generation",
21
  model="google/flan-t5-small",
 
25
 
26
 
27
  # =========================================================
28
+ # 2. 工具函数
29
  # =========================================================
30
 
31
  def dedup_sentences(text: str) -> str:
32
+ """按句子粗糙去重复,避免疯狂复读。"""
 
 
 
 
 
33
  if not text:
34
  return text
35
 
 
36
  parts = re.split(r'(?<=[。!?!?\.])\s+', text.strip())
37
  seen = set()
38
  result = []
 
48
  result.append(s_clean)
49
 
50
  out = ' '.join(result)
 
51
  if len(out) > 1200:
52
  out = out[:1200]
53
  return out
54
 
55
 
56
+ def extract_pdf_context(pdf_path: str, keywords: str, max_chars: int = 1200) -> str:
57
  """
58
  从上传的 PDF 中抽取和关键词最相关的几段文字,
59
  再作为 figure 的上下文摘要使用。
60
+ pdf_path 是文件路径字符串(来自 gr.File, type="filepath")。
61
  """
62
+ if not pdf_path:
63
  return ""
64
 
65
  try:
66
+ reader = PdfReader(pdf_path)
67
  pages_text = []
68
  for page in reader.pages:
69
  txt = page.extract_text() or ""
 
75
  if not full_text.strip():
76
  return ""
77
 
 
78
  paragraphs = [p.strip() for p in re.split(r'\n\s*\n', full_text) if p.strip()]
79
 
 
80
  kw_list = [k.strip().lower() for k in keywords.split(",") if k.strip()]
 
 
 
81
  scored = []
82
  for p in paragraphs:
83
  pl = p.lower()
 
85
  for kw in kw_list:
86
  if kw in pl:
87
  score += 1
 
88
  if "fig" in pl or "figure" in pl:
89
  score += 1
90
  if score > 0:
91
  scored.append((score, p))
92
 
 
93
  if not scored:
94
  top_text = "\n\n".join(paragraphs[:3])
95
  else:
 
97
  top_paras = [p for _, p in scored[:3]]
98
  top_text = "\n\n".join(top_paras)
99
 
 
100
  if len(top_text) > max_chars:
101
  top_text = top_text[:max_chars]
102
 
 
103
  prompt = (
104
  "You are summarizing background for one figure in a scientific paper.\n\n"
105
  f"Keywords: {', '.join(kw_list) if kw_list else 'N/A'}\n\n"
 
113
 
114
 
115
  def enhance_image_simple(image: Image.Image) -> Image.Image:
116
+ """简单增强亮度和对比度,生成 Step 4 图像。"""
 
 
 
117
  if image is None:
118
  return None
119
  img = image.convert("RGB")
 
123
 
124
 
125
  # =========================================================
126
+ # 3. 主逻辑函数
 
 
127
  # =========================================================
128
 
129
+ def analyze_and_explain(image, keywords, language, pdf_path):
130
  if image is None:
131
  return None, "", "", None, "", ""
132
 
133
+ # ---- Vision 初步 caption ----
134
  try:
135
  raw_caption = vision_pipe(image)[0]["generated_text"]
136
  except Exception:
137
  raw_caption = ""
138
 
139
+ # ---- PDF 上下文 ----
140
+ context_summary = extract_pdf_context(pdf_path, keywords or "")
141
 
142
+ # ---- Step 2自动图表描述(1 small T5)----
143
  step2_prompt = (
144
  "You are helping to describe one bar chart in a scientific paper.\n\n"
145
  f"Rough visual caption from an image model: {raw_caption}\n"
 
161
  )
162
  step2_en = text_pipe(step2_prompt)[0]["generated_text"].strip()
163
  step2_en = dedup_sentences(step2_en)
 
 
164
  if not step2_en.lower().startswith("a bar chart showing"):
165
  step2_en = "A bar chart showing " + step2_en.lstrip()
166
 
167
+ # ---- Step 3 + Step 5 合并1 small T5 ----
168
  context_part = (
169
  f"Short paper context: {context_summary}\n\n"
170
  if context_summary else ""
 
195
 
196
  multi_out = text_pipe(multi_prompt)[0]["generated_text"]
197
 
 
198
  expl_part = ""
199
  cap_part = ""
200
  sum_part = ""
 
218
  caption_en = dedup_sentences(cap_part)
219
  summary_en = dedup_sentences(sum_part)
220
 
221
+ # ---- Step 4:图像增强 ----
222
  enhanced_img = enhance_image_simple(image)
223
 
224
+ # ---- 中切换 ----
225
  if language == "中文":
226
  def translate_to_zh(txt):
227
  if not txt:
 
256
  gr.Markdown("## ChartSmith – AI 论文图表生成助手 (v2)")
257
 
258
  with gr.Row():
259
+ # 左侧:输入
260
  with gr.Column():
261
  img_in = gr.Image(
262
  type="pil",
 
273
  )
274
  pdf_in = gr.File(
275
  label="上传论文 PDF(可选,用于结合上下文解释图表)",
276
+ type="filepath" # ✅ 这里改成 filepath
277
  )
278
  run_btn = gr.Button("分析并美化图表", variant="primary")
279
 
280
+ # 右侧:输出
281
  with gr.Column():
282
  orig_img = gr.Image(label="原始图表预览", interactive=False)
283