makhdoomnaeem commited on
Commit
7d14fa6
·
verified ·
1 Parent(s): aead512

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -7
app.py CHANGED
@@ -1,11 +1,10 @@
1
  import streamlit as st
2
  import speech_recognition as sr
3
- import pyttsx3
4
  import os
5
  from groq import Groq
6
-
7
- # Initialize text-to-speech
8
- engine = pyttsx3.init()
9
 
10
  # Initialize Groq client
11
  GROQ_API_KEY = "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" # Replace with your Groq API key
@@ -29,10 +28,20 @@ def correct_grammar_with_groq(text):
29
  except Exception as e:
30
  return f"Error with Groq API: {str(e)}"
31
 
32
- # Function to speak text
33
  def speak_text(text):
34
- engine.say(text)
35
- engine.runAndWait()
 
 
 
 
 
 
 
 
 
 
36
 
37
  # Function to process speech input
38
  def get_speech():
 
1
  import streamlit as st
2
  import speech_recognition as sr
 
3
  import os
4
  from groq import Groq
5
+ from gtts import gTTS
6
+ import tempfile
7
+ import playsound
8
 
9
  # Initialize Groq client
10
  GROQ_API_KEY = "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" # Replace with your Groq API key
 
28
  except Exception as e:
29
  return f"Error with Groq API: {str(e)}"
30
 
31
+ # Function to speak text using gTTS
32
  def speak_text(text):
33
+ try:
34
+ # Try using pyttsx3
35
+ import pyttsx3
36
+ engine = pyttsx3.init()
37
+ engine.say(text)
38
+ engine.runAndWait()
39
+ except OSError:
40
+ # Fallback to gTTS
41
+ tts = gTTS(text=text, lang='en')
42
+ with tempfile.NamedTemporaryFile(delete=True, suffix=".mp3") as fp:
43
+ tts.save(fp.name)
44
+ playsound.playsound(fp.name)
45
 
46
  # Function to process speech input
47
  def get_speech():