Spaces:
Sleeping
Sleeping
Commit
·
4143e48
1
Parent(s):
cec245f
app.py
CHANGED
@@ -8,6 +8,7 @@ import os
|
|
8 |
import warnings
|
9 |
from pydub import AudioSegment
|
10 |
from datetime import datetime
|
|
|
11 |
|
12 |
warnings.filterwarnings("ignore")
|
13 |
|
@@ -72,8 +73,8 @@ class StressResponse(BaseModel):
|
|
72 |
stress_level: float
|
73 |
category: str
|
74 |
gender: str = None # Optional, only for audio analysis
|
75 |
-
|
76 |
-
|
77 |
size: str
|
78 |
|
79 |
@app.post("/analyze-stress/", response_model=StressResponse)
|
@@ -85,6 +86,8 @@ async def analyze_stress(
|
|
85 |
if file is None and file_path is None and text is None:
|
86 |
raise HTTPException(status_code=400, detail="Either a file, file path, or text input is required.")
|
87 |
|
|
|
|
|
88 |
# Handle audio file analysis
|
89 |
if file or file_path:
|
90 |
if file:
|
@@ -108,12 +111,13 @@ async def analyze_stress(
|
|
108 |
|
109 |
try:
|
110 |
result = analyze_voice_stress(temp_audio_path)
|
|
|
111 |
result.update({
|
112 |
-
"
|
113 |
-
"
|
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))
|
119 |
finally:
|
@@ -124,12 +128,13 @@ async def analyze_stress(
|
|
124 |
# Handle text analysis
|
125 |
elif text:
|
126 |
result = analyze_text_stress(text)
|
|
|
127 |
result.update({
|
128 |
-
"
|
129 |
-
"
|
130 |
"size": "N/A" # Size is not applicable for text input
|
131 |
})
|
132 |
-
return JSONResponse(content=result)
|
133 |
|
134 |
if __name__ == "__main__":
|
135 |
import uvicorn
|
|
|
8 |
import warnings
|
9 |
from pydub import AudioSegment
|
10 |
from datetime import datetime
|
11 |
+
import time
|
12 |
|
13 |
warnings.filterwarnings("ignore")
|
14 |
|
|
|
73 |
stress_level: float
|
74 |
category: str
|
75 |
gender: str = None # Optional, only for audio analysis
|
76 |
+
status_code: int
|
77 |
+
processing_time_ms: int
|
78 |
size: str
|
79 |
|
80 |
@app.post("/analyze-stress/", response_model=StressResponse)
|
|
|
86 |
if file is None and file_path is None and text is None:
|
87 |
raise HTTPException(status_code=400, detail="Either a file, file path, or text input is required.")
|
88 |
|
89 |
+
start_time = time.time()
|
90 |
+
|
91 |
# Handle audio file analysis
|
92 |
if file or file_path:
|
93 |
if file:
|
|
|
111 |
|
112 |
try:
|
113 |
result = analyze_voice_stress(temp_audio_path)
|
114 |
+
processing_time_ms = int((time.time() - start_time) * 1000) # Calculate time in ms
|
115 |
result.update({
|
116 |
+
"status_code": 200,
|
117 |
+
"processing_time_ms": processing_time_ms,
|
118 |
"size": f"{round(file_size / 1024, 2)} KB" # Convert size to KB and round to 2 decimal places
|
119 |
})
|
120 |
+
return JSONResponse(content=result, status_code=200)
|
121 |
except Exception as e:
|
122 |
raise HTTPException(status_code=500, detail=str(e))
|
123 |
finally:
|
|
|
128 |
# Handle text analysis
|
129 |
elif text:
|
130 |
result = analyze_text_stress(text)
|
131 |
+
processing_time_ms = int((time.time() - start_time) * 1000) # Calculate time in ms
|
132 |
result.update({
|
133 |
+
"status_code": 200,
|
134 |
+
"processing_time_ms": processing_time_ms,
|
135 |
"size": "N/A" # Size is not applicable for text input
|
136 |
})
|
137 |
+
return JSONResponse(content=result, status_code=200)
|
138 |
|
139 |
if __name__ == "__main__":
|
140 |
import uvicorn
|