tuan243 commited on
Commit
9d49358
·
verified ·
1 Parent(s): d5e6c24

Update app.py

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