Randa commited on
Commit
4946383
·
verified ·
1 Parent(s): 489b18a

Create app.py

Browse files

Gradio application for learning Arabic according to Noorani Qaida

Files changed (1) hide show
  1. app.py +121 -0
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Gradio application for learning Noorani Qaida
2
+
3
+ p = pipeline("automatic-speech-recognition", model="jonatasgrosman/wav2vec2-large-xlsr-53-arabic")
4
+
5
+ # Read excel file and store it in a dictionary
6
+ def read_excel_data(file_path, sheet_name):
7
+ data_dict = {}
8
+ try:
9
+ # Read the Excel file
10
+ df = pd.read_excel(file_path, sheet_name=sheet_name)
11
+ # Iterate over the rows
12
+ for index, row in df.iterrows():
13
+ # Extract values from the row
14
+ key = row['correct']
15
+ value1 = row['close']
16
+ value2 = row['wrong']
17
+ # Store values in the dictionary
18
+ data_dict[key] = (value1, value2)
19
+ return data_dict
20
+
21
+ except Exception as e:
22
+ print(f"Error reading Excel file: {e}")
23
+
24
+
25
+ # List of sample texts to read
26
+ excel_file_path = "dataset/ASR_live_test.xlsx"
27
+ sheet_name= 'sample_test'
28
+ choices = read_excel_data(excel_file_path, sheet_name)
29
+
30
+ def similar(a, b):
31
+ return SequenceMatcher(None, a, b).ratio()
32
+
33
+ def transcribe(audio, reference_text):
34
+ time.sleep(1)
35
+ text = p(audio)["text"]
36
+ text = buckwalter.untrans(text)
37
+ state = text
38
+ if state is not None:
39
+ if similar(reference_text, state) > 0.75:
40
+ score = "great correct !"
41
+ elif similar(reference_text, state) > 0.50 and similar(reference_text, state) < 0.75:
42
+ score = "close"
43
+ else:
44
+ score = "wrong try again"
45
+ return state, score
46
+ else :
47
+ print(" Null Object")
48
+
49
+ # Gradio apps
50
+ # get the list of the correct text to read
51
+ dropdown_choices = choices.keys()
52
+
53
+ correct_callback = gr.CSVLogger()
54
+ close_callback = gr.CSVLogger()
55
+ wrong_callback = gr.CSVLogger()
56
+
57
+
58
+ demo = gr.Blocks(theme=gr.themes.Soft())
59
+
60
+ with demo :
61
+
62
+ gr.Markdown(
63
+ """
64
+ # Noorani Qaida Test Interface
65
+ Select a text from the list, read it aloud, and get the transcription along with the similarity score.
66
+ """
67
+ )
68
+
69
+ with gr.Row():
70
+ # Columns for inputs
71
+ with gr.Column():
72
+ correct_text = gr.Dropdown(label="Select Text to Read (Correct Text)", choices=dropdown_choices)
73
+ close_text = gr.Textbox(label="Close Text")
74
+ wrong_text = gr.Textbox(label="Wrong Text")
75
+
76
+ # Function to get the corresponding values of close and wrong text for each correct text
77
+ def get_text(correct_text):
78
+ close_text = gr.Textbox(label="Close Text", value=choices[correct_text][0])
79
+ wrong_text = gr.Textbox(label="Wrong Text", value=choices[correct_text][1])
80
+ return close_text, wrong_text
81
+
82
+ # Update the close and wrong text according to the selected correct text
83
+ correct_text.input(get_text, correct_text, [close_text, wrong_text])
84
+
85
+ audio_record = gr.Audio(type="filepath")
86
+
87
+ transcribe_correct_bn = gr.Button("Transcribe and Compare with Correct", scale=3)#remove scale as not effect
88
+ transcribe_close_bn = gr.Button("Transcribe and Compare with Close", scale=3)
89
+ transcribe_wrong_bn = gr.Button("Transcribe and Compare with Wrong", scale=3)
90
+
91
+ with gr.Row():
92
+ clear_btn = gr.Button(value="Clear")
93
+
94
+ # Columns for outputs
95
+ with gr.Column():
96
+ trans_text = gr.Textbox(label="Transcription")
97
+ score = gr.Textbox(label="Score")
98
+ with gr.Row():
99
+ correct_flag_btn = gr.Button("Correct Flag", size="sm")
100
+ close_flag_btn = gr.Button("Close Flag", size="sm")
101
+ wrong_flag_btn = gr.Button("Wrong Flag", size="sm")
102
+
103
+ # Setup the components to flag
104
+ correct_callback.setup([correct_text, trans_text, score, audio_record], "correct_flagged_data")
105
+ close_callback.setup([close_text, trans_text, score, audio_record], "close_flagged_data")
106
+ wrong_callback.setup([wrong_text, trans_text, score, audio_record], "wrong_flagged_data")
107
+
108
+ transcribe_correct_bn.click(fn=transcribe, inputs=[audio_record, correct_text], outputs=[trans_text, score])
109
+ transcribe_close_bn.click(fn=transcribe, inputs=[audio_record, close_text], outputs=[trans_text, score])
110
+ transcribe_wrong_bn.click(fn=transcribe, inputs=[audio_record, wrong_text], outputs=[trans_text, score])
111
+
112
+ # We can choose which components to flag
113
+ correct_flag_btn.click(lambda *args: correct_callback.flag(args), [correct_text, trans_text, score, audio_record], None, preprocess=False)
114
+ close_flag_btn.click(lambda *args: close_callback.flag(args), [close_text, trans_text, score, audio_record], None, preprocess=False)
115
+ wrong_flag_btn.click(lambda *args: wrong_callback.flag(args), [wrong_text, trans_text, score, audio_record], None, preprocess=False)
116
+
117
+ # Clear the recording
118
+ clear_btn.click(lambda: None, None, audio_record, queue=False)
119
+
120
+ if __name__ == "__main__":
121
+ demo. Launch()