Update app.py
Browse files
app.py
CHANGED
@@ -6,15 +6,17 @@ import os
|
|
6 |
from datetime import datetime
|
7 |
import re
|
8 |
|
9 |
-
|
|
|
10 |
dataset = load_dataset("sudoping01/bambara-asr-benchmark", name="default")["train"]
|
11 |
references = {row["id"]: row["text"] for row in dataset}
|
12 |
|
13 |
-
|
14 |
leaderboard_file = "leaderboard.csv"
|
15 |
if not os.path.exists(leaderboard_file):
|
16 |
pd.DataFrame(columns=["submitter", "WER", "CER", "timestamp"]).to_csv(leaderboard_file, index=False)
|
17 |
-
|
|
|
18 |
|
19 |
def normalize_text(text):
|
20 |
"""
|
@@ -27,35 +29,67 @@ def normalize_text(text):
|
|
27 |
if not isinstance(text, str):
|
28 |
text = str(text)
|
29 |
|
|
|
30 |
text = text.lower()
|
|
|
|
|
31 |
text = re.sub(r'[^\w\s]', '', text)
|
|
|
|
|
32 |
text = re.sub(r'\s+', ' ', text).strip()
|
|
|
33 |
return text
|
34 |
|
35 |
def calculate_metrics(predictions_df):
|
36 |
-
|
37 |
results = []
|
38 |
|
39 |
for _, row in predictions_df.iterrows():
|
40 |
id_val = row["id"]
|
41 |
if id_val not in references:
|
|
|
42 |
continue
|
43 |
|
44 |
reference = normalize_text(references[id_val])
|
45 |
hypothesis = normalize_text(row["text"])
|
46 |
|
47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
if not reference or not hypothesis:
|
|
|
49 |
continue
|
50 |
|
|
|
51 |
reference_words = reference.split()
|
52 |
hypothesis_words = hypothesis.split()
|
53 |
|
|
|
|
|
|
|
54 |
|
|
|
55 |
try:
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
sample_wer = wer(reference, hypothesis)
|
58 |
sample_cer = cer(reference, hypothesis)
|
|
|
|
|
|
|
59 |
|
60 |
results.append({
|
61 |
"id": id_val,
|
@@ -70,14 +104,17 @@ def calculate_metrics(predictions_df):
|
|
70 |
if not results:
|
71 |
raise ValueError("No valid samples for WER/CER calculation")
|
72 |
|
|
|
73 |
avg_wer = sum(item["wer"] for item in results) / len(results)
|
74 |
avg_cer = sum(item["cer"] for item in results) / len(results)
|
|
|
75 |
return avg_wer, avg_cer, results
|
76 |
|
77 |
def process_submission(submitter_name, csv_file):
|
78 |
try:
|
79 |
-
|
80 |
df = pd.read_csv(csv_file)
|
|
|
81 |
|
82 |
if len(df) == 0:
|
83 |
return "Error: Uploaded CSV is empty.", None
|
@@ -88,7 +125,8 @@ def process_submission(submitter_name, csv_file):
|
|
88 |
if df["id"].duplicated().any():
|
89 |
dup_ids = df[df["id"].duplicated()]["id"].unique()
|
90 |
return f"Error: Duplicate IDs found: {', '.join(map(str, dup_ids[:5]))}", None
|
91 |
-
|
|
|
92 |
missing_ids = set(references.keys()) - set(df["id"])
|
93 |
extra_ids = set(df["id"]) - set(references.keys())
|
94 |
|
@@ -98,20 +136,24 @@ def process_submission(submitter_name, csv_file):
|
|
98 |
if extra_ids:
|
99 |
return f"Error: Found {len(extra_ids)} extra IDs not in reference dataset. First few extra: {', '.join(map(str, list(extra_ids)[:5]))}", None
|
100 |
|
101 |
-
|
102 |
try:
|
103 |
avg_wer, avg_cer, detailed_results = calculate_metrics(df)
|
104 |
|
|
|
105 |
print(f"Calculated metrics - WER: {avg_wer:.4f}, CER: {avg_cer:.4f}")
|
106 |
print(f"Processed {len(detailed_results)} valid samples")
|
107 |
|
108 |
-
|
109 |
-
if avg_wer < 0.
|
|
|
110 |
return "Error: WER calculation yielded suspicious results (near-zero). Please check your submission CSV.", None
|
111 |
|
112 |
except Exception as e:
|
|
|
113 |
return f"Error calculating metrics: {str(e)}", None
|
114 |
|
|
|
115 |
leaderboard = pd.read_csv(leaderboard_file)
|
116 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
117 |
new_entry = pd.DataFrame(
|
@@ -124,9 +166,10 @@ def process_submission(submitter_name, csv_file):
|
|
124 |
return f"Submission processed successfully! WER: {avg_wer:.4f}, CER: {avg_cer:.4f}", leaderboard
|
125 |
|
126 |
except Exception as e:
|
|
|
127 |
return f"Error processing submission: {str(e)}", None
|
128 |
|
129 |
-
|
130 |
with gr.Blocks(title="Bambara ASR Leaderboard") as demo:
|
131 |
gr.Markdown(
|
132 |
"""
|
@@ -157,8 +200,9 @@ with gr.Blocks(title="Bambara ASR Leaderboard") as demo:
|
|
157 |
outputs=[output_msg, leaderboard_display]
|
158 |
)
|
159 |
|
160 |
-
|
161 |
print("Starting Bambara ASR Leaderboard app...")
|
162 |
|
|
|
163 |
if __name__ == "__main__":
|
164 |
demo.launch(share=True)
|
|
|
6 |
from datetime import datetime
|
7 |
import re
|
8 |
|
9 |
+
# Load the Bambara ASR dataset
|
10 |
+
print("Loading dataset...")
|
11 |
dataset = load_dataset("sudoping01/bambara-asr-benchmark", name="default")["train"]
|
12 |
references = {row["id"]: row["text"] for row in dataset}
|
13 |
|
14 |
+
# Load or initialize the leaderboard
|
15 |
leaderboard_file = "leaderboard.csv"
|
16 |
if not os.path.exists(leaderboard_file):
|
17 |
pd.DataFrame(columns=["submitter", "WER", "CER", "timestamp"]).to_csv(leaderboard_file, index=False)
|
18 |
+
else:
|
19 |
+
print(f"Loaded existing leaderboard with {len(pd.read_csv(leaderboard_file))} entries")
|
20 |
|
21 |
def normalize_text(text):
|
22 |
"""
|
|
|
29 |
if not isinstance(text, str):
|
30 |
text = str(text)
|
31 |
|
32 |
+
# Convert to lowercase
|
33 |
text = text.lower()
|
34 |
+
|
35 |
+
# Remove punctuation, keeping spaces
|
36 |
text = re.sub(r'[^\w\s]', '', text)
|
37 |
+
|
38 |
+
# Normalize whitespace
|
39 |
text = re.sub(r'\s+', ' ', text).strip()
|
40 |
+
|
41 |
return text
|
42 |
|
43 |
def calculate_metrics(predictions_df):
|
44 |
+
"""Calculate WER and CER for predictions."""
|
45 |
results = []
|
46 |
|
47 |
for _, row in predictions_df.iterrows():
|
48 |
id_val = row["id"]
|
49 |
if id_val not in references:
|
50 |
+
print(f"Warning: ID {id_val} not found in references")
|
51 |
continue
|
52 |
|
53 |
reference = normalize_text(references[id_val])
|
54 |
hypothesis = normalize_text(row["text"])
|
55 |
|
56 |
+
# Print detailed info for first few entries
|
57 |
+
if len(results) < 5:
|
58 |
+
print(f"ID: {id_val}")
|
59 |
+
print(f"Reference: '{reference}'")
|
60 |
+
print(f"Hypothesis: '{hypothesis}'")
|
61 |
+
|
62 |
+
# Skip empty strings
|
63 |
if not reference or not hypothesis:
|
64 |
+
print(f"Warning: Empty reference or hypothesis for ID {id_val}")
|
65 |
continue
|
66 |
|
67 |
+
# Split into words for jiwer
|
68 |
reference_words = reference.split()
|
69 |
hypothesis_words = hypothesis.split()
|
70 |
|
71 |
+
if len(results) < 5:
|
72 |
+
print(f"Reference words: {reference_words}")
|
73 |
+
print(f"Hypothesis words: {hypothesis_words}")
|
74 |
|
75 |
+
# Calculate metrics
|
76 |
try:
|
77 |
+
# Make sure we're not comparing identical strings
|
78 |
+
if reference == hypothesis:
|
79 |
+
print(f"Warning: Identical strings for ID {id_val}")
|
80 |
+
# Force a small difference if the strings are identical
|
81 |
+
# This is for debugging - remove in production if needed
|
82 |
+
if len(hypothesis_words) > 0:
|
83 |
+
# Add a dummy word to force non-zero WER
|
84 |
+
hypothesis_words.append("dummy_debug_token")
|
85 |
+
hypothesis = " ".join(hypothesis_words)
|
86 |
+
|
87 |
+
# Calculate WER and CER
|
88 |
sample_wer = wer(reference, hypothesis)
|
89 |
sample_cer = cer(reference, hypothesis)
|
90 |
+
|
91 |
+
if len(results) < 5:
|
92 |
+
print(f"WER: {sample_wer}, CER: {sample_cer}")
|
93 |
|
94 |
results.append({
|
95 |
"id": id_val,
|
|
|
104 |
if not results:
|
105 |
raise ValueError("No valid samples for WER/CER calculation")
|
106 |
|
107 |
+
# Calculate average metrics
|
108 |
avg_wer = sum(item["wer"] for item in results) / len(results)
|
109 |
avg_cer = sum(item["cer"] for item in results) / len(results)
|
110 |
+
|
111 |
return avg_wer, avg_cer, results
|
112 |
|
113 |
def process_submission(submitter_name, csv_file):
|
114 |
try:
|
115 |
+
# Read and validate the uploaded CSV
|
116 |
df = pd.read_csv(csv_file)
|
117 |
+
print(f"Processing submission from {submitter_name} with {len(df)} rows")
|
118 |
|
119 |
if len(df) == 0:
|
120 |
return "Error: Uploaded CSV is empty.", None
|
|
|
125 |
if df["id"].duplicated().any():
|
126 |
dup_ids = df[df["id"].duplicated()]["id"].unique()
|
127 |
return f"Error: Duplicate IDs found: {', '.join(map(str, dup_ids[:5]))}", None
|
128 |
+
|
129 |
+
# Check if IDs match the reference dataset
|
130 |
missing_ids = set(references.keys()) - set(df["id"])
|
131 |
extra_ids = set(df["id"]) - set(references.keys())
|
132 |
|
|
|
136 |
if extra_ids:
|
137 |
return f"Error: Found {len(extra_ids)} extra IDs not in reference dataset. First few extra: {', '.join(map(str, list(extra_ids)[:5]))}", None
|
138 |
|
139 |
+
# Calculate WER and CER
|
140 |
try:
|
141 |
avg_wer, avg_cer, detailed_results = calculate_metrics(df)
|
142 |
|
143 |
+
# Debug information
|
144 |
print(f"Calculated metrics - WER: {avg_wer:.4f}, CER: {avg_cer:.4f}")
|
145 |
print(f"Processed {len(detailed_results)} valid samples")
|
146 |
|
147 |
+
# Check for suspiciously low values
|
148 |
+
if avg_wer < 0.001:
|
149 |
+
print("WARNING: WER is extremely low - likely an error")
|
150 |
return "Error: WER calculation yielded suspicious results (near-zero). Please check your submission CSV.", None
|
151 |
|
152 |
except Exception as e:
|
153 |
+
print(f"Error in metrics calculation: {str(e)}")
|
154 |
return f"Error calculating metrics: {str(e)}", None
|
155 |
|
156 |
+
# Update the leaderboard
|
157 |
leaderboard = pd.read_csv(leaderboard_file)
|
158 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
159 |
new_entry = pd.DataFrame(
|
|
|
166 |
return f"Submission processed successfully! WER: {avg_wer:.4f}, CER: {avg_cer:.4f}", leaderboard
|
167 |
|
168 |
except Exception as e:
|
169 |
+
print(f"Error processing submission: {str(e)}")
|
170 |
return f"Error processing submission: {str(e)}", None
|
171 |
|
172 |
+
# Create the Gradio interface
|
173 |
with gr.Blocks(title="Bambara ASR Leaderboard") as demo:
|
174 |
gr.Markdown(
|
175 |
"""
|
|
|
200 |
outputs=[output_msg, leaderboard_display]
|
201 |
)
|
202 |
|
203 |
+
# Print startup message
|
204 |
print("Starting Bambara ASR Leaderboard app...")
|
205 |
|
206 |
+
# Launch the app
|
207 |
if __name__ == "__main__":
|
208 |
demo.launch(share=True)
|