Spaces:
Runtime error
Runtime error
req and app initial
Browse files- app.py +30 -7
- requirements.txt +5 -0
app.py
CHANGED
@@ -1,14 +1,37 @@
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
import torch
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
@spaces.GPU
|
9 |
-
def
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import spaces
|
3 |
import torch
|
4 |
+
import librosa
|
5 |
+
import numpy as np
|
6 |
+
from transformers import Wav2Vec2FeatureExtractor, Wav2Vec2ForSequenceClassification
|
7 |
|
8 |
+
|
9 |
+
|
10 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
11 |
+
|
12 |
+
model_name = "Hemg/human-emotion-detection"
|
13 |
+
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name).to(device)
|
14 |
+
model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name).to(device)
|
15 |
+
|
16 |
+
def preprocess_audio(example):
|
17 |
+
audio_array, sampling_rate = librosa.load(audio_file_path, sr=16000) # Load and resample to 16kHz
|
18 |
+
return {'speech': audio_array, 'sampling_rate': sampling_rate}
|
19 |
|
20 |
@spaces.GPU
|
21 |
+
def inference(audio):
|
22 |
+
example = preprocess_audio(audio_file_path)
|
23 |
+
inputs = feature_extractor(example['speech'], sampling_rate=16000, return_tensors="pt", padding=True)
|
24 |
+
inputs = inputs.to(device) # Move inputs to GPU
|
25 |
+
with torch.no_grad():
|
26 |
+
logits = model(**inputs).logits
|
27 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
28 |
+
return model.config.id2label[predicted_ids.item()], logits, predicted_ids # Move tensors back to CPU for further processing
|
29 |
+
|
30 |
+
iface = gr.Interface(fn=predict_sentiment,
|
31 |
+
inputs=gr.inputs.Audio(source="microphone", type="filepath"),
|
32 |
+
outputs="text",
|
33 |
+
title="Audio Sentiment Analysis",
|
34 |
+
description="Upload an audio file or record one to analyze sentiment.")
|
35 |
+
|
36 |
|
37 |
+
iface.launch()
|
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
librosa
|
5 |
+
numpy
|