tuan243 commited on
Commit
e544543
·
verified ·
1 Parent(s): fa8db30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -49
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
- # # Load mô hình SenseVoiceSmall
24
- # model_dir = "FunAudioLLM/SenseVoiceSmall"
25
-
26
-
27
- # model = AutoModel(
28
- # model=model_dir,
29
- # vad_model="fsmn-vad",
30
- # vad_kwargs={"max_single_segment_time": 30000},
31
- # device="cuda:0",
32
- # hub="hf",
33
- # )
34
-
35
- # # Hàm tính RMS energy
36
- # def calculate_rms_energy(audio_path):
37
- # y, sr = librosa.load(audio_path)
38
- # rms = librosa.feature.rms(y=y)[0]
39
- # return np.mean(rms)
40
-
41
- # # Hàm phát hiện tiếng ồn
42
- # def detect_noise(audio_path):
43
- # rms_energy = calculate_rms_energy(audio_path)
44
- # res = model.generate(input=audio_path, language="auto", audio_event_detection=True)
45
- # audio_events = res[0].get("audio_event_detection", {})
46
-
47
- # if rms_energy > 0.02:
48
- # return "ồn ào"
49
- # elif rms_energy > 0.01:
50
- # for event_label, event_score in audio_events.items():
51
- # if event_score > 0.7 and event_label in ["laughter", "applause", "crying", "coughing"]:
52
- # return f"ồn ào ({event_label})"
53
- # return "yên tĩnh"
54
-
55
- # # API nhận file âm thanh từ Flutter
56
- # @app.post("/detect-noise/")
57
- # async def detect_noise_api(file: UploadFile = File(...)):
58
- # file_path = f"temp/{file.filename}"
59
- # with open(file_path, "wb") as buffer:
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}