Spaces:
Sleeping
Sleeping
added file upload feature
Browse files
app.py
CHANGED
@@ -8,42 +8,73 @@ REGEX_YAML_BLOCK = re.compile(r"---[\n\r]+([\S\s]*?)[\n\r]+---[\n\r]")
|
|
8 |
|
9 |
|
10 |
def parse_readme(filepath):
|
11 |
-
"""Parses a repositories README and removes"""
|
12 |
if not os.path.exists(filepath):
|
13 |
return "No README.md found."
|
14 |
with open(filepath, "r") as f:
|
15 |
text = f.read()
|
16 |
match = REGEX_YAML_BLOCK.search(text)
|
17 |
if match:
|
18 |
-
text = text[match.end()
|
19 |
return text
|
20 |
|
21 |
|
22 |
-
def
|
23 |
-
|
24 |
-
|
|
|
25 |
print(truths, preds, type(truths))
|
26 |
err = wer(truths, preds)
|
27 |
-
print(err)
|
28 |
return err
|
29 |
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
description = """
|
32 |
To calculate WER:
|
33 |
|
34 |
* Type the `prediction` and the `truth` in the respective columns in the below calculator.
|
35 |
* You can insert multiple predictions and truths by clicking on the `New row` button.
|
36 |
* To calculate the WER after inserting all the texts, click on `Submit`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
"""
|
38 |
|
39 |
demo = gr.Interface(
|
40 |
fn=compute,
|
41 |
-
inputs=
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
outputs=gr.components.Textbox(label="WER"),
|
48 |
description=description,
|
49 |
title="WER Calculator",
|
|
|
8 |
|
9 |
|
10 |
def parse_readme(filepath):
|
|
|
11 |
if not os.path.exists(filepath):
|
12 |
return "No README.md found."
|
13 |
with open(filepath, "r") as f:
|
14 |
text = f.read()
|
15 |
match = REGEX_YAML_BLOCK.search(text)
|
16 |
if match:
|
17 |
+
text = text[match.end():]
|
18 |
return text
|
19 |
|
20 |
|
21 |
+
def get_wer(df: pd.DataFrame):
|
22 |
+
print(df.keys())
|
23 |
+
preds = df.iloc[:, 0].tolist()
|
24 |
+
truths = df.iloc[:, 1].tolist()
|
25 |
print(truths, preds, type(truths))
|
26 |
err = wer(truths, preds)
|
|
|
27 |
return err
|
28 |
|
29 |
|
30 |
+
def compute(input_df: pd.DataFrame = None, input_file: str = None):
|
31 |
+
if input_df is not None and not input_df.empty and input_file is None:
|
32 |
+
print("in df")
|
33 |
+
if not (input_df.values == "").any():
|
34 |
+
print("in df but empty string")
|
35 |
+
return get_wer(input_df)
|
36 |
+
elif input_file and (input_df.values == "").any():
|
37 |
+
print("in file")
|
38 |
+
file_df = pd.read_csv(input_file.name)
|
39 |
+
print(file_df)
|
40 |
+
return get_wer(file_df)
|
41 |
+
else:
|
42 |
+
print("in error")
|
43 |
+
raise ValueError("Please don't provide both DataFrame and file.")
|
44 |
+
|
45 |
+
|
46 |
description = """
|
47 |
To calculate WER:
|
48 |
|
49 |
* Type the `prediction` and the `truth` in the respective columns in the below calculator.
|
50 |
* You can insert multiple predictions and truths by clicking on the `New row` button.
|
51 |
* To calculate the WER after inserting all the texts, click on `Submit`.
|
52 |
+
|
53 |
+
OR
|
54 |
+
|
55 |
+
* Upload a CSV file with the columns being `prediction` and `truth`.
|
56 |
+
* The first row of the file is supposed to have the column names.
|
57 |
+
* The sentences should be enclosed within `""` and the prediction and truth need to be separated by `,`.
|
58 |
+
* Find an example file [here](trial.csv).
|
59 |
+
|
60 |
+
NOTE: Pleasd don't use both the methods at once.
|
61 |
"""
|
62 |
|
63 |
demo = gr.Interface(
|
64 |
fn=compute,
|
65 |
+
inputs=[
|
66 |
+
gr.components.Dataframe(
|
67 |
+
headers=["prediction", "truth"],
|
68 |
+
col_count=2,
|
69 |
+
row_count=1,
|
70 |
+
label="Input"
|
71 |
+
),
|
72 |
+
gr.File(
|
73 |
+
file_count='single',
|
74 |
+
file_types=['.csv'],
|
75 |
+
label="CSV File"
|
76 |
+
)
|
77 |
+
],
|
78 |
outputs=gr.components.Textbox(label="WER"),
|
79 |
description=description,
|
80 |
title="WER Calculator",
|
trial.csv
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
prediction,truth
|
2 |
+
"hi this is a trial","hi thissss is a, trial"
|