Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import Wav2Vec2ForSequenceClassification, Wav2Vec2Processor
|
4 |
+
import librosa
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# تحميل النموذج والمعالج من Hugging Face
|
8 |
+
model_name = "facebook/wav2vec2-large-xlsr-53"
|
9 |
+
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name, num_labels=7)
|
10 |
+
processor = Wav2Vec2Processor.from_pretrained(model_name)
|
11 |
+
|
12 |
+
# دالة لمعالجة الصوت وتحويله إلى مشاعر
|
13 |
+
def recognize_emotion(audio):
|
14 |
+
# تحميل الصوت باستخدام librosa
|
15 |
+
audio_input, _ = librosa.load(audio, sr=16000)
|
16 |
+
|
17 |
+
# استخراج الميزات باستخدام Wav2Vec2 Processor
|
18 |
+
inputs = processor(audio_input, sampling_rate=16000, return_tensors="pt", padding=True)
|
19 |
+
|
20 |
+
# تمرير البيانات عبر النموذج
|
21 |
+
with torch.no_grad():
|
22 |
+
logits = model(**inputs).logits
|
23 |
+
|
24 |
+
# تحويل القيم إلى المشاعر
|
25 |
+
emotion_map = {
|
26 |
+
0: "Neutral",
|
27 |
+
1: "Happy",
|
28 |
+
2: "Angry",
|
29 |
+
3: "Sad",
|
30 |
+
4: "Surprised",
|
31 |
+
5: "Fearful",
|
32 |
+
6: "Disgusted"
|
33 |
+
}
|
34 |
+
|
35 |
+
# تصنيف الصوت
|
36 |
+
predicted_class = torch.argmax(logits, dim=-1).item()
|
37 |
+
emotion = emotion_map[predicted_class]
|
38 |
+
|
39 |
+
return emotion
|
40 |
+
|
41 |
+
# واجهة Gradio
|
42 |
+
iface = gr.Interface(
|
43 |
+
fn=recognize_emotion,
|
44 |
+
inputs=gr.inputs.Audio(source="microphone", type="filepath"),
|
45 |
+
outputs="text",
|
46 |
+
title="Speech Emotion Recognition",
|
47 |
+
description="Identify the emotion in the speech: Happy, Sad, Angry, Surprised, Neutral, Fearful, or Disgusted."
|
48 |
+
)
|
49 |
+
|
50 |
+
# تشغيل الواجهة
|
51 |
+
iface.launch()
|