jfforero commited on
Commit
81019be
·
verified ·
1 Parent(s): 54b66d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -52
app.py CHANGED
@@ -1,14 +1,12 @@
1
  import gradio as gr
2
  import numpy as np
3
  import librosa
4
- import time
5
  import requests
6
  from io import BytesIO
7
  from PIL import Image
8
  import os
9
  from tensorflow.keras.models import load_model
10
- import torch
11
- from transformers import Speech2TextProcessor, Speech2TextForConditionalGeneration
12
 
13
  # Load the emotion prediction model
14
  def load_emotion_model(model_path):
@@ -19,21 +17,11 @@ def load_emotion_model(model_path):
19
  print("Error loading emotion prediction model:", e)
20
  return None
21
 
22
-
23
- from faster_whisper import WhisperModel
24
-
25
-
26
  model_size = "small"
27
-
28
- # Run on GPU with FP16
29
  model = WhisperModel(model_size, device="cpu", compute_type="int8")
30
 
31
- def transcribe(audio):
32
- segments, _ = model.transcribe(audio, beam_size=5)
33
- return "".join([segment.text for segment in segments])
34
-
35
-
36
-
37
  model_path = 'mymodel_SER_LSTM_RAVDESS.h5'
38
  emotion_model = load_emotion_model(model_path)
39
 
@@ -69,46 +57,41 @@ api_key = os.getenv("DeepAI_api_key")
69
 
70
  # Predict emotion from audio
71
  def get_predictions(audio_input):
72
- audio_file_path = audio_input.name
73
- emotion_prediction = predict_emotion_from_audio(audio_file_path)
74
- # Generate image here or call a separate function
75
- image = generate_image(api_key, emotion_prediction)
76
- return emotion_prediction, image
77
-
78
-
79
-
80
 
81
  # Define a function to generate an image using DeepAI Text to Image API
82
  def generate_image(api_key, text):
83
- url = "https://api.deepai.org/api/text2img"
84
- headers = {'api-key': api_key}
85
- response = requests.post(
86
- url,
87
- data={
88
- 'text': text,
89
- },
90
- headers=headers
91
- )
92
- response_data = response.json()
93
- if 'output_url' in response_data:
94
- image_url = response_data['output_url']
95
- image_response = requests.get(image_url)
96
- image = Image.open(BytesIO(image_response.content))
97
- return image
98
- else:
 
 
99
  return None
100
 
101
  # Create the Gradio interface
102
- with gr.Blocks() as interface:
103
- gr.Markdown("Emotional Machines test: Load or Record an audio file to speech emotion analysis")
104
- with gr.Tabs():
105
- with gr.Tab("Acoustic and Semantic Predictions"):
106
- with gr.Row():
107
- input_audio = gr.Audio(label="Input Audio", type="filepath")
108
- submit_button = gr.Button("Submit")
109
- output_label = [gr.Label("Prediction"), gr.Image(type='pil')] # Use a single Label instead of a list
110
-
111
- # Set the function to be called when the button is clicked
112
- submit_button.click(get_predictions, inputs=input_audio, outputs=output_label)
113
-
114
- interface.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
  import librosa
 
4
  import requests
5
  from io import BytesIO
6
  from PIL import Image
7
  import os
8
  from tensorflow.keras.models import load_model
9
+ from faster_whisper import WhisperModel
 
10
 
11
  # Load the emotion prediction model
12
  def load_emotion_model(model_path):
 
17
  print("Error loading emotion prediction model:", e)
18
  return None
19
 
 
 
 
 
20
  model_size = "small"
21
+ # Run on CPU with INT8 compute
 
22
  model = WhisperModel(model_size, device="cpu", compute_type="int8")
23
 
24
+ # Load emotion prediction model
 
 
 
 
 
25
  model_path = 'mymodel_SER_LSTM_RAVDESS.h5'
26
  emotion_model = load_emotion_model(model_path)
27
 
 
57
 
58
  # Predict emotion from audio
59
  def get_predictions(audio_input):
60
+ try:
61
+ audio_data = audio_input.read() # Read the audio data
62
+ emotion_prediction = predict_emotion_from_audio(audio_data)
63
+ image = generate_image(api_key, emotion_prediction)
64
+ return emotion_prediction, image
65
+ except Exception as e:
66
+ print("Error processing audio:", e)
67
+ return None, None
68
 
69
  # Define a function to generate an image using DeepAI Text to Image API
70
  def generate_image(api_key, text):
71
+ try:
72
+ url = "https://api.deepai.org/api/text2img"
73
+ headers = {'api-key': api_key}
74
+ response = requests.post(
75
+ url,
76
+ data={'text': text},
77
+ headers=headers
78
+ )
79
+ response_data = response.json()
80
+ if 'output_url' in response_data:
81
+ image_url = response_data['output_url']
82
+ image_response = requests.get(image_url)
83
+ image = Image.open(BytesIO(image_response.content))
84
+ return image
85
+ else:
86
+ return None
87
+ except Exception as e:
88
+ print("Error generating image:", e)
89
  return None
90
 
91
  # Create the Gradio interface
92
+ with gr.Interface(get_predictions,
93
+ inputs=gr.inputs.Audio(label="Input Audio", type="file"),
94
+ outputs=[gr.outputs.Text(label="Prediction"), gr.outputs.Image(label="Generated Image")],
95
+ title="Emotional Machines Test",
96
+ description="Load or Record an audio file to perform emotion analysis") as iface:
97
+ iface.launch()