Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,77 +0,0 @@
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|