Umar4321 commited on
Commit
02cc100
·
verified ·
1 Parent(s): 80283da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -32
app.py CHANGED
@@ -4,13 +4,27 @@ from transformers import pipeline
4
  from gtts import gTTS
5
  import tempfile
6
  import os
 
7
 
8
  # ------------------------------
9
- # Setup models
10
  # ------------------------------
11
- stt_model = pipeline("automatic-speech-recognition", model="openai/whisper-small") # Pashto supported
12
- translator_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ps-en") # Pashto → English
13
- translator_ur = pipeline("translation", model="Helsinki-NLP/opus-mt-ps-ur") # Pashto → Urdu
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # ------------------------------
16
  # Streamlit UI
@@ -29,33 +43,53 @@ wav_audio_data = st_audiorec()
29
 
30
  if wav_audio_data is not None:
31
  st.audio(wav_audio_data, format="audio/wav")
32
-
33
- # ------------------------------
34
- # Step 1: Speech to Text (Pashto)
35
- # ------------------------------
36
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
37
  temp_wav.write(wav_audio_data)
38
- temp_wav.flush()
39
- pashto_text = stt_model(temp_wav.name)["text"]
40
-
41
- st.subheader("Pashto transcription:")
42
- st.info(pashto_text)
43
-
44
- # ------------------------------
45
- # Step 2: Translation
46
- # ------------------------------
47
- if target_lang == "English":
48
- translated = translator_en(pashto_text)[0]["translation_text"]
49
- else:
50
- translated = translator_ur(pashto_text)[0]["translation_text"]
51
-
52
- st.subheader(f"Translated ({target_lang}):")
53
- st.success(translated)
54
-
55
- # ------------------------------
56
- # Step 3: Text-to-Speech
57
- # ------------------------------
58
- tts = gTTS(text=translated, lang="en" if target_lang == "English" else "ur")
59
- with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_mp3:
60
- tts.save(temp_mp3.name)
61
- st.audio(temp_mp3.name, format="audio/mp3")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from gtts import gTTS
5
  import tempfile
6
  import os
7
+ import time
8
 
9
  # ------------------------------
10
+ # Setup models with progress indicators
11
  # ------------------------------
12
+ @st.cache_resource
13
+ def load_models():
14
+ with st.spinner("Loading speech-to-text model..."):
15
+ stt_model = pipeline("automatic-speech-recognition", model="openai/whisper-small")
16
+
17
+ with st.spinner("Loading translation models..."):
18
+ translator_en = pipeline("translation", model="Helsinki-NLP/opus-mt-ps-en")
19
+ translator_ur = pipeline("translation", model="Helsinki-NLP/opus-mt-ps-ur")
20
+
21
+ return stt_model, translator_en, translator_ur
22
+
23
+ try:
24
+ stt_model, translator_en, translator_ur = load_models()
25
+ except Exception as e:
26
+ st.error(f"Error loading models: {str(e)}")
27
+ st.stop()
28
 
29
  # ------------------------------
30
  # Streamlit UI
 
43
 
44
  if wav_audio_data is not None:
45
  st.audio(wav_audio_data, format="audio/wav")
46
+
47
+ # Save audio to a temporary file with proper handling
 
 
48
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
49
  temp_wav.write(wav_audio_data)
50
+ temp_filename = temp_wav.name
51
+
52
+ try:
53
+ # ------------------------------
54
+ # Step 1: Speech to Text (Pashto)
55
+ # ------------------------------
56
+ with st.spinner("Transcribing speech..."):
57
+ pashto_text = stt_model(temp_filename)["text"]
58
+
59
+ # Clean up temporary file
60
+ os.unlink(temp_filename)
61
+
62
+ st.subheader("Pashto transcription:")
63
+ st.info(pashto_text)
64
+
65
+ # ------------------------------
66
+ # Step 2: Translation
67
+ # ------------------------------
68
+ with st.spinner("Translating..."):
69
+ if target_lang == "English":
70
+ translated = translator_en(pashto_text)[0]["translation_text"]
71
+ else:
72
+ translated = translator_ur(pashto_text)[0]["translation_text"]
73
+
74
+ st.subheader(f"Translated ({target_lang}):")
75
+ st.success(translated)
76
+
77
+ # ------------------------------
78
+ # Step 3: Text-to-Speech
79
+ # ------------------------------
80
+ with st.spinner("Generating audio..."):
81
+ tts = gTTS(text=translated, lang="en" if target_lang == "English" else "ur")
82
+
83
+ # Use BytesIO to avoid file handling issues
84
+ from io import BytesIO
85
+ audio_bytes = BytesIO()
86
+ tts.write_to_fp(audio_bytes)
87
+ audio_bytes.seek(0)
88
+
89
+ st.audio(audio_bytes, format="audio/mp3")
90
+
91
+ except Exception as e:
92
+ st.error(f"An error occurred: {str(e)}")
93
+ # Ensure temporary file is cleaned up even if error occurs
94
+ if os.path.exists(temp_filename):
95
+ os.unlink(temp_filename)