Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,30 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
label_map = {
|
| 21 |
-
0: "Australia",
|
| 22 |
-
1: "Canada",
|
| 23 |
-
2: "England",
|
| 24 |
-
3: "India",
|
| 25 |
-
4: "Ireland",
|
| 26 |
-
5: "New Zealand",
|
| 27 |
-
6: "Scotland",
|
| 28 |
-
7: "South Africa",
|
| 29 |
-
8: "US",
|
| 30 |
-
16: "British Slang / Unknown Region"
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
def transcribe_audio(audio_path):
|
| 35 |
-
result = whisper_pipeline(audio_path)
|
| 36 |
-
return result['text']
|
| 37 |
-
|
| 38 |
-
def predict_accent(audio_path):
|
| 39 |
-
audio, sr = librosa.load(audio_path, sr=16000)
|
| 40 |
-
inputs = feature_extractor(audio, sampling_rate=16000, return_tensors="pt")
|
| 41 |
-
with torch.no_grad():
|
| 42 |
-
logits = accent_model(**inputs).logits
|
| 43 |
-
predicted_id = torch.argmax(logits, dim=-1).item()
|
| 44 |
-
return label_map.get(predicted_id, f"Unknown (ID: {predicted_id})")
|
| 45 |
-
|
| 46 |
-
def agent_run(audio_path):
|
| 47 |
-
start_time = time.time()
|
| 48 |
-
accent = predict_accent(audio_path)
|
| 49 |
-
transcription = transcribe_audio(audio_path)
|
| 50 |
-
elapsed = round(time.time() - start_time, 2)
|
| 51 |
-
return audio_path, accent, transcription, f"{elapsed} sec"
|
| 52 |
-
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
-
gr.Markdown("##
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
time_output = gr.Textbox(label="Processing Time")
|
| 65 |
-
|
| 66 |
-
run_button.click(
|
| 67 |
-
fn=agent_run,
|
| 68 |
-
inputs=[audio_input],
|
| 69 |
-
outputs=[audio_output, accent_output, transcription_output, time_output]
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
if __name__ == "__main__":
|
| 73 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# تحميل النموذج الجاهز من Hugging Face
|
| 5 |
+
accent_classifier = pipeline("audio-classification", model="Yactayo/AccentClassifier")
|
| 6 |
+
|
| 7 |
+
def classify_accent(audio_path):
|
| 8 |
+
try:
|
| 9 |
+
results = accent_classifier(audio_path)
|
| 10 |
+
top_result = results[0]
|
| 11 |
+
label = top_result["label"]
|
| 12 |
+
score = round(top_result["score"] * 100, 2)
|
| 13 |
+
return f"{label} ({score}%)"
|
| 14 |
+
except Exception as e:
|
| 15 |
+
return f"Error: {str(e)}"
|
| 16 |
+
|
| 17 |
+
# واجهة Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
with gr.Blocks() as demo:
|
| 19 |
+
gr.Markdown("## 🎙️ تصنيف اللهجات باستخدام AI")
|
| 20 |
+
|
| 21 |
+
with gr.Row():
|
| 22 |
+
audio_input = gr.Audio(type="filepath", label="🔊 سجل أو حمّل ملف صوتي", sources=["upload", "microphone"])
|
| 23 |
+
output_text = gr.Textbox(label="اللهجة المتوقعة")
|
| 24 |
+
|
| 25 |
+
analyze_btn = gr.Button("🔍 تحليل اللهجة")
|
| 26 |
+
analyze_btn.click(fn=classify_accent, inputs=audio_input, outputs=output_text)
|
| 27 |
+
|
| 28 |
+
# تشغيل التطبيق
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if __name__ == "__main__":
|
| 30 |
demo.launch()
|