Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -8,56 +8,47 @@ from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
|
8 |
from funasr.auto.auto_model import AutoModel
|
9 |
import os
|
10 |
|
11 |
-
model_dir = "FunAudioLLM/SenseVoiceSmall"
|
12 |
-
try:
|
13 |
-
model = AutoModel(model=model_dir, vad_model="fsmn-vad", device="cpu", hub="hf")
|
14 |
-
print("✅ Model loaded successfully!")
|
15 |
-
except Exception as e:
|
16 |
-
print("❌ Model loading error:", str(e))
|
17 |
-
|
18 |
|
|
|
19 |
|
|
|
|
|
20 |
|
21 |
-
app = FastAPI()
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
#
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
# shutil.copyfileobj(file.file, buffer)
|
61 |
-
|
62 |
-
# result = detect_noise(file_path)
|
63 |
-
# return {"noise_level": result}
|
|
|
8 |
from funasr.auto.auto_model import AutoModel
|
9 |
import os
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
app = FastAPI()
|
13 |
|
14 |
+
# Load mô hình SenseVoiceSmall
|
15 |
+
model_dir = "FunAudioLLM/SenseVoiceSmall"
|
16 |
|
|
|
17 |
|
18 |
+
model = AutoModel(
|
19 |
+
model=model_dir,
|
20 |
+
vad_model="fsmn-vad",
|
21 |
+
vad_kwargs={"max_single_segment_time": 30000},
|
22 |
+
device="cuda:0",
|
23 |
+
hub="hf",
|
24 |
+
)
|
25 |
+
|
26 |
+
# Hàm tính RMS energy
|
27 |
+
def calculate_rms_energy(audio_path):
|
28 |
+
y, sr = librosa.load(audio_path)
|
29 |
+
rms = librosa.feature.rms(y=y)[0]
|
30 |
+
return np.mean(rms)
|
31 |
+
|
32 |
+
# Hàm phát hiện tiếng ồn
|
33 |
+
def detect_noise(audio_path):
|
34 |
+
rms_energy = calculate_rms_energy(audio_path)
|
35 |
+
res = model.generate(input=audio_path, language="auto", audio_event_detection=True)
|
36 |
+
audio_events = res[0].get("audio_event_detection", {})
|
37 |
+
|
38 |
+
if rms_energy > 0.02:
|
39 |
+
return "ồn ào"
|
40 |
+
elif rms_energy > 0.01:
|
41 |
+
for event_label, event_score in audio_events.items():
|
42 |
+
if event_score > 0.7 and event_label in ["laughter", "applause", "crying", "coughing"]:
|
43 |
+
return f"ồn ào ({event_label})"
|
44 |
+
return "yên tĩnh"
|
45 |
+
|
46 |
+
# API nhận file âm thanh từ Flutter
|
47 |
+
@app.post("/detect-noise/")
|
48 |
+
async def detect_noise_api(file: UploadFile = File(...)):
|
49 |
+
file_path = f"temp/{file.filename}"
|
50 |
+
with open(file_path, "wb") as buffer:
|
51 |
+
shutil.copyfileobj(file.file, buffer)
|
52 |
+
|
53 |
+
result = detect_noise(file_path)
|
54 |
+
return {"noise_level": result}
|
|
|
|
|
|
|
|