DYDYLAN commited on
Commit
dd069b8
ยท
verified ยท
1 Parent(s): 7234f68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -48
app.py CHANGED
@@ -143,7 +143,8 @@ def analyze_and_explain(image, keywords, language, pdf_path):
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"
146
- f"Figure keywords from the user: {keywords}\n\n"
 
147
  "Rewrite this into exactly ONE clear English sentence that starts with "
148
  "'A bar chart showing'. The sentence must:\n"
149
  "- say what is on the horizontal axis in generic terms "
@@ -151,7 +152,7 @@ def analyze_and_explain(image, keywords, language, pdf_path):
151
  "- say what is on the vertical axis in generic terms "
152
  "(e.g. relative bleaching, response intensity);\n"
153
  "- mention what main conditions or treatments are compared "
154
- "(e.g. low vs high temperature);\n"
155
  "- briefly describe the overall trend "
156
  "(e.g. higher temperature causes stronger bleaching).\n\n"
157
  "Do NOT mention study duration, location, or sample size.\n"
@@ -164,59 +165,49 @@ def analyze_and_explain(image, keywords, language, pdf_path):
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 ""
171
- )
172
-
173
  multi_prompt = (
174
  "You are writing about one results figure in a scientific paper.\n\n"
175
- f"{context_part}"
176
  f"Figure description: {step2_en}\n\n"
177
- "Produce three parts:\n"
178
- "1) A Results-style explanation (3-4 sentences) describing the trend.\n"
179
- "2) A concise 1-2 sentence figure caption.\n"
180
- "3) A short 3-4 sentence paragraph for the Results/Discussion section.\n\n"
181
- "Write in formal academic English. Do NOT describe methods, duration, or location. "
182
- "Do NOT invent exact numbers or statistics.\n\n"
183
- "Separate the three parts with the markers:\n"
184
- "<EXPL>\n"
185
- "<CAP>\n"
186
- "<SUM>\n\n"
187
- "Output format:\n"
188
- "<EXPL>\n"
189
- "...explanation...\n"
190
- "<CAP>\n"
191
- "...caption...\n"
192
- "<SUM>\n"
193
- "...summary...\n"
194
  )
195
 
196
  multi_out = text_pipe(multi_prompt)[0]["generated_text"]
197
 
198
- expl_part = ""
199
- cap_part = ""
200
- sum_part = ""
201
-
202
- if "<EXPL>" in multi_out:
203
- _, rest = multi_out.split("<EXPL>", 1)
204
- else:
205
- rest = multi_out
206
-
207
- if "<CAP>" in rest:
208
- expl_part, rest2 = rest.split("<CAP>", 1)
209
- else:
210
- expl_part, rest2 = rest, ""
211
-
212
- if "<SUM>" in rest2:
213
- cap_part, sum_part = rest2.split("<SUM>", 1)
214
- else:
215
- cap_part, sum_part = rest2, ""
216
 
217
- explanation_en = dedup_sentences(expl_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)
@@ -273,7 +264,7 @@ with gr.Blocks() as demo:
273
  )
274
  pdf_in = gr.File(
275
  label="ไธŠไผ ่ฎบๆ–‡ PDF๏ผˆๅฏ้€‰๏ผŒ็”จไบŽ็ป“ๅˆไธŠไธ‹ๆ–‡่งฃ้‡Šๅ›พ่กจ๏ผ‰",
276
- type="filepath" # โœ… ่ฟ™้‡Œๆ”นๆˆ filepath
277
  )
278
  run_btn = gr.Button("ๅˆ†ๆžๅนถ็พŽๅŒ–ๅ›พ่กจ", variant="primary")
279
 
 
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"
146
+ f"Figure keywords from the user: {keywords}\n"
147
+ f"Short paper context: {context_summary}\n\n"
148
  "Rewrite this into exactly ONE clear English sentence that starts with "
149
  "'A bar chart showing'. The sentence must:\n"
150
  "- say what is on the horizontal axis in generic terms "
 
152
  "- say what is on the vertical axis in generic terms "
153
  "(e.g. relative bleaching, response intensity);\n"
154
  "- mention what main conditions or treatments are compared "
155
+ "(e.g. low vs high temperature or CO2);\n"
156
  "- briefly describe the overall trend "
157
  "(e.g. higher temperature causes stronger bleaching).\n\n"
158
  "Do NOT mention study duration, location, or sample size.\n"
 
165
  if not step2_en.lower().startswith("a bar chart showing"):
166
  step2_en = "A bar chart showing " + step2_en.lstrip()
167
 
168
+ # ---- Step 3 + Step 5 ๅˆๅนถ๏ผš1 ๆฌก small T5๏ผŒ่พ“ๅ‡บไธ‰่กŒ ----
 
 
 
 
 
169
  multi_prompt = (
170
  "You are writing about one results figure in a scientific paper.\n\n"
171
+ f"Short paper context: {context_summary}\n"
172
  f"Figure description: {step2_en}\n\n"
173
+ "Write three parts, in formal academic English:\n"
174
+ "1) Explanation: 3-4 sentences describing what the bar chart shows.\n"
175
+ "2) Caption: 1-2 sentences suitable as a figure caption.\n"
176
+ "3) Summary: 3-4 sentences for the Results/Discussion section.\n\n"
177
+ "Rules:\n"
178
+ "- Focus on how the response changes across treatments or coral groups.\n"
179
+ "- Mention which treatment tends to have higher or lower values.\n"
180
+ "- Do NOT describe methods, duration, location, or sample size.\n"
181
+ "- Do NOT invent exact numbers or statistics.\n\n"
182
+ "Output EXACTLY in this format (English):\n"
183
+ "Explanation: <your 3-4 sentence explanation>\n"
184
+ "Caption: <your 1-2 sentence caption>\n"
185
+ "Summary: <your 3-4 sentence summary>\n"
186
+ "Do not add any extra labels or sections."
 
 
 
187
  )
188
 
189
  multi_out = text_pipe(multi_prompt)[0]["generated_text"]
190
 
191
+ # ็”จๆญฃๅˆ™ๆŠŠ Explanation / Caption / Summary ไธ‰ๆฎตๅˆ‡ๅ‡บๆฅ
192
+ exp_match = re.search(
193
+ r"Explanation:\s*(.*?)(?:Caption:|Summary:|$)",
194
+ multi_out,
195
+ flags=re.S
196
+ )
197
+ cap_match = re.search(
198
+ r"Caption:\s*(.*?)(?:Summary:|$)",
199
+ multi_out,
200
+ flags=re.S
201
+ )
202
+ sum_match = re.search(
203
+ r"Summary:\s*(.*)$",
204
+ multi_out,
205
+ flags=re.S
206
+ )
 
 
207
 
208
+ explanation_en = dedup_sentences(exp_match.group(1).strip()) if exp_match else ""
209
+ caption_en = dedup_sentences(cap_match.group(1).strip()) if cap_match else ""
210
+ summary_en = dedup_sentences(sum_match.group(1).strip()) if sum_match else ""
211
 
212
  # ---- Step 4๏ผšๅ›พๅƒๅขžๅผบ ----
213
  enhanced_img = enhance_image_simple(image)
 
264
  )
265
  pdf_in = gr.File(
266
  label="ไธŠไผ ่ฎบๆ–‡ PDF๏ผˆๅฏ้€‰๏ผŒ็”จไบŽ็ป“ๅˆไธŠไธ‹ๆ–‡่งฃ้‡Šๅ›พ่กจ๏ผ‰",
267
+ type="filepath" # ่ฟ™้‡Œๅฟ…้กปๆ˜ฏ 'filepath'
268
  )
269
  run_btn = gr.Button("ๅˆ†ๆžๅนถ็พŽๅŒ–ๅ›พ่กจ", variant="primary")
270