Update app.py
Browse files
app.py
CHANGED
@@ -26,11 +26,11 @@ def load_whisper_model():
|
|
26 |
try:
|
27 |
model = pipeline(
|
28 |
"automatic-speech-recognition",
|
29 |
-
model="openai/whisper-
|
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="
|
56 |
device=-1
|
57 |
)
|
58 |
-
logger.warning("Fallback to
|
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 |
-
|
192 |
-
|
|
|
|
|
|
|
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.
|
204 |
audio_file = unique_path
|
205 |
-
logger.debug(f"
|
206 |
except Exception as e:
|
207 |
-
logger.error(f"Failed to
|
208 |
-
return f"Error: Failed to
|
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 |
-
|
253 |
-
|
|
|
|
|
|
|
254 |
|
255 |
-
sample_audio_path = os.path.join(temp_dir, "
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
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}")
|