-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
409 lines (365 loc) · 14.2 KB
/
Copy pathapp.py
File metadata and controls
409 lines (365 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
"""
Refract — See through every reference.
Phase 1: Streamlit MVP v2
Supports numbered [n] and author-year citation styles.
"""
import os
import json
import streamlit as st
from parser import process_pdf, CitationStyle
from scholar import enrich_references, format_authors
from summarizer import summarize_citation_context
# ─── Page Config ───────────────────────────────────────────────
st.set_page_config(
page_title="Refract",
page_icon="🔬",
layout="wide",
initial_sidebar_state="expanded",
)
# ─── Custom CSS ────────────────────────────────────────────────
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=DM+Sans:wght@400;500;700&family=JetBrains+Mono:wght@400;500&display=swap');
.stApp { font-family: 'DM Sans', sans-serif; }
.refract-hero {
text-align: center;
padding: 2rem 0 1rem 0;
}
.refract-hero h1 {
font-family: 'DM Sans', sans-serif;
font-size: 3rem;
font-weight: 700;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0.25rem;
}
.refract-hero p {
color: #8892a4;
font-size: 1.1rem;
}
.glass-card {
background: rgba(255, 255, 255, 0.05);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 16px;
padding: 1.5rem;
margin-bottom: 1rem;
transition: all 0.3s ease;
}
.glass-card:hover {
border-color: rgba(102, 126, 234, 0.4);
box-shadow: 0 8px 32px rgba(102, 126, 234, 0.1);
}
.ref-header {
display: flex;
align-items: flex-start;
gap: 0.75rem;
margin-bottom: 0.75rem;
}
.ref-number {
background: linear-gradient(135deg, #667eea, #764ba2);
color: white;
font-family: 'JetBrains Mono', monospace;
font-size: 0.8rem;
font-weight: 500;
padding: 0.25rem 0.6rem;
border-radius: 8px;
flex-shrink: 0;
max-width: 160px;
text-align: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.ref-title {
font-size: 1.05rem;
font-weight: 600;
color: #e2e8f0;
line-height: 1.4;
}
.ref-meta {
font-size: 0.85rem;
color: #8892a4;
margin-top: 0.25rem;
}
.ref-abstract {
font-size: 0.9rem;
color: #a0aec0;
line-height: 1.6;
margin-top: 0.75rem;
padding: 0.75rem;
background: rgba(255, 255, 255, 0.03);
border-radius: 8px;
border-left: 3px solid rgba(102, 126, 234, 0.5);
}
.ref-context-label {
font-size: 0.8rem;
font-weight: 600;
color: #667eea;
text-transform: uppercase;
letter-spacing: 0.05em;
margin-top: 1rem;
margin-bottom: 0.4rem;
}
.ref-context {
font-size: 0.85rem;
color: #94a3b8;
line-height: 1.5;
padding: 0.6rem;
background: rgba(102, 126, 234, 0.05);
border-radius: 8px;
font-style: italic;
}
.ref-tldr {
font-size: 0.9rem;
color: #c4b5fd;
margin-top: 0.5rem;
padding: 0.5rem 0.75rem;
background: rgba(118, 75, 162, 0.1);
border-radius: 8px;
}
.ref-stats {
display: flex;
gap: 1rem;
margin-top: 0.5rem;
flex-wrap: wrap;
}
.stat-badge {
font-family: 'JetBrains Mono', monospace;
font-size: 0.75rem;
color: #8892a4;
background: rgba(255,255,255,0.05);
padding: 0.2rem 0.5rem;
border-radius: 6px;
}
.not-found { opacity: 0.5; }
.status-bar {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.75rem 1rem;
background: rgba(102, 126, 234, 0.08);
border-radius: 10px;
margin: 1rem 0;
font-size: 0.9rem;
color: #a0aec0;
}
.style-badge {
display: inline-block;
font-family: 'JetBrains Mono', monospace;
font-size: 0.75rem;
padding: 0.15rem 0.5rem;
border-radius: 6px;
background: rgba(102, 126, 234, 0.15);
color: #667eea;
margin-left: 0.5rem;
}
</style>
""", unsafe_allow_html=True)
# ─── Header ────────────────────────────────────────────────────
st.markdown("""
<div class="refract-hero">
<h1>Refract</h1>
<p>See through every reference.</p>
</div>
""", unsafe_allow_html=True)
# ─── Sidebar ──────────────────────────────────────────────────
with st.sidebar:
st.markdown("### ⚙️ Settings")
llm_provider = st.selectbox(
"LLM Provider",
["anthropic", "openai", "none"],
help="API used for citation context summarization",
)
if llm_provider == "anthropic":
api_key = st.text_input("Anthropic API Key", type="password")
if api_key:
os.environ["ANTHROPIC_API_KEY"] = api_key
elif llm_provider == "openai":
api_key = st.text_input("OpenAI API Key", type="password")
if api_key:
os.environ["OPENAI_API_KEY"] = api_key
st.divider()
st.markdown("### 📊 Stats")
if "enriched" in st.session_state:
enriched = st.session_state["enriched"]
total = len(enriched)
found = sum(1 for v in enriched.values() if v["match_status"] == "found")
st.metric("Total References", total)
st.metric("Matched", f"{found}/{total}")
st.metric("Match Rate", f"{found/total*100:.0f}%" if total > 0 else "—")
if "citation_style" in st.session_state:
style = st.session_state["citation_style"]
style_label = {
CitationStyle.NUMBERED: "Numbered [1]",
CitationStyle.AUTHOR_YEAR: "Author-Year",
CitationStyle.UNKNOWN: "Auto-detected",
}.get(style, "Unknown")
st.metric("Citation Style", style_label)
st.divider()
st.markdown(
"<small style='color:#64748b'>Powered by Semantic Scholar API</small>",
unsafe_allow_html=True,
)
# ─── File Upload ───────────────────────────────────────────────
uploaded = st.file_uploader(
"Upload a research paper (PDF)",
type=["pdf"],
help="Supports both [1]-style and (Author, Year)-style citations.",
)
if uploaded:
tmp_path = f"/tmp/{uploaded.name}"
with open(tmp_path, "wb") as f:
f.write(uploaded.read())
# ─── Step 1: Parse ─────────────────────────────────────────
with st.spinner("📄 Extracting text and parsing references..."):
parsed = process_pdf(tmp_path)
if parsed.error:
st.error(f"⚠️ {parsed.error}")
st.info(
"이 PDF에서 References 섹션을 자동으로 찾지 못했습니다. "
"References, Bibliography 등의 섹션 헤더가 있는 논문에서 가장 잘 동작합니다."
)
st.stop()
st.session_state["citation_style"] = parsed.style
style_name = {
CitationStyle.NUMBERED: "Numbered [n]",
CitationStyle.AUTHOR_YEAR: "Author-Year (Author, Year)",
CitationStyle.UNKNOWN: "Auto",
}.get(parsed.style, "Unknown")
st.markdown(
f'<div class="status-bar">'
f'✅ <strong>{parsed.total_refs}개</strong>의 레퍼런스를 파싱했습니다.'
f'<span class="style-badge">{style_name}</span>'
f'</div>',
unsafe_allow_html=True,
)
# ─── Step 2: Enrich ───────────────────────────────────────
if "enriched" not in st.session_state or st.session_state.get("_last_file") != uploaded.name:
with st.spinner("🔍 Semantic Scholar에서 메타데이터를 가져오는 중..."):
enriched = enrich_references(parsed.references, delay=0.4)
st.session_state["enriched"] = enriched
st.session_state["_last_file"] = uploaded.name
st.rerun()
else:
enriched = st.session_state["enriched"]
# ─── Step 3: Display ──────────────────────────────────────
st.markdown("---")
col1, col2 = st.columns([1, 1])
with col1:
show_only_found = st.toggle("매칭된 레퍼런스만 보기", value=False)
with col2:
sort_by = st.selectbox("정렬", ["키 순", "인용수 높은순", "최신순"])
items = list(enriched.items())
if sort_by == "인용수 높은순":
items.sort(
key=lambda x: (x[1].get("scholar_data") or {}).get("citation_count") or 0,
reverse=True,
)
elif sort_by == "최신순":
items.sort(
key=lambda x: (x[1].get("scholar_data") or {}).get("year") or 0,
reverse=True,
)
for ref_key, ref in items:
if show_only_found and ref["match_status"] != "found":
continue
sd = ref.get("scholar_data")
with st.container():
if sd:
title = sd["title"] or ref["raw"][:80]
authors_str = format_authors(sd.get("authors", []))
year = sd.get("year", "")
citations = sd.get("citation_count", 0)
fields = ", ".join(sd.get("fields") or [])
abstract = sd.get("abstract", "")
tldr = sd.get("tldr", "")
url = sd.get("url", "")
# Truncate long keys for display badge
display_key = ref_key if len(ref_key) < 20 else ref_key[:17] + "…"
card_html = f"""
<div class="glass-card">
<div class="ref-header">
<span class="ref-number" title="{ref_key}">{display_key}</span>
<div>
<div class="ref-title">{title}</div>
<div class="ref-meta">{authors_str} · {year}</div>
</div>
</div>
<div class="ref-stats">
<span class="stat-badge">📊 cited {citations:,} times</span>
{'<span class="stat-badge">🏷️ ' + fields + '</span>' if fields else ''}
{'<a href="' + url + '" target="_blank" style="font-size:0.75rem;color:#667eea;">🔗 Semantic Scholar</a>' if url else ''}
</div>
"""
if tldr:
card_html += f'<div class="ref-tldr">💡 TL;DR: {tldr}</div>'
if abstract:
short_abs = abstract[:300] + "..." if len(abstract) > 300 else abstract
card_html += f'<div class="ref-abstract">{short_abs}</div>'
contexts = ref.get("citation_contexts", [])
if contexts:
card_html += '<div class="ref-context-label">📍 Citation Context</div>'
ctx = contexts[0][:250] + "..." if len(contexts[0]) > 250 else contexts[0]
card_html += f'<div class="ref-context">{ctx}</div>'
card_html += "</div>"
st.markdown(card_html, unsafe_allow_html=True)
if contexts and llm_provider != "none":
with st.expander(f"🤖 AI 인용 맥락 분석 — {display_key}"):
summary_key = f"summary_{ref_key}"
if summary_key not in st.session_state:
with st.spinner("분석 중..."):
summary = summarize_citation_context(
ref_title=title,
ref_abstract=abstract,
citation_contexts=contexts,
provider=llm_provider,
)
st.session_state[summary_key] = summary
st.markdown(st.session_state[summary_key])
else:
raw_preview = ref["raw"][:120] + "..." if len(ref["raw"]) > 120 else ref["raw"]
display_key = ref_key if len(ref_key) < 20 else ref_key[:17] + "…"
card_html = f"""
<div class="glass-card not-found">
<div class="ref-header">
<span class="ref-number">{display_key}</span>
<div>
<div class="ref-title" style="color:#64748b">{raw_preview}</div>
<div class="ref-meta">⚠️ Semantic Scholar에서 매칭되지 않음</div>
</div>
</div>
</div>
"""
st.markdown(card_html, unsafe_allow_html=True)
# ─── Export ────────────────────────────────────────────────
st.markdown("---")
if st.button("📥 결과를 JSON으로 내보내기"):
export_data = {}
for ref_key, ref in enriched.items():
export_data[ref_key] = {
"raw_reference": ref["raw"],
"extracted_title": ref.get("extracted_title"),
"match_status": ref["match_status"],
"scholar_data": ref.get("scholar_data"),
"citation_contexts": ref.get("citation_contexts", []),
}
json_str = json.dumps(export_data, ensure_ascii=False, indent=2)
st.download_button(
label="💾 Download JSON",
data=json_str,
file_name=f"refract_{uploaded.name.replace('.pdf','')}.json",
mime="application/json",
)
else:
st.markdown("""
<div style="text-align:center; padding:4rem 2rem; color:#64748b">
<div style="font-size:3rem; margin-bottom:1rem">📄</div>
<div style="font-size:1.1rem; margin-bottom:0.5rem">PDF 논문을 업로드하세요</div>
<div style="font-size:0.9rem; color:#8892a4">
<strong>[1]</strong> 형식과 <strong>(Author, Year)</strong> 형식 모두 지원합니다.
</div>
</div>
""", unsafe_allow_html=True)