import gradio as gr
from transformers import pipeline
# Load the audio classification model
pipe = pipeline("audio-classification", model="dima806/english_accents_classification")
# Define the inference function with styled, color-coded output
def classify_accent(audio):
try:
result = pipe(audio)
if not result:
return "
⚠️ No prediction returned. Please try a different audio file.
"
# Start HTML table with styling
table = """
Accent |
Confidence |
"""
for i, r in enumerate(result):
label = r['label'].capitalize()
score = f"{r['score'] * 100:.2f}%"
if i == 0:
# Highlight top accent with green background and bold text
row = f"""
{label} |
{score} |
"""
else:
row = f"""
{label} |
{score} |
"""
table += row
table += "
"
top_result = result[0]
return f"""
🎤 Predicted Accent: {top_result['label'].capitalize()}
{table}
"""
except Exception as e:
error_message = str(e)
if "numpy ndarray" in error_message.lower():
return "⚠️ Error: Invalid input.
Please end the recording then press submit.
"
else:
return f"⚠️ Unexpected Error: {error_message}
Please try again with a different audio file.
"
# Create and launch the Gradio app
gr.Interface(
fn=classify_accent,
inputs=gr.Audio(type="filepath", label="🎙 Record or Upload English Audio"),
outputs=gr.HTML(), # Use HTML to render styled output
title="🌍 English Accent Classifier",
description=(
"Upload or record an English audio sample to detect the speaker's accent.\n\n"
"**Supported accents:** American, British, Indian, African, Australian.\n"
"Audio Classification Model:\n"
"[dima806/english_accents_classification](https://huggingface.co/dima806/english_accents_classification)\n"
"Dataset: https://www.kaggle.com/code/dima806/common-voice-accent-classification\n"
),
flagging_mode="never",
theme="default"
).launch(share=True)