Kuuhaakuu commited on
Commit
ef0b4df
1 Parent(s): eb89e16

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import os
4
+ import random
5
+ import matplotlib.pyplot as plt
6
+
7
+ # Load transcription results
8
+ with open("./drive/MyDrive/results/gradio_results.json", "r", encoding='utf-8') as file:
9
+ gradio_transcriptions = json.load(file)
10
+
11
+ with open("./drive/MyDrive/results/openai_results.json", "r", encoding='utf-8') as file:
12
+ openai_transcriptions = json.load(file)
13
+
14
+ audio_files_directory = "./drive/MyDrive/chunks"
15
+
16
+ def get_random_audio_and_transcriptions():
17
+ random_choice = random.choice(os.listdir(audio_files_directory))
18
+ audio_path = os.path.join(audio_files_directory, random_choice)
19
+ base_name = os.path.splitext(random_choice)[0]
20
+ gradio_transcription = next((t for t in gradio_transcriptions if t['chunk'].startswith(base_name)), {'text': ''})['text']
21
+ openai_transcription = next((t for t in openai_transcriptions if t['chunk'].startswith(base_name)), {'text': ''})['text']
22
+ return audio_path, gradio_transcription, openai_transcription
23
+
24
+ def handle_vote(vote, audio_path, gradio_transcription, openai_transcription):
25
+ votes_file = "./drive/MyDrive/results/votes.json"
26
+
27
+ # Ensure vote key is in lowercase to match dictionary keys
28
+ vote = vote.lower()
29
+
30
+ if os.path.exists(votes_file):
31
+ with open(votes_file, "r", encoding='utf-8') as file:
32
+ votes = json.load(file)
33
+ else:
34
+ votes = {}
35
+
36
+ key = os.path.basename(audio_path)
37
+ if key not in votes:
38
+ votes[key] = {"seamlessm4t": 0, "whisper": 0, "tie": 0}
39
+
40
+ if vote in votes[key]:
41
+ votes[key][vote] += 1
42
+ else:
43
+ print(f"Invalid vote option: {vote}. Valid options are 'gradio', 'openai', and 'tie'.")
44
+
45
+ with open(votes_file, "w", encoding='utf-8') as file:
46
+ json.dump(votes, file, indent=4)
47
+
48
+ def calculate_vote_totals():
49
+ votes_file = "./drive/MyDrive/results/votes.json"
50
+ if os.path.exists(votes_file):
51
+ with open(votes_file, "r", encoding='utf-8') as file:
52
+ votes_data = json.load(file)
53
+ else:
54
+ print("No votes have been recorded yet.")
55
+ return None
56
+
57
+ # Initialize totals
58
+ totals = {"seamlessm4t": 0, "whisper": 0, "tie": 0}
59
+
60
+ # Aggregate votes
61
+ for _, vote_counts in votes_data.items():
62
+ for key in totals:
63
+ totals[key] += vote_counts.get(key, 0)
64
+
65
+ return totals
66
+
67
+ def show_results():
68
+ totals = calculate_vote_totals()
69
+ if totals:
70
+ # Create a bar graph
71
+ labels = list(["SeamlessM4T", "Whisper", "Tie"])
72
+ values = list(totals.values())
73
+
74
+ plt.figure(figsize=(8, 6))
75
+ plt.bar(labels, values, color=['cornflowerblue', 'lavender', 'red'])
76
+ plt.xlabel('Models')
77
+ plt.ylabel('Votes')
78
+ plt.title('Vote Distribution')
79
+ plt.xticks(labels)
80
+ plt.ylim(0, max(values) + 1) # Set y-axis limit to make the graph aesthetically pleasing
81
+
82
+ return plt
83
+ else:
84
+ # Return an empty plot if no votes are found
85
+ plt.figure(figsize=(8, 6))
86
+ return plt
87
+
88
+ def setup_interface():
89
+ with gr.Blocks() as demo:
90
+ vote_options = gr.Radio(choices=["SeamlessM4T", "Whisper", "Tie"], label="Vote")
91
+ submit_button = gr.Button("Submit Vote")
92
+ gradio_transcription = gr.Textbox(label="SeamlessM4T-V2-large Transcription", interactive=False)
93
+ openai_transcription = gr.Textbox(label="OpenAI Whisper Transcription", interactive=False)
94
+ audio_player = gr.Audio(label="Listen to the Audio", interactive=False)
95
+
96
+ def submit_vote(vote):
97
+ audio_path, gr_transcription, oa_transcription = get_random_audio_and_transcriptions()
98
+ if vote: # Ensure a vote was made
99
+ handle_vote(vote, audio_path, gr_transcription, oa_transcription)
100
+ # Return new data to update the UI components
101
+ return gr_transcription, oa_transcription, audio_path
102
+
103
+ submit_button.click(submit_vote, inputs=[vote_options], outputs=[gradio_transcription, openai_transcription, audio_player])
104
+ show_results_button = gr.Button("Show Results")
105
+ results_plot = gr.Plot()
106
+
107
+ show_results_button.click(show_results, inputs=[], outputs=results_plot)
108
+ # Initialize with data
109
+ initial_data = get_random_audio_and_transcriptions()
110
+ gradio_transcription.value = initial_data[1]
111
+ openai_transcription.value = initial_data[2]
112
+ audio_player.value = initial_data[0]
113
+
114
+ return demo
115
+
116
+ demo = setup_interface()
117
+ demo.launch(debug=True)