tusker123 commited on
Commit
4d98d92
·
verified ·
1 Parent(s): e0dc75b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ error_message = str(e)
64
+ if "numpy ndarray" in error_message.lower():
65
+ return "<p style='color: red; font-weight: bold;'>⚠️ Error: Invalid input.<br> Please end the recording then press submit.</p>"
66
+ else:
67
+ return f"<p style='color: red; font-weight: bold;'>⚠️ Unexpected Error: {error_message}<br>Please try again with a different audio file.</p>"
68
+
69
+ # Create and launch the Gradio app
70
+ gr.Interface(
71
+ fn=classify_accent,
72
+ inputs=gr.Audio(type="filepath", label="🎙 Record or Upload English Audio"),
73
+ outputs=gr.HTML(), # Use HTML to render styled output
74
+ title="🌍 English Accent Classifier",
75
+ description=(
76
+ "Upload or record an English audio sample to detect the speaker's accent.\n\n"
77
+ "**Supported accents:** American, British, Indian, African, Australian.\n"
78
+ "Audio Classification Model:\n"
79
+ "[dima806/english_accents_classification](https://huggingface.co/dima806/english_accents_classification)\n"
80
+ "Dataset: https://www.kaggle.com/code/dima806/common-voice-accent-classification\n"
81
+ ),
82
+ flagging_mode="never",
83
+ theme="default"
84
+ ).launch(share=True)