Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -116,28 +116,7 @@ LANGUAGE_NAME_TO_CODE = {
|
|
| 116 |
"Sundanese": "su",
|
| 117 |
}
|
| 118 |
|
| 119 |
-
def detect_language(audio_file):
|
| 120 |
-
"""Detect the language of the audio file."""
|
| 121 |
-
# Load the Whisper model (use "base" for faster detection)
|
| 122 |
-
model = whisper.load_model("base")
|
| 123 |
-
|
| 124 |
-
# Convert audio to 16kHz mono for better compatibility with Whisper
|
| 125 |
-
audio = AudioSegment.from_file(audio_file)
|
| 126 |
-
audio = audio.set_frame_rate(16000).set_channels(1)
|
| 127 |
-
processed_audio_path = "processed_audio.wav"
|
| 128 |
-
audio.export(processed_audio_path, format="wav")
|
| 129 |
-
|
| 130 |
-
# Detect the language
|
| 131 |
-
result = model.transcribe(processed_audio_path, task="detect_language", fp16=False)
|
| 132 |
-
detected_language = result.get("language", "unknown")
|
| 133 |
-
|
| 134 |
-
# Clean up processed audio file
|
| 135 |
-
os.remove(processed_audio_path)
|
| 136 |
-
|
| 137 |
-
return f"Detected Language: {detected_language}"
|
| 138 |
-
|
| 139 |
def transcribe_audio(audio_file, language="Auto Detect", model_size="Base (Faster)"):
|
| 140 |
-
"""Transcribe the audio file."""
|
| 141 |
# Load the selected Whisper model
|
| 142 |
model = whisper.load_model(MODELS[model_size])
|
| 143 |
|
|
@@ -163,34 +142,25 @@ def transcribe_audio(audio_file, language="Auto Detect", model_size="Base (Faste
|
|
| 163 |
return f"Detected Language: {detected_language}\n\nTranscription:\n{result['text']}"
|
| 164 |
|
| 165 |
# Define the Gradio interface
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
gr.
|
| 171 |
-
detect_audio_input = gr.Audio(type="filepath", label="Upload Audio File")
|
| 172 |
-
detect_language_output = gr.Textbox(label="Detected Language")
|
| 173 |
-
detect_button = gr.Button("Detect Language")
|
| 174 |
-
|
| 175 |
-
with gr.Tab("Transcribe Audio"):
|
| 176 |
-
gr.Markdown("Upload an audio file, select a language (or choose 'Auto Detect'), and choose a model for transcription.")
|
| 177 |
-
transcribe_audio_input = gr.Audio(type="filepath", label="Upload Audio File")
|
| 178 |
-
language_dropdown = gr.Dropdown(
|
| 179 |
choices=list(LANGUAGE_NAME_TO_CODE.keys()), # Full language names
|
| 180 |
label="Select Language",
|
| 181 |
value="Auto Detect"
|
| 182 |
-
)
|
| 183 |
-
|
| 184 |
choices=list(MODELS.keys()), # Model options
|
| 185 |
label="Select Model",
|
| 186 |
value="Base (Faster)" # Default to "Base" model
|
| 187 |
)
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
transcribe_button.click(transcribe_audio, inputs=[transcribe_audio_input, language_dropdown, model_dropdown], outputs=transcribe_output)
|
| 194 |
|
| 195 |
# Launch the Gradio interface
|
| 196 |
-
|
|
|
|
| 116 |
"Sundanese": "su",
|
| 117 |
}
|
| 118 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
def transcribe_audio(audio_file, language="Auto Detect", model_size="Base (Faster)"):
|
|
|
|
| 120 |
# Load the selected Whisper model
|
| 121 |
model = whisper.load_model(MODELS[model_size])
|
| 122 |
|
|
|
|
| 142 |
return f"Detected Language: {detected_language}\n\nTranscription:\n{result['text']}"
|
| 143 |
|
| 144 |
# Define the Gradio interface
|
| 145 |
+
iface = gr.Interface(
|
| 146 |
+
fn=transcribe_audio,
|
| 147 |
+
inputs=[
|
| 148 |
+
gr.Audio(type="filepath", label="Upload Audio File"),
|
| 149 |
+
gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
choices=list(LANGUAGE_NAME_TO_CODE.keys()), # Full language names
|
| 151 |
label="Select Language",
|
| 152 |
value="Auto Detect"
|
| 153 |
+
),
|
| 154 |
+
gr.Dropdown(
|
| 155 |
choices=list(MODELS.keys()), # Model options
|
| 156 |
label="Select Model",
|
| 157 |
value="Base (Faster)" # Default to "Base" model
|
| 158 |
)
|
| 159 |
+
],
|
| 160 |
+
outputs=gr.Textbox(label="Transcription and Detected Language"),
|
| 161 |
+
title="Audio Transcription with Language and Model Selection",
|
| 162 |
+
description="Upload an audio file, select a language (or choose 'Auto Detect'), and choose a model for transcription."
|
| 163 |
+
)
|
|
|
|
| 164 |
|
| 165 |
# Launch the Gradio interface
|
| 166 |
+
iface.launch()
|