Spaces:
Runtime error
Runtime error
Delete model-manager.py
Browse files- model-manager.py +0 -79
model-manager.py
DELETED
@@ -1,79 +0,0 @@
|
|
1 |
-
from transformers import (
|
2 |
-
WhisperProcessor, WhisperForConditionalGeneration,
|
3 |
-
AutoModelForSequenceClassification, AutoTokenizer
|
4 |
-
)
|
5 |
-
import torch
|
6 |
-
|
7 |
-
class ModelManager:
|
8 |
-
def __init__(self):
|
9 |
-
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
10 |
-
self.models = {}
|
11 |
-
self.tokenizers = {}
|
12 |
-
self.processors = {}
|
13 |
-
|
14 |
-
def load_models(self):
|
15 |
-
# Load Whisper for speech recognition
|
16 |
-
self.processors['whisper'] = WhisperProcessor.from_pretrained("openai/whisper-base")
|
17 |
-
self.models['whisper'] = WhisperForConditionalGeneration.from_pretrained(
|
18 |
-
"openai/whisper-base"
|
19 |
-
).to(self.device)
|
20 |
-
|
21 |
-
# Load EmoBERTa for emotion detection
|
22 |
-
self.tokenizers['emotion'] = AutoTokenizer.from_pretrained("arpanghoshal/EmoRoBERTa")
|
23 |
-
self.models['emotion'] = AutoModelForSequenceClassification.from_pretrained(
|
24 |
-
"arpanghoshal/EmoRoBERTa"
|
25 |
-
).to(self.device)
|
26 |
-
|
27 |
-
# Load ClinicalBERT for analysis
|
28 |
-
self.tokenizers['clinical'] = AutoTokenizer.from_pretrained(
|
29 |
-
"emilyalsentzer/Bio_ClinicalBERT"
|
30 |
-
)
|
31 |
-
self.models['clinical'] = AutoModelForSequenceClassification.from_pretrained(
|
32 |
-
"emilyalsentzer/Bio_ClinicalBERT"
|
33 |
-
).to(self.device)
|
34 |
-
|
35 |
-
def transcribe(self, audio_input):
|
36 |
-
inputs = self.processors['whisper'](
|
37 |
-
audio_input,
|
38 |
-
return_tensors="pt"
|
39 |
-
).input_features.to(self.device)
|
40 |
-
|
41 |
-
generated_ids = self.models['whisper'].generate(inputs)
|
42 |
-
transcription = self.processors['whisper'].batch_decode(
|
43 |
-
generated_ids,
|
44 |
-
skip_special_tokens=True
|
45 |
-
)[0]
|
46 |
-
return transcription
|
47 |
-
|
48 |
-
def analyze_emotions(self, text):
|
49 |
-
inputs = self.tokenizers['emotion'](
|
50 |
-
text,
|
51 |
-
return_tensors="pt",
|
52 |
-
padding=True,
|
53 |
-
truncation=True,
|
54 |
-
max_length=512
|
55 |
-
).to(self.device)
|
56 |
-
|
57 |
-
outputs = self.models['emotion'](**inputs)
|
58 |
-
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
59 |
-
|
60 |
-
emotions = ['anger', 'fear', 'joy', 'love', 'sadness', 'surprise']
|
61 |
-
return {emotion: float(prob) for emotion, prob in zip(emotions, probs[0])}
|
62 |
-
|
63 |
-
def analyze_mental_health(self, text):
|
64 |
-
inputs = self.tokenizers['clinical'](
|
65 |
-
text,
|
66 |
-
return_tensors="pt",
|
67 |
-
padding=True,
|
68 |
-
truncation=True,
|
69 |
-
max_length=512
|
70 |
-
).to(self.device)
|
71 |
-
|
72 |
-
outputs = self.models['clinical'](**inputs)
|
73 |
-
scores = torch.sigmoid(outputs.logits)
|
74 |
-
|
75 |
-
return {
|
76 |
-
'depression_risk': float(scores[0][0]),
|
77 |
-
'anxiety_risk': float(scores[0][1]),
|
78 |
-
'stress_level': float(scores[0][2])
|
79 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|