semakoc commited on
Commit
d164ea6
·
verified ·
1 Parent(s): 03162ca

Toggle Button test

Browse files
Files changed (1) hide show
  1. app.py +66 -158
app.py CHANGED
@@ -12,7 +12,7 @@ import tempfile
12
 
13
  # Initialize Firebase
14
  firebase_config = json.loads(os.environ.get('firebase_creds'))
15
- cred = credentials.Certificate(firebase_config) # Your Firebase JSON key file
16
  firebase_admin.initialize_app(cred)
17
  db = firestore.client()
18
 
@@ -32,7 +32,7 @@ def transcribe(audio_file):
32
  transcription = processor.batch_decode(predicted_ids)[0]
33
  return transcription.replace("[UNK]", "")
34
  except Exception as e:
35
- return f"处理文件错误: {e}"
36
 
37
  def transcribe_both(audio_file):
38
  start_time = datetime.now()
@@ -40,165 +40,73 @@ def transcribe_both(audio_file):
40
  processing_time = (datetime.now() - start_time).total_seconds()
41
  return transcription, transcription, processing_time
42
 
43
- def store_correction(original_transcription, corrected_transcription, audio_file, processing_time, age, native_speaker):
44
- try:
45
- audio_metadata = {}
46
- if audio_file and os.path.exists(audio_file):
47
- audio, sr = librosa.load(audio_file, sr=16000)
48
- duration = librosa.get_duration(y=audio, sr=sr)
49
- file_size = os.path.getsize(audio_file)
50
- audio_metadata = {'duration': duration, 'file_size': file_size}
51
- combined_data = {
52
- 'original_text': original_transcription,
53
- 'corrected_text': corrected_transcription,
54
- 'timestamp': datetime.now().isoformat(),
55
- 'processing_time': processing_time,
56
- 'audio_metadata': audio_metadata,
57
- 'audio_url': None,
58
- 'model_name': MODEL_NAME,
59
- 'user_info': {
60
- 'native_amis_speaker': native_speaker,
61
- 'age': age
62
- }
63
- }
64
- db.collection('transcriptions').add(combined_data)
65
- return "校正保存成功! (Correction saved successfully!)"
66
- except Exception as e:
67
- return f"保存失败: {e} (Error saving correction: {e})"
 
 
 
68
 
69
- def prepare_download(audio_file, original_transcription, corrected_transcription):
70
- if audio_file is None:
71
- return None
 
 
 
 
 
 
72
 
73
- tmp_zip = tempfile.NamedTemporaryFile(delete=False, suffix=".zip")
74
- tmp_zip.close()
75
- with zipfile.ZipFile(tmp_zip.name, "w") as zf:
76
- if os.path.exists(audio_file):
77
- zf.write(audio_file, arcname="audio.wav")
78
-
79
- orig_txt = "original_transcription.txt"
80
- with open(orig_txt, "w", encoding="utf-8") as f:
81
- f.write(original_transcription)
82
- zf.write(orig_txt, arcname="original_transcription.txt")
83
- os.remove(orig_txt)
84
 
85
- corr_txt = "corrected_transcription.txt"
86
- with open(corr_txt, "w", encoding="utf-8") as f:
87
- f.write(corrected_transcription)
88
- zf.write(corr_txt, arcname="corrected_transcription.txt")
89
- os.remove(corr_txt)
90
- return tmp_zip.name
91
 
92
- # 界面设计
93
- with gr.Blocks(css="""
94
- .container {
95
- max-width: 800px;
96
- margin: auto;
97
- padding: 20px;
98
- font-family: Arial, sans-serif;
99
- }
100
- .header {
101
- text-align: center;
102
- margin-bottom: 30px;
103
- }
104
- .section {
105
- margin-bottom: 30px;
106
- padding: 15px;
107
- border: 1px solid #ddd;
108
- border-radius: 8px;
109
- background-color: #808080;
110
- }
111
- .section h3 {
112
- margin-top: 0;
113
- margin-bottom: 15px;
114
- text-align: center;
115
- }
116
- .button-row {
117
- display: flex;
118
- justify-content: center;
119
- gap: 10px;
120
- flex-wrap: wrap;
121
- }
122
- @media (max-width: 600px) {
123
- .gradio-row {
124
- flex-direction: column;
125
- }
126
- }
127
- """) as demo:
128
- with gr.Column(elem_classes="container"):
129
- gr.Markdown("<h1 class='header'>阿美語轉錄與修正系統 (ASR Correction System)</h1>")
130
-
131
- with gr.Column(elem_classes="section"):
132
- gr.Markdown("### 步驟 1:音訊上傳與轉錄(Audio Upload & Transcription)")
133
- with gr.Row():
134
- audio_input = gr.Audio(
135
- sources=["upload", "microphone"],
136
- type="filepath",
137
- label="音訊輸入 (Audio Input)"
138
- )
139
- transcribe_button = gr.Button("轉錄音訊 (Transcribe Audio)", variant="primary")
140
- proc_time_state = gr.State()
141
-
142
- with gr.Column(elem_classes="section"):
143
- gr.Markdown("### 步驟 2:審閱與編輯轉錄 (Review & Edit Transcription)")
144
- with gr.Row():
145
- original_text = gr.Textbox(
146
- label="原始轉錄 (Original Transcription)",
147
- interactive=False,
148
- lines=5,
149
- placeholder="謄本將在此出現... (Transcription will appear here...)"
150
- )
151
- corrected_text = gr.Textbox(
152
- label="更正轉錄 (Corrected Transcription)",
153
- interactive=True,
154
- lines=5,
155
- placeholder="在此編輯轉錄... (Edit transcription here...)"
156
- )
157
-
158
- with gr.Column(elem_classes="section"):
159
- gr.Markdown("### 步驟 3:使用者資訊 (User Information)")
160
- with gr.Row():
161
- age_input = gr.Slider(
162
- minimum=0,
163
- maximum=100,
164
- step=1,
165
- label="年齡 (Age)",
166
- value=25
167
- )
168
- native_speaker_input = gr.Checkbox(
169
- label="以阿美語為母語? (Native Amis Speaker?)",
170
- value=True
171
- )
172
-
173
- with gr.Column(elem_classes="section"):
174
- gr.Markdown("### 步驟 4:儲存與下載 (Save & Download)")
175
- with gr.Row(elem_classes="button-row"):
176
- save_button = gr.Button("儲存更正 (Save Correction)", variant="primary")
177
- save_status = gr.Textbox(
178
- label="儲存狀態 (Save Status)",
179
- interactive=False,
180
- placeholder="狀態訊息會出現在這裡... (Status messages will appear here...)"
181
- )
182
- with gr.Row(elem_classes="button-row"):
183
- download_button = gr.Button("下載 ZIP 檔案 (Download ZIP)")
184
- download_output = gr.File(label="下載 ZIP 檔案 (Download ZIP)")
185
 
186
- transcribe_button.click(
187
- fn=transcribe_both,
188
- inputs=audio_input,
189
- outputs=[original_text, corrected_text, proc_time_state]
190
- )
191
-
192
- save_button.click(
193
- fn=store_correction,
194
- inputs=[original_text, corrected_text, audio_input, proc_time_state, age_input, native_speaker_input],
195
- outputs=save_status
196
- )
197
-
198
- download_button.click(
199
- fn=prepare_download,
200
- inputs=[audio_input, original_text, corrected_text],
201
- outputs=download_output
202
- )
203
 
204
- demo.launch(share=True)
 
12
 
13
  # Initialize Firebase
14
  firebase_config = json.loads(os.environ.get('firebase_creds'))
15
+ cred = credentials.Certificate(firebase_config)
16
  firebase_admin.initialize_app(cred)
17
  db = firestore.client()
18
 
 
32
  transcription = processor.batch_decode(predicted_ids)[0]
33
  return transcription.replace("[UNK]", "")
34
  except Exception as e:
35
+ return f"處理文件錯誤: {e}"
36
 
37
  def transcribe_both(audio_file):
38
  start_time = datetime.now()
 
40
  processing_time = (datetime.now() - start_time).total_seconds()
41
  return transcription, transcription, processing_time
42
 
43
+ def toggle_language(switch):
44
+ """Switch UI text between English and Traditional Chinese"""
45
+ if switch:
46
+ return (
47
+ "阿美語轉錄與修正系統",
48
+ "步驟 1:音訊上傳與轉錄",
49
+ "步驟 2:審閱與編輯轉錄",
50
+ "步驟 3:使用者資訊",
51
+ "步驟 4:儲存與下載",
52
+ "音訊輸入", "轉錄音訊",
53
+ "原始轉錄", "更正轉錄",
54
+ "年齡", "以阿美語為母語?",
55
+ "儲存更正", "儲存狀態",
56
+ "下載 ZIP 檔案"
57
+ )
58
+ else:
59
+ return (
60
+ "Amis ASR Transcription & Correction System",
61
+ "Step 1: Audio Upload & Transcription",
62
+ "Step 2: Review & Edit Transcription",
63
+ "Step 3: User Information",
64
+ "Step 4: Save & Download",
65
+ "Audio Input", "Transcribe Audio",
66
+ "Original Transcription", "Corrected Transcription",
67
+ "Age", "Native Amis Speaker?",
68
+ "Save Correction", "Save Status",
69
+ "Download ZIP File"
70
+ )
71
 
72
+ # Interface
73
+ with gr.Blocks() as demo:
74
+ lang_switch = gr.Checkbox(label="切換到繁體中文 (Switch to Traditional Chinese)")
75
+
76
+ title = gr.Markdown("Amis ASR Transcription & Correction System")
77
+ step1 = gr.Markdown("Step 1: Audio Upload & Transcription")
78
+ step2 = gr.Markdown("Step 2: Review & Edit Transcription")
79
+ step3 = gr.Markdown("Step 3: User Information")
80
+ step4 = gr.Markdown("Step 4: Save & Download")
81
 
82
+ with gr.Row():
83
+ audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Audio Input")
84
+ transcribe_button = gr.Button("Transcribe Audio")
85
+
86
+ original_text = gr.Textbox(label="Original Transcription", interactive=False, lines=5)
87
+ corrected_text = gr.Textbox(label="Corrected Transcription", interactive=True, lines=5)
 
 
 
 
 
88
 
89
+ age_input = gr.Slider(minimum=0, maximum=100, step=1, label="Age", value=25)
90
+ native_speaker_input = gr.Checkbox(label="Native Amis Speaker?", value=True)
 
 
 
 
91
 
92
+ save_button = gr.Button("Save Correction")
93
+ save_status = gr.Textbox(label="Save Status", interactive=False)
94
+ download_button = gr.Button("Download ZIP File")
95
+ download_output = gr.File()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
+ # Toggle language dynamically
98
+ lang_switch.change(
99
+ toggle_language,
100
+ inputs=lang_switch,
101
+ outputs=[title, step1, step2, step3, step4, audio_input, transcribe_button,
102
+ original_text, corrected_text, age_input, native_speaker_input,
103
+ save_button, save_status, download_button]
104
+ )
105
+
106
+ transcribe_button.click(transcribe_both, inputs=audio_input, outputs=[original_text, corrected_text])
107
+
108
+ save_button.click(store_correction, inputs=[original_text, corrected_text, audio_input, age_input, native_speaker_input], outputs=save_status)
109
+
110
+ download_button.click(prepare_download, inputs=[audio_input, original_text, corrected_text], outputs=download_output)
 
 
 
111
 
112
+ demo.launch()