Spaces:
Sleeping
Sleeping
File size: 9,702 Bytes
9ce3c93 962a236 ec35154 9ce3c93 962a236 9ce3c93 962a236 ec35154 6497d65 ec35154 2ba1ae3 ec35154 6497d65 ec35154 6497d65 ec35154 6497d65 9ce3c93 ec35154 2ba1ae3 9ce3c93 2cbbf1f ec35154 990149f 2cbbf1f 990149f ec35154 2cbbf1f 990149f 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f 6497d65 2cbbf1f ec35154 2cbbf1f ec35154 6497d65 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 990149f 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f 6497d65 990149f 2cbbf1f 6497d65 2cbbf1f ec35154 9ce3c93 990149f 2cbbf1f ec35154 9ce3c93 990149f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f 6497d65 990149f 2cbbf1f 990149f 6497d65 990149f 2cbbf1f 6497d65 990149f 2cbbf1f 990149f 2cbbf1f 990149f 2cbbf1f 990149f 2cbbf1f 990149f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 6497d65 ec35154 6497d65 990149f ec35154 2cbbf1f ec35154 2cbbf1f ec35154 2cbbf1f 6497d65 ec35154 6497d65 ec35154 6497d65 ec35154 2cbbf1f ec35154 6497d65 ec35154 9ce3c93 962a236 ec35154 9ce3c93 ec35154 |
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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
import gradio as gr
from transformers import pipeline
import re
import time
# Load both ASR models
whisper_asr = pipeline("automatic-speech-recognition", model="1morecupofhottea/Whisper-Code-Switching-Kh-En")
wav2vec_asr = pipeline("automatic-speech-recognition", model="1morecupofhottea/wav2vec2-Code-Switching-Kh_En")
def clean_transcript(text: str) -> str:
# Remove <km>, </km>, <en>, </en>, etc.
return re.sub(r"</?[^>]+>", "", text).strip()
def transcribe(audio, model_choice):
if audio is None:
return "Please upload or record an audio file first."
try:
if model_choice == "Whisper Model":
result = whisper_asr(audio)
return f"Transcription Complete:\n\n{result['text']}"
else:
result = wav2vec_asr(audio)
cleaned_text = clean_transcript(result["text"])
return f"Transcription Complete:\n\n{cleaned_text}"
except Exception as e:
return f"Error during transcription: {str(e)}"
def clear_all():
return None, "Whisper Model", ""
# Custom CSS with dark theme
custom_css = """
/*
DARK THEME COLOR PALETTE:
- Background: #000000 (Pure black - main background)
- Surface Dark: #1a1a1a (Dark gray - cards, containers)
- Surface Medium: #2d2d2d (Medium gray - inputs, components)
- Border: #404040 (Gray - borders, dividers)
- Text Primary: #ffffff (White - main text, headers)
- Text Secondary: #b0b0b0 (Light gray - secondary text)
- Accent: #4a9eff (Blue - buttons, highlights)
- Accent Hover: #357abd (Darker blue - hover states)
- Success: #28a745 (Green - success states)
- Warning: #ffc107 (Yellow - warnings)
*/
/* Global Styles */
.gradio-container {
background: #000000 !important;
font-family: 'Georgia', 'Times New Roman', serif !important;
color: #ffffff !important;
min-height: 100vh !important;
}
/* Header Styling */
.header-section {
background: #1a1a1a;
border: 2px solid #404040;
border-radius: 12px;
padding: 30px;
margin-bottom: 25px;
box-shadow: 0 4px 12px rgba(255, 255, 255, 0.1);
}
.header-section h1 {
color: #ffffff !important;
font-size: 2.4em !important;
font-weight: 700 !important;
text-align: center;
margin-bottom: 15px;
border-bottom: 3px solid #4a9eff;
padding-bottom: 15px;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}
.header-section p {
color: #b0b0b0;
font-size: 1.1em;
text-align: center;
margin: 8px 0;
line-height: 1.6;
}
/* Main Content Cards */
.input-card, .output-card {
background: #1a1a1a;
border: 2px solid #404040;
border-radius: 12px;
padding: 25px;
box-shadow: 0 4px 12px rgba(255, 255, 255, 0.05);
margin-bottom: 20px;
}
/* Button Styling */
.primary-button {
background: linear-gradient(135deg, #4a9eff, #357abd) !important;
border: 2px solid #4a9eff !important;
border-radius: 8px !important;
padding: 14px 28px !important;
font-size: 1.1em !important;
font-weight: 600 !important;
color: #ffffff !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 12px rgba(74, 158, 255, 0.3) !important;
}
.primary-button:hover {
background: linear-gradient(135deg, #357abd, #2a5d8f) !important;
border-color: #357abd !important;
transform: translateY(-2px) !important;
box-shadow: 0 6px 16px rgba(74, 158, 255, 0.4) !important;
}
.secondary-button {
background: #2d2d2d !important;
border: 2px solid #404040 !important;
border-radius: 8px !important;
padding: 12px 24px !important;
font-size: 1em !important;
font-weight: 500 !important;
color: #ffffff !important;
transition: all 0.3s ease !important;
}
.secondary-button:hover {
background: #404040 !important;
border-color: #555555 !important;
transform: translateY(-1px) !important;
}
/* Component Styling */
.audio-component, .dropdown-component {
border-radius: 8px !important;
border: 2px solid #404040 !important;
background: #2d2d2d !important;
color: #ffffff !important;
}
/* Text Styling */
.output-text {
background: #2d2d2d !important;
border-radius: 8px !important;
border: 2px solid #404040 !important;
padding: 20px !important;
font-size: 1.05em !important;
line-height: 1.7 !important;
font-family: 'Consolas', 'Courier New', monospace !important;
color: #ffffff !important;
}
/* Features Section */
.features-section {
background: #1a1a1a;
border: 2px solid #404040;
border-radius: 12px;
padding: 25px;
margin-top: 25px;
box-shadow: 0 4px 12px rgba(255, 255, 255, 0.05);
}
.feature-item {
margin-bottom: 15px;
color: #b0b0b0;
font-size: 1.05em;
line-height: 1.6;
padding: 10px;
background: #2d2d2d;
border-radius: 6px;
border-left: 4px solid #4a9eff;
}
.feature-item strong {
color: #ffffff;
}
/* Typography */
h3 {
font-family: 'Georgia', 'Times New Roman', serif !important;
color: #ffffff !important;
font-weight: 600 !important;
margin-bottom: 15px !important;
}
/* Component Overrides */
label {
color: #ffffff !important;
font-weight: 500 !important;
font-family: 'Georgia', 'Times New Roman', serif !important;
}
::placeholder {
color: #808080 !important;
opacity: 0.8;
}
*:focus {
outline: 2px solid #4a9eff !important;
outline-offset: 2px !important;
}
/* Gradio Specific Overrides */
.gradio-textbox textarea,
.gradio-dropdown,
.gradio-audio {
border: 2px solid #404040 !important;
border-radius: 8px !important;
background: #2d2d2d !important;
color: #ffffff !important;
font-family: inherit !important;
}
.gradio-textbox textarea:focus,
.gradio-dropdown:focus,
.gradio-audio:focus {
border-color: #4a9eff !important;
box-shadow: 0 0 0 3px rgba(74, 158, 255, 0.2) !important;
}
/* Scrollbar Styling */
::-webkit-scrollbar {
width: 8px;
}
::-webkit-scrollbar-track {
background: #1a1a1a;
}
::-webkit-scrollbar-thumb {
background: #404040;
border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover {
background: #4a9eff;
}
/* Selection Styling */
::selection {
background: #4a9eff;
color: #ffffff;
}
/* Responsive Design */
@media (max-width: 768px) {
.header-section h1 {
font-size: 2em !important;
}
.input-card, .output-card {
padding: 20px;
margin-bottom: 15px;
}
.primary-button, .secondary-button {
padding: 12px 20px !important;
font-size: 0.95em !important;
}
}
/* Animation for better UX */
@keyframes fadeIn {
from { opacity: 0; transform: translateY(10px); }
to { opacity: 1; transform: translateY(0); }
}
.input-card, .output-card, .features-section, .header-section {
animation: fadeIn 0.5s ease-out;
}
"""
# Create the main interface
with gr.Blocks(css=custom_css, title="CS-ASR | Code-Switching Speech Recognition") as demo:
# Header Section
with gr.Column(elem_classes="header-section"):
gr.HTML("""
<h1>Code-Switching ASR Platform</h1>
<p>Speech Recognition for Khmer-English Code-Switching</p>
""")
# Main Content
with gr.Row():
# Input Section
with gr.Column(scale=1, elem_classes="input-card"):
gr.HTML("<h3 style='color: #ffffff; margin-bottom: 20px; font-weight: 600;'>Audio Input</h3>")
audio_input = gr.Audio(
sources=["microphone", "upload"],
type="filepath",
label="Record or Upload Audio",
elem_classes="audio-component"
)
model_selector = gr.Dropdown(
choices=[
"Whisper Model",
"Wav2Vec2 Model"
],
value="Whisper Model",
label="Select AI Model",
elem_classes="dropdown-component",
info="Choose the model that best fits your needs"
)
# Action Buttons
with gr.Row():
transcribe_button = gr.Button(
"Start Transcription",
variant="primary",
elem_classes="primary-button",
scale=2
)
clear_button = gr.Button(
"Clear All",
elem_classes="secondary-button",
scale=1
)
# Output Section
with gr.Column(scale=1, elem_classes="output-card"):
gr.HTML("<h3 style='color: #ffffff; margin-bottom: 20px; font-weight: 600;'>Transcription Result</h3>")
output_text = gr.Textbox(
label="Your Transcription Will Appear Here",
placeholder="Upload an audio file and click 'Start Transcription' to begin processing.",
lines=12,
elem_classes="output-text",
interactive=False
)
# Event Handlers
transcribe_button.click(
fn=transcribe,
inputs=[audio_input, model_selector],
outputs=output_text,
show_progress=True
)
clear_button.click(
fn=clear_all,
outputs=[audio_input, model_selector, output_text]
)
# Auto-transcribe when audio is uploaded (optional)
audio_input.change(
fn=lambda audio, model: transcribe(audio, model) if audio is not None else "",
inputs=[audio_input, model_selector],
outputs=output_text,
show_progress=True
)
# Launch with custom configuration
if __name__ == "__main__":
demo.launch()
|