Spaces:
Running
Running
File size: 6,327 Bytes
441367e 8f2bb45 441367e ff99877 441367e 8f2bb45 441367e 8f2bb45 441367e ff99877 441367e 8f2bb45 441367e 8f2bb45 07b19d7 441367e 9581c74 441367e 8f2bb45 ff99877 441367e 1248b75 ff99877 1248b75 ff99877 441367e ff99877 441367e ff99877 07b19d7 ff99877 8f2bb45 ff99877 9581c74 ff99877 8f2bb45 9581c74 ff99877 9581c74 ff99877 07b19d7 9581c74 441367e 9484ee3 441367e ff99877 8f2bb45 f36f4ae 8f2bb45 441367e ff99877 441367e ff99877 441367e |
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 |
import sys
from importlib.metadata import version
import evaluate
import polars as pl
import polars_distance as pld
import gradio as gr
# Load evaluators
wer = evaluate.load("wer")
cer = evaluate.load("cer")
# Config
title = "See ASR Outputs"
# https://www.tablesgenerator.com/markdown_tables
authors_table = """
## Authors
Follow them on social networks and **contact** if you need any help or have any questions:
| <img src="https://avatars.githubusercontent.com/u/7875085?v=4" width="100"> **Yehor Smoliakov** |
|-------------------------------------------------------------------------------------------------|
| https://t.me/smlkw in Telegram |
| https://x.com/yehor_smoliakov at X |
| https://github.com/egorsmkv at GitHub |
| https://huggingface.co/Yehor at Hugging Face |
| or use [email protected] |
""".strip()
examples = [
["evaluation_results.jsonl", False, True, False],
["evaluation_results_batch.jsonl", True, False, False],
]
description_head = f"""
# {title}
## Overview
See generated JSONL files made by ASR models as a dataframe. Also, this app calculates WER and CER metrics for each row.
""".strip()
description_foot = f"""
{authors_table}
""".strip()
metrics_value = """
Metrics will appear here.
""".strip()
tech_env = f"""
#### Environment
- Python: {sys.version}
""".strip()
tech_libraries = f"""
#### Libraries
- gradio: {version("gradio")}
- jiwer: {version("jiwer")}
- evaluate: {version("evaluate")}
- pandas: {version("pandas")}
- polars: {version("polars")}
- polars-distance: {version("polars_distance")}
""".strip()
def compute_wer(prediction, reference):
return round(wer.compute(predictions=[prediction], references=[reference]), 4)
def compute_cer(prediction, reference):
return round(cer.compute(predictions=[prediction], references=[reference]), 4)
def process_file(file_name, _batch_mode, _calculate_distance, _calculate_metrics):
if not file_name:
raise gr.Error("Please paste your JSON file.")
df = pl.read_ndjson(file_name)
required_columns = [
"filename",
"inference_start",
"inference_end",
"inference_total",
"duration",
"reference",
"prediction",
]
required_columns_batch = [
"inference_start",
"inference_end",
"inference_total",
"filenames",
"durations",
"references",
"predictions",
]
if _batch_mode:
if not all(col in df.columns for col in required_columns_batch):
raise gr.Error(
f"Please provide a JSONL file with the following columns: {required_columns_batch}"
)
else:
if not all(col in df.columns for col in required_columns):
raise gr.Error(
f"Please provide a JSONL file with the following columns: {required_columns}"
)
# exclude inference_start, inference_end
if _batch_mode:
df = df.drop(
["inference_total", "inference_start", "inference_end", "filenames"]
)
else:
df = df.drop(
["inference_total", "inference_start", "inference_end", "filename"]
)
if _batch_mode:
predictions = []
references = []
for row in df.iter_rows(named=True):
for idx, prediction in enumerate(row["predictions"]):
reference = row["references"][idx]
predictions.append(prediction)
references.append(reference)
df = pl.DataFrame(
{
"prediction": predictions,
"reference": references,
}
)
if _calculate_metrics:
# Pandas is needed for applying functions
df_pd = df.to_pandas()
df_pd["wer"] = df_pd.apply(
lambda row: compute_wer(row["prediction"], row["reference"]),
axis=1,
)
df_pd["cer"] = df_pd.apply(
lambda row: compute_cer(row["prediction"], row["reference"]),
axis=1,
)
fields = [
"wer",
"cer",
"prediction",
"reference",
]
df = pl.DataFrame(df_pd)
else:
fields = [
"prediction",
"reference",
]
df = df.select(fields)
if _calculate_distance:
df = df.with_columns(
pld.col("prediction").dist_str.levenshtein("reference").alias("distance")
)
# add distance to the first position
fields = [
"distance",
*fields,
]
df = df.select(fields)
return df
demo = gr.Blocks(
title=title,
analytics_enabled=False,
theme=gr.themes.Base(),
)
with demo:
gr.Markdown(description_head)
gr.Markdown("## Usage")
with gr.Row():
df = gr.DataFrame(
label="Dataframe",
show_search="search",
show_row_numbers=True,
pinned_columns=1,
)
with gr.Row():
with gr.Column():
jsonl_file = gr.File(label="A JSONL file")
batch_mode = gr.Checkbox(
label="Use batch mode",
)
calculate_distance = gr.Checkbox(
label="Calculate Levenshtein distance",
value=False,
)
calculate_metrics = gr.Checkbox(
label="Calculate WER/CER metrics",
value=False,
)
gr.Button("Show").click(
process_file,
inputs=[jsonl_file, batch_mode, calculate_distance, calculate_metrics],
outputs=df,
)
with gr.Row():
gr.Examples(
label="Choose an example",
inputs=[jsonl_file, batch_mode, calculate_distance, calculate_metrics],
examples=examples,
)
gr.Markdown(description_foot)
gr.Markdown("### Gradio app uses:")
gr.Markdown(tech_env)
gr.Markdown(tech_libraries)
if __name__ == "__main__":
demo.queue()
demo.launch()
|