invincible-jha
commited on
Commit
•
fea9f64
1
Parent(s):
8c3c4a1
Upload 3 files
Browse files- utils/gpu-optimizer.py +30 -0
- utils/model-cache.py +18 -0
- utils/visualizer.py +74 -0
utils/gpu-optimizer.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gc
|
3 |
+
|
4 |
+
class GPUOptimizer:
|
5 |
+
def __init__(self):
|
6 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
7 |
+
|
8 |
+
def optimize(self):
|
9 |
+
if torch.cuda.is_available():
|
10 |
+
# Clear cache
|
11 |
+
torch.cuda.empty_cache()
|
12 |
+
gc.collect()
|
13 |
+
|
14 |
+
# Set memory fraction
|
15 |
+
torch.cuda.set_per_process_memory_fraction(0.9)
|
16 |
+
|
17 |
+
# Enable TF32 for better performance
|
18 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
19 |
+
torch.backends.cudnn.allow_tf32 = True
|
20 |
+
|
21 |
+
# Enable autocast for mixed precision
|
22 |
+
torch.cuda.amp.autocast(enabled=True)
|
23 |
+
|
24 |
+
def get_memory_usage(self):
|
25 |
+
if torch.cuda.is_available():
|
26 |
+
return {
|
27 |
+
'allocated': torch.cuda.memory_allocated() / 1024**2, # MB
|
28 |
+
'reserved': torch.cuda.memory_reserved() / 1024**2 # MB
|
29 |
+
}
|
30 |
+
return {'allocated': 0, 'reserved': 0}
|
utils/model-cache.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from functools import lru_cache
|
2 |
+
import hashlib
|
3 |
+
import json
|
4 |
+
|
5 |
+
class ModelCache:
|
6 |
+
def __init__(self, cache_size=128):
|
7 |
+
self.cache_size = cache_size
|
8 |
+
|
9 |
+
@lru_cache(maxsize=128)
|
10 |
+
def cache_result(self, input_key, result):
|
11 |
+
return result
|
12 |
+
|
13 |
+
def get_cache_key(self, audio_data):
|
14 |
+
# Create hash of audio data for cache key
|
15 |
+
return hashlib.md5(audio_data).hexdigest()
|
16 |
+
|
17 |
+
def clear_cache(self):
|
18 |
+
self.cache_result.cache_clear()
|
utils/visualizer.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import plotly.graph_objects as go
|
2 |
+
from typing import Dict
|
3 |
+
|
4 |
+
def create_emotion_plot(emotions: Dict[str, float]) -> str:
|
5 |
+
"""Create emotion distribution plot"""
|
6 |
+
fig = go.Figure()
|
7 |
+
|
8 |
+
# Add bar plot
|
9 |
+
fig.add_trace(go.Bar(
|
10 |
+
x=list(emotions.keys()),
|
11 |
+
y=list(emotions.values()),
|
12 |
+
marker_color='rgb(55, 83, 109)'
|
13 |
+
))
|
14 |
+
|
15 |
+
# Update layout
|
16 |
+
fig.update_layout(
|
17 |
+
title='Emotion Distribution',
|
18 |
+
xaxis_title='Emotion',
|
19 |
+
yaxis_title='Score',
|
20 |
+
yaxis_range=[0, 1],
|
21 |
+
template='plotly_white',
|
22 |
+
height=400
|
23 |
+
)
|
24 |
+
|
25 |
+
return fig.to_html(include_plotlyjs=True)
|
26 |
+
|
27 |
+
def create_pitch_plot(pitch_data: Dict) -> str:
|
28 |
+
"""Create pitch analysis plot"""
|
29 |
+
fig = go.Figure()
|
30 |
+
|
31 |
+
# Add box plot
|
32 |
+
fig.add_trace(go.Box(
|
33 |
+
y=[pitch_data['min'], pitch_data['mean'], pitch_data['max']],
|
34 |
+
name='Pitch Distribution',
|
35 |
+
boxpoints='all'
|
36 |
+
))
|
37 |
+
|
38 |
+
# Update layout
|
39 |
+
fig.update_layout(
|
40 |
+
title='Pitch Analysis',
|
41 |
+
yaxis_title='Frequency (Hz)',
|
42 |
+
template='plotly_white',
|
43 |
+
height=400
|
44 |
+
)
|
45 |
+
|
46 |
+
return fig.to_html(include_plotlyjs=True)
|
47 |
+
|
48 |
+
def create_energy_plot(energy_data: Dict) -> str:
|
49 |
+
"""Create energy analysis plot"""
|
50 |
+
fig = go.Figure()
|
51 |
+
|
52 |
+
# Add indicator
|
53 |
+
fig.add_trace(go.Indicator(
|
54 |
+
mode='gauge+number',
|
55 |
+
value=energy_data['mean'],
|
56 |
+
title={'text': 'Voice Energy Level'},
|
57 |
+
gauge={
|
58 |
+
'axis': {'range': [0, 1]},
|
59 |
+
'bar': {'color': 'darkblue'},
|
60 |
+
'steps': [
|
61 |
+
{'range': [0, 0.3], 'color': 'lightgray'},
|
62 |
+
{'range': [0.3, 0.7], 'color': 'gray'},
|
63 |
+
{'range': [0.7, 1], 'color': 'darkgray'}
|
64 |
+
]
|
65 |
+
}
|
66 |
+
))
|
67 |
+
|
68 |
+
# Update layout
|
69 |
+
fig.update_layout(
|
70 |
+
height=300,
|
71 |
+
template='plotly_white'
|
72 |
+
)
|
73 |
+
|
74 |
+
return fig.to_html(include_plotlyjs=True)
|