Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pyvista as pv
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import librosa
|
| 5 |
import requests
|
|
@@ -8,11 +9,20 @@ from PIL import Image
|
|
| 8 |
import os
|
| 9 |
from tensorflow.keras.models import load_model
|
| 10 |
from faster_whisper import WhisperModel
|
|
|
|
| 11 |
from textblob import TextBlob
|
| 12 |
import torch
|
| 13 |
import scipy.io.wavfile
|
| 14 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
| 15 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# Load the emotion prediction model
|
| 18 |
def load_emotion_model(model_path):
|
|
@@ -210,49 +220,95 @@ def generate_image(sentiment_prediction, transcribed_text):
|
|
| 210 |
# Return a fallback image
|
| 211 |
return Image.new('RGB', (1024, 512), color='white')
|
| 212 |
|
| 213 |
-
# Function to create a
|
|
|
|
| 214 |
def create_texture_and_sphere_preview(image):
|
| 215 |
try:
|
| 216 |
-
#
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
texture_file = tmp.name
|
| 220 |
|
| 221 |
-
# Create a
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
-
#
|
| 225 |
-
|
| 226 |
|
| 227 |
-
#
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
plotter.camera.elevation = 30
|
| 233 |
-
plotter.background_color = 'white'
|
| 234 |
|
| 235 |
-
#
|
| 236 |
-
|
| 237 |
-
|
|
|
|
| 238 |
|
| 239 |
-
#
|
| 240 |
-
|
|
|
|
| 241 |
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
-
#
|
| 250 |
-
|
| 251 |
-
draw = ImageDraw.Draw(composite)
|
| 252 |
-
draw.text((width + 20, height//2 - 20), "3D Sphere Preview", fill='black')
|
| 253 |
-
draw.text((width + 20, height//2), "(Texture mapped sphere)", fill='gray')
|
| 254 |
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
# Function to get predictions
|
| 258 |
def get_predictions(audio_input):
|
|
@@ -271,10 +327,10 @@ def get_predictions(audio_input):
|
|
| 271 |
# Generate music using ACOUSTIC EMOTION prediction with specific prompt
|
| 272 |
music_path = generate_music(transcribed_text, emotion_prediction)
|
| 273 |
|
| 274 |
-
# Create visualization with texture
|
| 275 |
-
|
| 276 |
|
| 277 |
-
return emotion_prediction, transcribed_text, f"Sentiment: {sentiment} (Polarity: {polarity:.2f})",
|
| 278 |
|
| 279 |
# Create the Gradio interface
|
| 280 |
interface = gr.Interface(
|
|
@@ -284,11 +340,12 @@ interface = gr.Interface(
|
|
| 284 |
gr.Label(label="Acoustic Emotion Prediction (for music)"),
|
| 285 |
gr.Label(label="Transcribed Text"),
|
| 286 |
gr.Label(label="Sentiment Analysis (for image)"),
|
| 287 |
-
gr.Image(type='pil', label="
|
| 288 |
-
gr.Audio(label="Generated Music", type="filepath")
|
|
|
|
| 289 |
],
|
| 290 |
title="Affective Virtual Environments",
|
| 291 |
-
description="Create an AVE using your voice. Get emotion prediction (for music), transcription, sentiment analysis (for image), a
|
| 292 |
)
|
| 293 |
|
| 294 |
interface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pyvista as pv
|
| 3 |
+
from pyvista import examples
|
| 4 |
import numpy as np
|
| 5 |
import librosa
|
| 6 |
import requests
|
|
|
|
| 9 |
import os
|
| 10 |
from tensorflow.keras.models import load_model
|
| 11 |
from faster_whisper import WhisperModel
|
| 12 |
+
import random
|
| 13 |
from textblob import TextBlob
|
| 14 |
import torch
|
| 15 |
import scipy.io.wavfile
|
| 16 |
from transformers import AutoProcessor, MusicgenForConditionalGeneration
|
| 17 |
import tempfile
|
| 18 |
+
import base64
|
| 19 |
+
import plotly.graph_objects as go
|
| 20 |
+
from plotly.subplots import make_subplots
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
|
| 26 |
|
| 27 |
# Load the emotion prediction model
|
| 28 |
def load_emotion_model(model_path):
|
|
|
|
| 220 |
# Return a fallback image
|
| 221 |
return Image.new('RGB', (1024, 512), color='white')
|
| 222 |
|
| 223 |
+
# Function to create a visualization with both the equirectangular image and a 3D sphere
|
| 224 |
+
# Function to create a visualization with both the equirectangular image and a 3D sphere
|
| 225 |
def create_texture_and_sphere_preview(image):
|
| 226 |
try:
|
| 227 |
+
# Convert PIL image to numpy array
|
| 228 |
+
img_array = np.array(image)
|
| 229 |
+
height, width = img_array.shape[0], img_array.shape[1]
|
|
|
|
| 230 |
|
| 231 |
+
# Create a subplot with the equirectangular image and a 3D sphere
|
| 232 |
+
fig = make_subplots(
|
| 233 |
+
rows=1, cols=2,
|
| 234 |
+
subplot_titles=("Equirectangular Texture", "3D Sphere with Texture Mapping"),
|
| 235 |
+
specs=[[{"type": "image"}, {"type": "scatter3d"}]],
|
| 236 |
+
horizontal_spacing=0.1
|
| 237 |
+
)
|
| 238 |
|
| 239 |
+
# Add the equirectangular image to the first subplot
|
| 240 |
+
fig.add_trace(go.Image(z=img_array), row=1, col=1)
|
| 241 |
|
| 242 |
+
# Create sphere coordinates
|
| 243 |
+
u_res, v_res = 50, 25
|
| 244 |
+
u = np.linspace(0, 2 * np.pi, u_res)
|
| 245 |
+
v = np.linspace(0, np.pi, v_res)
|
| 246 |
+
u, v = np.meshgrid(u, v)
|
|
|
|
|
|
|
| 247 |
|
| 248 |
+
# Convert spherical coordinates to Cartesian coordinates
|
| 249 |
+
x = np.sin(v) * np.cos(u)
|
| 250 |
+
y = np.sin(v) * np.sin(u)
|
| 251 |
+
z = np.cos(v)
|
| 252 |
|
| 253 |
+
# Sample colors from the equirectangular image based on UV coordinates
|
| 254 |
+
# This approximates texture mapping by sampling the image at the correct UV coordinates
|
| 255 |
+
texture_colors = np.zeros((v_res, u_res, 3), dtype=np.uint8)
|
| 256 |
|
| 257 |
+
for i in range(v_res):
|
| 258 |
+
for j in range(u_res):
|
| 259 |
+
# Convert spherical coordinates to image coordinates
|
| 260 |
+
img_x = int((u[i, j] / (2 * np.pi)) * (width - 1))
|
| 261 |
+
img_y = int((v[i, j] / np.pi) * (height - 1))
|
| 262 |
+
|
| 263 |
+
# Ensure coordinates are within bounds
|
| 264 |
+
img_x = max(0, min(img_x, width - 1))
|
| 265 |
+
img_y = max(0, min(img_y, height - 1))
|
| 266 |
+
|
| 267 |
+
# Get color from image
|
| 268 |
+
if len(img_array.shape) == 3: # RGB image
|
| 269 |
+
texture_colors[i, j] = img_array[img_y, img_x, :3]
|
| 270 |
+
else: # Grayscale image
|
| 271 |
+
texture_colors[i, j] = [img_array[img_y, img_x]] * 3
|
| 272 |
|
| 273 |
+
# Convert colors to Plotly format (normalized to [0,1])
|
| 274 |
+
surface_colors = texture_colors.astype(float) / 255.0
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
+
# Create surface with sampled colors
|
| 277 |
+
fig.add_trace(go.Surface(
|
| 278 |
+
x=x, y=y, z=z,
|
| 279 |
+
surfacecolor=surface_colors,
|
| 280 |
+
showscale=False,
|
| 281 |
+
opacity=1.0,
|
| 282 |
+
lighting=dict(ambient=0.8, diffuse=0.8, specular=0.1, roughness=0.5),
|
| 283 |
+
lightposition=dict(x=100, y=100, z=100)
|
| 284 |
+
), row=1, col=2)
|
| 285 |
+
|
| 286 |
+
# Update layout
|
| 287 |
+
fig.update_layout(
|
| 288 |
+
height=500,
|
| 289 |
+
title_text="Equirectangular Texture and 3D Sphere Preview",
|
| 290 |
+
showlegend=False,
|
| 291 |
+
scene2=dict(
|
| 292 |
+
xaxis=dict(visible=False, showticklabels=False),
|
| 293 |
+
yaxis=dict(visible=False, showticklabels=False),
|
| 294 |
+
zaxis=dict(visible=False, showticklabels=False),
|
| 295 |
+
aspectmode='data',
|
| 296 |
+
camera=dict(
|
| 297 |
+
eye=dict(x=1.8, y=1.8, z=1.8)
|
| 298 |
+
),
|
| 299 |
+
bgcolor='rgba(0,0,0,0)'
|
| 300 |
+
)
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
+
# Update axes for the image subplot
|
| 304 |
+
fig.update_xaxes(visible=False, row=1, col=1)
|
| 305 |
+
fig.update_yaxes(visible=False, row=1, col=1)
|
| 306 |
+
|
| 307 |
+
return fig
|
| 308 |
+
|
| 309 |
+
except Exception as e:
|
| 310 |
+
print("Error creating texture and sphere preview:", e)
|
| 311 |
+
return go.Figure()
|
| 312 |
|
| 313 |
# Function to get predictions
|
| 314 |
def get_predictions(audio_input):
|
|
|
|
| 327 |
# Generate music using ACOUSTIC EMOTION prediction with specific prompt
|
| 328 |
music_path = generate_music(transcribed_text, emotion_prediction)
|
| 329 |
|
| 330 |
+
# Create visualization with both texture and sphere
|
| 331 |
+
preview_fig = create_texture_and_sphere_preview(image)
|
| 332 |
|
| 333 |
+
return emotion_prediction, transcribed_text, f"Sentiment: {sentiment} (Polarity: {polarity:.2f})", image, music_path, preview_fig
|
| 334 |
|
| 335 |
# Create the Gradio interface
|
| 336 |
interface = gr.Interface(
|
|
|
|
| 340 |
gr.Label(label="Acoustic Emotion Prediction (for music)"),
|
| 341 |
gr.Label(label="Transcribed Text"),
|
| 342 |
gr.Label(label="Sentiment Analysis (for image)"),
|
| 343 |
+
gr.Image(type='pil', label="Generated Equirectangular Image"),
|
| 344 |
+
gr.Audio(label="Generated Music", type="filepath"),
|
| 345 |
+
gr.Plot(label="Texture and Sphere Preview")
|
| 346 |
],
|
| 347 |
title="Affective Virtual Environments",
|
| 348 |
+
description="Create an AVE using your voice. Get emotion prediction (for music), transcription, sentiment analysis (for image), a generated equirectangular image, music, and a preview of how it would look as a texture on a sphere."
|
| 349 |
)
|
| 350 |
|
| 351 |
interface.launch()
|