HK0712 commited on
Commit
c179039
·
1 Parent(s): b0b3dfc

enabled gpu

Browse files
.devcontainer/devcontainer.json CHANGED
@@ -1,26 +1,32 @@
1
- // For format details, see https://aka.ms/devcontainer.json. For config options, see the
2
- // README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
3
  {
4
- "name": "Existing Dockerfile",
5
- "build": {
6
- // Sets the run context to one level up instead of the .devcontainer folder.
7
- "context": "..",
8
- // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
9
- "dockerfile": "../Dockerfile"
10
- }
11
 
12
- // Features to add to the dev container. More info: https://containers.dev/features.
13
- // "features": {},
 
 
14
 
15
- // Use 'forwardPorts' to make a list of ports inside the container available locally.
16
- // "forwardPorts": [],
17
 
18
- // Uncomment the next line to run commands after the container is created.
19
- // "postCreateCommand": "cat /etc/os-release",
 
20
 
21
- // Configure tool-specific properties.
22
- // "customizations": {},
23
 
24
- // Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
25
- // "remoteUser": "devcontainer"
 
 
 
 
 
 
 
 
 
 
26
  }
 
 
 
1
  {
2
+ "name": "FYP Backend (GPU)",
3
+ "image": "e226274b3239", // 直接使用您已有的鏡像 ID
 
 
 
 
 
4
 
5
+ // 這是最最最關鍵的部分!
6
+ "runArgs": [
7
+ "--gpus=all"
8
+ ],
9
 
10
+ // 轉發端口,以便您可以訪問 FastAPI
11
+ "forwardPorts": [8000],
12
 
13
+ // 將工作區掛載到容器中
14
+ "workspaceMount": "source=${localWorkspaceFolder},target=/workspaces/FYP-Backend,type=bind,consistency=cached",
15
+ "workspaceFolder": "/workspaces/FYP-Backend",
16
 
17
+ // 讓容器在 VS Code 關閉後保持運行
18
+ "shutdownAction": "none",
19
 
20
+ // 在容器創建後運行的命令 (可選,但推薦)
21
+ "postCreateCommand": "pip install -r requirements.txt",
22
+
23
+ // VS Code 擴展推薦 (可選)
24
+ "customizations": {
25
+ "vscode": {
26
+ "extensions": [
27
+ "ms-python.python",
28
+ "ms-python.vscode-pylance"
29
+ ]
30
+ }
31
+ }
32
  }
analyzer/ASR_en_us.py CHANGED
@@ -7,6 +7,10 @@ from phonemizer import phonemize
7
  import numpy as np
8
  from datetime import datetime, timezone
9
 
 
 
 
 
10
  # --- 1. 全域設定與模型載入函數 (保持不變) ---
11
  MODEL_NAME = "MultiBridge/wav2vec-LnNor-IPA-ft"
12
  MODEL_SAVE_PATH = "./ASRs/MultiBridge-wav2vec-LnNor-IPA-ft-local"
@@ -38,6 +42,7 @@ def load_model():
38
 
39
  processor = Wav2Vec2Processor.from_pretrained(MODEL_SAVE_PATH)
40
  model = Wav2Vec2ForCTC.from_pretrained(MODEL_SAVE_PATH)
 
41
  print("英文 (en-us) 模型和處理器載入成功!")
42
  return True
43
  except Exception as e:
@@ -95,6 +100,7 @@ def analyze(audio_file_path: str, target_sentence: str) -> dict:
95
  raise IOError(f"讀取或處理音訊時發生錯誤: {e}")
96
 
97
  input_values = processor(speech, sampling_rate=16000, return_tensors="pt").input_values
 
98
  with torch.no_grad():
99
  logits = model(input_values).logits
100
  predicted_ids = torch.argmax(logits, dim=-1)
 
7
  import numpy as np
8
  from datetime import datetime, timezone
9
 
10
+ # 【【【【【 新增程式碼 #1:自動檢測可用設備 】】】】】
11
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
12
+ print(f"INFO: ASR_fr_fr.py is configured to use device: {DEVICE}")
13
+
14
  # --- 1. 全域設定與模型載入函數 (保持不變) ---
15
  MODEL_NAME = "MultiBridge/wav2vec-LnNor-IPA-ft"
16
  MODEL_SAVE_PATH = "./ASRs/MultiBridge-wav2vec-LnNor-IPA-ft-local"
 
42
 
43
  processor = Wav2Vec2Processor.from_pretrained(MODEL_SAVE_PATH)
44
  model = Wav2Vec2ForCTC.from_pretrained(MODEL_SAVE_PATH)
45
+ model.to(DEVICE) # 將模型移動到檢測到的設備上
46
  print("英文 (en-us) 模型和處理器載入成功!")
47
  return True
48
  except Exception as e:
 
100
  raise IOError(f"讀取或處理音訊時發生錯誤: {e}")
101
 
102
  input_values = processor(speech, sampling_rate=16000, return_tensors="pt").input_values
103
+ input_values = input_values.to(DEVICE)
104
  with torch.no_grad():
105
  logits = model(input_values).logits
106
  predicted_ids = torch.argmax(logits, dim=-1)
analyzer/ASR_fr_fr.py CHANGED
@@ -10,6 +10,10 @@ import unicodedata
10
  import re
11
  import epitran
12
 
 
 
 
 
13
  # --- 1. 全域設定與模型載入函數 (已修改為法語模型) ---
14
  MODEL_NAME = "Cnam-LMSSC/wav2vec2-french-phonemizer"
15
  MODEL_SAVE_PATH = "./ASRs/Cnam-LMSSC-wav2vec2-french-phonemizer-local"
@@ -41,6 +45,7 @@ def load_model():
41
 
42
  processor = Wav2Vec2Processor.from_pretrained(MODEL_SAVE_PATH)
43
  model = Wav2Vec2ForCTC.from_pretrained(MODEL_SAVE_PATH)
 
44
  print("法語 (fr-fr) 模型和處理器載入成功!")
45
  return True
46
  except Exception as e:
@@ -106,6 +111,7 @@ def analyze(audio_file_path: str, target_sentence: str) -> dict:
106
  raise IOError(f"讀取或處理音訊時發生錯誤: {e}")
107
 
108
  input_values = processor(speech, sampling_rate=16000, return_tensors="pt").input_values
 
109
  with torch.no_grad():
110
  logits = model(input_values).logits
111
  predicted_ids = torch.argmax(logits, dim=-1)
 
10
  import re
11
  import epitran
12
 
13
+ # 【【【【【 新增程式碼 #1:自動檢測可用設備 】】】】】
14
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
15
+ print(f"INFO: ASR_fr_fr.py is configured to use device: {DEVICE}")
16
+
17
  # --- 1. 全域設定與模型載入函數 (已修改為法語模型) ---
18
  MODEL_NAME = "Cnam-LMSSC/wav2vec2-french-phonemizer"
19
  MODEL_SAVE_PATH = "./ASRs/Cnam-LMSSC-wav2vec2-french-phonemizer-local"
 
45
 
46
  processor = Wav2Vec2Processor.from_pretrained(MODEL_SAVE_PATH)
47
  model = Wav2Vec2ForCTC.from_pretrained(MODEL_SAVE_PATH)
48
+ model.to(DEVICE) # 將模型移動到檢測到的設備上
49
  print("法語 (fr-fr) 模型和處理器載入成功!")
50
  return True
51
  except Exception as e:
 
111
  raise IOError(f"讀取或處理音訊時發生錯誤: {e}")
112
 
113
  input_values = processor(speech, sampling_rate=16000, return_tensors="pt").input_values
114
+ input_values = input_values.to(DEVICE)
115
  with torch.no_grad():
116
  logits = model(input_values).logits
117
  predicted_ids = torch.argmax(logits, dim=-1)