|
|
import gradio as gr |
|
|
import librosa |
|
|
import numpy as np |
|
|
import torch |
|
|
from transformers import Wav2Vec2Processor, Wav2Vec2Model |
|
|
from simple_salesforce import Salesforce |
|
|
import os |
|
|
from datetime import datetime |
|
|
|
|
|
|
|
|
SF_USERNAME = os.getenv("SF_USERNAME", "[email protected]") |
|
|
SF_PASSWORD = os.getenv("SF_PASSWORD", "voicebot1") |
|
|
SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN", "jq4VVHUFti6TmzJDjjegv2h6b") |
|
|
SF_INSTANCE_URL = os.getenv("SF_INSTANCE_URL", "https://voicebot-dev-ed.develop.lightning.force.com/lightning/setup/SetupOneHome/home") |
|
|
|
|
|
|
|
|
try: |
|
|
sf = Salesforce( |
|
|
username=SF_USERNAME, |
|
|
password=SF_PASSWORD, |
|
|
security_token=SF_SECURITY_TOKEN, |
|
|
instance_url=SF_INSTANCE_URL |
|
|
) |
|
|
except Exception as e: |
|
|
print(f"Failed to connect to Salesforce: {str(e)}") |
|
|
sf = None |
|
|
|
|
|
|
|
|
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h") |
|
|
model = Wav2Vec2Model.from_pretrained("facebook/wav2vec2-base-960h") |
|
|
|
|
|
def analyze_voice(audio_file): |
|
|
"""Analyze voice for health indicators.""" |
|
|
try: |
|
|
|
|
|
audio, sr = librosa.load(audio_file, sr=16000) |
|
|
|
|
|
|
|
|
inputs = processor(audio, sampling_rate=16000, return_tensors="pt", padding=True) |
|
|
with torch.no_grad(): |
|
|
outputs = model(**inputs) |
|
|
|
|
|
|
|
|
features = outputs.last_hidden_state.mean(dim=1).numpy() |
|
|
|
|
|
|
|
|
respiratory_score = np.mean(features) |
|
|
mental_health_score = np.std(features) |
|
|
feedback = "" |
|
|
if respiratory_score > 0.5: |
|
|
feedback += "Possible respiratory issue detected; consult a doctor. " |
|
|
if mental_health_score > 0.3: |
|
|
feedback += "Possible stress indicators detected; consider professional advice. " |
|
|
|
|
|
if not feedback: |
|
|
feedback = "No significant health indicators detected." |
|
|
|
|
|
feedback += "\n\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice." |
|
|
|
|
|
|
|
|
if sf: |
|
|
store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_score) |
|
|
|
|
|
return feedback |
|
|
except Exception as e: |
|
|
return f"Error processing audio: {str(e)}" |
|
|
|
|
|
def store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_score): |
|
|
"""Store analysis results in Salesforce.""" |
|
|
try: |
|
|
sf.HealthAssessment__c.create({ |
|
|
"AssessmentDate__c": datetime.utcnow().isoformat(), |
|
|
"Feedback__c": feedback, |
|
|
"RespiratoryScore__c": float(respiratory_score), |
|
|
"MentalHealthScore__c": float(mental_health_score), |
|
|
"AudioFileName__c": os.path.basename(audio_file) |
|
|
}) |
|
|
except Exception as e: |
|
|
print(f"Failed to store in Salesforce: {str(e)}") |
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=analyze_voice, |
|
|
inputs=gr.Audio(type="filepath", label="Record or Upload Voice"), |
|
|
outputs=gr.Textbox(label="Health Assessment Feedback"), |
|
|
title="Health Voice Analyzer", |
|
|
description="Record or upload a voice sample for preliminary health assessment. Supports English, Spanish, Hindi, Mandarin." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
iface.launch(server_name="0.0.0.0", server_port=7860) |