File size: 8,135 Bytes
06fc5c8
00885ea
 
 
 
 
 
6c96ec5
 
 
0075f67
6c96ec5
00885ea
 
631422a
6c96ec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631422a
0075f67
 
 
6c96ec5
 
631422a
6c96ec5
631422a
333984b
631422a
6c96ec5
 
 
 
 
631422a
6c96ec5
 
 
631422a
3cd1f60
 
631422a
6c96ec5
 
 
 
 
 
 
 
 
 
 
631422a
6c96ec5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4baf7c2
3cd1f60
 
 
 
 
 
 
 
 
 
 
4baf7c2
3cd1f60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4baf7c2
3cd1f60
 
 
00885ea
3cd1f60
e8353f3
 
3cd1f60
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
import gradio as gr
import torch
import torchaudio
import numpy as np
from denoiser.demucs import Demucs
from pydub import AudioSegment
import soundfile as sf
import librosa
 
modelpath = './denoiser/master64.th'
 
def transcribe(file_upload, microphone):
    file = microphone if microphone is not None else file_upload
    
    # 統一音訊預處理流程
    def preprocess_audio(input_path, output_path):
        # 用 pydub 處理格式轉換
        audio = AudioSegment.from_file(input_path)
        
        # 強制轉單聲道 + 16kHz 採樣率
        if audio.channels > 1:
            audio = audio.set_channels(1)
        if audio.frame_rate != 16000:
            audio = audio.set_frame_rate(16000)
        
        # 導出為 WAV 暫存檔
        audio.export(output_path, format="wav")
        return output_path
 
    # 處理 MP3 特殊流程
    if file.lower().endswith(".mp3"):
        temp_wav = "temp_input.wav"
        preprocess_audio(file, temp_wav)
        file = temp_wav
 
    # 載入模型
    model = Demucs(hidden=64)
    state_dict = torch.load(modelpath, map_location='cpu')
    model.load_state_dict(state_dict)
    model.eval()
 
    # 載入音訊並強制轉單聲道
    x, sr = torchaudio.load(file)
    if x.shape[0] > 1:
        x = torch.mean(x, dim=0, keepdim=True)
    
    # 音訊長度檢查
    MAX_AUDIO_SECONDS = 600
    if x.shape[1] / sr > MAX_AUDIO_SECONDS:
        raise ValueError(f"音訊過長!限制:{MAX_AUDIO_SECONDS} 秒,當前:{x.shape[1]/sr:.1f} 秒")
 
    # 執行降噪
    with torch.no_grad():
        out = model(x[None])[0]
 
    # 後處理
    out = out / max(out.abs().max().item(), 1)
    torchaudio.save('enhanced.wav', out, sr)
    
    # 轉 MP3 輸出
    enhanced_mp3 = 'enhanced.mp3'
    AudioSegment.from_wav('enhanced.wav').export(
        enhanced_mp3,
        format="mp3",
        bitrate="256k"
    )
    
    # 清理暫存檔
    if os.path.exists("temp_input.wav"):
        os.remove("temp_input.wav")
    
    return enhanced_mp3
 
# 👇 重要:修正 Gradio 類型推導問題
transcribe.__annotations__ = {
    "file_upload": str,
    "microphone": str,
    "return": str
}

demo = gr.Interface(
    fn=transcribe,
    inputs=[
        gr.Audio(type="filepath", label="上傳音訊檔案", sources=["upload", "microphone"])
    ],
    outputs=[
        gr.Audio(type="filepath", label="處理後音訊")
    ],
    live=True,
    allow_flagging="never",
    title="<h1>語音質檢/噪音去除 (語音增強)</h1>",
    description="""<h2><a href='https://www.twman.org' target='_blank'>TonTon Huang Ph.D.</a> | <a href='https://blog.twman.org/p/deeplearning101.html' target='_blank'>手把手帶你一起踩AI坑</a><br></h2><br>
                為了提升語音識別的效果,可以在識別前先進行噪音去除<br>
                    <a href='https://github.com/Deep-Learning-101' target='_blank'>Deep Learning 101 Github</a> | <a href='http://deeplearning101.twman.org' target='_blank'>Deep Learning 101</a> | <a href='https://www.facebook.com/groups/525579498272187/' target='_blank'>台灣人工智慧社團 FB</a> | <a href='https://www.youtube.com/c/DeepLearning101' target='_blank'>YouTube</a><br>
                    <a href='https://blog.twman.org/2025/03/AIAgent.html' target='_blank'>那些 AI Agent 要踩的坑</a>:探討多種 AI 代理人工具的應用經驗與挑戰,分享實用經驗與工具推薦。<br>
                    <a href='https://blog.twman.org/2024/08/LLM.html' target='_blank'>白話文手把手帶你科普 GenAI</a>:淺顯介紹生成式人工智慧核心概念,強調硬體資源和數據的重要性。<br>
                    <a href='https://blog.twman.org/2024/09/LLM.html' target='_blank'>大型語言模型直接就打完收工?</a>:回顧 LLM 領域探索歷程,討論硬體升級對 AI 開發的重要性。<br>
                    <a href='https://blog.twman.org/2024/07/RAG.html' target='_blank'>那些檢索增強生成要踩的坑</a>:探討 RAG 技術應用與挑戰,提供實用經驗分享和工具建議。<br>
                    <a href='https://blog.twman.org/2024/02/LLM.html' target='_blank'>那些大型語言模型要踩的坑</a>:探討多種 LLM 工具的應用與挑戰,強調硬體資源的重要性。<br>
                    <a href='https://blog.twman.org/2023/04/GPT.html' target='_blank'>Large Language Model,LLM</a>:探討 LLM 的發展與應用,強調硬體資源在開發中的關鍵作用。。<br>
                    <a href='https://blog.twman.org/2024/11/diffusion.html' target='_blank'>ComfyUI + Stable Diffuision</a>:深入探討影像生成與分割技術的應用,強調硬體資源的重要性。<br>
                    <a href='https://blog.twman.org/2024/02/asr-tts.html' target='_blank'>那些ASR和TTS可能會踩的坑</a>:探討 ASR 和 TTS 技術應用中的問題,強調數據質量的重要性。<br>
                    <a href='https://blog.twman.org/2021/04/NLP.html' target='_blank'>那些自然語言處理 (Natural Language Processing, NLP) 踩的坑</a>:分享 NLP 領域的實踐經驗,強調數據質量對模型效果的影響。<br>
                    <a href='https://blog.twman.org/2021/04/ASR.html' target='_blank'>那些語音處理 (Speech Processing) 踩的坑</a>:分享語音處理領域的實務經驗,強調資料品質對模型效果的影響。<br>
                    <a href='https://blog.twman.org/2023/07/wsl.html' target='_blank'>用PPOCRLabel來幫PaddleOCR做OCR的微調和標註</a><br>
                    <a href='https://blog.twman.org/2023/07/HugIE.html' target='_blank'>基於機器閱讀理解和指令微調的統一信息抽取框架之診斷書醫囑資訊擷取分析</a><br>                  
                <a href='https://github.com/facebookresearch/denoiser' target='_blank'> Real Time Speech Enhancement in the Waveform Domain (Interspeech 2020)</a>""",
)

demo.launch(debug=True, share=True)

# import os
# import time
# import json
# import gradio as gr
# import torch
# import torchaudio
# import numpy as np
# from denoiser.demucs import Demucs
# from pydub import AudioSegment
# import soundfile as sf
# import librosa

# modelpath = './denoiser/master64.th'

# def transcribe(file_upload, microphone):
#     file = microphone if microphone is not None else file_upload

#     # 新增音訊預處理 → 統一格式
#     def preprocess_audio(path):
#         data, sr = sf.read(path)
        
#         # 如果是雙聲道 → 轉單聲道
#         if len(data.shape) > 1:
#             data = data.mean(axis=1)

#         # 如果不是 16kHz → 重採樣
#         if sr != 16000:
#             data = librosa.resample(data, orig_sr=sr, target_sr=16000)
#             sr = 16000

#         # 儲存為 WAV 供模型使用
#         sf.write("enhanced.wav", data, sr)
#         return "enhanced.wav"

#     # 如果是 MP3,先轉成 WAV 再處理
#     if file.lower().endswith(".mp3"):
#         audio = AudioSegment.from_file(file)
#         audio = audio.set_frame_rate(16000).set_channels(1)  # 轉單聲道 + 16kHz
#         audio.export("enhanced.wav", format="wav")
#         file = "enhanced.wav"
#     else:
#         file = preprocess_audio(file)

#     model = Demucs(hidden=64)
#     state_dict = torch.load(modelpath, map_location='cpu')
#     model.load_state_dict(state_dict)
#     demucs = model.eval()

#     x, sr = torchaudio.load(file)
#     x = x[0:1]  # 強制取第一個聲道(確保是單聲道)

#     with torch.no_grad():
#         out = demucs(x[None])[0]

#     out = out / max(out.abs().max().item(), 1)
#     torchaudio.save('enhanced_final.wav', out, sr)

#     # 輸出 WAV 格式給前端播放
#     enhanced = AudioSegment.from_wav('enhanced_final.wav')
#     enhanced.export('enhanced_final.mp3', format="mp3", bitrate="256k")

#     return "enhanced_final.mp3"  # 回傳 MP3 更省空間


# # 👇 加上這一行,解決 Gradio schema 推導錯誤
# transcribe.__annotations__ = {
#     "file_upload": str,
#     "microphone": str,
#     "return": str
# }