wesam0099 commited on
Commit
e16eb92
·
verified ·
1 Parent(s): 6fd77bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -69
app.py CHANGED
@@ -1,73 +1,30 @@
1
- # app.py
2
-
3
  import gradio as gr
4
- import torch
5
- import librosa
6
- import time
7
- import os
8
- import tempfile
9
- from pydub import AudioSegment
10
- from transformers import pipeline, AutoFeatureExtractor, AutoModelForAudioClassification
11
-
12
- # إعداد Whisper
13
- whisper_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-base")
14
-
15
- # تحميل نموذج تصنيف اللهجات
16
- MODEL_ID = "ylacombe/accent-classifier"
17
- feature_extractor = AutoFeatureExtractor.from_pretrained(MODEL_ID)
18
- accent_model = AutoModelForAudioClassification.from_pretrained(MODEL_ID)
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("## 🧠 Accent Detection Agent")
55
- gr.Markdown("Record or upload audio and let the AI detect the accent and transcribe your speech.")
56
-
57
- # source="microphone" تمت إزالته
58
- audio_input = gr.Audio(type="filepath", label="🎙️ Upload or Record Audio")
59
- run_button = gr.Button("Analyze")
60
-
61
- audio_output = gr.Audio(label="Audio")
62
- accent_output = gr.Textbox(label="Detected Accent")
63
- transcription_output = gr.Textbox(label="Transcription")
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()