Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,6 @@ import torch
|
|
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,68 +29,38 @@ except Exception as e:
|
|
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()
|
62 |
-
print(f"Features shape: {features.shape}, Sample values: {features[0, 0, :5]}")
|
63 |
|
64 |
-
#
|
65 |
-
respiratory_score = np.mean(features
|
66 |
-
mental_health_score = np.std(features
|
67 |
-
|
68 |
-
# Log scores
|
69 |
-
print(f"Respiratory Score: {respiratory_score:.4f}, Mental Health Score: {mental_health_score:.4f}")
|
70 |
-
|
71 |
-
# Threshold-based feedback
|
72 |
feedback = ""
|
73 |
-
if respiratory_score > 0.1:
|
74 |
-
feedback +=
|
75 |
-
if mental_health_score > 0.
|
76 |
-
feedback +=
|
77 |
|
78 |
if not feedback:
|
79 |
feedback = "No significant health indicators detected."
|
80 |
|
81 |
-
feedback +=
|
82 |
-
feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
83 |
|
84 |
# Store in Salesforce
|
85 |
if sf:
|
86 |
store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_score)
|
87 |
|
88 |
-
# Clean up temporary audio file
|
89 |
-
try:
|
90 |
-
os.remove(audio_file)
|
91 |
-
print(f"Deleted temporary audio file: {audio_file}")
|
92 |
-
except Exception as e:
|
93 |
-
print(f"Failed to delete audio file: {str(e)}")
|
94 |
-
|
95 |
return feedback
|
96 |
except Exception as e:
|
97 |
return f"Error processing audio: {str(e)}"
|
@@ -111,7 +80,7 @@ def store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_s
|
|
111 |
|
112 |
def test_with_sample_audio():
|
113 |
"""Test the app with a sample audio file."""
|
114 |
-
sample_audio_path = "audio_samples/sample.wav"
|
115 |
if os.path.exists(sample_audio_path):
|
116 |
return analyze_voice(sample_audio_path)
|
117 |
return "Sample audio file not found."
|
@@ -126,5 +95,5 @@ iface = gr.Interface(
|
|
126 |
)
|
127 |
|
128 |
if __name__ == "__main__":
|
129 |
-
print(test_with_sample_audio())
|
130 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
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 |
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 |
# Load audio file
|
36 |
audio, sr = librosa.load(audio_file, sr=16000)
|
|
|
|
|
37 |
|
38 |
# Process audio for Wav2Vec2
|
39 |
inputs = processor(audio, sampling_rate=16000, return_tensors="pt", padding=True)
|
|
|
|
|
40 |
with torch.no_grad():
|
41 |
outputs = model(**inputs)
|
42 |
|
43 |
+
# Extract features (simplified for demo)
|
44 |
+
features = outputs.last_hidden_state.mean(dim=1).numpy()
|
|
|
45 |
|
46 |
+
# Adjusted thresholds for testing (lower to trigger feedback)
|
47 |
+
respiratory_score = np.mean(features) # Mock score
|
48 |
+
mental_health_score = np.std(features) # Mock score
|
|
|
|
|
|
|
|
|
|
|
49 |
feedback = ""
|
50 |
+
if respiratory_score > 0.1: # Lowered from 0.5
|
51 |
+
feedback += "Possible respiratory issue detected; consult a doctor. "
|
52 |
+
if mental_health_score > 0.1: # Lowered from 0.3
|
53 |
+
feedback += "Possible stress indicators detected; consider professional advice. "
|
54 |
|
55 |
if not feedback:
|
56 |
feedback = "No significant health indicators detected."
|
57 |
|
58 |
+
feedback += "\n\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
|
|
|
59 |
|
60 |
# Store in Salesforce
|
61 |
if sf:
|
62 |
store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_score)
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
return feedback
|
65 |
except Exception as e:
|
66 |
return f"Error processing audio: {str(e)}"
|
|
|
80 |
|
81 |
def test_with_sample_audio():
|
82 |
"""Test the app with a sample audio file."""
|
83 |
+
sample_audio_path = "audio_samples/sample.wav" # Or "audio_samples/common_voice_sample.wav"
|
84 |
if os.path.exists(sample_audio_path):
|
85 |
return analyze_voice(sample_audio_path)
|
86 |
return "Sample audio file not found."
|
|
|
95 |
)
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
+
print(test_with_sample_audio()) # Run test on startup
|
99 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|