Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -116,7 +116,28 @@ LANGUAGE_NAME_TO_CODE = {
|
|
| 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,25 +163,34 @@ def transcribe_audio(audio_file, language="Auto Detect", model_size="Base (Faste
|
|
| 142 |
return f"Detected Language: {detected_language}\n\nTranscription:\n{result['text']}"
|
| 143 |
|
| 144 |
# Define the Gradio interface
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
)
|
|
|
|
| 164 |
|
| 165 |
# Launch the Gradio interface
|
| 166 |
-
|
|
|
|
| 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 |
return f"Detected Language: {detected_language}\n\nTranscription:\n{result['text']}"
|
| 164 |
|
| 165 |
# Define the Gradio interface
|
| 166 |
+
with gr.Blocks() as demo:
|
| 167 |
+
gr.Markdown("# Audio Transcription and Language Detection")
|
| 168 |
+
|
| 169 |
+
with gr.Tab("Detect Language"):
|
| 170 |
+
gr.Markdown("Upload an audio file to detect its language.")
|
| 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 |
+
model_dropdown = gr.Dropdown(
|
| 184 |
choices=list(MODELS.keys()), # Model options
|
| 185 |
label="Select Model",
|
| 186 |
value="Base (Faster)" # Default to "Base" model
|
| 187 |
)
|
| 188 |
+
transcribe_output = gr.Textbox(label="Transcription and Detected Language")
|
| 189 |
+
transcribe_button = gr.Button("Transcribe Audio")
|
| 190 |
+
|
| 191 |
+
# Link buttons to functions
|
| 192 |
+
detect_button.click(detect_language, inputs=detect_audio_input, outputs=detect_language_output)
|
| 193 |
+
transcribe_button.click(transcribe_audio, inputs=[transcribe_audio_input, language_dropdown, model_dropdown], outputs=transcribe_output)
|
| 194 |
|
| 195 |
# Launch the Gradio interface
|
| 196 |
+
demo.launch()
|