Update app.py
Browse files
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
|
| 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 |
-
|
| 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 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 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 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
'text': text,
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
| 99 |
return None
|
| 100 |
|
| 101 |
# Create the Gradio interface
|
| 102 |
-
with gr.
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|