vitorcalvi commited on
Commit
50c246b
·
1 Parent(s): 9ef6f93
Files changed (2) hide show
  1. app.py +22 -10
  2. requirements.txt +4 -5
app.py CHANGED
@@ -6,14 +6,21 @@ import numpy as np
6
  import tempfile
7
  import os
8
  import warnings
 
 
9
 
10
  warnings.filterwarnings("ignore", category=UserWarning, module='librosa')
11
 
12
  app = FastAPI()
13
 
 
 
 
 
 
14
  def extract_audio_features(audio_file_path):
15
  # Load the audio file and extract features
16
- y, sr = librosa.load(audio_file_path, sr=None)
17
  f0, voiced_flag, voiced_probs = librosa.pyin(y, fmin=75, fmax=600)
18
  f0 = f0[~np.isnan(f0)]
19
  energy = librosa.feature.rms(y=y)[0]
@@ -72,26 +79,31 @@ async def analyze_stress(
72
  # Handle audio file analysis
73
  if file or file_path:
74
  if file:
75
- if not file.filename.endswith(".opus"):
76
- raise HTTPException(status_code=400, detail="Only .opus files are supported.")
77
- with tempfile.NamedTemporaryFile(delete=False, suffix=".opus") as temp_file:
78
  temp_file.write(await file.read())
79
- temp_file_path = temp_file.name
 
 
80
  else:
81
- if not file_path.endswith(".opus"):
82
- raise HTTPException(status_code=400, detail="Only .opus files are supported.")
83
  if not os.path.exists(file_path):
84
  raise HTTPException(status_code=400, detail="File path does not exist.")
85
- temp_file_path = file_path
 
86
 
87
  try:
88
- result = analyze_voice_stress(temp_file_path)
89
  return JSONResponse(content=result)
90
  except Exception as e:
91
  raise HTTPException(status_code=500, detail=str(e))
92
  finally:
 
93
  if file:
94
- os.remove(temp_file_path)
 
95
 
96
  # Handle text analysis
97
  elif text:
 
6
  import tempfile
7
  import os
8
  import warnings
9
+ import soundfile as sf
10
+ from pydub import AudioSegment
11
 
12
  warnings.filterwarnings("ignore", category=UserWarning, module='librosa')
13
 
14
  app = FastAPI()
15
 
16
+ def convert_mp3_to_wav(mp3_path, wav_path):
17
+ # Convert mp3 to wav using pydub and ffmpeg
18
+ audio = AudioSegment.from_mp3(mp3_path)
19
+ audio.export(wav_path, format="wav")
20
+
21
  def extract_audio_features(audio_file_path):
22
  # Load the audio file and extract features
23
+ y, sr = sf.read(audio_file_path)
24
  f0, voiced_flag, voiced_probs = librosa.pyin(y, fmin=75, fmax=600)
25
  f0 = f0[~np.isnan(f0)]
26
  energy = librosa.feature.rms(y=y)[0]
 
79
  # Handle audio file analysis
80
  if file or file_path:
81
  if file:
82
+ if not file.filename.endswith(".mp3"):
83
+ raise HTTPException(status_code=400, detail="Only .mp3 files are supported.")
84
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
85
  temp_file.write(await file.read())
86
+ temp_mp3_path = temp_file.name
87
+ temp_wav_path = temp_mp3_path.replace(".mp3", ".wav")
88
+ convert_mp3_to_wav(temp_mp3_path, temp_wav_path)
89
  else:
90
+ if not file_path.endswith(".mp3"):
91
+ raise HTTPException(status_code=400, detail="Only .mp3 files are supported.")
92
  if not os.path.exists(file_path):
93
  raise HTTPException(status_code=400, detail="File path does not exist.")
94
+ temp_wav_path = file_path.replace(".mp3", ".wav")
95
+ convert_mp3_to_wav(file_path, temp_wav_path)
96
 
97
  try:
98
+ result = analyze_voice_stress(temp_wav_path)
99
  return JSONResponse(content=result)
100
  except Exception as e:
101
  raise HTTPException(status_code=500, detail=str(e))
102
  finally:
103
+ # Clean up temporary files
104
  if file:
105
+ os.remove(temp_mp3_path)
106
+ os.remove(temp_wav_path)
107
 
108
  # Handle text analysis
109
  elif text:
requirements.txt CHANGED
@@ -1,9 +1,8 @@
1
  fastapi
2
  uvicorn
3
- pydantic
4
  librosa
5
  numpy
6
- matplotlib
7
- python-multipart
8
-
9
-
 
1
  fastapi
2
  uvicorn
 
3
  librosa
4
  numpy
5
+ pydantic
6
+ soundfile
7
+ pydub
8
+ ffmpeg-python