DeepLearning101 commited on
Commit
7094a64
·
verified ·
1 Parent(s): b851f75

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -15
app.py CHANGED
@@ -3,33 +3,23 @@ import gradio as gr
3
  import whisper
4
  import os
5
 
 
 
 
6
  # 加載 Whisper 模型
7
  model = whisper.load_model("large-v2", device="cuda" if torch.cuda.is_available() else "cpu")
8
 
9
  def transcribe(audio_file):
10
- # 從 Gradio 文件輸入獲取文件路徑
11
  audio_path = audio_file
12
-
13
- # 使用 Whisper 進行語音識別
14
  result = model.transcribe(audio_path)
15
  text = result["text"]
16
-
17
- # 提取上載的音頻文件的基本名字,用作保存轉錄文本的文件名
18
  base_name = os.path.splitext(os.path.basename(audio_path))[0]
19
- # 定義保存轉錄結果的文件路徑
20
  transcript_file_path = f"txt/{base_name}_transcript.txt"
21
-
22
- # 確保 txt 目錄存在
23
  os.makedirs("txt", exist_ok=True)
24
-
25
- # 將轉錄文本保存到文件
26
  with open(transcript_file_path, "w") as file:
27
  file.write(text)
28
-
29
- # 返回文本和文件路徑,使得文件可以在界麵上下載
30
  return text, transcript_file_path
31
 
32
- # 創建 Gradio 界麵
33
  with gr.Blocks(css=".container { max-width: 800px; margin: auto; } .gradio-app { background-color: #f0f0f0; } button { background-color: #4CAF50; color: white; }") as demo:
34
  gr.Markdown("ASR 語音語料辨識修正工具")
35
  with gr.Row():
@@ -37,7 +27,6 @@ with gr.Blocks(css=".container { max-width: 800px; margin: auto; } .gradio-app {
37
  submit_button = gr.Button("語音識別")
38
  output_text = gr.TextArea(label="識別結果")
39
  download_link = gr.File(label="下載轉錄文件")
40
-
41
  submit_button.click(fn=transcribe, inputs=audio_input, outputs=[output_text, download_link])
42
 
43
- demo.launch()
 
3
  import whisper
4
  import os
5
 
6
+ # 確保 Whisper 模塊被正確加載
7
+ print("Whisper module contents:", dir(whisper))
8
+
9
  # 加載 Whisper 模型
10
  model = whisper.load_model("large-v2", device="cuda" if torch.cuda.is_available() else "cpu")
11
 
12
  def transcribe(audio_file):
 
13
  audio_path = audio_file
 
 
14
  result = model.transcribe(audio_path)
15
  text = result["text"]
 
 
16
  base_name = os.path.splitext(os.path.basename(audio_path))[0]
 
17
  transcript_file_path = f"txt/{base_name}_transcript.txt"
 
 
18
  os.makedirs("txt", exist_ok=True)
 
 
19
  with open(transcript_file_path, "w") as file:
20
  file.write(text)
 
 
21
  return text, transcript_file_path
22
 
 
23
  with gr.Blocks(css=".container { max-width: 800px; margin: auto; } .gradio-app { background-color: #f0f0f0; } button { background-color: #4CAF50; color: white; }") as demo:
24
  gr.Markdown("ASR 語音語料辨識修正工具")
25
  with gr.Row():
 
27
  submit_button = gr.Button("語音識別")
28
  output_text = gr.TextArea(label="識別結果")
29
  download_link = gr.File(label="下載轉錄文件")
 
30
  submit_button.click(fn=transcribe, inputs=audio_input, outputs=[output_text, download_link])
31
 
32
+ demo.launch()