File size: 53,313 Bytes
4caa453 2d58484 4caa453 2d58484 4caa453 384bb2f 4caa453 f92e88d f301618 384bb2f 97025d7 f301618 97025d7 384bb2f 97025d7 f301618 97025d7 f301618 97025d7 384bb2f 97025d7 384bb2f 97025d7 384bb2f 97025d7 f301618 384bb2f f301618 384bb2f f301618 384bb2f 97025d7 f301618 97025d7 f301618 97025d7 f301618 97025d7 384bb2f f301618 384bb2f f301618 97025d7 384bb2f 97025d7 384bb2f 97025d7 384bb2f f301618 97025d7 384bb2f 97025d7 f301618 97025d7 4caa453 384bb2f 4caa453 97025d7 384bb2f 97025d7 4caa453 384bb2f f301618 97025d7 4caa453 384bb2f 4caa453 f301618 4caa453 97025d7 f301618 4caa453 f301618 4caa453 97025d7 f301618 4caa453 f92e88d 4caa453 f92e88d 4caa453 2d58484 4caa453 2d58484 4caa453 2d58484 4caa453 2d58484 4caa453 3836087 4caa453 f92e88d 4caa453 1db96ea 4caa453 a2c1375 4caa453 a2c1375 4caa453 f92e88d 4caa453 bd1072c 4caa453 |
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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 |
"""Main Streamlit app for ScicoQA Discrepancy Detection Demo."""
import logging
import os
import time
from pathlib import Path
import streamlit as st
from dotenv import load_dotenv
from core.arxiv2md_demo import Arxiv2MD
from core.code_loader_demo import CodeLoader
from core.llm_demo import LLM
from core.model_config import (
PROVIDER_PRESETS,
# create_local_model_config, # TODO: Re-enable when local models are fixed
create_provider_model_config,
get_api_key_env_name,
get_provider_from_model,
)
# from core.ollama_models import fetch_ollama_models # TODO: Re-enable when local models are fixed
from core.openrouter_models import fetch_free_models, get_model_config
from core.prompt_demo import Prompt
from core.token_counter_demo import TokenCounter
from parsing import parse_discrepancies
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
# Constants
CONTEXT_BUFFER_FACTOR = 0.9
MAX_CONTEXT_SIZE = 131072 # Default max context
# Page configuration
st.set_page_config(
page_title="SciCoQA Paper- Code Discrepancy Detection",
page_icon="π¬",
layout="wide",
initial_sidebar_state=400,
)
def _redact_secrets(text: str, secrets: list[str | None]) -> str:
"""Best-effort redaction for secrets that may appear in exception strings/logs."""
redacted = text
for secret in secrets:
if secret and secret in redacted:
redacted = redacted.replace(secret, "***REDACTED***")
return redacted
def _safe_model_config_for_session(model_config: dict | None) -> dict | None:
"""Store model config in session state WITHOUT sensitive fields like API keys."""
if not model_config:
return model_config
# Shallow-copy and drop known secret keys
safe = dict(model_config)
safe.pop("api_key", None)
safe.pop("apiKey", None)
return safe
def _is_context_length_error(error_msg: str) -> bool:
"""
Check if an error message indicates a context length error.
Args:
error_msg: The error message string
Returns:
True if it's a context length error, False otherwise
"""
error_lower = error_msg.lower()
return (
"maximum context length" in error_lower
or "requested about" in error_lower
or ("context length is" in error_lower and "you requested" in error_lower)
or "context window" in error_lower
)
def _build_prompt(
paper_text: str,
code_loader: CodeLoader | None,
code_text: str | None,
model_config: dict,
token_counter: TokenCounter,
code_reduction_factor: float = 1.0,
) -> tuple[str, str, int, bool]:
"""
Build prompt by counting tokens and truncating code until prompt + paper + code < CONTEXT_BUFFER_FACTOR * model context length.
Args:
paper_text: The paper text
code_loader: CodeLoader instance (if using GitHub repo)
code_text: Raw code text (if using uploaded file)
model_config: Model configuration dictionary
token_counter: TokenCounter instance
code_reduction_factor: Factor to reduce code tokens (1.0 = no reduction, 0.9 = 10% reduction, etc.)
Returns:
Tuple of (final_prompt, code_prompt, final_tokens, code_was_truncated)
"""
max_context = model_config["max_context"]
max_total_tokens = int(max_context * CONTEXT_BUFFER_FACTOR)
# Build prompt template
prompt_template = Prompt("discrepancy_generation")
# Calculate tokens for template + paper
template_with_paper = prompt_template(paper=paper_text, code="")
tokens_template_and_paper = token_counter(template_with_paper)
# Calculate remaining tokens for code (with reduction factor)
remaining_code_tokens = int((max_total_tokens - tokens_template_and_paper) * code_reduction_factor)
if remaining_code_tokens <= 0:
raise ValueError(
f"Paper text too long: {tokens_template_and_paper} tokens exceeds "
f"{int(CONTEXT_BUFFER_FACTOR * 100)}% of context limit ({max_total_tokens} tokens)"
)
logger.info(
f"Template + paper tokens: {tokens_template_and_paper}, "
f"Remaining for code (with {code_reduction_factor:.1%} factor): {remaining_code_tokens}"
)
# Track original code size to detect truncation
original_code_size = 0
if code_loader:
# For CodeLoader, we can't easily get original size, so we'll check if code_prompt is empty/minimal
original_code_size = -1 # Special value to indicate we can't determine
elif code_text:
original_code_size = len(code_text)
# Get code prompt with token limit
code_was_truncated = False
if code_loader:
# Use CodeLoader for GitHub repos
code_prompt = code_loader.get_code_prompt(
token_counter=token_counter,
max_tokens=remaining_code_tokens,
)
# Check if code was truncated by comparing token count
code_tokens_used = token_counter(code_prompt)
code_was_truncated = code_tokens_used >= remaining_code_tokens * 0.95 # If we used 95%+ of limit, likely truncated
else:
# Truncate code text to fit within token limit
code_prompt = ""
code_tokens = 0
if code_text and remaining_code_tokens > 0:
code_lines = code_text.split('\n')
for line in code_lines:
line_with_newline = line + '\n'
line_tokens = token_counter(line_with_newline)
if code_tokens + line_tokens > remaining_code_tokens:
logger.warning(f"Truncating code at {code_tokens} tokens (limit: {remaining_code_tokens})")
code_was_truncated = True
break
code_prompt += line_with_newline
code_tokens += line_tokens
# Check if we truncated (code_prompt is shorter than original)
if len(code_prompt) < original_code_size:
code_was_truncated = True
# Construct final prompt and verify it's within limit
final_prompt = prompt_template(paper=paper_text, code=code_prompt)
final_tokens = token_counter(final_prompt)
if final_tokens > max_total_tokens:
raise ValueError(
f"Final prompt too long: {final_tokens} tokens exceeds "
f"{int(CONTEXT_BUFFER_FACTOR * 100)}% of context limit ({max_total_tokens} tokens)"
)
logger.info(f"Final prompt tokens: {final_tokens} (limit: {max_total_tokens})")
return final_prompt, code_prompt, final_tokens, code_was_truncated
def validate_urls(arxiv_url: str, github_url: str) -> tuple[bool, str]:
"""Validate input URLs."""
if not arxiv_url:
return False, "Please provide an arXiv URL"
if not github_url:
return False, "Please provide a GitHub URL"
if "arxiv.org" not in arxiv_url and not arxiv_url.startswith("http"):
# Try to construct URL from ID
if arxiv_url.replace(".", "").replace("v", "").isdigit():
arxiv_url = f"https://arxiv.org/abs/{arxiv_url}"
else:
return False, "Invalid arXiv URL format"
if "github.com" not in github_url:
return False, "Please provide a valid GitHub URL"
return True, ""
def validate_files(paper_file, code_file) -> tuple[bool, str]:
"""Validate uploaded files."""
if paper_file is None:
return False, "Please upload a paper markdown file"
if code_file is None:
return False, "Please upload a repository text file"
# Check file types
if paper_file.name and not paper_file.name.endswith(('.md', '.markdown', '.txt')):
return False, "Paper file should be a markdown (.md) or text (.txt) file"
if code_file.name and not code_file.name.endswith('.txt'):
return False, "Repository file should be a text (.txt) file"
return True, ""
def process_discrepancy_detection(
paper_text: str | None = None,
code_text: str | None = None,
arxiv_url: str | None = None,
github_url: str | None = None,
model_config: dict | None = None,
):
"""Main processing pipeline for discrepancy detection."""
results = {
"paper_text": None,
"code_prompt": None,
"prompt": None,
"llm_response": None,
"discrepancies": None,
"error": None,
"step_timings": None,
}
# Use a single compact status container
step_timings = {} # Store timings for each step
# Note: Uploaded files (paper_text, code_text) are only in memory and never saved
# URL fetches (arxiv_url, github_url) use persistent cache directories for performance
try:
with st.status("π Processing...", expanded=False) as status:
try:
# Step 1: Fetch/process paper
step_start = time.time()
if arxiv_url:
# Fetch from arXiv - use persistent cache directory
status.update(label="π Fetching paper from arXiv...", state="running")
try:
# Use persistent directory for caching (OK to save fetched papers)
arxiv2md = Arxiv2MD(output_dir=Path("data/papers"))
paper_text = arxiv2md(arxiv_url)
results["paper_text"] = paper_text
step_time = time.time() - step_start
step_timings["Paper Fetch"] = step_time
st.write(f"β
Paper fetched: {step_time:.1f}s")
status.update(
label=f"β
Paper fetched ({step_time:.1f}s)",
state="running",
)
except Exception as e:
error_msg = f"Error fetching paper: {str(e)}"
logger.error(error_msg)
results["error"] = error_msg
status.update(label="β Error fetching paper", state="error")
return results
else:
# Use provided paper text
status.update(label="π Processing paper...", state="running")
try:
results["paper_text"] = paper_text
step_time = time.time() - step_start
step_timings["Paper Processing"] = step_time
st.write(f"β
Paper processed: {step_time:.1f}s")
status.update(
label=f"β
Paper processed ({step_time:.1f}s)",
state="running",
)
except Exception as e:
error_msg = f"Error processing paper: {str(e)}"
logger.error(error_msg)
results["error"] = error_msg
status.update(label="β Error processing paper", state="error")
return results
# Step 2: Fetch/process code
step_start = time.time()
code_loader = None
if github_url:
# Fetch from GitHub - use persistent cache directory
status.update(label="π¦ Fetching code from GitHub...", state="running")
try:
# Use persistent directory for caching (OK to save fetched repos)
code_loader = CodeLoader(
github_url=github_url,
max_file_size_mb=1.0,
raw_repo_dir=Path("data/repos-raw"),
)
step_time = time.time() - step_start
step_timings["Repository Clone"] = step_time
st.write(f"β
Repository cloned: {step_time:.1f}s")
status.update(
label=f"β
Repository cloned ({step_time:.1f}s)",
state="running",
)
except Exception as e:
error_msg = f"Error cloning repository: {str(e)}"
logger.error(error_msg)
results["error"] = error_msg
status.update(label="β Error cloning repository", state="error")
return results
else:
# Code text is already provided
status.update(label="π¦ Processing repository...", state="running")
step_time = time.time() - step_start
step_timings["Code Processing"] = step_time
st.write(f"β
Repository processed: {step_time:.1f}s")
status.update(
label=f"β
Repository processed ({step_time:.1f}s)",
state="running",
)
# Step 5: Calculate tokens and prepare prompt
step_start = time.time()
status.update(label="π Preparing prompt...", state="running")
# Create token counter
tokenizer_name = model_config["tokenizer"]
token_counter = TokenCounter(model=tokenizer_name)
try:
# Build prompt with simple token counting
final_prompt, code_prompt, final_tokens, code_was_truncated = _build_prompt(
paper_text=paper_text,
code_loader=code_loader,
code_text=code_text,
model_config=model_config,
token_counter=token_counter,
)
results["code_prompt"] = code_prompt
results["prompt"] = final_prompt
step_time = time.time() - step_start
step_timings["Prompt Preparation"] = step_time
st.write(f"β
Prompt prepared: {step_time:.1f}s ({final_tokens:,} tokens)")
status.update(
label=f"β
Prompt prepared ({step_time:.1f}s, {final_tokens:,} tokens)",
state="running",
)
except Exception as e:
error_msg = f"Error preparing prompt: {str(e)}"
logger.error(error_msg)
results["error"] = error_msg
status.update(label="β Error preparing prompt", state="error")
return results
# Step 6: Detect discrepancies with LLM (with retry on context length errors)
step_start = time.time()
status.update(label="π€\uFE0F Detecting discrepancies (this may take a while)...", state="running")
# Retry configuration
code_reduction_factor = 1.0 # Start with no reduction
reduction_step = 0.1 # Reduce by 10% each time
max_retries = 10
retry_count = 0
success = False
current_final_prompt = final_prompt
current_code_was_truncated = code_was_truncated
while not success and retry_count < max_retries:
try:
# Rebuild prompt with reduced code if retrying
if retry_count > 0:
logger.info(
f"Retrying with code reduction factor: {code_reduction_factor:.1%} "
f"(attempt {retry_count}/{max_retries})"
)
status.update(
label=f"π Retrying with reduced code ({code_reduction_factor:.0%})...",
state="running"
)
st.write(f"π Retrying with reduced code ({code_reduction_factor:.0%})...")
# Rebuild prompt with reduced code
current_final_prompt, code_prompt, final_tokens, current_code_was_truncated = _build_prompt(
paper_text=paper_text,
code_loader=code_loader,
code_text=code_text,
model_config=model_config,
token_counter=token_counter,
code_reduction_factor=code_reduction_factor,
)
results["code_prompt"] = code_prompt
results["prompt"] = current_final_prompt
# Extract model configuration
model = model_config["model"]
api_key = model_config.get("api_key")
api_base = model_config.get("api_base")
max_context = model_config.get("max_context")
llm = LLM(
model=model,
api_key=api_key,
api_base=api_base,
temperature=1.0,
top_p=1.0,
reasoning_effort="high",
max_context=max_context,
)
response = llm(current_final_prompt)
results["llm_response"] = response
# Extract content from response
choices = response.get("choices", [])
if not choices:
raise ValueError("No choices in LLM response")
content = (
choices[0]
.get("message", {})
.get("content", "")
)
if not content:
raise ValueError("Empty content in LLM response")
# Parse discrepancies
discrepancies = parse_discrepancies(content)
results["discrepancies"] = discrepancies
step_time = time.time() - step_start
step_timings["LLM Inference"] = step_time
total_time = sum(step_timings.values())
st.write(f"β
LLM inference: {step_time:.1f}s")
# Inform user if code was truncated
if current_code_was_truncated:
st.warning("β οΈ **Note**: Some code was truncated from the prompt due to context length limitations.")
st.write("---")
st.write(f"**Total time: {total_time:.1f}s**")
if discrepancies:
count = len(discrepancies)
discrepancy_text = "discrepancy" if count == 1 else "discrepancies"
status.update(
label=f"β
Complete! Found {count} {discrepancy_text} ({total_time:.1f}s total)",
state="complete",
)
else:
status.update(
label=f"β
Complete! No discrepancies found ({total_time:.1f}s total)",
state="complete",
)
success = True
except Exception as e:
error_msg = str(e)
api_key = model_config.get("api_key") if isinstance(model_config, dict) else None
redacted_error = _redact_secrets(error_msg, [api_key])
# Check if it's a context length error
if _is_context_length_error(error_msg):
retry_count += 1
# Check if we can reduce code further
# If code_reduction_factor is already at minimum (0.1), paper must be too long
if code_reduction_factor <= 0.1:
# Code is already minimal, paper must be too long
error_msg = (
f"The paper text is too long for the model's context window. "
f"Even with all code removed, the paper alone exceeds the context limit. "
f"Please use a model with a larger context window or provide a shorter paper."
)
logger.error(error_msg)
results["error"] = error_msg
status.update(label="β Paper too long for model", state="error")
return results
# Reduce code by 10% for next attempt
code_reduction_factor = max(0.1, code_reduction_factor - reduction_step)
logger.warning(
f"Context length error detected: {redacted_error}. "
f"Retrying with reduced code ({code_reduction_factor:.0%}) (attempt {retry_count}/{max_retries})"
)
continue # Retry with reduced code
else:
# Not a context length error
logger.error(f"Error during LLM inference: {redacted_error}")
results["error"] = f"Error during LLM inference: {redacted_error}"
status.update(label="β Error during inference", state="error")
return results
# If we exhausted retries
if not success:
error_msg = (
f"Could not fit prompt within context limits after {retry_count} retries. "
f"The paper text may be too long for this model's context window."
)
logger.error(error_msg)
results["error"] = error_msg
status.update(label="β Prompt too large for model", state="error")
return results
except Exception as e:
api_key = model_config.get("api_key") if isinstance(model_config, dict) else None
error_msg = f"Unexpected error: {_redact_secrets(str(e), [api_key])}"
logger.error(error_msg, exc_info=True)
results["error"] = error_msg
status.update(label="β Unexpected error", state="error")
return results
results["step_timings"] = step_timings
return results
except Exception as e:
# Handle any errors that occur outside the status context
api_key = model_config.get("api_key") if isinstance(model_config, dict) else None
error_msg = f"Unexpected error: {_redact_secrets(str(e), [api_key])}"
logger.error(error_msg, exc_info=True)
results["error"] = error_msg
return results
def main():
"""Main Streamlit app."""
st.title("π¬ :rainbow[SciCoQA] Paper-Code Discrepancy Detection")
st.markdown(
"""
_Detect discrepancies between scientific papers and their code implementations._
"""
)
# About section in main area
with st.expander("βΉοΈ About", expanded=False):
st.markdown(
"""
This tool is a demo of our research paper on detecting discrepancies between scientific papers and their
code implementations. You can read our paper here: [arXiv:2601.XXXX](https://arxiv.org/pdf/2601.XXXX).
This tool helps researchers and developers identify inconsistencies between scientific papers and their
corresponding code implementations. Such discrepancies can lead to reproducibility issues, incorrect
implementations, or misunderstandings of the research. By using advanced LLMs to analyze both the paper
text and code, this app automatically detects mismatches in algorithms, parameters, data processing steps,
and other implementation details.
**β οΈ Important Limitations:**
Our research found that **recall is still low** - meaning the tool may miss some discrepancies.
**All outputs should be used with human verification** and should not be relied upon as the sole method
for discrepancy detection.
**LLM Provider Recommendations:**
- **Free Models (OpenRouter)**: Best for quick checks of already public paper+code combinations
- **Provider Models (OpenAI, Anthropic, etc.)**: Best for high precision and best recall
**Features:**
- Support for multiple LLM providers (free or premium models)
- Automatic content fetching from arXiv and GitHub
- File upload support for custom papers and repositories
- Secure API key handling (keys never stored or logged)
**Resources:**
- π¦ **Code**: [GitHub Repository](https://github.com/UKPLab/scicoqa)
- π **Dataset**: [Hugging Face Dataset](https://huggingface.co/datasets/ukplab/scicoqa)
- π **Project Website**: [ukplab.github.io/scicoqa](https://ukplab.github.io/scicoqa)
**Citation:**
If you find this tool useful, please cite our paper:
```bibtex
@article{scicoqa2026,
title = {SciCoQA: Quality Assurance for Scientific Paper-Code Alignment},
author = {BaumgΓ€rtner, Tim and Gurevych, Iryna},
journal = {arXiv preprint arXiv:XXXX.XXXXX},
year = {2026},
url = {https://github.com/UKPLab/scicoqa}
}
```
"""
)
# ========== SIDEBAR: Model Configuration ==========
with st.sidebar:
st.header("π€\uFE0F Model Configuration")
# Determine label based on current selection
model_config = None
model_name = None
display_model_name = None
# Check if we have a model config in session state
if "model_config" in st.session_state and st.session_state.model_config:
existing_config = st.session_state.model_config
display_model_name = existing_config.get("name") or existing_config.get("model", "Unknown")
if display_model_name:
st.caption(f"Current: {display_model_name}")
# Model type selection
model_type = st.radio(
"Model Type",
options=["Free Models (OpenRouter)", "Provider (OpenAI, Anthropic, Gemini, etc.)"],
# options=["Free Models (OpenRouter)", "Local Model (Ollama/vLLM)", "Provider (OpenAI, Anthropic, Gemini, etc.)"], # TODO: Re-enable Local Model option when fixed
help="Select free models (no API key) or provider models (requires API key)",
# help="Select free models (no API key), local models (Ollama/vLLM), or provider models (requires API key)", # TODO: Re-enable when local models are fixed
key="model_type_radio",
index=0, # Default to Free Models
)
# Store in session state for access outside sidebar
st.session_state.model_type = model_type
st.divider()
# Model selection based on type
if model_type == "Free Models (OpenRouter)":
# Fetch free models from OpenRouter API (uses file-based cache, refreshes daily)
if "free_models_cache" not in st.session_state:
with st.spinner("Loading free models from OpenRouter..."):
free_models_raw = fetch_free_models()
st.session_state.free_models_cache = free_models_raw
free_models_raw = st.session_state.free_models_cache
if not free_models_raw:
st.error("β οΈ Could not fetch free models from OpenRouter. Please try again later or use a different model type.")
model_config = None
else:
# Show privacy warning
st.warning(
"β οΈ **Privacy Notice**: Free models are provided via [OpenRouter](https://openrouter.ai). "
"The model provider may log your prompts and outputs. For enhanced privacy, consider using Provider models with your own API keys."
)
# Create model options from fetched models
model_options = {get_model_config(m)["name"]: get_model_config(m) for m in free_models_raw}
if model_options:
# Find default index for gpt-oss
model_names = list(model_options.keys())
default_index = 0
for idx, name in enumerate(model_names):
if "nemotron 3 nano 30b" in name.lower():
default_index = idx
break
model_name = st.selectbox(
"Select Free Model",
options=model_names,
help="Free models via OpenRouter (no API key required)",
key="free_model_select",
index=default_index,
)
model_config = model_options[model_name]
else:
st.error("β οΈ No free models available. Please try again later or use a different model type.")
model_config = None
# TODO: Re-enable when local models are fixed
# elif model_type == "Local Model (Ollama/vLLM)":
# st.info("π₯οΈ **Local Model**: Use models running locally via Ollama or vLLM (OpenAI-compatible server).")
#
# local_model_type = st.radio(
# "Local Server Type",
# options=["Ollama", "vLLM (OpenAI-compatible)"],
# help="Select the type of local server",
# key="local_server_type",
# )
#
# if local_model_type == "Ollama":
# # API Base URL comes first
# api_base = st.text_input(
# "API Base URL",
# value="http://localhost:11434",
# help="Ollama API base URL",
# key="ollama_api_base",
# )
#
# # Query Ollama for available models if API base is provided
# model_input = None
# if api_base and api_base.strip():
# try:
# with st.spinner("Fetching available models from Ollama..."):
# available_models = fetch_ollama_models(api_base.strip())
#
# if available_models:
# model_input = st.selectbox(
# "Select Model",
# options=available_models,
# help="Select a model from your Ollama server",
# key="ollama_model_select",
# )
# else:
# st.warning("β οΈ No models found or unable to connect to Ollama. You can still enter a model name manually.")
# model_input = st.text_input(
# "Model Name (manual entry)",
# placeholder="e.g., llama2, mistral, codellama",
# help="Enter the Ollama model name manually (without 'ollama/' prefix)",
# key="ollama_model_input_manual",
# )
# except Exception as e:
# logger.error(f"Error fetching Ollama models: {e}")
# st.warning(f"β οΈ Could not fetch models from Ollama: {str(e)}. You can still enter a model name manually.")
# model_input = st.text_input(
# "Model Name (manual entry)",
# placeholder="e.g., llama2, mistral, codellama",
# help="Enter the Ollama model name manually (without 'ollama/' prefix)",
# key="ollama_model_input_manual",
# )
# else:
# st.info("π‘ Enter the API Base URL above to see available models, or enter a model name manually below.")
# model_input = st.text_input(
# "Model Name",
# placeholder="e.g., llama2, mistral, codellama",
# help="Enter the Ollama model name (without 'ollama/' prefix)",
# key="ollama_model_input",
# )
#
# max_context = st.number_input(
# "Max Context (tokens)",
# min_value=1000,
# max_value=1000000,
# value=131072,
# step=1000,
# help="Maximum context window size in tokens",
# key="ollama_max_context",
# )
#
# if model_input and api_base:
# model_name = f"ollama/{model_input}"
# model_config = create_local_model_config(
# model=model_name,
# api_base=api_base.strip(),
# max_context=max_context,
# )
# else: # vLLM
# model_input = st.text_input(
# "Model Name",
# placeholder="e.g., gpt-3.5-turbo, mistralai/Mistral-7B-Instruct-v0.1",
# help="Enter the model name for vLLM",
# key="vllm_model_input",
# )
# api_base = st.text_input(
# "API Base URL",
# value="http://localhost:8000/v1",
# help="vLLM API base URL (OpenAI-compatible endpoint)",
# key="vllm_api_base",
# )
# max_context = st.number_input(
# "Max Context (tokens)",
# min_value=1000,
# max_value=1000000,
# value=131072,
# step=1000,
# help="Maximum context window size in tokens",
# key="vllm_max_context",
# )
#
# if model_input:
# model_name = model_input
# model_config = create_local_model_config(
# model=model_name,
# api_base=api_base,
# max_context=max_context,
# )
else: # Provider Model
st.info("π **Provider Model**: Use your own API keys to access premium models. Your keys are never stored, logged, or displayed.")
provider_subtype = st.radio(
"Model Selection",
options=["Preset", "Custom"],
help="Select from preset models or enter a custom model",
key="provider_subtype",
)
if provider_subtype == "Preset":
model_name = st.selectbox(
"Select Model",
options=list(PROVIDER_PRESETS.keys()),
help="Select a preset model (API key required)",
key="preset_model_select",
)
preset_config = PROVIDER_PRESETS[model_name]
api_key_env = preset_config["api_key_env"]
api_key_label = api_key_env.replace("_", " ").title()
api_key = st.text_input(
f"{api_key_label}",
type="password",
help=f"Enter your {api_key_label}. Your key is never stored, logged, or displayed.",
placeholder=f"sk-..." if "OPENAI" in api_key_env else "Enter API key",
key="preset_api_key",
)
if api_key:
model_config = create_provider_model_config(
model=preset_config["model"],
api_key=api_key,
max_context=preset_config["max_context"],
tokenizer=preset_config["tokenizer"],
)
else: # Custom
custom_model_name = st.text_input(
"Model Name (litellm format)",
placeholder="e.g., openai/gpt-5.2, anthropic/claude-sonnet-4-5, gemini/gemini-3-pro-preview",
help="Enter the model name in litellm format. See [litellm documentation](https://docs.litellm.ai/docs/providers) for supported formats.",
key="custom_model_name",
)
custom_max_context = st.number_input(
"Max Context (tokens)",
min_value=1000,
max_value=10000000,
value=128000,
step=1000,
help="Maximum context window size in tokens",
key="custom_max_context",
)
if custom_model_name:
provider = get_provider_from_model(custom_model_name)
api_key_env = get_api_key_env_name(provider)
api_key_label = api_key_env.replace("_", " ").title()
api_key = st.text_input(
f"{api_key_label}",
type="password",
help=f"Enter your {api_key_label}. Your key is never stored, logged, or displayed.",
placeholder=f"sk-..." if "OPENAI" in api_key_env else "Enter API key",
key="custom_api_key",
)
if api_key:
model_name = custom_model_name
model_config = create_provider_model_config(
model=custom_model_name,
api_key=api_key,
max_context=custom_max_context,
)
st.markdown(
"π **Need help with model format?** See the [litellm documentation](https://docs.litellm.ai/docs/providers) "
"for supported providers and model naming conventions."
)
st.caption("π Your API key is secure: never stored, logged, or displayed")
# Show model info if model is selected
if model_config:
display_name = model_config.get("name") or model_config.get("model", model_name or "Unknown")
st.caption(f"π Max Context: {model_config['max_context']:,} tokens")
# ========== MAIN AREA: Input Form and Results ==========
# Store model config in session state for next render
if model_config:
st.session_state.model_config = _safe_model_config_for_session(model_config)
st.session_state.model_name = model_config.get("name") or model_config.get("model", model_name or "Unknown")
# Input form
with st.form("discrepancy_form"):
# Input method selection using tabs
tab_links, tab_files = st.tabs(["arXiv and GitHub Links", "Upload Paper and Code Files"])
# Initialize variables
arxiv_url = None
github_url = None
paper_file = None
code_file = None
input_method = None
with tab_links:
col1, col2 = st.columns(2)
with col1:
arxiv_url = st.text_input(
"arXiv Paper",
value=st.session_state.get("example_arxiv_url", ""),
placeholder="https://arxiv.org/abs/2006.12834 or 2006.12834",
help="Enter the arXiv paper URL or just the paper ID",
label_visibility="visible",
)
with col2:
github_url = st.text_input(
"GitHub Code",
value=st.session_state.get("example_github_url", ""),
placeholder="https://github.com/username/repo",
help="Enter the full GitHub repository URL",
label_visibility="visible",
)
if arxiv_url or github_url:
input_method = "arXiv and GitHub Links"
with tab_files:
# Instructions section for file preparation
with st.expander("π How to prepare files", expanded=False):
st.markdown("""
<h3>Converting PDF to Markdown with Pandoc</h3>
1. Install pandoc:
```
brew install pandoc
```
For installing pandoc on Windows or Linux, see the [pandoc documentation](https://pandoc.org/installing.html).
2. Convert your latex to markdown:
```bash
pandoc main.tex -f latex -t markdown -s --wrap=none -o paper.md
```
<h3>Converting Repository to Text with Gitingest</h3>
1. Install gitingest:
```bash
pip install gitingest
```
2. Generate repository text file:
```bash
gitingest https://github.com/your-username/your-repo \\
-i "*.c,*.cc,*.cpp,*.cu,*.h,*.hpp,*.java,*.jl,*.m,*.matlab,Makefile,*.md,*.pl,*.ps1,*.py,*.r,*.sh,config.txt,*.rs,readme.txt,requirements_dev.txt,requirements-dev.txt,requirements.dev.txt,requirements.txt,*.scala,*.yaml,*.yml" \\
-o repo.txt \\
--token YOUR_GITHUB_TOKEN
```
**Note**: Modify the file extension list to include the files you want to include in the repository text file. For private repositories, you'll need a GitHub token. For public repositories, you can omit the `--token` parameter.
""", unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
paper_file = st.file_uploader(
"Paper Markdown File",
type=["md", "markdown", "txt"],
help="Upload the paper as a markdown file",
label_visibility="visible",
accept_multiple_files=False,
)
with col2:
code_file = st.file_uploader(
"Repository Text File",
type=["txt"],
help="Upload the repository as a text file (generated using gitingest)",
label_visibility="visible",
accept_multiple_files=False,
)
if paper_file or code_file:
input_method = "Upload Paper and Code Files"
submitted = st.form_submit_button("Detect Discrepancies", type="primary", use_container_width=True)
# Store model info in session state
st.session_state.model_config = _safe_model_config_for_session(model_config)
# Process form submission
if submitted:
# Determine input method based on which inputs are filled
# Check if files are provided (Upload method) - prioritize files if any are uploaded
if paper_file is not None or code_file is not None:
is_valid, error_msg = validate_files(paper_file, code_file)
if not is_valid:
st.error(error_msg)
return
# Read file contents
try:
paper_text = paper_file.read().decode("utf-8") if paper_file else None
code_text = code_file.read().decode("utf-8") if code_file else None
except Exception as e:
st.error(f"Error reading files: {str(e)}")
return
arxiv_url = None
github_url = None
# Otherwise check if URLs are provided (Links method)
elif arxiv_url or github_url:
is_valid, error_msg = validate_urls(arxiv_url, github_url)
if not is_valid:
st.error(error_msg)
return
paper_text = None
code_text = None
else:
st.error("Please provide either arXiv and GitHub links, or upload paper and code files.")
return
# Clear example values after form submission
if "example_arxiv_url" in st.session_state:
del st.session_state["example_arxiv_url"]
if "example_github_url" in st.session_state:
del st.session_state["example_github_url"]
# Validate model selection
if model_config is None:
st.error("Please select a valid model.")
return
# Validate API key for provider models
model_type = st.session_state.get("model_type", "Provider (OpenAI, Anthropic, Gemini, etc.)")
if model_type == "Provider (OpenAI, Anthropic, Gemini, etc.)":
if "api_key" not in model_config or not model_config.get("api_key"):
st.error("β οΈ API key required for provider models. Please enter your API key.")
return
# Process
with st.spinner("Processing..."):
results = process_discrepancy_detection(
paper_text=paper_text,
code_text=code_text,
arxiv_url=arxiv_url,
github_url=github_url,
model_config=model_config,
)
# Display results
if results["error"]:
st.error(f"β Error: {results['error']}")
return
# Display discrepancies
st.divider()
st.header("Results")
if results["discrepancies"]:
count = len(results["discrepancies"])
discrepancy_text = "discrepancy" if count == 1 else "discrepancies"
st.success(f"Found {count} {discrepancy_text}")
# Display each discrepancy in a tab
tab_labels = [f"Discrepancy {idx}" for idx in range(1, count + 1)]
tabs = st.tabs(tab_labels)
for idx, (tab, discrepancy) in enumerate(zip(tabs, results["discrepancies"])):
with tab:
st.markdown(discrepancy)
st.divider()
else:
st.info("β
No discrepancies found between the paper and code.")
st.divider()
# Technical Details - Combined debug sections
with st.expander("π§ Technical Details", expanded=False):
# Raw prompt section
if results["prompt"]:
st.subheader("π Raw Prompt")
st.markdown("**Final prompt sent to the LLM (after truncation):**")
model_config = st.session_state.get("model_config")
if model_config:
tokenizer_name = model_config["tokenizer"]
token_counter = TokenCounter(model=tokenizer_name)
prompt_tokens = token_counter(results["prompt"])
st.caption(f"Prompt tokens: {prompt_tokens:,}")
# Make prompt scrollable using container
with st.container(height=500):
st.code(results["prompt"], language="text")
st.divider()
# Raw output section
if results["llm_response"]:
st.subheader("π Raw LLM Output")
content = (
results["llm_response"]
.get("choices", [{}])[0]
.get("message", {})
.get("content", "")
)
# Show token count instead of character count
model_config = st.session_state.get("model_config")
if model_config:
tokenizer_name = model_config["tokenizer"]
token_counter = TokenCounter(model=tokenizer_name)
output_tokens = token_counter(content)
st.caption(f"Output tokens: {output_tokens:,}")
st.code(content, language="yaml")
st.divider()
# Step timing information
if results.get("step_timings"):
st.subheader("β±οΈ Step Timing")
step_timings = results["step_timings"]
total_time = sum(step_timings.values())
# Display timing for each step
for step_name, step_time in step_timings.items():
percentage = (step_time / total_time * 100) if total_time > 0 else 0
st.write(f"**{step_name}**: {step_time:.2f}s ({percentage:.1f}%)")
st.metric("**Total Time**", f"{total_time:.2f}s")
st.divider()
# Debug info
st.subheader("π Debug Information")
col1, col2, col3 = st.columns(3)
with col1:
# Get model config from session state for token counting
model_config = st.session_state.get("model_config")
if model_config:
tokenizer_name = model_config["tokenizer"]
token_counter = TokenCounter(model=tokenizer_name)
if results["paper_text"]:
paper_tokens = token_counter(results["paper_text"])
st.metric("Paper Tokens", f"{paper_tokens:,}")
if results["code_prompt"]:
code_tokens = token_counter(results["code_prompt"])
st.metric("Code Tokens", f"{code_tokens:,}")
with col2:
if results["llm_response"]:
usage = results["llm_response"].get("usage", {})
if usage:
input_tokens = usage.get("prompt_tokens", "N/A")
output_tokens = usage.get("completion_tokens", "N/A")
st.metric("Input Tokens", f"{input_tokens:,}" if input_tokens != "N/A" else "N/A")
st.metric("Output Tokens", f"{output_tokens:,}" if output_tokens != "N/A" else "N/A")
with col3:
if results["llm_response"]:
usage = results["llm_response"].get("usage", {})
if usage:
total_tokens = usage.get("total_tokens", "N/A")
st.metric("Total Tokens", f"{total_tokens:,}" if total_tokens != "N/A" else "N/A")
# Extract cost from response metadata
cost = results["llm_response"].get("metadata", {}).get("cost", 0.0)
if cost > 0:
st.metric("Cost", f"${cost:.4f}")
else:
st.metric("Cost", "Free")
if __name__ == "__main__":
main()
|