muralipala1504 commited on
Commit
d61ee9c
Β·
1 Parent(s): 7750bd0

feat: sentence-by-sentence Hindi TTS, LibreTranslate auto-start with warmup

Browse files
Files changed (3) hide show
  1. app.js +32 -11
  2. start.sh +14 -0
  3. stop.sh +11 -0
app.js CHANGED
@@ -95,20 +95,41 @@ window.speakText = function(text) {
95
  .replace(/🎯|πŸ’»|πŸ”|⚠️|πŸš€|πŸ’‘/g, "")
96
  .replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
97
  .trim();
98
- // Translate if non-English selected
99
  if (window.ttsLang !== 'en-US' && window.ttsLang !== 'en-GB') {
100
  const langCode = window.ttsLang.split('-')[0];
101
- fetch('http://192.168.217.160:5000/translate', {
102
- method: 'POST',
103
- headers: {'Content-Type': 'application/json'},
104
- body: JSON.stringify({q: clean, source: 'en', target: langCode})
105
- })
106
- .then(r => r.json())
107
- .then(data => {
108
- const translated = data.translatedText || clean;
109
- window._speakDirect(translated);
110
  })
111
- .catch(() => window._speakDirect(clean));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  return;
113
  }
114
  window._speakDirect(clean);
 
95
  .replace(/🎯|πŸ’»|πŸ”|⚠️|πŸš€|πŸ’‘/g, "")
96
  .replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
97
  .trim();
98
+ // Translate sentence by sentence to reduce lag
99
  if (window.ttsLang !== 'en-US' && window.ttsLang !== 'en-GB') {
100
  const langCode = window.ttsLang.split('-')[0];
101
+ const sentences = clean.match(/[^.!?]+[.!?]+/g) || [clean];
102
+ let i = 0;
103
+ function translateAndSpeakNext() {
104
+ if (i >= sentences.length || !window.ttsEnabled) return;
105
+ const sentence = sentences[i].trim();
106
+ fetch('http://192.168.217.160:5000/translate', {
107
+ method: 'POST',
108
+ headers: {'Content-Type': 'application/json'},
109
+ body: JSON.stringify({q: sentence, source: 'en', target: langCode})
110
  })
111
+ .then(r => r.json())
112
+ .then(data => {
113
+ const translated = data.translatedText || sentence;
114
+ const utt = new SpeechSynthesisUtterance(translated);
115
+ utt.lang = window.ttsLang;
116
+ const voices = window.speechSynthesis.getVoices();
117
+ const match = voices.find(v => v.lang === window.ttsLang);
118
+ if (match) utt.voice = match;
119
+ utt.rate = 0.95;
120
+ utt.pitch = 1.0;
121
+ utt.volume = 1.0;
122
+ utt.onend = () => { i++; translateAndSpeakNext(); };
123
+ window.speechSynthesis.speak(utt);
124
+ })
125
+ .catch(() => {
126
+ const utt = new SpeechSynthesisUtterance(sentence);
127
+ utt.lang = window.ttsLang;
128
+ utt.onend = () => { i++; translateAndSpeakNext(); };
129
+ window.speechSynthesis.speak(utt);
130
+ });
131
+ }
132
+ translateAndSpeakNext();
133
  return;
134
  }
135
  window._speakDirect(clean);
start.sh CHANGED
@@ -14,6 +14,20 @@ if [ -f deepshell.pid ]; then
14
  fi
15
  fi
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Explicitly pass GROQ_API_KEY to subprocess
18
  GROQ_API_KEY=$GROQ_API_KEY nohup python run_deepshell.py > deepshell.log 2>&1 &
19
  echo $! > deepshell.pid
 
14
  fi
15
  fi
16
 
17
+ # Start LibreTranslate if not already running
18
+ if ! pgrep -f "libretranslate" > /dev/null; then
19
+ source ~/.venvs/libretranslate/bin/activate
20
+ nohup libretranslate --host 0.0.0.0 --port 5000 --load-only en,hi > libretranslate.log 2>&1 &
21
+ echo $! > libretranslate.pid
22
+ echo "LibreTranslate started (PID $(cat libretranslate.pid))"
23
+ deactivate
24
+ # Warmup β€” trigger model load in background
25
+ sleep 5 && curl -s -X POST http://localhost:5000/translate -H "Content-Type: application/json" -d '{"q":"warmup","source":"en","target":"hi"}' > /dev/null 2>&1 &
26
+ echo "LibreTranslate warmup triggered"
27
+ else
28
+ echo "LibreTranslate already running"
29
+ fi
30
+
31
  # Explicitly pass GROQ_API_KEY to subprocess
32
  GROQ_API_KEY=$GROQ_API_KEY nohup python run_deepshell.py > deepshell.log 2>&1 &
33
  echo $! > deepshell.pid
stop.sh CHANGED
@@ -8,3 +8,14 @@ PID=$(cat deepshell.pid)
8
  fuser -k 8001/tcp 2>/dev/null
9
  rm -f deepshell.pid
10
  echo "DeepShell stopped (PID $PID)"
 
 
 
 
 
 
 
 
 
 
 
 
8
  fuser -k 8001/tcp 2>/dev/null
9
  rm -f deepshell.pid
10
  echo "DeepShell stopped (PID $PID)"
11
+
12
+ # Stop LibreTranslate
13
+ if [ -f libretranslate.pid ]; then
14
+ LT_PID=$(cat libretranslate.pid)
15
+ kill $LT_PID 2>/dev/null
16
+ rm -f libretranslate.pid
17
+ echo "LibreTranslate stopped (PID $LT_PID)"
18
+ else
19
+ pkill -f libretranslate 2>/dev/null
20
+ echo "LibreTranslate stopped"
21
+ fi