Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -94,45 +94,61 @@ def analyze_and_enhance(image, style, language, paper_file):
|
|
| 94 |
if image is None:
|
| 95 |
return None, "请先上传图表。", "", None, "", ""
|
| 96 |
|
| 97 |
-
#
|
| 98 |
full_text = extract_full_text_from_pdf(paper_file)
|
| 99 |
-
|
| 100 |
-
raw_caption_tmp = vision_pipe(image)[0]["generated_text"]
|
| 101 |
snippet = select_relevant_snippets(full_text, raw_caption_tmp) if full_text else ""
|
| 102 |
paper_context = summarise_context_for_figure(snippet) if snippet else ""
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
|
|
|
| 112 |
|
|
|
|
|
|
|
| 113 |
prompt_academic = (
|
| 114 |
"You are writing the Results section of a scientific paper.\n\n"
|
| 115 |
f"{context_part}"
|
| 116 |
f"Figure description: {raw_caption}\n\n"
|
| 117 |
-
"
|
| 118 |
-
"
|
| 119 |
-
"
|
| 120 |
-
"
|
| 121 |
-
"
|
| 122 |
-
"- which group appears more sensitive or shows a stronger change, if any.\n"
|
| 123 |
-
"Do not invent exact numbers. Answer directly with the explanation:"
|
| 124 |
)
|
| 125 |
academic_desc = text_pipe(prompt_academic)[0]["generated_text"]
|
| 126 |
|
| 127 |
-
#
|
| 128 |
caption_prompt = (
|
| 129 |
-
"Based on the explanation below, write a 1–2 sentence figure caption for a
|
| 130 |
-
"Mention that this is a bar chart and highlight the main
|
|
|
|
| 131 |
f"{academic_desc}\n\nCaption:"
|
| 132 |
)
|
| 133 |
caption_text = text_pipe(caption_prompt)[0]["generated_text"]
|
| 134 |
|
| 135 |
-
#
|
| 136 |
summary_prompt = (
|
| 137 |
"Rewrite the following explanation as a short paragraph (3–4 sentences) "
|
| 138 |
"for the Results section, highlighting the main pattern and conclusion. "
|
|
@@ -141,7 +157,7 @@ def analyze_and_enhance(image, style, language, paper_file):
|
|
| 141 |
)
|
| 142 |
summary_text = text_pipe(summary_prompt)[0]["generated_text"]
|
| 143 |
|
| 144 |
-
# 语言切换
|
| 145 |
if language == "中文":
|
| 146 |
academic_desc_out = text_pipe(
|
| 147 |
f"Translate the following academic explanation into Chinese:\n{academic_desc}"
|
|
@@ -155,11 +171,12 @@ def analyze_and_enhance(image, style, language, paper_file):
|
|
| 155 |
else:
|
| 156 |
academic_desc_out = academic_desc
|
| 157 |
|
| 158 |
-
#
|
| 159 |
img_rgb = image.convert("RGB")
|
| 160 |
img_c = ImageEnhance.Contrast(img_rgb).enhance(1.4)
|
| 161 |
enhanced_image = ImageEnhance.Brightness(img_c).enhance(1.1)
|
| 162 |
|
|
|
|
| 163 |
return image, raw_caption, academic_desc_out, enhanced_image, caption_text, summary_text
|
| 164 |
|
| 165 |
|
|
|
|
| 94 |
if image is None:
|
| 95 |
return None, "请先上传图表。", "", None, "", ""
|
| 96 |
|
| 97 |
+
# ===== 0. 论文上下文 =====
|
| 98 |
full_text = extract_full_text_from_pdf(paper_file)
|
| 99 |
+
raw_caption_tmp = vision_pipe(image)[0]["generated_text"] # 只当内部信息
|
|
|
|
| 100 |
snippet = select_relevant_snippets(full_text, raw_caption_tmp) if full_text else ""
|
| 101 |
paper_context = summarise_context_for_figure(snippet) if snippet else ""
|
| 102 |
|
| 103 |
+
# ===== 1. 用论文上下文 + 常识来写 Step 2 的描述 =====
|
| 104 |
+
if paper_context:
|
| 105 |
+
figdesc_prompt = (
|
| 106 |
+
"You are given the context of a scientific study.\n\n"
|
| 107 |
+
f"{paper_context}\n\n"
|
| 108 |
+
"In ONE sentence starting with 'A bar chart showing', describe what a "
|
| 109 |
+
"typical results figure in this study would plot on the horizontal and "
|
| 110 |
+
"vertical axes and which treatments or groups are compared.\n\n"
|
| 111 |
+
"Sentence:"
|
| 112 |
+
)
|
| 113 |
+
display_caption = text_pipe(figdesc_prompt)[0]["generated_text"]
|
| 114 |
+
# 防止它没按要求开头
|
| 115 |
+
if not display_caption.lower().startswith("a bar chart"):
|
| 116 |
+
display_caption = "A bar chart showing " + display_caption.lstrip()
|
| 117 |
+
else:
|
| 118 |
+
# 没有论文就给一个通用的安全句子
|
| 119 |
+
display_caption = (
|
| 120 |
+
"A bar chart showing the response of several experimental groups under "
|
| 121 |
+
"increasing levels of environmental stress."
|
| 122 |
+
)
|
| 123 |
|
| 124 |
+
# 下面我们不再依赖 vision 的“钟楼 caption”,
|
| 125 |
+
# 而是用刚才生成的 display_caption 作为 figure description
|
| 126 |
+
raw_caption = display_caption
|
| 127 |
|
| 128 |
+
# ===== 2. 学术化解释 =====
|
| 129 |
+
context_part = f"Paper context: {paper_context}\n\n" if paper_context else ""
|
| 130 |
prompt_academic = (
|
| 131 |
"You are writing the Results section of a scientific paper.\n\n"
|
| 132 |
f"{context_part}"
|
| 133 |
f"Figure description: {raw_caption}\n\n"
|
| 134 |
+
"In 3–4 sentences, explain what this bar chart shows using formal academic "
|
| 135 |
+
"English. Describe in generic terms what is on the vertical axis, how the "
|
| 136 |
+
"bars change from control to stronger treatments, and which group appears "
|
| 137 |
+
"more sensitive if any. Do not invent exact numbers.\n\n"
|
| 138 |
+
"Explanation:"
|
|
|
|
|
|
|
| 139 |
)
|
| 140 |
academic_desc = text_pipe(prompt_academic)[0]["generated_text"]
|
| 141 |
|
| 142 |
+
# ===== 3. Caption =====
|
| 143 |
caption_prompt = (
|
| 144 |
+
"Based on the explanation below, write a 1–2 sentence figure caption for a "
|
| 145 |
+
"scientific paper. Mention that this is a bar chart and highlight the main "
|
| 146 |
+
"comparison or trend.\n\n"
|
| 147 |
f"{academic_desc}\n\nCaption:"
|
| 148 |
)
|
| 149 |
caption_text = text_pipe(caption_prompt)[0]["generated_text"]
|
| 150 |
|
| 151 |
+
# ===== 4. Summary / 讨论 =====
|
| 152 |
summary_prompt = (
|
| 153 |
"Rewrite the following explanation as a short paragraph (3–4 sentences) "
|
| 154 |
"for the Results section, highlighting the main pattern and conclusion. "
|
|
|
|
| 157 |
)
|
| 158 |
summary_text = text_pipe(summary_prompt)[0]["generated_text"]
|
| 159 |
|
| 160 |
+
# ===== 5. 语言切换 =====
|
| 161 |
if language == "中文":
|
| 162 |
academic_desc_out = text_pipe(
|
| 163 |
f"Translate the following academic explanation into Chinese:\n{academic_desc}"
|
|
|
|
| 171 |
else:
|
| 172 |
academic_desc_out = academic_desc
|
| 173 |
|
| 174 |
+
# ===== 6. 图像增强 =====
|
| 175 |
img_rgb = image.convert("RGB")
|
| 176 |
img_c = ImageEnhance.Contrast(img_rgb).enhance(1.4)
|
| 177 |
enhanced_image = ImageEnhance.Brightness(img_c).enhance(1.1)
|
| 178 |
|
| 179 |
+
# 注意这里输出的是 display_caption,而不是 vision 的原始结果
|
| 180 |
return image, raw_caption, academic_desc_out, enhanced_image, caption_text, summary_text
|
| 181 |
|
| 182 |
|