Update app.py
Browse files
app.py
CHANGED
|
@@ -16,13 +16,148 @@ HF_TOKEN = os.getenv("HF_TOKEN")
|
|
| 16 |
if not HF_TOKEN:
|
| 17 |
raise ValueError("HF_TOKEN environment variable is not set or invalid.")
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def initialize_leaderboard_file():
|
| 20 |
"""
|
| 21 |
Ensure the leaderboard file exists and has the correct headers.
|
| 22 |
"""
|
| 23 |
if not os.path.exists(LEADERBOARD_FILE):
|
| 24 |
pd.DataFrame(columns=[
|
| 25 |
-
|
| 26 |
"Correct Predictions", "Total Questions", "Timestamp"
|
| 27 |
]).to_csv(LEADERBOARD_FILE, index=False)
|
| 28 |
elif os.stat(LEADERBOARD_FILE).st_size == 0:
|
|
@@ -44,7 +179,7 @@ def update_leaderboard(results):
|
|
| 44 |
Append new submission results to the leaderboard file and push updates to the Hugging Face repository.
|
| 45 |
"""
|
| 46 |
new_entry = {
|
| 47 |
-
|
| 48 |
"Overall Accuracy": round(results['overall_accuracy'] * 100, 2),
|
| 49 |
"Valid Accuracy": round(results['valid_accuracy'] * 100, 2),
|
| 50 |
"Correct Predictions": results['correct_predictions'],
|
|
@@ -139,6 +274,9 @@ def evaluate_predictions(prediction_file, model_name, add_to_leaderboard):
|
|
| 139 |
results = {
|
| 140 |
'model_name': model_name if model_name else "Unknown Model",
|
| 141 |
'overall_accuracy': overall_accuracy,
|
|
|
|
|
|
|
|
|
|
| 142 |
}
|
| 143 |
|
| 144 |
if add_to_leaderboard:
|
|
@@ -152,6 +290,7 @@ def evaluate_predictions(prediction_file, model_name, add_to_leaderboard):
|
|
| 152 |
|
| 153 |
initialize_leaderboard_file()
|
| 154 |
|
|
|
|
| 155 |
# Function to set default mode
|
| 156 |
# Function to set default mode
|
| 157 |
import gradio as gr
|
|
|
|
| 16 |
if not HF_TOKEN:
|
| 17 |
raise ValueError("HF_TOKEN environment variable is not set or invalid.")
|
| 18 |
|
| 19 |
+
# def initialize_leaderboard_file():
|
| 20 |
+
# """
|
| 21 |
+
# Ensure the leaderboard file exists and has the correct headers.
|
| 22 |
+
# """
|
| 23 |
+
# if not os.path.exists(LEADERBOARD_FILE):
|
| 24 |
+
# pd.DataFrame(columns=[
|
| 25 |
+
# "Model Name", "Overall Accuracy", "Valid Accuracy",
|
| 26 |
+
# "Correct Predictions", "Total Questions", "Timestamp"
|
| 27 |
+
# ]).to_csv(LEADERBOARD_FILE, index=False)
|
| 28 |
+
# elif os.stat(LEADERBOARD_FILE).st_size == 0:
|
| 29 |
+
# pd.DataFrame(columns=[
|
| 30 |
+
# "Model Name", "Overall Accuracy", "Valid Accuracy",
|
| 31 |
+
# "Correct Predictions", "Total Questions", "Timestamp"
|
| 32 |
+
# ]).to_csv(LEADERBOARD_FILE, index=False)
|
| 33 |
+
|
| 34 |
+
# def clean_answer(answer):
|
| 35 |
+
# if pd.isna(answer):
|
| 36 |
+
# return None
|
| 37 |
+
# answer = str(answer)
|
| 38 |
+
# clean = re.sub(r'[^A-Da-d]', '', answer)
|
| 39 |
+
# return clean[0].upper() if clean else None
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# def update_leaderboard(results):
|
| 43 |
+
# """
|
| 44 |
+
# Append new submission results to the leaderboard file and push updates to the Hugging Face repository.
|
| 45 |
+
# """
|
| 46 |
+
# new_entry = {
|
| 47 |
+
# "Model Name": results['model_name'],
|
| 48 |
+
# "Overall Accuracy": round(results['overall_accuracy'] * 100, 2),
|
| 49 |
+
# "Valid Accuracy": round(results['valid_accuracy'] * 100, 2),
|
| 50 |
+
# "Correct Predictions": results['correct_predictions'],
|
| 51 |
+
# "Total Questions": results['total_questions'],
|
| 52 |
+
# "Timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 53 |
+
# }
|
| 54 |
+
|
| 55 |
+
# try:
|
| 56 |
+
# # Update the local leaderboard file
|
| 57 |
+
# new_entry_df = pd.DataFrame([new_entry])
|
| 58 |
+
# file_exists = os.path.exists(LEADERBOARD_FILE)
|
| 59 |
+
|
| 60 |
+
# new_entry_df.to_csv(
|
| 61 |
+
# LEADERBOARD_FILE,
|
| 62 |
+
# mode='a', # Append mode
|
| 63 |
+
# index=False,
|
| 64 |
+
# header=not file_exists # Write header only if the file is new
|
| 65 |
+
# )
|
| 66 |
+
# print(f"Leaderboard updated successfully at {LEADERBOARD_FILE}")
|
| 67 |
+
|
| 68 |
+
# # Push the updated file to the Hugging Face repository using HTTP API
|
| 69 |
+
# api = HfApi()
|
| 70 |
+
# token = HfFolder.get_token()
|
| 71 |
+
|
| 72 |
+
# api.upload_file(
|
| 73 |
+
# path_or_fileobj=LEADERBOARD_FILE,
|
| 74 |
+
# path_in_repo="leaderboard.csv",
|
| 75 |
+
# repo_id="SondosMB/ss", # Your Space repository
|
| 76 |
+
# repo_type="space",
|
| 77 |
+
# token=token
|
| 78 |
+
# )
|
| 79 |
+
# print("Leaderboard changes pushed to Hugging Face repository.")
|
| 80 |
+
|
| 81 |
+
# except Exception as e:
|
| 82 |
+
# print(f"Error updating leaderboard file: {e}")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
# def load_leaderboard():
|
| 87 |
+
# if not os.path.exists(LEADERBOARD_FILE) or os.stat(LEADERBOARD_FILE).st_size == 0:
|
| 88 |
+
# return pd.DataFrame({
|
| 89 |
+
# "Model Name": [],
|
| 90 |
+
# "Overall Accuracy": [],
|
| 91 |
+
# "Valid Accuracy": [],
|
| 92 |
+
# "Correct Predictions": [],
|
| 93 |
+
# "Total Questions": [],
|
| 94 |
+
# "Timestamp": [],
|
| 95 |
+
# })
|
| 96 |
+
# return pd.read_csv(LEADERBOARD_FILE)
|
| 97 |
+
|
| 98 |
+
# def evaluate_predictions(prediction_file, model_name, add_to_leaderboard):
|
| 99 |
+
# try:
|
| 100 |
+
# ground_truth_path = hf_hub_download(
|
| 101 |
+
# repo_id="SondosMB/ground-truth-dataset",
|
| 102 |
+
# filename="ground_truth.csv",
|
| 103 |
+
# repo_type="dataset",
|
| 104 |
+
# use_auth_token=True
|
| 105 |
+
# )
|
| 106 |
+
# ground_truth_df = pd.read_csv(ground_truth_path)
|
| 107 |
+
# except FileNotFoundError:
|
| 108 |
+
# return "Ground truth file not found in the dataset repository.", load_leaderboard()
|
| 109 |
+
# except Exception as e:
|
| 110 |
+
# return f"Error loading ground truth: {e}", load_leaderboard()
|
| 111 |
+
|
| 112 |
+
# if not prediction_file:
|
| 113 |
+
# return "Prediction file not uploaded.", load_leaderboard()
|
| 114 |
+
|
| 115 |
+
# try:
|
| 116 |
+
# #load predition file
|
| 117 |
+
# predictions_df = pd.read_csv(prediction_file.name)
|
| 118 |
+
# # Validate required columns in prediction file
|
| 119 |
+
# required_columns = ['question_id', 'predicted_answer']
|
| 120 |
+
# missing_columns = [col for col in required_columns if col not in predictions_df.columns]
|
| 121 |
+
# if missing_columns:
|
| 122 |
+
# return (f"Error: Missing required columns in prediction file: {', '.join(missing_columns)}.",
|
| 123 |
+
# load_leaderboard())
|
| 124 |
+
|
| 125 |
+
# # Validate 'Answer' column in ground truth file
|
| 126 |
+
# if 'Answer' not in ground_truth_df.columns:
|
| 127 |
+
# return "Error: 'Answer' column is missing in the ground truth dataset.", load_leaderboard()
|
| 128 |
+
# merged_df = pd.merge(predictions_df, ground_truth_df, on='question_id', how='inner')
|
| 129 |
+
# merged_df['pred_answer'] = merged_df['predicted_answer'].apply(clean_answer)
|
| 130 |
+
|
| 131 |
+
# valid_predictions = merged_df.dropna(subset=['pred_answer'])
|
| 132 |
+
# correct_predictions = (valid_predictions['pred_answer'] == valid_predictions['Answer']).sum()
|
| 133 |
+
# total_predictions = len(merged_df)
|
| 134 |
+
# total_valid_predictions = len(valid_predictions)
|
| 135 |
+
|
| 136 |
+
# overall_accuracy = correct_predictions / total_predictions if total_predictions > 0 else 0
|
| 137 |
+
# valid_accuracy = correct_predictions / total_valid_predictions if total_valid_predictions > 0 else 0
|
| 138 |
+
|
| 139 |
+
# results = {
|
| 140 |
+
# 'model_name': model_name if model_name else "Unknown Model",
|
| 141 |
+
# 'overall_accuracy': overall_accuracy,
|
| 142 |
+
# }
|
| 143 |
+
|
| 144 |
+
# if add_to_leaderboard:
|
| 145 |
+
# update_leaderboard(results)
|
| 146 |
+
# return "Evaluation completed and added to leaderboard.", load_leaderboard()
|
| 147 |
+
# else:
|
| 148 |
+
# return "Evaluation completed but not added to leaderboard.", load_leaderboard()
|
| 149 |
+
|
| 150 |
+
# except Exception as e:
|
| 151 |
+
# return f"Error during evaluation: {str(e)}", load_leaderboard()
|
| 152 |
+
|
| 153 |
+
# initialize_leaderboard_file()
|
| 154 |
def initialize_leaderboard_file():
|
| 155 |
"""
|
| 156 |
Ensure the leaderboard file exists and has the correct headers.
|
| 157 |
"""
|
| 158 |
if not os.path.exists(LEADERBOARD_FILE):
|
| 159 |
pd.DataFrame(columns=[
|
| 160 |
+
"Model Name", "Overall Accuracy", "Valid Accuracy",
|
| 161 |
"Correct Predictions", "Total Questions", "Timestamp"
|
| 162 |
]).to_csv(LEADERBOARD_FILE, index=False)
|
| 163 |
elif os.stat(LEADERBOARD_FILE).st_size == 0:
|
|
|
|
| 179 |
Append new submission results to the leaderboard file and push updates to the Hugging Face repository.
|
| 180 |
"""
|
| 181 |
new_entry = {
|
| 182 |
+
"Model Name": results['model_name'],
|
| 183 |
"Overall Accuracy": round(results['overall_accuracy'] * 100, 2),
|
| 184 |
"Valid Accuracy": round(results['valid_accuracy'] * 100, 2),
|
| 185 |
"Correct Predictions": results['correct_predictions'],
|
|
|
|
| 274 |
results = {
|
| 275 |
'model_name': model_name if model_name else "Unknown Model",
|
| 276 |
'overall_accuracy': overall_accuracy,
|
| 277 |
+
'valid_accuracy': valid_accuracy,
|
| 278 |
+
'correct_predictions': correct_predictions,
|
| 279 |
+
'total_questions': total_predictions,
|
| 280 |
}
|
| 281 |
|
| 282 |
if add_to_leaderboard:
|
|
|
|
| 290 |
|
| 291 |
initialize_leaderboard_file()
|
| 292 |
|
| 293 |
+
|
| 294 |
# Function to set default mode
|
| 295 |
# Function to set default mode
|
| 296 |
import gradio as gr
|