Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,50 +3,17 @@ import whisper
|
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
sinhala_processor = WhisperProcessor.from_pretrained("Subhaka/whisper-small-Sinhala-Fine_Tune")
|
15 |
-
except Exception as e:
|
16 |
-
print("Failed to load fine-tuned Sinhala model. Falling back to the base model.")
|
17 |
-
print(f"Error: {e}")
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
audio = AudioSegment.from_file(audio_file)
|
22 |
-
audio = audio.set_frame_rate(16000).set_channels(1)
|
23 |
-
processed_audio_path = "processed_audio.wav"
|
24 |
-
audio.export(processed_audio_path, format="wav")
|
25 |
-
|
26 |
-
# Load the appropriate model based on the selected language
|
27 |
-
if language == "Sinhala" and sinhala_model is not None:
|
28 |
-
print("Using fine-tuned Sinhala model.")
|
29 |
-
model = sinhala_model
|
30 |
-
processor = sinhala_processor
|
31 |
-
else:
|
32 |
-
print("Using base Whisper model.")
|
33 |
-
model = base_model
|
34 |
-
processor = None
|
35 |
-
|
36 |
-
# Transcribe the audio
|
37 |
-
if language == "Auto Detect":
|
38 |
-
result = model.transcribe(processed_audio_path, fp16=False) # Auto-detect language
|
39 |
-
detected_language = result.get("language", "unknown")
|
40 |
-
else:
|
41 |
-
language_code = LANGUAGE_NAME_TO_CODE.get(language, "en") # Default to English if not found
|
42 |
-
result = model.transcribe(processed_audio_path, language=language_code, fp16=False)
|
43 |
-
detected_language = language_code
|
44 |
-
|
45 |
-
# Clean up processed audio file
|
46 |
-
os.remove(processed_audio_path)
|
47 |
-
|
48 |
-
# Return transcription and detected language
|
49 |
-
return f"Detected Language: {detected_language}\n\nTranscription:\n{result['text']}"
|
50 |
|
51 |
# Mapping of full language names to language codes
|
52 |
LANGUAGE_NAME_TO_CODE = {
|
@@ -152,21 +119,69 @@ LANGUAGE_NAME_TO_CODE = {
|
|
152 |
"Sundanese": "su",
|
153 |
}
|
154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
# Define the Gradio interface
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
gr.
|
|
|
|
|
161 |
choices=list(LANGUAGE_NAME_TO_CODE.keys()), # Full language names
|
162 |
label="Select Language",
|
163 |
value="Auto Detect"
|
164 |
)
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
# Launch the Gradio interface
|
172 |
-
|
|
|
3 |
import os
|
4 |
from pydub import AudioSegment
|
5 |
|
6 |
+
# Mapping of model names to Whisper model sizes
|
7 |
+
MODELS = {
|
8 |
+
"Tiny (Fastest)": "tiny",
|
9 |
+
"Base (Faster)": "base",
|
10 |
+
"Small (Balanced)": "small",
|
11 |
+
"Medium (Accurate)": "medium",
|
12 |
+
"Large (Most Accurate)": "large"
|
13 |
+
}
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Fine-tuned Sinhala model
|
16 |
+
SINHALA_MODEL = "malakazzz/Subhaka-whisper-small-Sinhala-Fine_Tune"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Mapping of full language names to language codes
|
19 |
LANGUAGE_NAME_TO_CODE = {
|
|
|
119 |
"Sundanese": "su",
|
120 |
}
|
121 |
|
122 |
+
def transcribe_audio(audio_file, language="Auto Detect", model_size="Base (Faster)"):
|
123 |
+
"""Transcribe the audio file."""
|
124 |
+
# Load the appropriate model
|
125 |
+
if language == "Sinhala":
|
126 |
+
# Use the fine-tuned Sinhala model
|
127 |
+
model = gr.load(SINHALA_MODEL)
|
128 |
+
else:
|
129 |
+
# Use the selected Whisper model
|
130 |
+
model = whisper.load_model(MODELS[model_size])
|
131 |
+
|
132 |
+
# Convert audio to 16kHz mono for better compatibility with Whisper
|
133 |
+
audio = AudioSegment.from_file(audio_file)
|
134 |
+
audio = audio.set_frame_rate(16000).set_channels(1)
|
135 |
+
processed_audio_path = "processed_audio.wav"
|
136 |
+
audio.export(processed_audio_path, format="wav")
|
137 |
+
|
138 |
+
# Transcribe the audio
|
139 |
+
if language == "Auto Detect":
|
140 |
+
result = model.transcribe(processed_audio_path, fp16=False) # Auto-detect language
|
141 |
+
detected_language = result.get("language", "unknown")
|
142 |
+
else:
|
143 |
+
language_code = LANGUAGE_NAME_TO_CODE.get(language, "en") # Default to English if not found
|
144 |
+
result = model.transcribe(processed_audio_path, language=language_code, fp16=False)
|
145 |
+
detected_language = language_code
|
146 |
+
|
147 |
+
# Clean up processed audio file
|
148 |
+
os.remove(processed_audio_path)
|
149 |
+
|
150 |
+
# Return transcription and detected language
|
151 |
+
return f"Detected Language: {detected_language}\n\nTranscription:\n{result['text']}"
|
152 |
+
|
153 |
# Define the Gradio interface
|
154 |
+
with gr.Blocks() as demo:
|
155 |
+
gr.Markdown("# Audio Transcription and Language Detection")
|
156 |
+
|
157 |
+
with gr.Tab("Transcribe Audio"):
|
158 |
+
gr.Markdown("Upload an audio file, select a language (or choose 'Auto Detect'), and choose a model for transcription.")
|
159 |
+
transcribe_audio_input = gr.Audio(type="filepath", label="Upload Audio File")
|
160 |
+
language_dropdown = gr.Dropdown(
|
161 |
choices=list(LANGUAGE_NAME_TO_CODE.keys()), # Full language names
|
162 |
label="Select Language",
|
163 |
value="Auto Detect"
|
164 |
)
|
165 |
+
model_dropdown = gr.Dropdown(
|
166 |
+
choices=list(MODELS.keys()), # Model options
|
167 |
+
label="Select Model",
|
168 |
+
value="Base (Faster)", # Default to "Base" model
|
169 |
+
interactive=True # Allow model selection by default
|
170 |
+
)
|
171 |
+
transcribe_output = gr.Textbox(label="Transcription and Detected Language")
|
172 |
+
transcribe_button = gr.Button("Transcribe Audio")
|
173 |
+
|
174 |
+
# Update model dropdown based on language selection
|
175 |
+
def update_model_dropdown(language):
|
176 |
+
if language == "Sinhala":
|
177 |
+
return gr.Dropdown(interactive=False, value="Fine-Tuned Sinhala Model")
|
178 |
+
else:
|
179 |
+
return gr.Dropdown(choices=list(MODELS.keys()), interactive=True, value="Base (Faster)")
|
180 |
+
|
181 |
+
language_dropdown.change(update_model_dropdown, inputs=language_dropdown, outputs=model_dropdown)
|
182 |
+
|
183 |
+
# Link button to function
|
184 |
+
transcribe_button.click(transcribe_audio, inputs=[transcribe_audio_input, language_dropdown, model_dropdown], outputs=transcribe_output)
|
185 |
|
186 |
# Launch the Gradio interface
|
187 |
+
demo.launch()
|