piyushmaharana commited on
Commit
a7e5c6a
·
verified ·
1 Parent(s): 4c80028

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -2
app.py CHANGED
@@ -1,4 +1,159 @@
1
  import streamlit as st
 
 
 
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import jiwer
3
+ import pandas as pd
4
+ from typing import List, Optional
5
 
6
+ def calculate_wer_metrics(
7
+ hypothesis: str,
8
+ reference: str,
9
+ normalize: bool = True,
10
+ words_to_filter: Optional[List[str]] = None
11
+ ) -> dict:
12
+ """
13
+ Calculate WER metrics between hypothesis and reference texts.
14
+
15
+ Args:
16
+ hypothesis (str): The hypothesis text
17
+ reference (str): The reference text
18
+ normalize (bool): Whether to normalize texts before comparison
19
+ words_to_filter (List[str], optional): Words to filter out before comparison
20
+
21
+ Returns:
22
+ dict: Dictionary containing WER metrics
23
+ """
24
+ # Create transformation pipeline
25
+ if normalize:
26
+ transformation = jiwer.Compose([
27
+ jiwer.ToLowerCase(),
28
+ jiwer.RemoveMultipleSpaces(),
29
+ jiwer.RemovePunctuation(),
30
+ jiwer.Strip()
31
+ ])
32
+
33
+ # Add custom word filtering if specified
34
+ if words_to_filter:
35
+ transformation = jiwer.Compose([
36
+ transformation,
37
+ lambda x: ' '.join(word for word in x.split()
38
+ if word.lower() not in [w.lower() for w in words_to_filter])
39
+ ])
40
+ else:
41
+ transformation = None
42
+
43
+ # Calculate WER measures
44
+ measures = jiwer.compute_measures(
45
+ reference=reference,
46
+ hypothesis=hypothesis,
47
+ truth_transform=transformation,
48
+ hypothesis_transform=transformation
49
+ )
50
+
51
+ return measures
52
+
53
+ def main():
54
+ st.set_page_config(
55
+ page_title="WER Evaluation Tool",
56
+ page_icon="🎯",
57
+ layout="wide"
58
+ )
59
+
60
+ st.title("Word Error Rate (WER) Evaluation Tool")
61
+ st.markdown("""
62
+ This tool helps you evaluate the Word Error Rate (WER) between a reference text and a hypothesis text.
63
+ WER is commonly used in speech recognition and machine translation evaluation.
64
+ """)
65
+
66
+ # Example button
67
+ if st.button("Load Example"):
68
+ reference = "the quick brown fox jumps over the lazy dog"
69
+ hypothesis = "the quick brown fox jumped over lazy dog"
70
+ else:
71
+ reference = ""
72
+ hypothesis = ""
73
+
74
+ # Input fields
75
+ col1, col2 = st.columns(2)
76
+
77
+ with col1:
78
+ reference = st.text_area(
79
+ "Reference Text",
80
+ value=reference,
81
+ height=150,
82
+ placeholder="Enter the reference text here..."
83
+ )
84
+
85
+ with col2:
86
+ hypothesis = st.text_area(
87
+ "Hypothesis Text",
88
+ value=hypothesis,
89
+ height=150,
90
+ placeholder="Enter the hypothesis text here..."
91
+ )
92
+
93
+ # Options
94
+ normalize = st.checkbox("Normalize text (lowercase, remove punctuation)", value=True)
95
+
96
+ words_to_filter_input = st.text_input(
97
+ "Words to filter (comma-separated)",
98
+ placeholder="e.g., um, uh, ah"
99
+ )
100
+
101
+ words_to_filter = [word.strip() for word in words_to_filter_input.split(",")] if words_to_filter_input else None
102
+
103
+ # Calculate button
104
+ if st.button("Calculate WER"):
105
+ if not reference or not hypothesis:
106
+ st.error("Please provide both reference and hypothesis texts.")
107
+ return
108
+
109
+ try:
110
+ measures = calculate_wer_metrics(
111
+ hypothesis=hypothesis,
112
+ reference=reference,
113
+ normalize=normalize,
114
+ words_to_filter=words_to_filter
115
+ )
116
+
117
+ # Display results
118
+ col1, col2 = st.columns(2)
119
+
120
+ with col1:
121
+ st.subheader("Main Metrics")
122
+ metrics_df = pd.DataFrame({
123
+ 'Metric': ['WER', 'MER', 'WIL', 'WIP'],
124
+ 'Value': [
125
+ f"{measures['wer']:.3f}",
126
+ f"{measures['mer']:.3f}",
127
+ f"{measures['wil']:.3f}",
128
+ f"{measures['wip']:.3f}"
129
+ ]
130
+ })
131
+ st.table(metrics_df)
132
+
133
+ with col2:
134
+ st.subheader("Error Analysis")
135
+ error_df = pd.DataFrame({
136
+ 'Metric': ['Substitutions', 'Deletions', 'Insertions', 'Hits'],
137
+ 'Count': [
138
+ measures['substitutions'],
139
+ measures['deletions'],
140
+ measures['insertions'],
141
+ measures['hits']
142
+ ]
143
+ })
144
+ st.table(error_df)
145
+
146
+ # Add explanations
147
+ st.markdown("""
148
+ ### Metrics Explanation:
149
+ - **WER (Word Error Rate)**: The percentage of words that were incorrectly predicted
150
+ - **MER (Match Error Rate)**: The percentage of words that were incorrectly matched
151
+ - **WIL (Word Information Lost)**: The percentage of word information that was lost
152
+ - **WIP (Word Information Preserved)**: The percentage of word information that was preserved
153
+ """)
154
+
155
+ except Exception as e:
156
+ st.error(f"Error calculating WER: {str(e)}")
157
+
158
+ if __name__ == "__main__":
159
+ main()