Hatman commited on
Commit
4a065d2
1 Parent(s): f24fccb
Files changed (1) hide show
  1. app.py +34 -9
app.py CHANGED
@@ -6,7 +6,7 @@ from transformers import Wav2Vec2FeatureExtractor, Wav2Vec2ForSequenceClassifica
6
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
- model_name = "Hemg/human-emotion-detection"
10
  feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name)
11
  model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
12
  print(device)
@@ -27,12 +27,37 @@ def inference(audio):
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
 
31
- iface = gr.Interface(fn=inference,
32
- inputs=gr.Audio(type="filepath"),
33
- outputs=[gr.Label(label="Predicted Sentiment")],
34
- title="Audio Sentiment Analysis",
35
- description="Upload an audio file or record one to analyze sentiment.")
36
-
37
-
38
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  device = "cuda" if torch.cuda.is_available() else "cpu"
8
 
9
+ model_name = "Hatman/audio-emotion-detection"
10
  feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name)
11
  model = Wav2Vec2ForSequenceClassification.from_pretrained(model_name)
12
  print(device)
 
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
+ @spaces.GPU
31
+ def inference_label(audio):
32
+ example = preprocess_audio(audio)
33
+ inputs = feature_extractor(example['speech'], sampling_rate=16000, return_tensors="pt", padding=True)
34
+ inputs = inputs # Move inputs to GPU
35
+ with torch.no_grad():
36
+ logits = model(**inputs).logits
37
+ predicted_ids = torch.argmax(logits, dim=-1)
38
+ return model.config.id2label[predicted_ids.item()]
39
 
40
+ with gr.Blocks() as demo:
41
+ gr.Markdown("# Audio Sentiment Analysis")
42
+
43
+
44
+
45
+ with gr.Tab("Label Only Inference"):
46
+ gr.Interface(
47
+ fn=inference_label,
48
+ inputs=gr.Audio(type="filepath"),
49
+ outputs=gr.Label(label="Predicted Sentiment"),
50
+ title="Audio Sentiment Analysis",
51
+ description="Upload an audio file or record one to get the predicted sentiment label."
52
+ )
53
+
54
+ with gr.Tab("Full Inference"):
55
+ gr.Interface(
56
+ fn=inference,
57
+ inputs=gr.Audio(type="filepath"),
58
+ outputs=[gr.Label(label="Predicted Sentiment"), gr.Textbox(label="Logits"), gr.Textbox(label="Predicted IDs")],
59
+ title="Audio Sentiment Analysis (Full)",
60
+ description="Upload an audio file or record one to analyze sentiment and get detailed results."
61
+ )
62
+
63
+ demo.launch(share=True)