MaryWambo commited on
Commit
cbf450b
·
verified ·
1 Parent(s): 01bf76e

Upload 2 files

Browse files
Files changed (2) hide show
  1. ASR_deployment.py +121 -0
  2. requirements.txt +7 -0
ASR_deployment.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """ASR_Deployment.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1MmePYOn1Ho2FhILi00u9UbvsujEoHhot
8
+ """
9
+
10
+ import gradio as gr
11
+ from transformers import WhisperForConditionalGeneration, WhisperProcessor, GenerationConfig
12
+ import torch
13
+ import librosa
14
+ import os
15
+
16
+ # --- 1. CONFIGURATION ---
17
+ # Note: Ensure your token has "Read" access to the repository
18
+ MODEL_PATH = "MaryWambo/whisper-base-kikuyu4"
19
+ device = "cuda" if torch.cuda.is_available() else "cpu"
20
+
21
+ # --- 2. LOAD MODEL & PROCESSOR ---
22
+ print(f"Loading model to {device}...")
23
+ try:
24
+ processor = WhisperProcessor.from_pretrained(MODEL_PATH)
25
+ model = WhisperForConditionalGeneration.from_pretrained(MODEL_PATH).to(device)
26
+
27
+ # Define Generation Config to avoid "outdated" errors
28
+ # We set language and task here so they don't conflict in the generate() call
29
+ gen_config = GenerationConfig.from_pretrained(MODEL_PATH)
30
+ gen_config.language = "swahili" # Using full name or "sw" depending on how it was trained
31
+ gen_config.task = "transcribe"
32
+ gen_config.forced_decoder_ids = None
33
+ gen_config.suppress_tokens = []
34
+
35
+ model.generation_config = gen_config
36
+
37
+ except Exception as e:
38
+ print(f"Error loading model: {e}")
39
+
40
+ # --- 3. CUSTOM CSS ---
41
+ custom_css = """
42
+ body, .gradio-container { background-color: white !important; }
43
+ #title-text h1 { color: #8b0000 !important; font-weight: 900 !important; text-align: center; }
44
+ .upload-button svg, .mic-button svg, .clear-button svg, .record-button svg {
45
+ transform: scale(1.5) !important;
46
+ color: #8b0000 !important;
47
+ }
48
+ #predict-box textarea {
49
+ font-size: 1.6rem !important;
50
+ font-weight: 800 !important;
51
+ color: #000000 !important;
52
+ border: 3px solid #8b0000 !important;
53
+ }
54
+ #run-btn {
55
+ background: #8b0000 !important;
56
+ color: white !important;
57
+ font-weight: bold !important;
58
+ font-size: 1.4rem !important;
59
+ }
60
+ """
61
+
62
+ # --- 4. LOGIC FUNCTIONS ---
63
+ def transcribe_kikuyu(audio):
64
+ if audio is None:
65
+ return "Please record or upload audio."
66
+
67
+ try:
68
+ # Load audio and resample to 16kHz (standard for Whisper)
69
+ speech_array, sampling_rate = librosa.load(audio, sr=16000)
70
+
71
+ # Process audio features
72
+ inputs = processor(speech_array, sampling_rate=sampling_rate, return_tensors="pt")
73
+ input_features = inputs.input_features.to(device)
74
+
75
+ with torch.no_grad():
76
+ # We no longer pass 'language' or 'task' here because
77
+ # they are already defined in model.generation_config
78
+ generated_ids = model.generate(
79
+ input_features=input_features,
80
+ num_beams=5,
81
+ max_new_tokens=255
82
+ )
83
+
84
+ # Decode the predicted IDs to text
85
+ prediction = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
86
+ return prediction
87
+
88
+ except Exception as e:
89
+ return f"Error during transcription: {str(e)}"
90
+
91
+ # --- 5. BUILD GRADIO UI ---
92
+ with gr.Blocks(theme=gr.themes.Default(), css=custom_css) as demo:
93
+ gr.Markdown("# 🎙️ Kikuyu ASR ", elem_id="title-text")
94
+
95
+ with gr.Row():
96
+ with gr.Column(scale=1):
97
+ audio_input = gr.Audio(
98
+ sources=["microphone", "upload"],
99
+ type="filepath",
100
+ label="🎤 Record/Upload Kikuyu Speech"
101
+ )
102
+ submit_btn = gr.Button("🚀 RUN TRANSCRIPTION", elem_id="run-btn")
103
+
104
+ with gr.Column(scale=1):
105
+ text_out = gr.Textbox(
106
+ label="🤖 AI Prediction",
107
+ elem_id="predict-box",
108
+ lines=8
109
+ )
110
+
111
+ submit_btn.click(
112
+ fn=transcribe_kikuyu,
113
+ inputs=[audio_input],
114
+ outputs=[text_out]
115
+ )
116
+
117
+ # --- 6. LAUNCH ---
118
+ if __name__ == "__main__":
119
+ # share=True creates a public URL valid for 72 hours
120
+ demo.launch(share=True, debug=True)
121
+
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ transformers
3
+ torch
4
+ librosa
5
+ datasets
6
+ accelerate
7
+ soundfile