Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
# Load the audio classification model
|
5 |
+
pipe = pipeline("audio-classification", model="dima806/english_accents_classification")
|
6 |
+
|
7 |
+
# Define the inference function with styled, color-coded output
|
8 |
+
def classify_accent(audio):
|
9 |
+
try:
|
10 |
+
result = pipe(audio)
|
11 |
+
if not result:
|
12 |
+
return "<p style='color: red; font-weight: bold;'>⚠️ No prediction returned. Please try a different audio file.</p>"
|
13 |
+
|
14 |
+
# Start HTML table with styling
|
15 |
+
table = """
|
16 |
+
<table style="
|
17 |
+
width: 100%;
|
18 |
+
border-collapse: collapse;
|
19 |
+
font-family: Arial, sans-serif;
|
20 |
+
margin-top: 1em;
|
21 |
+
">
|
22 |
+
<thead>
|
23 |
+
<tr style="border-bottom: 2px solid #4CAF50; background-color: #f2f2f2;">
|
24 |
+
<th style="text-align:left; padding: 8px; font-size: 1.1em; color: #333;">Accent</th>
|
25 |
+
<th style="text-align:left; padding: 8px; font-size: 1.1em; color: #333;">Confidence</th>
|
26 |
+
</tr>
|
27 |
+
</thead>
|
28 |
+
<tbody>
|
29 |
+
"""
|
30 |
+
|
31 |
+
for i, r in enumerate(result):
|
32 |
+
label = r['label'].capitalize()
|
33 |
+
score = f"{r['score'] * 100:.2f}%"
|
34 |
+
|
35 |
+
if i == 0:
|
36 |
+
# Highlight top accent with green background and bold text
|
37 |
+
row = f"""
|
38 |
+
<tr style="background-color:#d4edda; font-weight: bold; color: #155724;">
|
39 |
+
<td style="padding: 8px; border-bottom: 1px solid #c3e6cb;">{label}</td>
|
40 |
+
<td style="padding: 8px; border-bottom: 1px solid #c3e6cb;">{score}</td>
|
41 |
+
</tr>
|
42 |
+
"""
|
43 |
+
else:
|
44 |
+
row = f"""
|
45 |
+
<tr style="color: #333;">
|
46 |
+
<td style="padding: 8px; border-bottom: 1px solid #ddd;">{label}</td>
|
47 |
+
<td style="padding: 8px; border-bottom: 1px solid #ddd;">{score}</td>
|
48 |
+
</tr>
|
49 |
+
"""
|
50 |
+
table += row
|
51 |
+
|
52 |
+
table += "</tbody></table>"
|
53 |
+
|
54 |
+
top_result = result[0]
|
55 |
+
return f"""
|
56 |
+
<h3 style='color: #2E7D32; font-family: Arial, sans-serif;'>
|
57 |
+
🎤 Predicted Accent: <span style='font-weight:bold'>{top_result['label'].capitalize()}</span>
|
58 |
+
</h3>
|
59 |
+
{table}
|
60 |
+
"""
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
return f"<p style='color: red; font-weight: bold;'>⚠️ Error: {str(e)}<br>Please upload a valid English audio file (e.g., .wav, .mp3).</p>"
|
64 |
+
|
65 |
+
# Create and launch the Gradio app
|
66 |
+
gr.Interface(
|
67 |
+
fn=classify_accent,
|
68 |
+
inputs=gr.Audio(type="filepath", label="🎙 Record or Upload English Audio"),
|
69 |
+
outputs=gr.HTML(), # Important: Use HTML output here to render the table properly
|
70 |
+
title="🌍 English Accent Classifier",
|
71 |
+
description=(
|
72 |
+
"Upload or record an English audio sample to detect the speaker's accent.\n\n"
|
73 |
+
"**Supported accents:** American, British, Indian, African, Australian.\n"
|
74 |
+
"Audio Classification Model:\n"
|
75 |
+
"[dima806/english_accents_classification](https://huggingface.co/dima806/english_accents_classification)\n"
|
76 |
+
"Dataset: https://www.kaggle.com/code/dima806/common-voice-accent-classification\n"
|
77 |
+
),
|
78 |
+
flagging_mode="never",
|
79 |
+
theme="default"
|
80 |
+
).launch(share=True)
|