AbdulQadir96 commited on
Commit
fe04930
·
verified ·
1 Parent(s): f6c9def

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -23
app.py CHANGED
@@ -1,8 +1,8 @@
1
  import streamlit as st
2
  from transformers import pipeline
3
  import torch
4
- import librosa
5
  import tempfile
 
6
  import math
7
 
8
  st.set_page_config(page_title="Urdu Speech-to-Text", layout="centered")
@@ -14,13 +14,13 @@ st.title("🎙️ Urdu Speech-to-Text (Whisper Turbo Urdu)")
14
  def get_device():
15
  if torch.cuda.is_available():
16
  st.success("GPU active ✓ (Fast Mode)")
17
- return 0 # GPU
18
  else:
19
- st.warning("GPU not available – switching to CPU (Slower but still works)")
20
- return -1 # CPU fallback
21
 
22
  # --------------------------
23
- # Load pipeline only once
24
  # --------------------------
25
  @st.cache_resource
26
  def load_asr(device):
@@ -35,42 +35,47 @@ def load_asr(device):
35
  )
36
 
37
  # --------------------------
38
- # Transcribe in chunks with progress bar
39
  # --------------------------
40
  def transcribe_in_chunks(asr, audio_path):
41
- y, sr = librosa.load(audio_path, sr=16000)
42
- duration = librosa.get_duration(y=y, sr=sr)
43
 
44
- chunk_sec = 30
45
- total_chunks = math.ceil(duration / chunk_sec)
 
46
 
47
- st.info(f"⏳ Estimated time: {total_chunks} chunks")
48
 
49
  progress = st.progress(0)
50
- full_transcript = ""
51
 
52
  for i in range(total_chunks):
53
- start = i * chunk_sec
54
- end = min((i + 1) * chunk_sec, duration)
55
 
56
- y_chunk = y[int(start * sr): int(end * sr)]
57
 
58
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
59
- librosa.output.write_wav(tmp.name, y_chunk, sr)
60
  chunk_path = tmp.name
61
 
62
- result = asr(chunk_path, return_timestamps=True)
 
 
 
 
 
63
 
64
- full_transcript += result["text"] + " "
65
 
66
  progress.progress((i + 1) / total_chunks)
67
 
68
- return full_transcript.strip()
69
 
70
  # --------------------------
71
- # MAIN UI
72
  # --------------------------
73
- uploaded_file = st.file_uploader("Upload audio", type=["mp3", "wav", "m4a", "ogg"])
74
 
75
  if uploaded_file:
76
  with tempfile.NamedTemporaryFile(delete=False, suffix=uploaded_file.name) as tmp:
@@ -79,11 +84,11 @@ if uploaded_file:
79
 
80
  st.success("✔ Audio uploaded")
81
 
82
- # Choose device
83
  device = get_device()
84
  asr = load_asr(device)
85
 
86
- st.info("Processing… please wait")
 
87
  transcript = transcribe_in_chunks(asr, audio_path)
88
 
89
  st.subheader("📝 Urdu Transcription")
 
1
  import streamlit as st
2
  from transformers import pipeline
3
  import torch
 
4
  import tempfile
5
+ from pydub import AudioSegment
6
  import math
7
 
8
  st.set_page_config(page_title="Urdu Speech-to-Text", layout="centered")
 
14
  def get_device():
15
  if torch.cuda.is_available():
16
  st.success("GPU active ✓ (Fast Mode)")
17
+ return 0
18
  else:
19
+ st.warning("GPU not available – switching to CPU (slow mode)")
20
+ return -1
21
 
22
  # --------------------------
23
+ # Load ASR Model
24
  # --------------------------
25
  @st.cache_resource
26
  def load_asr(device):
 
35
  )
36
 
37
  # --------------------------
38
+ # Chunk Transcription
39
  # --------------------------
40
  def transcribe_in_chunks(asr, audio_path):
41
+ audio = AudioSegment.from_file(audio_path)
 
42
 
43
+ total_ms = len(audio) # audio duration in milliseconds
44
+ chunk_ms = 30 * 1000 # 30 sec chunks
45
+ total_chunks = math.ceil(total_ms / chunk_ms)
46
 
47
+ st.info(f"⏳ Estimated chunks: {total_chunks}")
48
 
49
  progress = st.progress(0)
50
+ full_text = ""
51
 
52
  for i in range(total_chunks):
53
+ start = i * chunk_ms
54
+ end = min((i+1) * chunk_ms, total_ms)
55
 
56
+ chunk = audio[start:end]
57
 
58
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
59
+ chunk.export(tmp.name, format="wav")
60
  chunk_path = tmp.name
61
 
62
+ result = asr(
63
+ chunk_path,
64
+ return_timestamps=True,
65
+ chunk_length_s=30,
66
+ stride_length_s=5
67
+ )
68
 
69
+ full_text += result["text"] + " "
70
 
71
  progress.progress((i + 1) / total_chunks)
72
 
73
+ return full_text.strip()
74
 
75
  # --------------------------
76
+ # APP UI
77
  # --------------------------
78
+ uploaded_file = st.file_uploader("Upload audio", type=["mp3","wav","m4a","ogg"])
79
 
80
  if uploaded_file:
81
  with tempfile.NamedTemporaryFile(delete=False, suffix=uploaded_file.name) as tmp:
 
84
 
85
  st.success("✔ Audio uploaded")
86
 
 
87
  device = get_device()
88
  asr = load_asr(device)
89
 
90
+ st.info(" Transcribing audio…")
91
+
92
  transcript = transcribe_in_chunks(asr, audio_path)
93
 
94
  st.subheader("📝 Urdu Transcription")