File size: 6,771 Bytes
b5e57ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# import sounddevice as sd
# import scipy.io.wavfile as wav
# import nemo.collections.asr as nemo_asr

# # ===== SETTINGS =====
# SAMPLE_RATE = 16000
# DURATION = 10  # seconds
# OUTPUT_FILE = "arabic_recording.wav"

# # ===== STEP 1: Record audio =====
# print("🎙️ Recording... Speak Arabic now!")
# audio = sd.rec(int(SAMPLE_RATE * DURATION), samplerate=SAMPLE_RATE, channels=1, dtype='int16')
# sd.wait()
# wav.write(OUTPUT_FILE, SAMPLE_RATE, audio)
# print(f"✅ Recording finished. Saved as {OUTPUT_FILE}")

# # ===== STEP 2: Load ASR model =====
# print("📥 Loading Arabic ASR model...")
# asr_model = nemo_asr.models.EncDecRNNTBPEModel.restore_from(
#     "C:/Users/thegh/Python_Projects/Expertflow/UnderProgress/Arabic_Contextual_ASR/PreparingDatasetStreamlitApp/4_Finetuning_Nemo_ASR_arabic_names_and_complaints_for_phones/output_finetuned/finetuned_model_best.nemo"
# )

# # ===== STEP 3: Configure Decoding =====
# print("🔍 Configuring decoding strategy...")

# # Get decoding config
# decoding_cfg = asr_model.cfg.decoding

# # Print available parameters to debug
# print(f"Available decoding strategies: {decoding_cfg.keys() if hasattr(decoding_cfg, 'keys') else 'N/A'}")
# # After loading the model, add this to inspect the config:
# print("🔍 Beam config structure:")
# print(decoding_cfg.beam)
# # Set beam search strategy
# decoding_cfg.strategy = "beam"
# decoding_cfg.beam.beam_size = 128
# decoding_cfg.beam.return_best_hypothesis = True

# # Only set parameters that exist
# if hasattr(decoding_cfg.beam, 'beam_alpha'):
#     decoding_cfg.beam.beam_alpha = 0.3
#     print("✓ Set beam_alpha")

# if hasattr(decoding_cfg.beam, 'beam_beta'):
#     decoding_cfg.beam.beam_beta = 0.5
#     print("✓ Set beam_beta")

# # Remove softmax_temperature - it's not supported in this config
# # If you need temperature sampling, you might need to use a different strategy

# # Apply the decoding configuration
# asr_model.change_decoding_strategy(decoding_cfg)

# # ===== STEP 4: Transcribe =====
# print("🔍 Transcribing...")
# transcription = asr_model.transcribe(
#     [OUTPUT_FILE],
#     batch_size=1,
#     num_workers=0
# )

# print("📝 Transcription:", transcription[0])



# import sounddevice as sd
# import scipy.io.wavfile as wav
# import nemo.collections.asr as nemo_asr

# # ===== SETTINGS =====
# SAMPLE_RATE = 16000
# DURATION = 10
# OUTPUT_FILE = "arabic_recording.wav"

# # ===== STEP 1: Record audio =====
# print("🎙️ Recording... Speak Arabic now!")
# audio = sd.rec(int(SAMPLE_RATE * DURATION), samplerate=SAMPLE_RATE, channels=1, dtype='int16')
# sd.wait()
# wav.write(OUTPUT_FILE, SAMPLE_RATE, audio)
# print(f"✅ Recording finished. Saved as {OUTPUT_FILE}")

# # ===== STEP 2: Load ASR model =====
# print("📥 Loading Arabic ASR model...")
# asr_model = nemo_asr.models.EncDecRNNTBPEModel.restore_from(
#     "C:/Users/thegh/Python_Projects/Expertflow/UnderProgress/Arabic_Contextual_ASR/PreparingDatasetStreamlitApp/4_Finetuning_Nemo_ASR_arabic_names_and_complaints_for_phones/output_finetuned/finetuned_model_best.nemo"
# )

# # ===== STEP 3: Configure for LITERAL transcription =====
# print("🔍 Configuring greedy decoding for literal output...")

# decoding_cfg = asr_model.cfg.decoding
# decoding_cfg.strategy = "greedy"

# # CRITICAL: Increase max_symbols to avoid truncating repetitions
# # The default is only 10, which is very restrictive!
# decoding_cfg.greedy.max_symbols = 1000  # Allow much longer sequences
# decoding_cfg.beam.beam_size = 64
# decoding_cfg.beam.search_type = "beam"
# print(f"✓ Set max_symbols to {decoding_cfg.greedy.max_symbols}")
# print("Updated config:", decoding_cfg)

# # Apply configuration
# asr_model.change_decoding_strategy(decoding_cfg)

# # ===== STEP 4: Transcribe =====
# print("🔍 Transcribing...")
# transcription = asr_model.transcribe(
#     [OUTPUT_FILE],
#     batch_size=1,
#     num_workers=0
# )

# print("📝 Literal Transcription:", transcription[0])


import sounddevice as sd
import scipy.io.wavfile as wav
import nemo.collections.asr as nemo_asr
from omegaconf import OmegaConf

# ===== SETTINGS =====
SAMPLE_RATE = 16000
DURATION = 10
OUTPUT_FILE = "arabic_recording.wav"
# ===== STEP 2: Load ASR model =====
print("📥 Loading Arabic ASR model...")
asr_model = nemo_asr.models.EncDecHybridRNNTCTCModel.restore_from(
    "C:/Users/thegh/Python_Projects/Expertflow/UnderProgress/Arabic_Contextual_ASR/PreparingDatasetStreamlitApp/4_Finetuning_Nemo_ASR_arabic_names_and_complaints_for_phones/output_finetuned/finetuned_model_best.nemo"
)


# Add this right after loading the model to see what's actually available:
print("Available greedy parameters:")
print(OmegaConf.to_yaml(asr_model.cfg.decoding.greedy))

# ===== STEP 3: Configure for LITERAL transcription =====
print("🔍 Configuring greedy decoding for literal output...")

# Set struct mode to False temporarily to allow modifications
OmegaConf.set_struct(asr_model.cfg.decoding, False)
OmegaConf.set_struct(asr_model.cfg.decoding.greedy, False)

decoding_cfg = asr_model.cfg.decoding
decoding_cfg.strategy = "maes"

# Now try setting the parameters
try:
    decoding_cfg.greedy.max_symbols_per_step = 300
    print(f"✓ max_symbols_per_step: {decoding_cfg.greedy.max_symbols_per_step}")
except:
    print("⚠ Could not set max_symbols_per_step")

decoding_cfg.greedy.max_symbols = 500
decoding_cfg.greedy.loop_labels = True
decoding_cfg.greedy.preserve_alignments = True
decoding_cfg.preserve_alignments = True
decoding_cfg.compute_timestamps = True
decoding_cfg.temperature = 1.3

decoding_cfg.beam.beam_size = 64
decoding_cfg.beam.softmax_temperature = 1.3
decoding_cfg.beam.search_type = "beam"
print(f"✓ max_symbols: {decoding_cfg.greedy.max_symbols}")
print(f"✓ loop_labels: {decoding_cfg.greedy.loop_labels}")
print(f"✓ temperature: {decoding_cfg.temperature}")

# Re-enable struct mode
OmegaConf.set_struct(asr_model.cfg.decoding, True)
OmegaConf.set_struct(asr_model.cfg.decoding.greedy, True)

# Apply configuration
asr_model.change_decoding_strategy(decoding_cfg)


# ===== STEP 1: Record audio =====
print("🎙️ Recording... Speak Arabic now!")
audio = sd.rec(int(SAMPLE_RATE * DURATION), samplerate=SAMPLE_RATE, channels=1, dtype='int16')
sd.wait()
wav.write(OUTPUT_FILE, SAMPLE_RATE, audio)
print(f"✅ Recording finished. Saved as {OUTPUT_FILE}")



# ===== STEP 4: Transcribe =====
print("🔍 Transcribing...")
transcription = asr_model.transcribe(
    [OUTPUT_FILE],
    batch_size=1,
    num_workers=0
)

print("📝 Literal Transcription:", transcription[0])