RathodHarish commited on
Commit
a6dea81
·
verified ·
1 Parent(s): b3db979

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -31
app.py CHANGED
@@ -1,11 +1,11 @@
1
  import gradio as gr
 
2
  import librosa
3
  import numpy as np
4
- 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)
11
  SF_USERNAME = os.getenv("SF_USERNAME", "your_salesforce_username")
@@ -13,6 +13,9 @@ SF_PASSWORD = os.getenv("SF_PASSWORD", "your_salesforce_password")
13
  SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN", "your_salesforce_security_token")
14
  SF_INSTANCE_URL = os.getenv("SF_INSTANCE_URL", "https://your-salesforce-instance.salesforce.com")
15
 
 
 
 
16
  # Initialize Salesforce connection
17
  try:
18
  sf = Salesforce(
@@ -25,54 +28,105 @@ except Exception as e:
25
  print(f"Failed to connect to Salesforce: {str(e)}")
26
  sf = None
27
 
28
- # Load Wav2Vec2 model for speech feature extraction
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)}"
67
 
68
- def store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_score):
69
  """Store analysis results in Salesforce."""
70
  try:
71
  sf.HealthAssessment__c.create({
72
  "AssessmentDate__c": datetime.utcnow().isoformat(),
73
  "Feedback__c": feedback,
74
- "RespiratoryScore__c": float(respiratory_score),
75
- "MentalHealthScore__c": float(mental_health_score),
 
76
  "AudioFileName__c": os.path.basename(audio_file)
77
  })
78
  except Exception as e:
@@ -80,7 +134,7 @@ def store_in_salesforce(audio_file, feedback, respiratory_score, mental_health_s
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."
@@ -91,9 +145,9 @@ iface = gr.Interface(
91
  inputs=gr.Audio(type="filepath", label="Record or Upload Voice"),
92
  outputs=gr.Textbox(label="Health Assessment Feedback"),
93
  title="Health Voice Analyzer",
94
- description="Record or upload a voice sample for preliminary health assessment. Supports English, Spanish, Hindi, Mandarin."
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)
 
1
  import gradio as gr
2
+ import requests
3
  import librosa
4
  import numpy as np
 
 
 
5
  import os
6
+ import hashlib
7
  from datetime import datetime
8
+ from simple_salesforce import Salesforce
9
 
10
  # Salesforce credentials (store securely in environment variables)
11
  SF_USERNAME = os.getenv("SF_USERNAME", "your_salesforce_username")
 
13
  SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN", "your_salesforce_security_token")
14
  SF_INSTANCE_URL = os.getenv("SF_INSTANCE_URL", "https://your-salesforce-instance.salesforce.com")
15
 
16
+ # Hugging Face Inference API token (store in environment variables)
17
+ HF_TOKEN = os.getenv("HF_TOKEN", "your_huggingface_token")
18
+
19
  # Initialize Salesforce connection
20
  try:
21
  sf = Salesforce(
 
28
  print(f"Failed to connect to Salesforce: {str(e)}")
29
  sf = None
30
 
31
+ # Hugging Face API endpoints
32
+ WHISPER_API_URL = "https://api-inference.huggingface.co/models/openai/whisper-tiny.en"
33
+ SYMPTOM_API_URL = "https://api-inference.huggingface.co/models/abhirajeshbhai/symptom-2-disease-net"
34
+ HEADERS = {"Authorization": f"Bearer {HF_TOKEN}"}
35
+
36
+ def compute_file_hash(file_path):
37
+ """Compute MD5 hash of a file to check uniqueness."""
38
+ hash_md5 = hashlib.md5()
39
+ with open(file_path, "rb") as f:
40
+ for chunk in iter(lambda: f.read(4096), b""):
41
+ hash_md5.update(chunk)
42
+ return hash_md5.hexdigest()
43
+
44
+ def transcribe_audio(audio_file):
45
+ """Transcribe audio using Whisper API."""
46
+ try:
47
+ with open(audio_file, "rb") as f:
48
+ data = f.read()
49
+ response = requests.post(WHISPER_API_URL, headers=HEADERS, data=data)
50
+ response.raise_for_status()
51
+ result = response.json()
52
+ transcription = result.get("text", "")
53
+ print(f"Transcription: {transcription}")
54
+ return transcription
55
+ except Exception as e:
56
+ print(f"Whisper API error: {str(e)}")
57
+ return f"Error transcribing audio: {str(e)}"
58
+
59
+ def analyze_symptoms(text):
60
+ """Analyze symptoms using Symptom-2-Disease API."""
61
+ try:
62
+ payload = {"inputs": text}
63
+ response = requests.post(SYMPTOM_API_URL, headers=HEADERS, json=payload)
64
+ response.raise_for_status()
65
+ result = response.json()
66
+ if result and isinstance(result, list) and len(result) > 0:
67
+ prediction = result[0][0]["label"]
68
+ score = result[0][0]["score"]
69
+ print(f"Health Prediction: {prediction}, Score: {score:.4f}")
70
+ return prediction, score
71
+ return "No health condition predicted", 0.0
72
+ except Exception as e:
73
+ print(f"Symptom API error: {str(e)}")
74
+ return f"Error analyzing symptoms: {str(e)}", 0.0
75
 
76
  def analyze_voice(audio_file):
77
  """Analyze voice for health indicators."""
78
  try:
79
+ # Log audio file info
80
+ file_hash = compute_file_hash(audio_file)
81
+ print(f"Processing audio file: {audio_file}, Hash: {file_hash}")
82
 
83
+ # Load audio to verify format
84
+ audio, sr = librosa.load(audio_file, sr=16000)
85
+ 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}")
 
86
 
87
+ # Transcribe audio
88
+ transcription = transcribe_audio(audio_file)
89
+ if "Error transcribing" in transcription:
90
+ return transcription
91
 
92
+ # Analyze symptoms
93
+ prediction, score = analyze_symptoms(transcription)
94
+ if "Error analyzing" in prediction:
95
+ return prediction
 
 
 
 
96
 
97
+ # Generate feedback
98
+ if prediction == "No health condition predicted":
99
  feedback = "No significant health indicators detected."
100
+ else:
101
+ feedback = f"Possible health condition: {prediction} (confidence: {score:.4f}). Consult a doctor."
102
 
103
+ feedback += f"\n\n**Debug Info**: Transcription = '{transcription}', Prediction = {prediction}, Confidence = {score:.4f}, File Hash = {file_hash}"
104
+ feedback += "\n**Disclaimer**: This is not a diagnostic tool. Consult a healthcare provider for medical advice."
105
 
106
  # Store in Salesforce
107
  if sf:
108
+ store_in_salesforce(audio_file, feedback, transcription, prediction, score)
109
+
110
+ # Clean up temporary audio file
111
+ try:
112
+ os.remove(audio_file)
113
+ print(f"Deleted temporary audio file: {audio_file}")
114
+ except Exception as e:
115
+ print(f"Failed to delete audio file: {str(e)}")
116
 
117
  return feedback
118
  except Exception as e:
119
  return f"Error processing audio: {str(e)}"
120
 
121
+ def store_in_salesforce(audio_file, feedback, transcription, prediction, score):
122
  """Store analysis results in Salesforce."""
123
  try:
124
  sf.HealthAssessment__c.create({
125
  "AssessmentDate__c": datetime.utcnow().isoformat(),
126
  "Feedback__c": feedback,
127
+ "Transcription__c": transcription,
128
+ "Prediction__c": prediction,
129
+ "Confidence__c": float(score),
130
  "AudioFileName__c": os.path.basename(audio_file)
131
  })
132
  except Exception as e:
 
134
 
135
  def test_with_sample_audio():
136
  """Test the app with a sample audio file."""
137
+ sample_audio_path = "audio_samples/sample.wav"
138
  if os.path.exists(sample_audio_path):
139
  return analyze_voice(sample_audio_path)
140
  return "Sample audio file not found."
 
145
  inputs=gr.Audio(type="filepath", label="Record or Upload Voice"),
146
  outputs=gr.Textbox(label="Health Assessment Feedback"),
147
  title="Health Voice Analyzer",
148
+ description="Record or upload a voice sample describing symptoms for preliminary health assessment. Supports English (transcription), with symptom analysis in English."
149
  )
150
 
151
  if __name__ == "__main__":
152
+ print(test_with_sample_audio())
153
  iface.launch(server_name="0.0.0.0", server_port=7860)