geethareddy commited on
Commit
9bb4c44
·
verified ·
1 Parent(s): b694114

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -28
app.py CHANGED
@@ -26,11 +26,11 @@ def load_whisper_model():
26
  try:
27
  model = pipeline(
28
  "automatic-speech-recognition",
29
- model="openai/whisper-tiny.en",
30
  device=-1, # CPU; use device=0 for GPU if available
31
  model_kwargs={"use_safetensors": True}
32
  )
33
- logger.info("Whisper model loaded successfully")
34
  return model
35
  except Exception as e:
36
  logger.error(f"Failed to load Whisper model: {str(e)}")
@@ -52,10 +52,10 @@ def load_symptom_model():
52
  try:
53
  model = pipeline(
54
  "text-classification",
55
- model="distilbert-base-uncased",
56
  device=-1
57
  )
58
- logger.warning("Fallback to distilbert-base-uncased model")
59
  return model
60
  except Exception as fallback_e:
61
  logger.error(f"Fallback model failed: {str(fallback_e)}")
@@ -188,8 +188,11 @@ def analyze_voice(audio_file):
188
 
189
  temp_dir = os.path.join(tempfile.gettempdir(), "gradio")
190
  if not ensure_writable_dir(temp_dir):
191
- logger.error(f"Temp directory {temp_dir} not writable")
192
- return f"Error: Temp directory {temp_dir} not writable"
 
 
 
193
 
194
  if not os.path.exists(audio_file):
195
  logger.error(f"Audio file not found: {audio_file}")
@@ -200,12 +203,12 @@ def analyze_voice(audio_file):
200
  f"audio_{datetime.utcnow().strftime('%Y%m%d%H%M%S%f')}_{os.path.basename(audio_file)}"
201
  )
202
  try:
203
- shutil.move(audio_file, unique_path)
204
  audio_file = unique_path
205
- logger.debug(f"Moved to: {audio_file}")
206
  except Exception as e:
207
- logger.error(f"Failed to move audio file: {str(e)}")
208
- return f"Error: Failed to move audio file: {str(e)}"
209
 
210
  file_hash = compute_file_hash(audio_file)
211
  logger.info(f"Processing audio, Hash: {file_hash}")
@@ -249,25 +252,26 @@ def test_with_sample_audio():
249
  """Test with sample or synthetic audio."""
250
  temp_dir = os.path.join(tempfile.gettempdir(), "audio_samples")
251
  if not ensure_writable_dir(temp_dir):
252
- logger.error(f"Temp directory {temp_dir} not writable")
253
- return f"Error: Temp directory {temp_dir} not writable"
 
 
 
254
 
255
- sample_audio_path = os.path.join(temp_dir, "sample.wav")
256
- if not os.path.exists(sample_audio_path):
257
- logger.warning("Sample audio not found; generating synthetic audio")
258
- sr = 16000
259
- t = np.linspace(0, 2, 2 * sr)
260
- freq_mod = 440 + 10 * np.sin(2 * np.pi * 0.5 * t)
261
- amplitude_mod = 0.5 + 0.1 * np.sin(2 * np.pi * 0.3 * t)
262
- noise = 0.01 * np.random.normal(0, 1, len(t))
263
- dummy_audio = amplitude_mod * np.sin(2 * np.pi * freq_mod * t) + noise
264
- sample_audio_path = os.path.join(temp_dir, "dummy_test.wav")
265
- try:
266
- soundfile.write(dummy_audio, sr, sample_audio_path)
267
- logger.info(f"Generated synthetic audio: {sample_audio_path}")
268
- except Exception as e:
269
- logger.error(f"Failed to write synthetic audio: {str(e)}")
270
- return f"Error: Failed to generate synthetic audio: {str(e)}"
271
 
272
  if not os.path.exists(sample_audio_path):
273
  logger.error(f"Synthetic audio not created: {sample_audio_path}")
 
26
  try:
27
  model = pipeline(
28
  "automatic-speech-recognition",
29
+ model="openai/whisper-small.en",
30
  device=-1, # CPU; use device=0 for GPU if available
31
  model_kwargs={"use_safetensors": True}
32
  )
33
+ logger.info("Whisper-small.en model loaded successfully")
34
  return model
35
  except Exception as e:
36
  logger.error(f"Failed to load Whisper model: {str(e)}")
 
52
  try:
53
  model = pipeline(
54
  "text-classification",
55
+ model="allenai/scibert_scivocab_uncased",
56
  device=-1
57
  )
58
+ logger.warning("Fallback to allenai/scibert_scivocab_uncased model")
59
  return model
60
  except Exception as fallback_e:
61
  logger.error(f"Fallback model failed: {str(fallback_e)}")
 
188
 
189
  temp_dir = os.path.join(tempfile.gettempdir(), "gradio")
190
  if not ensure_writable_dir(temp_dir):
191
+ fallback_dir = os.path.join(os.getcwd(), "temp_gradio")
192
+ if not ensure_writable_dir(fallback_dir):
193
+ logger.error(f"Both temp directories {temp_dir} and {fallback_dir} not writable")
194
+ return f"Error: Temp directories not writable"
195
+ temp_dir = fallback_dir
196
 
197
  if not os.path.exists(audio_file):
198
  logger.error(f"Audio file not found: {audio_file}")
 
203
  f"audio_{datetime.utcnow().strftime('%Y%m%d%H%M%S%f')}_{os.path.basename(audio_file)}"
204
  )
205
  try:
206
+ shutil.copy(audio_file, unique_path)
207
  audio_file = unique_path
208
+ logger.debug(f"Copied to: {audio_file}")
209
  except Exception as e:
210
+ logger.error(f"Failed to copy audio file: {str(e)}")
211
+ return f"Error: Failed to copy audio file: {str(e)}"
212
 
213
  file_hash = compute_file_hash(audio_file)
214
  logger.info(f"Processing audio, Hash: {file_hash}")
 
252
  """Test with sample or synthetic audio."""
253
  temp_dir = os.path.join(tempfile.gettempdir(), "audio_samples")
254
  if not ensure_writable_dir(temp_dir):
255
+ fallback_dir = os.path.join(os.getcwd(), "temp_audio_samples")
256
+ if not ensure_writable_dir(fallback_dir):
257
+ logger.error(f"Both temp directories {temp_dir} and {fallback_dir} not writable")
258
+ return f"Error: Temp directories not writable"
259
+ temp_dir = fallback_dir
260
 
261
+ sample_audio_path = os.path.join(temp_dir, "dummy_test.wav")
262
+ logger.info(f"Generating synthetic audio at: {sample_audio_path}")
263
+ sr = 16000
264
+ t = np.linspace(0, 2, 2 * sr)
265
+ freq_mod = 440 + 10 * np.sin(2 * np.pi * 0.5 * t)
266
+ amplitude_mod = 0.5 + 0.1 * np.sin(2 * np.pi * 0.3 * t)
267
+ noise = 0.01 * np.random.normal(0, 1, len(t))
268
+ dummy_audio = amplitude_mod * np.sin(2 * np.pi * freq_mod * t) + noise
269
+ try:
270
+ soundfile.write(dummy_audio, sr, sample_audio_path)
271
+ logger.info(f"Generated synthetic audio: {sample_audio_path}")
272
+ except Exception as e:
273
+ logger.error(f"Failed to write synthetic audio: {str(e)}")
274
+ return f"Error: Failed to generate synthetic audio: {str(e)}"
 
 
275
 
276
  if not os.path.exists(sample_audio_path):
277
  logger.error(f"Synthetic audio not created: {sample_audio_path}")