Spaces:
Sleeping
Sleeping
File size: 19,099 Bytes
f77ceac eab192f f77ceac 3564a93 f77ceac 3564a93 67ffe67 f77ceac 3564a93 f77ceac | 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 | import { useState, useRef, useCallback } from "react";
const API_BASE = "";
const SEV_COLOR = { high: "#ff4d6d", medium: "#ff9f1c", low: "#2ec4b6" };
const SEV_BG = { high: "#fff0f3", medium: "#fff8ee", low: "#f0fafa" };
function Badge({ sev }) {
const col = SEV_COLOR[sev] || "#888";
return (
<span style={{
background: `${col}20`, color: col, borderRadius: 4,
padding: "2px 8px", fontSize: 11, fontWeight: 600,
textTransform: "uppercase", marginLeft: 6,
}}>{sev}</span>
);
}
function StatCard({ value, label, color, bg }) {
return (
<div style={{
background: bg, borderLeft: `4px solid ${color}`,
borderRadius: 10, padding: "16px 22px", minWidth: 110,
}}>
<div style={{ fontSize: 36, fontWeight: 800, color }}>{value}</div>
<div style={{ fontSize: 11, color: "#888", textTransform: "uppercase", letterSpacing: ".06em" }}>{label}</div>
</div>
);
}
export default function App() {
const [files, setFiles] = useState([]);
const [dragging, setDragging] = useState(false);
const [loading, setLoading] = useState(false);
const [progress, setProgress] = useState(0);
const [statusMsg, setStatusMsg] = useState("");
const [results, setResults] = useState(null);
const [error, setError] = useState(null);
const [activeTab, setActiveTab] = useState("ranked");
const fileRef = useRef();
const addFiles = useCallback((incoming) => {
const pdfs = Array.from(incoming).filter(f => f.type === "application/pdf");
if (pdfs.length < incoming.length) {
setError("Only PDF files are supported.");
}
setFiles(prev => {
const existing = new Set(prev.map(f => f.name));
return [...prev, ...pdfs.filter(f => !existing.has(f.name))];
});
}, []);
const removeFile = (name) => setFiles(f => f.filter(x => x.name !== name));
const reset = () => {
setFiles([]); setResults(null); setError(null);
setProgress(0); setStatusMsg(""); setActiveTab("ranked");
};
const analyze = async () => {
if (files.length < 2) { setError("Please upload at least 2 PDF files."); return; }
setError(null); setLoading(true); setProgress(10); setResults(null);
// Fake progress while waiting
const ticker = setInterval(() => {
setProgress(p => p < 85 ? p + 3 : p);
}, 1200);
const msgs = [
"Extracting text from PDFs...",
"Sending to Groq for protocol extraction...",
"Comparing protocols across papers...",
"Generating contradiction report...",
];
let mi = 0;
setStatusMsg(msgs[mi]);
const msgTicker = setInterval(() => {
mi = Math.min(mi + 1, msgs.length - 1);
setStatusMsg(msgs[mi]);
}, 6000);
try {
const form = new FormData();
files.forEach(f => form.append("files", f));
const resp = await fetch(`${API_BASE}/analyze`, { method: "POST", body: form });
if (!resp.ok) {
const err = await resp.json().catch(() => ({}));
throw new Error(err.detail || `Server error ${resp.status}`);
}
const data = await resp.json();
setProgress(100);
setStatusMsg("Analysis complete!");
setResults(data);
setActiveTab("ranked");
} catch (e) {
setError(e.message);
} finally {
clearInterval(ticker);
clearInterval(msgTicker);
setLoading(false);
}
};
const downloadReport = () => {
if (!results?.html_report) return;
const blob = new Blob([results.html_report], { type: "text/html" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "protocol_report.html";
a.click();
URL.revokeObjectURL(url);
};
const comp = results?.comparison || {};
const summary = comp.summary || {};
const contras = comp.contradictions || [];
const ranked = comp.ranked_issues || [];
const optimal = comp.optimal_protocol || {};
const paperNames = results?.paper_names || [];
const extracted = results?.extracted || [];
return (
<div style={{ fontFamily: "'Segoe UI', sans-serif", background: "#f7f7fa", minHeight: "100vh", padding: "40px 24px" }}>
<div style={{ maxWidth: 1100, margin: "0 auto" }}>
{/* ββ Header ββ */}
<div style={{ background: "#1a1a2e", borderRadius: 12, padding: "32px 36px", marginBottom: 28, color: "white" }}>
<div style={{ fontSize: 11, letterSpacing: ".15em", textTransform: "uppercase", color: "#a78bfa", marginBottom: 8 }}>
ContraGenAI Β· Bioengineering Β· Reproducibility
</div>
<h1 style={{ fontSize: 28, fontWeight: 800, margin: "0 0 8px" }}>ContraGenAI</h1>
<div style={{ fontSize: 14, color: "#a78bfa", marginBottom: 8 }}>A Protocol Contradiction Detector</div>
<p style={{ color: "#aab", fontSize: 13, lineHeight: 1.6, maxWidth: 620 }}>
Upload 2 or more research papers and detect methodological contradictions across experimental protocols β
reagents, temperatures, cell lines, antibodies, timing and more.
<b style={{ color: "#a78bfa" }}>ContraGenAI</b> Β· Powered by Groq + LLaMA 3.3 70B
</p>
</div>
{/* ββ Upload section ββ */}
{!results && (
<div style={{ background: "white", borderRadius: 12, padding: "24px 28px", marginBottom: 20, boxShadow: "0 1px 4px rgba(0,0,0,.07)" }}>
<div style={{ fontSize: 16, fontWeight: 700, borderLeft: "4px solid #7b2d8b", paddingLeft: 12, marginBottom: 18 }}>
Upload Research Papers
</div>
{/* Drop zone */}
<div
onClick={() => fileRef.current?.click()}
onDragOver={e => { e.preventDefault(); setDragging(true); }}
onDragLeave={() => setDragging(false)}
onDrop={e => { e.preventDefault(); setDragging(false); addFiles(e.dataTransfer.files); }}
style={{
border: `2px dashed ${dragging ? "#7b2d8b" : "#ddd"}`,
borderRadius: 10, padding: "36px 24px", textAlign: "center",
cursor: "pointer", background: dragging ? "#f5f0ff" : "#fafafa",
transition: "all .2s", marginBottom: 16,
}}
>
<input ref={fileRef} type="file" multiple accept=".pdf"
style={{ display: "none" }} onChange={e => addFiles(e.target.files)} />
<div style={{ fontSize: 32, marginBottom: 10 }}>π</div>
<div style={{ fontWeight: 600, color: "#1a1a2e", marginBottom: 4 }}>
Drag and drop PDFs here
</div>
<div style={{ fontSize: 12, color: "#aaa" }}>or click to browse Β· multiple files supported</div>
</div>
{/* File list */}
{files.length > 0 && (
<div style={{ marginBottom: 20 }}>
{files.map(f => (
<div key={f.name} style={{
display: "flex", alignItems: "center", gap: 12,
background: "#f5f0ff", borderRadius: 6, padding: "8px 14px", marginBottom: 6,
}}>
<span style={{ fontSize: 16 }}>π</span>
<span style={{ flex: 1, fontSize: 13, color: "#1a1a2e" }}>{f.name}</span>
<span style={{ fontSize: 11, color: "#aaa" }}>{(f.size / 1024).toFixed(0)} KB</span>
<button onClick={() => removeFile(f.name)}
style={{ background: "none", border: "none", cursor: "pointer", color: "#aaa", fontSize: 18, lineHeight: 1 }}>Γ</button>
</div>
))}
<div style={{ fontSize: 12, color: "#aaa", marginTop: 4 }}>{files.length} file(s) ready</div>
</div>
)}
{/* Error */}
{error && (
<div style={{ background: "#fff0f3", border: "1px solid #ff4d6d40", borderRadius: 8, padding: "12px 16px", color: "#ff4d6d", fontSize: 13, marginBottom: 16 }}>
β {error}
</div>
)}
{/* Progress */}
{loading && (
<div style={{ marginBottom: 20 }}>
<div style={{ display: "flex", justifyContent: "space-between", fontSize: 12, color: "#888", marginBottom: 6 }}>
<span>{statusMsg}</span>
<span>{progress}%</span>
</div>
<div style={{ height: 6, background: "#eee", borderRadius: 3, overflow: "hidden" }}>
<div style={{
height: "100%", borderRadius: 3,
background: "linear-gradient(90deg,#7b2d8b,#2ec4b6)",
width: `${progress}%`, transition: "width .4s ease",
}} />
</div>
</div>
)}
{/* Analyze button */}
<button
onClick={analyze}
disabled={loading || files.length < 2}
style={{
padding: "13px 32px", background: files.length >= 2 && !loading ? "#7b2d8b" : "#ddd",
color: files.length >= 2 && !loading ? "white" : "#aaa",
border: "none", borderRadius: 8, fontWeight: 700, fontSize: 14,
cursor: files.length >= 2 && !loading ? "pointer" : "not-allowed",
transition: "all .2s",
}}
>
{loading ? "Analyzing..." : `Detect Contradictions β`}
</button>
{files.length < 2 && !loading && (
<span style={{ marginLeft: 14, fontSize: 12, color: "#aaa" }}>
Add {2 - files.length} more paper{files.length === 1 ? "" : "s"} to start
</span>
)}
</div>
)}
{/* ββ Results ββ */}
{results && (
<>
{/* Results header */}
<div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 20, flexWrap: "wrap", gap: 12 }}>
<div>
<div style={{ fontSize: 22, fontWeight: 800, color: "#1a1a2e" }}>Analysis Results</div>
<div style={{ fontSize: 13, color: "#888", marginTop: 3 }}>
{paperNames.length} papers Β· {comp.protocol_type || "Protocol"}
</div>
</div>
<div style={{ display: "flex", gap: 10 }}>
<button onClick={downloadReport} style={{
padding: "10px 20px", background: "#2ec4b6", color: "white",
border: "none", borderRadius: 8, fontWeight: 600, fontSize: 13, cursor: "pointer",
}}>β¬ Download Report</button>
<button onClick={reset} style={{
padding: "10px 20px", background: "white", color: "#1a1a2e",
border: "1px solid #ddd", borderRadius: 8, fontWeight: 600, fontSize: 13, cursor: "pointer",
}}>β New Analysis</button>
</div>
</div>
{/* Summary stats */}
<div style={{ display: "flex", gap: 14, flexWrap: "wrap", marginBottom: 20 }}>
<StatCard value={summary.total_contradictions ?? 0} label="Total" color="#7b2d8b" bg="#f5f0ff" />
<StatCard value={summary.high ?? 0} label="High" color="#ff4d6d" bg="#fff0f3" />
<StatCard value={summary.medium ?? 0} label="Medium" color="#ff9f1c" bg="#fff8ee" />
<StatCard value={summary.low ?? 0} label="Low" color="#2ec4b6" bg="#f0fafa" />
</div>
{/* Tab nav */}
<div style={{ display: "flex", gap: 0, borderBottom: "1px solid #eee", marginBottom: 20 }}>
{[
{ id: "ranked", label: "Ranked Issues" },
{ id: "table", label: "Comparison Table" },
{ id: "optimal", label: "Optimal Protocol" },
{ id: "extraction", label: "Extraction Summary" },
].map(t => (
<button key={t.id} onClick={() => setActiveTab(t.id)} style={{
padding: "10px 20px", background: "none", border: "none",
borderBottom: activeTab === t.id ? "2px solid #7b2d8b" : "2px solid transparent",
color: activeTab === t.id ? "#7b2d8b" : "#888",
fontWeight: activeTab === t.id ? 600 : 400,
fontSize: 13, cursor: "pointer", marginBottom: -1,
}}>{t.label}</button>
))}
</div>
{/* Tab: Ranked */}
{activeTab === "ranked" && (
<div style={{ background: "white", borderRadius: 12, padding: "24px 28px", boxShadow: "0 1px 4px rgba(0,0,0,.07)" }}>
{ranked.length === 0
? <p style={{ color: "#aaa" }}>No ranked issues found.</p>
: ranked.map((item, i) => {
const sev = item.severity || "low";
const col = SEV_COLOR[sev] || "#888";
const bg = SEV_BG[sev] || "#fafafa";
return (
<div key={i} style={{
display: "flex", gap: 14, background: bg,
border: `1px solid ${col}30`, borderRadius: 8,
padding: "14px 16px", marginBottom: 10,
}}>
<div style={{ fontSize: 24, fontWeight: 800, color: col, minWidth: 34 }}>#{item.rank}</div>
<div>
<div style={{ fontSize: 14, fontWeight: 600, color: "#1a1a2e" }}>
{item.parameter}<Badge sev={sev} />
</div>
<div style={{ fontSize: 12, color: "#555", marginTop: 4, lineHeight: 1.5 }}>{item.brief}</div>
</div>
</div>
);
})
}
</div>
)}
{/* Tab: Comparison Table */}
{activeTab === "table" && (
<div style={{ background: "white", borderRadius: 12, padding: "24px 28px", boxShadow: "0 1px 4px rgba(0,0,0,.07)", overflowX: "auto" }}>
{contras.length === 0
? <p style={{ color: "#aaa" }}>No contradictions found.</p>
: (
<table style={{ width: "100%", borderCollapse: "collapse", fontSize: 13 }}>
<thead>
<tr>
{["Parameter", "Category", "Severity",
...paperNames.map((n, i) => `Paper ${i + 1}`),
"Impact on Reproducibility"
].map(h => (
<th key={h} style={{
background: "#f5f0ff", fontSize: 11, textTransform: "uppercase",
letterSpacing: ".06em", padding: "10px 14px", textAlign: "left",
}}>{h}</th>
))}
</tr>
</thead>
<tbody>
{contras.map((c, i) => {
const sev = c.severity || "low";
const col = SEV_COLOR[sev] || "#888";
return (
<tr key={i}>
<td style={{ padding: "10px 14px", borderBottom: "1px solid #f0f0f5", fontWeight: 600 }}>{c.parameter}</td>
<td style={{ padding: "10px 14px", borderBottom: "1px solid #f0f0f5", color: "#888", fontSize: 12 }}>{c.category}</td>
<td style={{ padding: "10px 14px", borderBottom: "1px solid #f0f0f5" }}><Badge sev={sev} /></td>
{paperNames.map((_, j) => (
<td key={j} style={{ padding: "10px 14px", borderBottom: "1px solid #f0f0f5", fontFamily: "monospace", fontSize: 12 }}>
{c.values?.[`paper_${j}`] || "β"}
</td>
))}
<td style={{ padding: "10px 14px", borderBottom: "1px solid #f0f0f5", fontSize: 12, color: "#444", lineHeight: 1.5, maxWidth: 260 }}>{c.explanation}</td>
</tr>
);
})}
</tbody>
</table>
)
}
</div>
)}
{/* Tab: Optimal Protocol */}
{activeTab === "optimal" && (
<div style={{ background: "white", borderRadius: 12, padding: "24px 28px", boxShadow: "0 1px 4px rgba(0,0,0,.07)" }}>
<p style={{ fontSize: 13, color: "#444", lineHeight: 1.6, marginBottom: 20 }}>{optimal.rationale}</p>
<div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fill,minmax(240px,1fr))", gap: 14 }}>
{(optimal.parameters || []).map((p, i) => (
<div key={i} style={{ borderLeft: "3px solid #2ec4b6", paddingLeft: 12 }}>
<div style={{ fontSize: 11, textTransform: "uppercase", color: "#aaa", letterSpacing: ".08em" }}>{p.label}</div>
<div style={{ fontSize: 14, fontWeight: 600, color: "#1a1a2e", margin: "3px 0" }}>{p.value}</div>
<div style={{ fontSize: 12, color: "#666", lineHeight: 1.5 }}>{p.reason}</div>
</div>
))}
</div>
</div>
)}
{/* Tab: Extraction Summary */}
{activeTab === "extraction" && (
<div style={{ background: "white", borderRadius: 12, padding: "24px 28px", boxShadow: "0 1px 4px rgba(0,0,0,.07)" }}>
{extracted.map((e, i) => {
const cats = e.parameters || {};
const total = Object.values(cats).reduce((s, v) => s + (Array.isArray(v) ? v.length : 0), 0);
return (
<div key={i} style={{ background: "#fafafa", border: "1px solid #eee", borderRadius: 8, padding: "14px 16px", marginBottom: 12 }}>
<div style={{ fontWeight: 600, color: "#1a1a2e", marginBottom: 4 }}>Paper {i + 1}: {e.title || e._filename}</div>
<div style={{ fontSize: 12, color: "#888", marginBottom: 6 }}>
{e.authors} | {e.year} | {e.protocol_type}
</div>
<div style={{ fontSize: 12, color: "#7b2d8b", fontWeight: 600 }}>
{total} parameters extracted across {Object.keys(cats).length} categories
</div>
</div>
);
})}
</div>
)}
</>
)}
</div>
</div>
);
}
|