vitorcalvi commited on
Commit
23ba234
·
1 Parent(s): 0073001
Files changed (1) hide show
  1. app.py +21 -10
app.py CHANGED
@@ -6,11 +6,19 @@ import numpy as np
6
  import tempfile
7
  import os
8
  import warnings
 
9
 
10
  warnings.filterwarnings("ignore")
11
 
12
  app = FastAPI()
13
 
 
 
 
 
 
 
 
14
  def extract_audio_features(audio_file_path):
15
  # Load the audio file using soundfile
16
  waveform, sample_rate = sf.read(audio_file_path)
@@ -24,7 +32,6 @@ def extract_audio_features(audio_file_path):
24
  mfccs = np.mean(np.abs(np.fft.fft(waveform)[:13]), axis=0) # Simplified MFCC-like features
25
 
26
  # Placeholder for speech rate and fundamental frequency
27
- # Speech rate and pitch extraction would require more complex DSP techniques or external libraries.
28
  speech_rate = 4.0 # Arbitrary placeholder value for speech rate
29
  f0 = np.mean(np.abs(np.diff(waveform))) * sample_rate / (2 * np.pi) # Rough pitch estimate
30
 
@@ -77,27 +84,31 @@ async def analyze_stress(
77
  # Handle audio file analysis
78
  if file or file_path:
79
  if file:
80
- if not file.filename.endswith(".wav"):
81
- raise HTTPException(status_code=400, detail="Only .wav files are supported.")
82
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
83
  temp_file.write(await file.read())
84
- temp_wav_path = temp_file.name
85
  else:
86
- if not file_path.endswith(".wav"):
87
- raise HTTPException(status_code=400, detail="Only .wav files are supported.")
88
  if not os.path.exists(file_path):
89
  raise HTTPException(status_code=400, detail="File path does not exist.")
90
- temp_wav_path = file_path
 
 
 
 
91
 
92
  try:
93
- result = analyze_voice_stress(temp_wav_path)
94
  return JSONResponse(content=result)
95
  except Exception as e:
96
  raise HTTPException(status_code=500, detail=str(e))
97
  finally:
98
  # Clean up temporary files
99
  if file:
100
- os.remove(temp_wav_path)
101
 
102
  # Handle text analysis
103
  elif text:
 
6
  import tempfile
7
  import os
8
  import warnings
9
+ from pydub import AudioSegment
10
 
11
  warnings.filterwarnings("ignore")
12
 
13
  app = FastAPI()
14
 
15
+ def convert_mp3_to_wav(mp3_path):
16
+ # Convert MP3 to WAV
17
+ sound = AudioSegment.from_mp3(mp3_path)
18
+ wav_path = mp3_path.replace(".mp3", ".wav")
19
+ sound.export(wav_path, format="wav")
20
+ return wav_path
21
+
22
  def extract_audio_features(audio_file_path):
23
  # Load the audio file using soundfile
24
  waveform, sample_rate = sf.read(audio_file_path)
 
32
  mfccs = np.mean(np.abs(np.fft.fft(waveform)[:13]), axis=0) # Simplified MFCC-like features
33
 
34
  # Placeholder for speech rate and fundamental frequency
 
35
  speech_rate = 4.0 # Arbitrary placeholder value for speech rate
36
  f0 = np.mean(np.abs(np.diff(waveform))) * sample_rate / (2 * np.pi) # Rough pitch estimate
37
 
 
84
  # Handle audio file analysis
85
  if file or file_path:
86
  if file:
87
+ if not (file.filename.endswith(".wav") or file.filename.endswith(".mp3")):
88
+ raise HTTPException(status_code=400, detail="Only .wav and .mp3 files are supported.")
89
+ with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[-1]) as temp_file:
90
  temp_file.write(await file.read())
91
+ temp_audio_path = temp_file.name
92
  else:
93
+ if not (file_path.endswith(".wav") or file_path.endswith(".mp3")):
94
+ raise HTTPException(status_code=400, detail="Only .wav and .mp3 files are supported.")
95
  if not os.path.exists(file_path):
96
  raise HTTPException(status_code=400, detail="File path does not exist.")
97
+ temp_audio_path = file_path
98
+
99
+ # Convert MP3 to WAV if needed
100
+ if temp_audio_path.endswith(".mp3"):
101
+ temp_audio_path = convert_mp3_to_wav(temp_audio_path)
102
 
103
  try:
104
+ result = analyze_voice_stress(temp_audio_path)
105
  return JSONResponse(content=result)
106
  except Exception as e:
107
  raise HTTPException(status_code=500, detail=str(e))
108
  finally:
109
  # Clean up temporary files
110
  if file:
111
+ os.remove(temp_audio_path)
112
 
113
  # Handle text analysis
114
  elif text: