Spaces:
Sleeping
Sleeping
Commit ·
cec245f
1
Parent(s): 03548eb
app.py
CHANGED
|
@@ -7,6 +7,7 @@ import tempfile
|
|
| 7 |
import os
|
| 8 |
import warnings
|
| 9 |
from pydub import AudioSegment
|
|
|
|
| 10 |
|
| 11 |
warnings.filterwarnings("ignore")
|
| 12 |
|
|
@@ -52,7 +53,7 @@ def analyze_voice_stress(audio_file_path):
|
|
| 52 |
z_energy = (mean_energy - norm_mean_energy) / norm_std_energy
|
| 53 |
z_speech_rate = (speech_rate - norm_speech_rate) / norm_std_speech_rate
|
| 54 |
stress_score = (0.4 * z_f0) + (0.4 * z_speech_rate) + (0.2 * z_energy)
|
| 55 |
-
stress_level = float(1 / (1 + np.exp(-stress_score)) * 100)
|
| 56 |
categories = ["Very Low Stress", "Low Stress", "Moderate Stress", "High Stress", "Very High Stress"]
|
| 57 |
category_idx = min(int(stress_level / 20), 4)
|
| 58 |
stress_category = categories[category_idx]
|
|
@@ -71,6 +72,9 @@ class StressResponse(BaseModel):
|
|
| 71 |
stress_level: float
|
| 72 |
category: str
|
| 73 |
gender: str = None # Optional, only for audio analysis
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
@app.post("/analyze-stress/", response_model=StressResponse)
|
| 76 |
async def analyze_stress(
|
|
@@ -89,12 +93,14 @@ async def analyze_stress(
|
|
| 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"):
|
|
@@ -102,6 +108,11 @@ async def analyze_stress(
|
|
| 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))
|
|
@@ -113,6 +124,11 @@ async def analyze_stress(
|
|
| 113 |
# Handle text analysis
|
| 114 |
elif text:
|
| 115 |
result = analyze_text_stress(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
return JSONResponse(content=result)
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
|
|
|
| 7 |
import os
|
| 8 |
import warnings
|
| 9 |
from pydub import AudioSegment
|
| 10 |
+
from datetime import datetime
|
| 11 |
|
| 12 |
warnings.filterwarnings("ignore")
|
| 13 |
|
|
|
|
| 53 |
z_energy = (mean_energy - norm_mean_energy) / norm_std_energy
|
| 54 |
z_speech_rate = (speech_rate - norm_speech_rate) / norm_std_speech_rate
|
| 55 |
stress_score = (0.4 * z_f0) + (0.4 * z_speech_rate) + (0.2 * z_energy)
|
| 56 |
+
stress_level = round(float(1 / (1 + np.exp(-stress_score)) * 100), 2) # Rounded to 2 decimal places
|
| 57 |
categories = ["Very Low Stress", "Low Stress", "Moderate Stress", "High Stress", "Very High Stress"]
|
| 58 |
category_idx = min(int(stress_level / 20), 4)
|
| 59 |
stress_category = categories[category_idx]
|
|
|
|
| 72 |
stress_level: float
|
| 73 |
category: str
|
| 74 |
gender: str = None # Optional, only for audio analysis
|
| 75 |
+
status: str
|
| 76 |
+
time: str
|
| 77 |
+
size: str
|
| 78 |
|
| 79 |
@app.post("/analyze-stress/", response_model=StressResponse)
|
| 80 |
async def analyze_stress(
|
|
|
|
| 93 |
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[-1]) as temp_file:
|
| 94 |
temp_file.write(await file.read())
|
| 95 |
temp_audio_path = temp_file.name
|
| 96 |
+
file_size = os.path.getsize(temp_audio_path)
|
| 97 |
else:
|
| 98 |
if not (file_path.endswith(".wav") or file_path.endswith(".mp3")):
|
| 99 |
raise HTTPException(status_code=400, detail="Only .wav and .mp3 files are supported.")
|
| 100 |
if not os.path.exists(file_path):
|
| 101 |
raise HTTPException(status_code=400, detail="File path does not exist.")
|
| 102 |
temp_audio_path = file_path
|
| 103 |
+
file_size = os.path.getsize(file_path)
|
| 104 |
|
| 105 |
# Convert MP3 to WAV if needed
|
| 106 |
if temp_audio_path.endswith(".mp3"):
|
|
|
|
| 108 |
|
| 109 |
try:
|
| 110 |
result = analyze_voice_stress(temp_audio_path)
|
| 111 |
+
result.update({
|
| 112 |
+
"status": "Success",
|
| 113 |
+
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 114 |
+
"size": f"{round(file_size / 1024, 2)} KB" # Convert size to KB and round to 2 decimal places
|
| 115 |
+
})
|
| 116 |
return JSONResponse(content=result)
|
| 117 |
except Exception as e:
|
| 118 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 124 |
# Handle text analysis
|
| 125 |
elif text:
|
| 126 |
result = analyze_text_stress(text)
|
| 127 |
+
result.update({
|
| 128 |
+
"status": "Success",
|
| 129 |
+
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 130 |
+
"size": "N/A" # Size is not applicable for text input
|
| 131 |
+
})
|
| 132 |
return JSONResponse(content=result)
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|