Update app.py
Browse files
app.py
CHANGED
|
@@ -32,25 +32,32 @@ model = Wav2Vec2Model.from_pretrained("facebook/wav2vec2-base-960h")
|
|
| 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
|
| 44 |
features = outputs.last_hidden_state.mean(dim=1).numpy()
|
|
|
|
| 45 |
|
| 46 |
-
# Mock health analysis
|
| 47 |
respiratory_score = np.mean(features)
|
| 48 |
mental_health_score = np.std(features)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
print(f"Respiratory Score: {respiratory_score:.4f}, Mental Health Score: {mental_health_score:.4f}")
|
| 52 |
|
| 53 |
-
#
|
| 54 |
feedback = ""
|
| 55 |
if respiratory_score > 0.1:
|
| 56 |
feedback += f"Possible respiratory issue detected (score: {respiratory_score:.4f}); consult a doctor. "
|
|
@@ -67,7 +74,7 @@ def analyze_voice(audio_file):
|
|
| 67 |
if sf:
|
| 68 |
store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_score)
|
| 69 |
|
| 70 |
-
# Clean up temporary audio file
|
| 71 |
try:
|
| 72 |
os.remove(audio_file)
|
| 73 |
print(f"Deleted temporary audio file: {audio_file}")
|
|
@@ -93,7 +100,7 @@ def store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_s
|
|
| 93 |
|
| 94 |
def test_with_sample_audio():
|
| 95 |
"""Test the app with a sample audio file."""
|
| 96 |
-
sample_audio_path = "audio_samples/sample.wav"
|
| 97 |
if os.path.exists(sample_audio_path):
|
| 98 |
return analyze_voice(sample_audio_path)
|
| 99 |
return "Sample audio file not found."
|
|
@@ -108,5 +115,5 @@ iface = gr.Interface(
|
|
| 108 |
)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
-
print(test_with_sample_audio())
|
| 112 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 32 |
def analyze_voice(audio_file):
|
| 33 |
"""Analyze voice for health indicators."""
|
| 34 |
try:
|
| 35 |
+
# Log audio file info
|
| 36 |
+
print(f"Processing audio file: {audio_file}")
|
| 37 |
+
|
| 38 |
# Load audio file
|
| 39 |
audio, sr = librosa.load(audio_file, sr=16000)
|
| 40 |
+
print(f"Audio shape: {audio.shape}, Sampling rate: {sr}, Duration: {len(audio)/sr:.2f}s")
|
| 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.mean(dim=1).numpy()
|
| 51 |
+
print(f"Features shape: {features.shape}, Sample values: {features[0][:5]}")
|
| 52 |
|
| 53 |
+
# Mock health analysis
|
| 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}")
|
| 59 |
|
| 60 |
+
# Threshold-based feedback
|
| 61 |
feedback = ""
|
| 62 |
if respiratory_score > 0.1:
|
| 63 |
feedback += f"Possible respiratory issue detected (score: {respiratory_score:.4f}); consult a doctor. "
|
|
|
|
| 74 |
if sf:
|
| 75 |
store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_score)
|
| 76 |
|
| 77 |
+
# Clean up temporary audio file
|
| 78 |
try:
|
| 79 |
os.remove(audio_file)
|
| 80 |
print(f"Deleted temporary audio file: {audio_file}")
|
|
|
|
| 100 |
|
| 101 |
def test_with_sample_audio():
|
| 102 |
"""Test the app with a sample audio file."""
|
| 103 |
+
sample_audio_path = "audio_samples/sample.wav"
|
| 104 |
if os.path.exists(sample_audio_path):
|
| 105 |
return analyze_voice(sample_audio_path)
|
| 106 |
return "Sample audio file not found."
|
|
|
|
| 115 |
)
|
| 116 |
|
| 117 |
if __name__ == "__main__":
|
| 118 |
+
print(test_with_sample_audio())
|
| 119 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|