Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,93 +3,140 @@ from transformers import pipeline
|
|
| 3 |
from PIL import Image, ImageEnhance
|
| 4 |
from pypdf import PdfReader
|
| 5 |
|
| 6 |
-
# ========== 1.
|
|
|
|
| 7 |
vision_pipe = pipeline(
|
| 8 |
"image-to-text",
|
| 9 |
-
model="nlpconnect/vit-gpt2-image-captioning",
|
| 10 |
device=-1
|
| 11 |
)
|
| 12 |
|
| 13 |
text_pipe = pipeline(
|
| 14 |
"text2text-generation",
|
| 15 |
-
model="google/flan-t5-small",
|
| 16 |
max_length=256,
|
| 17 |
device=-1
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# ========== 2. 论文 PDF
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
if paper_file is None:
|
| 23 |
return ""
|
| 24 |
try:
|
| 25 |
path = paper_file if isinstance(paper_file, str) else paper_file.name
|
| 26 |
reader = PdfReader(path)
|
| 27 |
text = ""
|
| 28 |
-
for page in reader.pages[:
|
| 29 |
page_text = page.extract_text() or ""
|
| 30 |
text += page_text + "\n"
|
| 31 |
-
|
| 32 |
-
return text[:3000] # 截短一点,避免太长
|
| 33 |
except Exception as e:
|
| 34 |
print("PDF error:", e)
|
| 35 |
return ""
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
return ""
|
|
|
|
| 40 |
prompt = (
|
| 41 |
-
"
|
| 42 |
-
"In 4
|
| 43 |
-
"
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
return text_pipe(prompt)[0]["generated_text"]
|
| 47 |
|
|
|
|
| 48 |
# ========== 3. 核心工作流 ==========
|
|
|
|
| 49 |
def analyze_and_enhance(image, style, language, paper_file):
|
| 50 |
if image is None:
|
| 51 |
return None, "请先上传图表。", "", None, "", ""
|
| 52 |
|
| 53 |
-
# 论文上下文
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
# Step 2:
|
| 58 |
-
raw_caption =
|
| 59 |
rc = raw_caption.lower()
|
| 60 |
-
if not any(k in rc for k in ["bar", "chart", "graph", "plot"
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
f"Paper context: {paper_context}\n\n"
|
| 67 |
-
if paper_context else
|
| 68 |
-
""
|
| 69 |
-
)
|
| 70 |
prompt_academic = (
|
| 71 |
"You are writing the Results section of a scientific paper.\n\n"
|
| 72 |
f"{context_part}"
|
| 73 |
f"Figure description: {raw_caption}\n\n"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
)
|
| 78 |
academic_desc = text_pipe(prompt_academic)[0]["generated_text"]
|
| 79 |
|
| 80 |
-
# Step
|
| 81 |
caption_prompt = (
|
| 82 |
-
"
|
| 83 |
-
"
|
| 84 |
-
"comparison or trend.\n\n"
|
| 85 |
f"{academic_desc}\n\nCaption:"
|
| 86 |
)
|
| 87 |
caption_text = text_pipe(caption_prompt)[0]["generated_text"]
|
| 88 |
|
| 89 |
-
# Step
|
| 90 |
summary_prompt = (
|
| 91 |
"Rewrite the following explanation as a short paragraph (3–4 sentences) "
|
| 92 |
-
"for the Results section
|
|
|
|
| 93 |
f"{academic_desc}\n\nParagraph:"
|
| 94 |
)
|
| 95 |
summary_text = text_pipe(summary_prompt)[0]["generated_text"]
|
|
@@ -115,7 +162,9 @@ def analyze_and_enhance(image, style, language, paper_file):
|
|
| 115 |
|
| 116 |
return image, raw_caption, academic_desc_out, enhanced_image, caption_text, summary_text
|
| 117 |
|
|
|
|
| 118 |
# ========== 4. Gradio UI ==========
|
|
|
|
| 119 |
with gr.Blocks(css="""
|
| 120 |
.wide_textbox textarea {
|
| 121 |
font-size: 14px;
|
|
|
|
| 3 |
from PIL import Image, ImageEnhance
|
| 4 |
from pypdf import PdfReader
|
| 5 |
|
| 6 |
+
# ========== 1. 轻量模型(适配 CPU Basic) ==========
|
| 7 |
+
|
| 8 |
vision_pipe = pipeline(
|
| 9 |
"image-to-text",
|
| 10 |
+
model="nlpconnect/vit-gpt2-image-captioning", # 小模型
|
| 11 |
device=-1
|
| 12 |
)
|
| 13 |
|
| 14 |
text_pipe = pipeline(
|
| 15 |
"text2text-generation",
|
| 16 |
+
model="google/flan-t5-small", # 比 flan-base 小很多
|
| 17 |
max_length=256,
|
| 18 |
device=-1
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# ========== 2. 从论文 PDF 提取“和图相关的上下文” ==========
|
| 22 |
+
|
| 23 |
+
def extract_full_text_from_pdf(paper_file):
|
| 24 |
+
"""读 PDF 前几页的文本"""
|
| 25 |
if paper_file is None:
|
| 26 |
return ""
|
| 27 |
try:
|
| 28 |
path = paper_file if isinstance(paper_file, str) else paper_file.name
|
| 29 |
reader = PdfReader(path)
|
| 30 |
text = ""
|
| 31 |
+
for page in reader.pages[:4]:
|
| 32 |
page_text = page.extract_text() or ""
|
| 33 |
text += page_text + "\n"
|
| 34 |
+
return text.strip()
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
print("PDF error:", e)
|
| 37 |
return ""
|
| 38 |
|
| 39 |
+
|
| 40 |
+
def select_relevant_snippets(full_text, raw_caption):
|
| 41 |
+
"""
|
| 42 |
+
用几个关键词在论文里找“和图可能相关”的局部片段,
|
| 43 |
+
避免 summarise 整篇导致太泛。
|
| 44 |
+
"""
|
| 45 |
+
if not full_text:
|
| 46 |
+
return ""
|
| 47 |
+
|
| 48 |
+
text_lower = full_text.lower()
|
| 49 |
+
|
| 50 |
+
# 从 caption 中抽出关键词(长度>4的词)
|
| 51 |
+
cap_words = [w.strip(".,;:()[]") for w in raw_caption.lower().split()]
|
| 52 |
+
cap_keywords = {w for w in cap_words if len(w) > 4}
|
| 53 |
+
|
| 54 |
+
# 再加上一些通用的结果关键词
|
| 55 |
+
extra_keywords = {"bleaching", "productivity", "calcification",
|
| 56 |
+
"figure", "fig.", "response", "treatment"}
|
| 57 |
+
keywords = list(cap_keywords | extra_keywords)
|
| 58 |
+
|
| 59 |
+
snippets = []
|
| 60 |
+
for kw in keywords:
|
| 61 |
+
idx = text_lower.find(kw)
|
| 62 |
+
if idx != -1:
|
| 63 |
+
start = max(0, idx - 400)
|
| 64 |
+
end = min(len(full_text), idx + 400)
|
| 65 |
+
snippets.append(full_text[start:end])
|
| 66 |
+
|
| 67 |
+
if not snippets:
|
| 68 |
+
# 找不到就退回到前 2500 字
|
| 69 |
+
return full_text[:2500]
|
| 70 |
+
|
| 71 |
+
joined = "\n...\n".join(snippets)
|
| 72 |
+
return joined[:2500]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def summarise_context_for_figure(snippet_text):
|
| 76 |
+
"""把筛出来的 snippet 压缩成几句“图相关背景”"""
|
| 77 |
+
if not snippet_text:
|
| 78 |
return ""
|
| 79 |
+
|
| 80 |
prompt = (
|
| 81 |
+
"The following text is an excerpt from a scientific paper. "
|
| 82 |
+
"In 3–4 sentences, summarise ONLY the experimental context and variables "
|
| 83 |
+
"that are directly relevant for interpreting one result figure. "
|
| 84 |
+
"Mention what organisms or samples are used and what stresses or treatments "
|
| 85 |
+
"are being compared.\n\n"
|
| 86 |
+
f"{snippet_text}\n\nSummary:"
|
| 87 |
)
|
| 88 |
return text_pipe(prompt)[0]["generated_text"]
|
| 89 |
|
| 90 |
+
|
| 91 |
# ========== 3. 核心工作流 ==========
|
| 92 |
+
|
| 93 |
def analyze_and_enhance(image, style, language, paper_file):
|
| 94 |
if image is None:
|
| 95 |
return None, "请先上传图表。", "", None, "", ""
|
| 96 |
|
| 97 |
+
# Step 0: 论文上下文
|
| 98 |
+
full_text = extract_full_text_from_pdf(paper_file)
|
| 99 |
+
# 先随便给一个默认 caption(用来抽关键字)
|
| 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 |
+
# Step 2: Vision → 粗略描述(我们主要用来识别“这是柱状图”)
|
| 105 |
+
raw_caption = raw_caption_tmp
|
| 106 |
rc = raw_caption.lower()
|
| 107 |
+
if not any(k in rc for k in ["bar", "chart", "graph", "plot"]):
|
| 108 |
+
raw_caption = "a bar chart showing the response of different experimental groups under several treatments"
|
| 109 |
+
|
| 110 |
+
# Step 3: 学术化解释(重点“读柱状图的趋势”)
|
| 111 |
+
context_part = f"Paper context: {paper_context}\n\n" if paper_context else ""
|
| 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 |
+
"Assume this is a bar chart with several groups and treatments. "
|
| 118 |
+
"In 3–4 sentences, describe what the figure shows using formal academic English. "
|
| 119 |
+
"Explain:\n"
|
| 120 |
+
"- what kind of response or variable is on the vertical axis in generic terms;\n"
|
| 121 |
+
"- how the bars change when moving from control to more intense treatments;\n"
|
| 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 |
+
# Step 5a: Caption
|
| 128 |
caption_prompt = (
|
| 129 |
+
"Based on the explanation below, write a 1–2 sentence figure caption for a scientific paper. "
|
| 130 |
+
"Mention that this is a bar chart and highlight the main comparison or trend.\n\n"
|
|
|
|
| 131 |
f"{academic_desc}\n\nCaption:"
|
| 132 |
)
|
| 133 |
caption_text = text_pipe(caption_prompt)[0]["generated_text"]
|
| 134 |
|
| 135 |
+
# Step 5b: Summary / 讨论
|
| 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. "
|
| 139 |
+
"Do not add new variables or numbers.\n\n"
|
| 140 |
f"{academic_desc}\n\nParagraph:"
|
| 141 |
)
|
| 142 |
summary_text = text_pipe(summary_prompt)[0]["generated_text"]
|
|
|
|
| 162 |
|
| 163 |
return image, raw_caption, academic_desc_out, enhanced_image, caption_text, summary_text
|
| 164 |
|
| 165 |
+
|
| 166 |
# ========== 4. Gradio UI ==========
|
| 167 |
+
|
| 168 |
with gr.Blocks(css="""
|
| 169 |
.wide_textbox textarea {
|
| 170 |
font-size: 14px;
|