tusker123 commited on
Commit
fac0ed8
·
verified ·
1 Parent(s): c70e657

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -80
app.py CHANGED
@@ -1,80 +1,77 @@
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)
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import os
4
+
5
+ # Load the model
6
+ pipe = pipeline("audio-classification", model="dima806/english_accents_classification")
7
+
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
+ table = """
15
+ <table style='width: 100%; border-collapse: collapse; font-family: Arial, sans-serif; margin-top: 1em;'>
16
+ <thead>
17
+ <tr style='border-bottom: 2px solid #4CAF50; background-color: #f2f2f2;'>
18
+ <th style='text-align:left; padding: 8px; font-size: 1.1em; color: #333;'>Accent</th>
19
+ <th style='text-align:left; padding: 8px; font-size: 1.1em; color: #333;'>Confidence</th>
20
+ </tr>
21
+ </thead>
22
+ <tbody>
23
+ """
24
+
25
+ for i, r in enumerate(result):
26
+ label = r['label'].capitalize()
27
+ score = f"{r['score'] * 100:.2f}%"
28
+ if i == 0:
29
+ row = f"""
30
+ <tr style='background-color:#d4edda; font-weight: bold; color: #155724;'>
31
+ <td style='padding: 8px; border-bottom: 1px solid #c3e6cb;'>{label}</td>
32
+ <td style='padding: 8px; border-bottom: 1px solid #c3e6cb;'>{score}</td>
33
+ </tr>
34
+ """
35
+ else:
36
+ row = f"""
37
+ <tr style='color: #333;'>
38
+ <td style='padding: 8px; border-bottom: 1px solid #ddd;'>{label}</td>
39
+ <td style='padding: 8px; border-bottom: 1px solid #ddd;'>{score}</td>
40
+ </tr>
41
+ """
42
+ table += row
43
+
44
+ table += "</tbody></table>"
45
+
46
+ top_result = result[0]
47
+ return f"""
48
+ <h3 style='color: #2E7D32; font-family: Arial, sans-serif;'>
49
+ 🎤 Predicted Accent: <span style='font-weight:bold'>{top_result['label'].capitalize()}</span>
50
+ </h3>
51
+ {table}
52
+ """
53
+
54
+ except Exception as e:
55
+ 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>"
56
+
57
+ # Determine if it's safe to enable the submit button
58
+ def enable_submit(audio_path):
59
+ if audio_path is None:
60
+ return gr.update(interactive=False)
61
+ # If file name contains "microphone", assume it’s from mic input and delay submission
62
+ if "microphone" in os.path.basename(audio_path).lower():
63
+ return gr.update(interactive=True) # Enable only when recording finishes
64
+ return gr.update(interactive=True) # File upload: enable immediately
65
+
66
+ # Build UI
67
+ with gr.Blocks(theme="default") as demo:
68
+ gr.Markdown("## 🌍 English Accent Classifier\nRecord or upload English audio to detect accent.")
69
+ audio_input = gr.Audio(type="filepath", label="🎙 Record or Upload English Audio")
70
+ submit_button = gr.Button("Submit", interactive=False)
71
+ result_output = gr.HTML()
72
+
73
+ # Reactively enable submit only after audio is uploaded or recording stops
74
+ audio_input.change(enable_submit, inputs=audio_input, outputs=submit_button)
75
+ submit_button.click(classify_accent, inputs=audio_input, outputs=result_output)
76
+
77
+ demo.launch(share=True)