File size: 47,042 Bytes
9c68964 ec86ed3 9c68964 ec86ed3 9c68964 ec86ed3 9c68964 ec86ed3 9b3a3bd 9c68964 9b3a3bd 9c68964 9b3a3bd 49865eb 9c68964 bde00a5 ec86ed3 55a2fb1 ec86ed3 9c68964 bedcfff 9c68964 bedcfff 9b3a3bd bedcfff 9c68964 bde00a5 9c68964 9b3a3bd 9c68964 0f4d845 9b3a3bd 9c68964 b52baf5 9c68964 9371dc4 9c68964 b52baf5 9c68964 9371dc4 9c68964 9371dc4 9c68964 b52baf5 9c68964 9371dc4 9c68964 9371dc4 9c68964 501e8a3 9c68964 0f4d845 9c68964 0f4d845 9c68964 3b4c3ae 0f4d845 5c3511e 0f4d845 9c68964 0f4d845 9c68964 0f4d845 ad40aea 5c3511e ad40aea 19a995e ad40aea 19a995e 5c3511e ad40aea 5c3511e 19a995e 5c3511e 19a995e 1024d82 abec366 0f4d845 1024d82 0f4d845 b52baf5 9c68964 bde00a5 9c68964 bde00a5 9c68964 ec86ed3 9c68964 70d6370 9c68964 5c3511e 9c68964 5c3511e 9c68964 291e22b 9c68964 054913b 9cccab0 978f8be 9cccab0 054913b 9c68964 27c6085 9c68964 49cb056 27c6085 9c68964 49865eb 9c68964 9429b61 9c68964 0f4d845 a5c143f ec86ed3 1de872d ec86ed3 0f4d845 9c68964 ec86ed3 1de872d 9c68964 ec86ed3 9c68964 ec86ed3 9c68964 ec86ed3 9c68964 a5c143f 229f990 a5c143f 229f990 a5c143f 229f990 a5c143f ad40aea 9c68964 a5c143f 9c68964 a5c143f 5ce765e ad40aea a5c143f 55a2fb1 a5c143f ad40aea 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f ad40aea 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 a5c143f 55a2fb1 0f4d845 a5c143f 0f4d845 ec86ed3 1e0fb78 9c68964 ec86ed3 9c68964 ec86ed3 9c68964 55a2fb1 9c68964 55a2fb1 ad40aea 55a2fb1 ad40aea 55a2fb1 9c68964 55a2fb1 9c68964 55a2fb1 9c68964 55a2fb1 9c68964 55a2fb1 ad40aea 55a2fb1 ad40aea 55a2fb1 9c68964 55a2fb1 9c68964 55a2fb1 9c68964 03acfce 9c68964 ec86ed3 9c68964 bde00a5 9c68964 0f4d845 9c68964 0f4d845 9c68964 10d942e 03acfce ad40aea 10d942e ad40aea ec86ed3 9c68964 a5c143f 03acfce ec86ed3 03acfce 9c68964 ad40aea 10d942e ad40aea 9c68964 10d942e 9c68964 ad40aea 10d942e 9c68964 10d942e ad40aea 10d942e ad40aea 10d942e 9c68964 10d942e 9c68964 ad40aea 10d942e 9c68964 ad40aea 10d942e ad40aea 9c68964 ad40aea 10d942e ad40aea 9c68964 b52baf5 9c68964 3b4c3ae 9c68964 ad40aea 9c68964 0f4d845 9c68964 03acfce 9c68964 6616442 bc49057 9c68964 adfb223 |
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 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 |
import dotenv
import evalica
import gitlab
import io
import json
import os
import random
import re
import threading
import gradio as gr
import pandas as pd
from datetime import datetime
from github import Github
from gradio_leaderboard import Leaderboard
from huggingface_hub import upload_file, hf_hub_download, HfFolder, HfApi
from openai import OpenAI
# Load environment variables
dotenv.load_dotenv()
# Initialize OpenAI Client
api_key = os.getenv("API_KEY")
base_url = "https://api.pandalla.ai/v1"
openai_client = OpenAI(api_key=api_key, base_url=base_url)
# Timeout in seconds for model responses
TIMEOUT = 90
# Hint string constant
SHOW_HINT_STRING = True # Set to False to hide the hint string altogether
HINT_STRING = "Once signed in, your votes will be recorded securely."
# Load context length limits
with open("context_window.json", "r") as file:
context_window = json.load(file)
# Get list of available models
available_models = list(context_window.keys())
# Initialize global variables
models_state = {}
conversation_state = {}
def fetch_github_content(url):
"""Fetch detailed content from a GitHub URL using PyGithub."""
token = os.getenv("GITHUB_TOKEN")
if not token:
print("GITHUB_TOKEN not set.")
return None
g = Github(token)
try:
match = re.match(
r"https?://github\.com/([^/]+)/([^/]+)/(commit|pull|issues|discussions)/([a-z0-9]+)",
url,
)
if not match:
repo_part = re.match(r"https?://github\.com/([^/]+)/([^/]+)/?", url)
if repo_part:
owner, repo = repo_part.groups()
repo = g.get_repo(f"{owner}/{repo}")
try:
readme = repo.get_readme()
return readme.decoded_content.decode()
except:
return repo.description
return None
owner, repo, category, identifier = match.groups()
repo = g.get_repo(f"{owner}/{repo}")
if category == "commit":
commit = repo.get_commit(identifier)
return commit.__dict__
elif category in ["pull", "issues"]:
obj = (
repo.get_pull(int(identifier))
if category == "pull"
else repo.get_issue(int(identifier))
)
return obj.__dict__
except Exception as e:
print(f"GitHub API error: {e}")
return None
def fetch_gitlab_content(url):
"""Fetch content from GitLab URL using python-gitlab."""
token = os.getenv("GITLAB_TOKEN")
if not token:
print("GITLAB_TOKEN not set.")
return None
gl = gitlab.Gitlab(private_token=token)
try:
match = re.match(
r"https?://gitlab\.com/([^/]+)/([^/]+)/-/?(commit|merge_requests|issues)/([^/]+)",
url,
)
if not match:
repo_part = re.match(r"https?://gitlab\.com/([^/]+)/([^/]+)/?", url)
if repo_part:
owner, repo = repo_part.groups()
project = gl.projects.get(f"{owner}/{repo}")
try:
readme = project.files.get(file_path="README.md", ref="master")
return readme.decode()
except gitlab.exceptions.GitlabGetError:
return project.description
return None
owner, repo, category, identifier = match.groups()
project = gl.projects.get(f"{owner}/{repo}")
if category == "commit":
commit = project.commits.get(identifier)
return commit.__dict__
elif category == "merge_requests":
merge_request = project.mergerequests.get(int(identifier))
return merge_request.__dict__
elif category == "issues":
issue = project.issues.get(int(identifier))
return issue.__dict__
except Exception as e:
print(f"GitLab API error: {e}")
return None
def fetch_huggingface_content(url):
"""Fetch detailed content from a Hugging Face URL using huggingface_hub API."""
token = os.getenv("HF_TOKEN")
if not token:
print("HF_TOKEN not set.")
return None
api = HfApi(token=token)
try:
if "/commit/" in url:
commit_hash = url.split("/commit/")[-1]
repo_id = url.split("/commit/")[0].split("huggingface.co/")[-1]
commits = api.list_repo_commits(repo_id=repo_id, revision=commit_hash)
if commits:
commit = commits[0]
return commit.__dict__
return None
elif "/discussions/" in url:
discussion_num = int(url.split("/discussions/")[-1])
repo_id = url.split("/discussions/")[0].split("/huggingface.co/")[-1]
discussion = api.get_discussion_details(
repo_id=repo_id, discussion_num=discussion_num
)
return discussion.__dict__
else:
repo_id = url.split("huggingface.co/")[-1]
repo_info = api.repo_info(repo_id=repo_id)
return repo_info.__dict__
except Exception as e:
print(f"Hugging Face API error: {e}")
return None
def fetch_url_content(url):
"""Main URL content fetcher that routes to platform-specific handlers."""
try:
if "github.com" in url:
return fetch_github_content(url)
elif "gitlab.com" in url:
return fetch_gitlab_content(url)
elif "huggingface.co" in url:
return fetch_huggingface_content(url)
except Exception as e:
print(f"Error fetching URL content: {e}")
return ""
# Truncate prompt
def truncate_prompt(user_input, model_alias, models, conversation_state):
"""
Truncate the conversation history and user input to fit within the model's context window.
Args:
user_input (str): The latest input from the user.
model_alias (str): Alias for the model being used (e.g., "Model A", "Model B").
models (dict): Dictionary mapping model aliases to their names.
conversation_state (dict): State containing the conversation history for all models.
Returns:
str: Truncated conversation history and user input.
"""
model_name = models[model_alias]
context_length = context_window.get(model_name, 4096)
# Get the full conversation history for the model
history = conversation_state.get(model_name, [])
full_conversation = [
{"role": msg["role"], "content": msg["content"]} for msg in history
]
full_conversation.append({"role": "user", "content": user_input})
# Convert to JSON string for accurate length measurement
json_conversation = json.dumps(full_conversation)
if len(json_conversation) <= context_length:
# If the full conversation fits, return it as-is
return full_conversation
# Truncate based on the current round
if not history: # First round, truncate FILO
while len(json.dumps(full_conversation)) > context_length:
full_conversation.pop(0) # Remove from the start
else: # Subsequent rounds, truncate FIFO
while len(json.dumps(full_conversation)) > context_length:
full_conversation.pop(-1) # Remove from the end
return full_conversation
def chat_with_models(
user_input, model_alias, models, conversation_state, timeout=TIMEOUT
):
model_name = models[model_alias]
truncated_input = truncate_prompt(
user_input, model_alias, models, conversation_state
)
conversation_state.setdefault(model_name, []).append(
{"role": "user", "content": user_input}
)
response_event = threading.Event() # Event to signal response completion
model_response = {"content": None, "error": None}
def request_model_response():
try:
request_params = {"model": model_name, "messages": truncated_input}
response = openai_client.chat.completions.create(**request_params)
model_response["content"] = response.choices[0].message.content
except Exception as e:
model_response["error"] = f"{model_name} model is not available. Error: {e}"
finally:
response_event.set() # Signal that the response is completed
# Start the model request in a separate thread
response_thread = threading.Thread(target=request_model_response)
response_thread.start()
# Wait for the specified timeout
response_event_occurred = response_event.wait(timeout)
if not response_event_occurred:
raise TimeoutError(
f"The {model_alias} model did not respond within {timeout} seconds."
)
elif model_response["error"]:
raise Exception(model_response["error"])
else:
formatted_response = f"```\n{model_response['content']}\n```"
conversation_state[model_name].append(
{"role": "assistant", "content": model_response["content"]}
)
return formatted_response
def save_content_to_hf(feedback_data, repo_name):
"""
Save feedback content to Hugging Face repository organized by quarter.
"""
# Serialize the content to JSON and encode it as bytes
json_content = json.dumps(feedback_data, indent=4).encode("utf-8")
# Create a binary file-like object
file_like_object = io.BytesIO(json_content)
# Get the current year and quarter
now = datetime.now()
quarter = (now.month - 1) // 3 + 1
year_quarter = f"{now.year}_Q{quarter}"
day_hour_minute_second = now.strftime("%d_%H%M%S")
# Define the path in the repository
filename = f"{year_quarter}/{day_hour_minute_second}.json"
# Ensure the user is authenticated with HF
token = HfFolder.get_token()
if token is None:
raise ValueError("Please log in to Hugging Face using `huggingface-cli login`.")
# Upload to Hugging Face repository
upload_file(
path_or_fileobj=file_like_object,
path_in_repo=filename,
repo_id=repo_name,
repo_type="dataset",
use_auth_token=token,
)
def load_content_from_hf(repo_name="SE-Arena/votes"):
"""
Read feedback content from a Hugging Face repository based on the current quarter.
Args:
repo_name (str): Hugging Face repository name.
Returns:
list: Aggregated feedback data read from the repository.
"""
feedback_data = []
# Get the current year and quarter
now = datetime.now()
quarter = (now.month - 1) // 3 + 1
year_quarter = f"{now.year}_Q{quarter}"
try:
api = HfApi()
# List all files in the repository
for file in api.list_repo_files(repo_id=repo_name, repo_type="dataset"):
# Filter files by current year and month
if year_quarter not in file:
continue
# Download and aggregate data
local_path = hf_hub_download(
repo_id=repo_name, filename=file, repo_type="dataset"
)
with open(local_path, "r") as f:
data = json.load(f)
if isinstance(data, list):
feedback_data.extend(data)
elif isinstance(data, dict):
feedback_data.append(data)
return feedback_data
except:
raise Exception("Error loading feedback data from Hugging Face repository.")
def get_leaderboard_data(feedback_entry=None):
# Load feedback data from the Hugging Face repository
feedback_data = load_content_from_hf()
feedback_df = pd.DataFrame(feedback_data)
# Concatenate the new feedback with the existing leaderboard data
if feedback_entry is not None:
feedback_df = pd.concat(
[feedback_df, pd.DataFrame([feedback_entry])], ignore_index=True
)
if feedback_df.empty:
return pd.DataFrame(
columns=[
"Rank",
"Model",
"Elo Score",
"Consistency Score",
"Average Win Rate",
"Bradley-Terry Coefficient",
"Eigenvector Centrality Value",
"Newman Modularity Score",
"PageRank Score",
]
)
# map vote to winner
feedback_df["winner"] = feedback_df["winner"].map(
{
"left": evalica.Winner.X,
"right": evalica.Winner.Y,
"tie": evalica.Winner.Draw,
}
)
# Calculate scores using various metrics
avr_result = evalica.average_win_rate(
feedback_df["left"], feedback_df["right"], feedback_df["winner"]
)
bt_result = evalica.bradley_terry(
feedback_df["left"], feedback_df["right"], feedback_df["winner"]
)
newman_result = evalica.newman(
feedback_df["left"], feedback_df["right"], feedback_df["winner"]
)
eigen_result = evalica.eigen(
feedback_df["left"], feedback_df["right"], feedback_df["winner"]
)
elo_result = evalica.elo(
feedback_df["left"], feedback_df["right"], feedback_df["winner"]
)
pagerank_result = evalica.pagerank(
feedback_df["left"], feedback_df["right"], feedback_df["winner"]
)
# Calculate consistency score as a pandas Series aligned with other metrics
is_result = pd.Series(
"N/A", index=elo_result.scores.index
) # Initialize with zeros using same index
# Loop through models and update values
for model in is_result.index:
# Filter self-matches for this model
self_matches = feedback_df[
(feedback_df["left"] == model) & (feedback_df["right"] == model)
]
totals = len(self_matches)
if totals:
# Count non-draw outcomes (wins or losses)
draws = self_matches[self_matches["winner"] == evalica.Winner.Draw].shape[0]
# Store as percentage directly
is_result[model] = round(draws / totals * 100, 2)
# Combine all results into a single DataFrame
leaderboard_data = pd.DataFrame(
{
"Model": elo_result.scores.index,
"Elo Score": elo_result.scores.values,
"Consistency Score": is_result.values,
"Average Win Rate": avr_result.scores.values * 100,
"Bradley-Terry Coefficient": bt_result.scores.values,
"Eigenvector Centrality Value": eigen_result.scores.values,
"Newman Modularity Score": newman_result.scores.values,
"PageRank Score": pagerank_result.scores.values,
}
)
# Round all numeric columns to two decimal places
leaderboard_data = leaderboard_data.round(
{
"Elo Score": 2,
"Average Win Rate": 2,
"Bradley-Terry Coefficient": 2,
"Eigenvector Centrality Value": 2,
"Newman Modularity Score": 2,
"PageRank Score": 2,
}
)
# Add a Rank column based on Elo scores
leaderboard_data["Rank"] = (
leaderboard_data["Elo Score"].rank(ascending=False).astype(int)
)
# Place rank in the first column
leaderboard_data = leaderboard_data[
["Rank"] + [col for col in leaderboard_data.columns if col != "Rank"]
]
return leaderboard_data
# Function to enable or disable submit buttons based on textbox content
def toggle_submit_button(text):
if not text or text.strip() == "":
return gr.update(interactive=False) # Disable the button
else:
return gr.update(interactive=True) # Enable the button
# Gradio Interface
with gr.Blocks() as app:
user_authenticated = gr.State(False)
models_state = gr.State({})
conversation_state = gr.State({})
with gr.Tab("🏆Leaderboard"):
# Add title and description as a Markdown component
leaderboard_intro = gr.Markdown(
"""
# 🏆 Software Engineering (SE) Chatbot Leaderboard: Community-Driven Evaluation of Top SE Chatbots
The SE Arena is an open-source platform designed to evaluate language models through human preference, fostering transparency and collaboration. Developed by researchers at [Software Analysis and Intelligence Lab (SAIL)](https://sail.cs.queensu.ca), the platform empowers the community to assess and compare the performance of leading foundation models in SE tasks. For technical details, check out our [paper](https://arxiv.org/abs/2502.01860).
""",
elem_classes="leaderboard-intro",
)
# Initialize the leaderboard with the DataFrame containing the expected columns
leaderboard_component = Leaderboard(
value=get_leaderboard_data(),
select_columns=[
"Rank",
"Model",
"Elo Score",
"Consistency Score",
],
search_columns=["Model"],
filter_columns=[
"Elo Score",
"Consistency Score",
"Average Win Rate",
"Bradley-Terry Coefficient",
"Eigenvector Centrality Value",
"Newman Modularity Score",
"PageRank Score",
],
)
# Add a citation block in Markdown
citation_component = gr.Markdown(
"""
Made with ❤️ for SE Arena. If this work is useful to you, please consider citing:
```
@inproceedings{zhao2025se,
title={SE Arena: An Interactive Platform for Evaluating Foundation Models in Software Engineering},
author={Zhao, Zhimin},
booktitle={ACM international conference on AI Foundation Models and Software Engineering},
year={2025}}
```
"""
)
with gr.Tab("⚔️Arena"):
# Add title and description as a Markdown component
arena_intro = gr.Markdown(
f"""
# ⚔️ Software Engineering (SE) Arena: Explore and Test the Best SE Chatbots with Long-Context Interactions
## 📜How It Works
- **Blind Comparison**: Submit a SE-related query to two anonymous chatbots randomly selected from up to {len(available_models)} top models from OpenAI, Gemini, Grok, Claude, Deepseek, Qwen, Llama, Mistral, and others.
- **Interactive Voting**: Engage in multi-turn dialogues with both chatbots and compare their responses. You can continue the conversation until you confidently choose the better model.
- **Fair Play Rules**: Votes are counted only if chatbot identities remain anonymous. Revealing a chatbot's identity disqualifies the session.
**Note:** Due to budget constraints, responses that take longer than {TIMEOUT} seconds to generate will be discarded.
""",
elem_classes="arena-intro",
)
# Add Hugging Face Sign In button and message
with gr.Row():
# Define the markdown text with or without the hint string
markdown_text = "## Please sign in first to vote!"
if SHOW_HINT_STRING:
markdown_text += f"\n{HINT_STRING}"
hint_markdown = gr.Markdown(markdown_text, elem_classes="markdown-text")
login_button = gr.Button(
"Sign in with Hugging Face", elem_id="oauth-button"
)
guardrail_message = gr.Markdown("", visible=False, elem_id="guardrail-message")
# NEW: Add a textbox for the repository URL above the user prompt
repo_url = gr.Textbox(
show_label=False,
placeholder="Optional: Enter the URL of a repository (GitHub, GitLab, Hugging Face), issue, commit, or pull request.",
lines=1,
interactive=False,
)
# Components with initial non-interactive state
shared_input = gr.Textbox(
show_label=False,
placeholder="Enter your query for both models here.",
lines=2,
interactive=False, # Initially non-interactive
)
send_first = gr.Button(
"Submit", visible=True, interactive=False
) # Initially non-interactive
# Add event listener to shared_input to toggle send_first button
shared_input.change(
fn=toggle_submit_button, inputs=shared_input, outputs=send_first
)
user_prompt_md = gr.Markdown(value="", visible=False)
with gr.Column():
shared_input
user_prompt_md
with gr.Row():
response_a_title = gr.Markdown(value="", visible=False)
response_b_title = gr.Markdown(value="", visible=False)
with gr.Row():
response_a = gr.Markdown(label="Response from Model A")
response_b = gr.Markdown(label="Response from Model B")
# Add a popup component for timeout notification
with gr.Row(visible=False) as timeout_popup:
timeout_message = gr.Markdown(
"### Timeout\n\nOne of the models did not respond within 1 minute. Please try again."
)
close_popup_btn = gr.Button("Okay")
def close_timeout_popup():
# Re-enable or disable the submit buttons based on the current textbox content
shared_input_state = gr.update(interactive=True)
send_first_state = toggle_submit_button(shared_input.value)
model_a_input_state = gr.update(interactive=True)
model_a_send_state = toggle_submit_button(model_a_input.value)
model_b_input_state = gr.update(interactive=True)
model_b_send_state = toggle_submit_button(model_b_input.value)
# Keep repo_url in sync with shared_input
repo_url_state = gr.update(interactive=True)
return (
gr.update(visible=False), # Hide the timeout popup
shared_input_state, # Update shared_input
send_first_state, # Update send_first button
model_a_input_state, # Update model_a_input
model_a_send_state, # Update model_a_send button
model_b_input_state, # Update model_b_input
model_b_send_state, # Update model_b_send button
repo_url_state, # Update repo_url button
)
# Multi-round inputs, initially hidden
with gr.Row(visible=False) as multi_round_inputs:
model_a_input = gr.Textbox(label="Model A Input", lines=1)
model_a_send = gr.Button(
"Send to Model A", interactive=False
) # Initially disabled
model_b_input = gr.Textbox(label="Model B Input", lines=1)
model_b_send = gr.Button(
"Send to Model B", interactive=False
) # Initially disabled
# Add event listeners to model_a_input and model_b_input to toggle their submit buttons
model_a_input.change(
fn=toggle_submit_button, inputs=model_a_input, outputs=model_a_send
)
model_b_input.change(
fn=toggle_submit_button, inputs=model_b_input, outputs=model_b_send
)
close_popup_btn.click(
close_timeout_popup,
inputs=[],
outputs=[
timeout_popup,
shared_input,
send_first,
model_a_input,
model_a_send,
model_b_input,
model_b_send,
repo_url,
],
)
def guardrail_check_se_relevance(user_prompt):
"""
Use gpt-4o-mini to check if the user_prompt is SE-related.
Return True if it is SE-related, otherwise False.
"""
# Example instructions for classification — adjust to your needs
system_message = {
"role": "system",
"content": (
"You are a classifier that decides if a user's question is relevant to software engineering. "
"If the question is about software engineering concepts, tools, processes, or code, respond with 'Yes'. "
"Otherwise, respond with 'No'."
),
}
user_message = {"role": "user", "content": user_prompt}
try:
# Make the chat completion call
response = openai_client.chat.completions.create(
model="gpt-4o-mini", messages=[system_message, user_message]
)
classification = response.choices[0].message.content.strip().lower()
# Check if the LLM responded with 'Yes'
return classification.lower().startswith("yes")
except Exception as e:
print(f"Guardrail check failed: {e}")
# If there's an error, you might decide to fail open (allow) or fail closed (block).
# Here we default to fail open, but you can change as needed.
return True
def disable_first_submit_ui():
"""First function to immediately disable UI elements"""
return (
# [0] guardrail_message: hide
gr.update(visible=False),
# [1] shared_input: disable but keep visible
gr.update(interactive=False),
# [2] repo_url: disable but keep visible
gr.update(interactive=False),
# [3] send_first: disable and show loading state
gr.update(interactive=False, value="Processing..."),
)
# Function to update model titles and responses
def update_model_titles_and_responses(
repo_url, user_input, models_state, conversation_state
):
# Guardrail check first
if not repo_url and not guardrail_check_se_relevance(user_input):
# Return updates to show the guardrail message and re-enable UI
return (
# [0] guardrail_message: Show guardrail message
gr.update(
value="### Oops! Try asking something about software engineering. Thanks!",
visible=True,
),
# [1] shared_input: clear and re-enable
gr.update(value="", interactive=True, visible=True),
# [2] repo_url: clear and re-enable
gr.update(value="", interactive=True, visible=True),
# [3] user_prompt_md: clear and hide
gr.update(value="", visible=False),
# [4] response_a_title: clear and hide
gr.update(value="", visible=False),
# [5] response_b_title: clear and hide
gr.update(value="", visible=False),
# [6] response_a: clear response
gr.update(value=""),
# [7] response_b: clear response
gr.update(value=""),
# [8] multi_round_inputs: hide
gr.update(visible=False),
# [9] vote_panel: hide
gr.update(visible=False),
# [10] send_first: re-enable button with original text
gr.update(visible=True, interactive=True, value="Submit"),
# [11] feedback: enable the selection
gr.update(interactive=True),
# [12] models_state: pass state as-is
models_state,
# [13] conversation_state: pass state as-is
conversation_state,
# [14] timeout_popup: hide
gr.update(visible=False),
# [15] model_a_send: disable
gr.update(interactive=False),
# [16] model_b_send: disable
gr.update(interactive=False),
# [17] thanks_message: hide
gr.update(visible=False),
)
repo_info = fetch_url_content(repo_url)
# Combine repository info (if available) with the user query.
combined_user_input = (
f"Repo-related Information: {repo_info}\n\n{user_input}"
if repo_info
else user_input
)
# Randomly select two models for the comparison
selected_models = [random.choice(available_models) for _ in range(2)]
models = {"Model A": selected_models[0], "Model B": selected_models[1]}
# Update the states
models_state.clear()
models_state.update(models)
conversation_state.clear()
conversation_state.update({name: [] for name in models.values()})
try:
response_a = chat_with_models(
combined_user_input, "Model A", models_state, conversation_state
)
response_b = chat_with_models(
combined_user_input, "Model B", models_state, conversation_state
)
except TimeoutError as e:
# Handle timeout by resetting components and showing a popup.
return (
# [0] guardrail_message: hide
gr.update(visible=False),
# [1] shared_input: re-enable and clear
gr.update(value="", interactive=True, visible=True),
# [2] repo_url: re-enable and clear
gr.update(value="", interactive=True, visible=True),
# [3] user_prompt_md: hide
gr.update(value="", visible=False),
# [4] response_a_title: hide
gr.update(value="", visible=False),
# [5] response_b_title: hide
gr.update(value="", visible=False),
# [6] response_a: clear
gr.update(value=""),
# [7] response_b: clear
gr.update(value=""),
# [8] multi_round_inputs: hide
gr.update(visible=False),
# [9] vote_panel: hide
gr.update(visible=False),
# [10] send_first: re-enable with original text
gr.update(visible=True, interactive=True, value="Submit"),
# [11] feedback: disable
gr.update(interactive=False),
# [12] models_state: pass state as-is
models_state,
# [13] conversation_state: pass state as-is
conversation_state,
# [14] timeout_popup: show popup
gr.update(visible=True),
# [15] model_a_send: disable
gr.update(interactive=False),
# [16] model_b_send: disable
gr.update(interactive=False),
# [17] thanks_message: hide
gr.update(visible=False),
)
except Exception as e:
raise gr.Error(str(e))
# Determine the initial state of the multi-round send buttons
model_a_send_state = toggle_submit_button("")
model_b_send_state = toggle_submit_button("")
# Return the updates for all 18 outputs.
return (
# [0] guardrail_message: hide (since no guardrail issue)
gr.update(visible=False),
# [1] shared_input: re-enable but hide
gr.update(interactive=True, visible=False),
# [2] repo_url: re-enable but hide
gr.update(interactive=True, visible=False),
# [3] user_prompt_md: display the user's query
gr.update(value=f"**Your Query:**\n\n{user_input}", visible=True),
# [4] response_a_title: show title for Model A
gr.update(value="### Model A:", visible=True),
# [5] response_b_title: show title for Model B
gr.update(value="### Model B:", visible=True),
# [6] response_a: display Model A response
gr.update(value=response_a),
# [7] response_b: display Model B response
gr.update(value=response_b),
# [8] multi_round_inputs: show the input section for multi-round dialogues
gr.update(visible=True),
# [9] vote_panel: show vote panel
gr.update(visible=True),
# [10] send_first: hide the submit button but restore label
gr.update(visible=False, value="Submit"),
# [11] feedback: enable the feedback selection
gr.update(interactive=True),
# [12] models_state: pass updated models_state
models_state,
# [13] conversation_state: pass updated conversation_state
conversation_state,
# [14] timeout_popup: hide any timeout popup if visible
gr.update(visible=False),
# [15] model_a_send: set state of the model A send button
model_a_send_state,
# [16] model_b_send: set state of the model B send button
model_b_send_state,
# [17] thanks_message: hide the thank-you message
gr.update(visible=False),
)
# Feedback panel, initially hidden
with gr.Row(visible=False) as vote_panel:
feedback = gr.Radio(
choices=["Model A", "Model B", "Can't Decide"],
label="Which model do you prefer?",
value="Can't Decide",
interactive=False, # Initially not interactive
)
submit_feedback_btn = gr.Button("Submit Feedback", interactive=False)
thanks_message = gr.Markdown(
value="## Thanks for your vote!", visible=False
) # Add thank you message
def hide_thanks_message():
return gr.update(visible=False)
# Function to handle login
def handle_login():
"""
Handle user login using Hugging Face OAuth with automatic redirection.
"""
try:
# Use Hugging Face OAuth to initiate login
HfApi()
token = HfFolder.get_token()
if not token:
raise Exception("Authentication token not found.")
# If token is successfully retrieved, update the interface state
return (
gr.update(visible=False), # Hide the login button
gr.update(interactive=True), # repo_url -> Enable in sync
gr.update(interactive=True), # Enable shared_input
gr.update(
interactive=False
), # Keep send_first button disabled initially
gr.update(interactive=True), # Enable feedback radio buttons
gr.update(interactive=True), # Enable submit_feedback_btn
gr.update(visible=False), # Hide the hint string
)
except Exception as e:
# Handle login failure
print(f"Login failed: {e}")
return (
gr.update(visible=True), # Keep the login button visible
gr.update(interactive=False), # repo_url -> disable if login failed
gr.update(interactive=False), # Keep shared_input disabled
gr.update(interactive=False), # Keep send_first disabled
gr.update(
interactive=False
), # Keep feedback radio buttons disabled
gr.update(interactive=False), # Keep submit_feedback_btn disabled
gr.update(visible=True), # Show the hint string
)
# Handle the login button click
login_button.click(
handle_login,
inputs=[],
outputs=[
login_button, # Hide the login button after successful login
repo_url, # Keep this in sync with shared_input
shared_input, # Enable shared_input
send_first, # Enable send_first button
feedback, # Enable feedback radio buttons
submit_feedback_btn, # Enable submit_feedback_btn
hint_markdown, # Hide the hint string
],
)
# First round handling
send_first.click(
fn=hide_thanks_message, inputs=[], outputs=[thanks_message]
).then(
fn=disable_first_submit_ui, # First disable UI
inputs=[],
outputs=[
guardrail_message,
shared_input,
repo_url,
send_first, # Just the essential UI elements to update immediately
],
).then(
fn=update_model_titles_and_responses, # Then do the actual processing
inputs=[repo_url, shared_input, models_state, conversation_state],
outputs=[
guardrail_message,
shared_input,
repo_url,
user_prompt_md,
response_a_title,
response_b_title,
response_a,
response_b,
multi_round_inputs,
vote_panel,
send_first,
feedback,
models_state,
conversation_state,
timeout_popup,
model_a_send,
model_b_send,
thanks_message,
],
)
def disable_model_a_ui():
"""First function to immediately disable model A UI elements"""
return (
# [0] model_a_input: disable
gr.update(interactive=False),
# [1] model_a_send: disable and show loading state
gr.update(interactive=False, value="Processing..."),
)
# Handle subsequent rounds
def handle_model_a_send(user_input, models_state, conversation_state):
try:
response = chat_with_models(
user_input, "Model A", models_state, conversation_state
)
# Clear the input box and disable the send button
return (
response,
conversation_state,
gr.update(visible=False),
gr.update(
value="", interactive=True
), # Clear and enable model_a_input
gr.update(
interactive=False, value="Send to Model A"
), # Reset button text
)
except TimeoutError as e:
# Disable inputs when timeout occurs
return (
gr.update(value=""), # Clear response
conversation_state,
gr.update(visible=True), # Show the timeout popup
gr.update(interactive=True), # Re-enable model_a_input
gr.update(
interactive=True, value="Send to Model A"
), # Re-enable model_a_send button
)
except Exception as e:
raise gr.Error(str(e))
def disable_model_b_ui():
"""First function to immediately disable model B UI elements"""
return (
# [0] model_b_input: disable
gr.update(interactive=False),
# [1] model_b_send: disable and show loading state
gr.update(interactive=False, value="Processing..."),
)
def handle_model_b_send(user_input, models_state, conversation_state):
try:
response = chat_with_models(
user_input, "Model B", models_state, conversation_state
)
# Clear the input box and disable the send button
return (
response,
conversation_state,
gr.update(visible=False),
gr.update(
value="", interactive=True
), # Clear and enable model_b_input
gr.update(
interactive=False, value="Send to Model B"
), # Reset button text
)
except TimeoutError as e:
# Disable inputs when timeout occurs
return (
gr.update(value=""), # Clear response
conversation_state,
gr.update(visible=True), # Show the timeout popup
gr.update(interactive=True), # Re-enable model_b_input
gr.update(
interactive=True, value="Send to Model B"
), # Re-enable model_b_send button
)
except Exception as e:
raise gr.Error(str(e))
model_a_send.click(
fn=disable_model_a_ui, # First disable UI
inputs=[],
outputs=[model_a_input, model_a_send],
).then(
fn=handle_model_a_send, # Then do the actual processing
inputs=[model_a_input, models_state, conversation_state],
outputs=[
response_a,
conversation_state,
timeout_popup,
model_a_input,
model_a_send,
],
)
model_b_send.click(
fn=disable_model_b_ui, # First disable UI
inputs=[],
outputs=[model_b_input, model_b_send],
).then(
fn=handle_model_b_send, # Then do the actual processing
inputs=[model_b_input, models_state, conversation_state],
outputs=[
response_b,
conversation_state,
timeout_popup,
model_b_input,
model_b_send,
],
)
def submit_feedback(vote, models_state, conversation_state):
# Map vote to actual model names
match vote:
case "Model A":
winner_model = "left"
case "Model B":
winner_model = "right"
case "Can't Decide":
winner_model = "tie"
# Create feedback entry
feedback_entry = {
"left": models_state["Model A"],
"right": models_state["Model B"],
"winner": winner_model,
"timestamp": datetime.now().strftime("%Y%m%d_%H%M%S"),
}
# Save feedback back to the Hugging Face dataset
save_content_to_hf(feedback_entry, "SE-Arena/votes")
# Save conversations back to the Hugging Face dataset
save_content_to_hf(conversation_state, "SE-Arena/conversations")
# Clear state
models_state.clear()
conversation_state.clear()
# Adjust output count to match the interface definition
return (
gr.update(
value="", interactive=True, visible=True
), # [0] Clear shared_input textbox
gr.update(
value="", interactive=True, visible=True
), # [1] Clear repo_url textbox
gr.update(
value="", visible=False
), # [2] Hide user_prompt_md markdown component
gr.update(
value="", visible=False
), # [3] Hide response_a_title markdown component
gr.update(
value="", visible=False
), # [4] Hide response_b_title markdown component
gr.update(value=""), # [5] Clear Model A response markdown component
gr.update(value=""), # [6] Clear Model B response markdown component
gr.update(visible=False), # [7] Hide multi_round_inputs row
gr.update(visible=False), # [8] Hide vote_panel row
gr.update(
value="Submit", interactive=True, visible=True
), # [9] Reset send_first button
gr.update(
value="Can't Decide", interactive=True
), # [10] Reset feedback radio selection
get_leaderboard_data(feedback_entry), # [11] Updated leaderboard data
gr.update(
visible=True
), # [12] Show the thanks_message markdown component
)
# Update the click event for the submit feedback button
submit_feedback_btn.click(
submit_feedback,
inputs=[feedback, models_state, conversation_state],
outputs=[
shared_input, # Reset shared_input
repo_url, # Show the repo-related URL message
user_prompt_md, # Hide user_prompt_md
response_a_title, # Hide Model A title
response_b_title, # Hide Model B title
response_a, # Clear Model A response
response_b, # Clear Model B response
multi_round_inputs, # Hide multi-round input section
vote_panel, # Hide vote panel
send_first, # Reset and update send_first button
feedback, # Reset feedback selection
leaderboard_component, # Update leaderboard data dynamically
thanks_message, # Show the "Thanks for your vote!" message
],
)
# Add Terms of Service at the bottom
terms_of_service = gr.Markdown(
"""
## Terms of Service
Users are required to agree to the following terms before using the service:
- The service is a **research preview**. It only provides limited safety measures and may generate offensive content.
- It must not be used for any **illegal, harmful, violent, racist, or sexual** purposes.
- Please do not upload any **private information**.
- The service collects user dialogue data, including both text and images, and reserves the right to distribute it under a **Creative Commons Attribution (CC-BY)** or a similar license.
"""
)
app.launch()
|