Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import torch
|
|
5 |
from transformers import Wav2Vec2Processor, Wav2Vec2Model
|
6 |
from simple_salesforce import Salesforce
|
7 |
import os
|
|
|
8 |
from datetime import datetime
|
9 |
|
10 |
# Salesforce credentials (store securely in environment variables)
|
@@ -29,30 +30,40 @@ except Exception as e:
|
|
29 |
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
|
30 |
model = Wav2Vec2Model.from_pretrained("facebook/wav2vec2-base-960h")
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
def analyze_voice(audio_file):
|
33 |
"""Analyze voice for health indicators."""
|
34 |
try:
|
35 |
# Log audio file info
|
36 |
-
|
|
|
37 |
|
38 |
# Load audio file
|
39 |
audio, sr = librosa.load(audio_file, sr=16000)
|
40 |
-
|
|
|
41 |
|
42 |
# Process audio for Wav2Vec2
|
43 |
inputs = processor(audio, sampling_rate=16000, return_tensors="pt", padding=True)
|
44 |
-
print(f"Input tensor shape: {inputs['input_values'].shape}")
|
45 |
|
46 |
with torch.no_grad():
|
47 |
outputs = model(**inputs)
|
48 |
|
49 |
# Extract features
|
50 |
-
features = outputs.last_hidden_state.
|
51 |
-
print(f"Features shape: {features.shape}, Sample values: {features[0
|
52 |
|
53 |
-
#
|
54 |
-
respiratory_score = np.mean(features)
|
55 |
-
mental_health_score = np.std(features)
|
56 |
|
57 |
# Log scores
|
58 |
print(f"Respiratory Score: {respiratory_score:.4f}, Mental Health Score: {mental_health_score:.4f}")
|
@@ -61,13 +72,13 @@ def analyze_voice(audio_file):
|
|
61 |
feedback = ""
|
62 |
if respiratory_score > 0.1:
|
63 |
feedback += f"Possible respiratory issue detected (score: {respiratory_score:.4f}); consult a doctor. "
|
64 |
-
if mental_health_score > 0.1
|
65 |
feedback += f"Possible stress indicators detected (score: {mental_health_score:.4f}); consider professional advice. "
|
66 |
|
67 |
if not feedback:
|
68 |
feedback = "No significant health indicators detected."
|
69 |
|
70 |
-
feedback += f"\n\n**Debug Info**: Respiratory Score = {respiratory_score:.4f}, Mental Health Score = {mental_health_score:.4f}"
|
71 |
feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
72 |
|
73 |
# Store in Salesforce
|
|
|
5 |
from transformers import Wav2Vec2Processor, Wav2Vec2Model
|
6 |
from simple_salesforce import Salesforce
|
7 |
import os
|
8 |
+
import hashlib
|
9 |
from datetime import datetime
|
10 |
|
11 |
# Salesforce credentials (store securely in environment variables)
|
|
|
30 |
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
|
31 |
model = Wav2Vec2Model.from_pretrained("facebook/wav2vec2-base-960h")
|
32 |
|
33 |
+
def compute_file_hash(file_path):
|
34 |
+
"""Compute MD5 hash of a file to check uniqueness."""
|
35 |
+
hash_md5 = hashlib.md5()
|
36 |
+
with open(file_path, "rb") as f:
|
37 |
+
for chunk in iter(lambda: f.read(4096), b""):
|
38 |
+
hash_md5.update(chunk)
|
39 |
+
return hash_md5.hexdigest()
|
40 |
+
|
41 |
def analyze_voice(audio_file):
|
42 |
"""Analyze voice for health indicators."""
|
43 |
try:
|
44 |
# Log audio file info
|
45 |
+
file_hash = compute_file_hash(audio_file)
|
46 |
+
print(f"Processing audio file: {audio_file}, Hash: {file_hash}")
|
47 |
|
48 |
# Load audio file
|
49 |
audio, sr = librosa.load(audio_file, sr=16000)
|
50 |
+
audio = audio / (np.max(np.abs(audio)) + 1e-10) # Normalize audio
|
51 |
+
print(f"Audio shape: {audio.shape}, Sampling rate: {sr}, Duration: {len(audio)/sr:.2f}s, Mean: {np.mean(audio):.4f}, Std: {np.std(audio):.4f}")
|
52 |
|
53 |
# Process audio for Wav2Vec2
|
54 |
inputs = processor(audio, sampling_rate=16000, return_tensors="pt", padding=True)
|
55 |
+
print(f"Input tensor shape: {inputs['input_values'].shape}, Sample values: {inputs['input_values'][0][:5]}")
|
56 |
|
57 |
with torch.no_grad():
|
58 |
outputs = model(**inputs)
|
59 |
|
60 |
# Extract features
|
61 |
+
features = outputs.last_hidden_state.numpy() # Use full hidden states
|
62 |
+
print(f"Features shape: {features.shape}, Sample values: {features[0, 0, :5]}")
|
63 |
|
64 |
+
# Compute scores
|
65 |
+
respiratory_score = np.mean(features, axis=(1, 2))
|
66 |
+
mental_health_score = np.std(features, axis=(1, 2))
|
67 |
|
68 |
# Log scores
|
69 |
print(f"Respiratory Score: {respiratory_score:.4f}, Mental Health Score: {mental_health_score:.4f}")
|
|
|
72 |
feedback = ""
|
73 |
if respiratory_score > 0.1:
|
74 |
feedback += f"Possible respiratory issue detected (score: {respiratory_score:.4f}); consult a doctor. "
|
75 |
+
if mental_health_score > 0.2: # Raised from 0.1 to reduce false positives
|
76 |
feedback += f"Possible stress indicators detected (score: {mental_health_score:.4f}); consider professional advice. "
|
77 |
|
78 |
if not feedback:
|
79 |
feedback = "No significant health indicators detected."
|
80 |
|
81 |
+
feedback += f"\n\n**Debug Info**: Respiratory Score = {respiratory_score:.4f}, Mental Health Score = {mental_health_score:.4f}, File Hash = {file_hash}"
|
82 |
feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
83 |
|
84 |
# Store in Salesforce
|