Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
|
|
|
| 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"{
|
| 176 |
f"Figure description: {step2_en}\n\n"
|
| 177 |
-
"
|
| 178 |
-
"1)
|
| 179 |
-
"2)
|
| 180 |
-
"3)
|
| 181 |
-
"
|
| 182 |
-
"
|
| 183 |
-
"
|
| 184 |
-
"
|
| 185 |
-
"
|
| 186 |
-
"
|
| 187 |
-
"
|
| 188 |
-
"<
|
| 189 |
-
"
|
| 190 |
-
"
|
| 191 |
-
"...caption...\n"
|
| 192 |
-
"<SUM>\n"
|
| 193 |
-
"...summary...\n"
|
| 194 |
)
|
| 195 |
|
| 196 |
multi_out = text_pipe(multi_prompt)[0]["generated_text"]
|
| 197 |
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
else:
|
| 215 |
-
cap_part, sum_part = rest2, ""
|
| 216 |
|
| 217 |
-
explanation_en = dedup_sentences(
|
| 218 |
-
caption_en = dedup_sentences(
|
| 219 |
-
summary_en = dedup_sentences(
|
| 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" #
|
| 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 |
|