makhdoomnaeem commited on
Commit
59892e7
·
verified ·
1 Parent(s): b14b886

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -35
app.py CHANGED
@@ -1,17 +1,45 @@
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
11
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
12
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
13
 
14
- # Function to use Groq API for grammar correction
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def correct_grammar_with_groq(text):
16
  try:
17
  chat_completion = client.chat.completions.create(
@@ -30,45 +58,26 @@ def correct_grammar_with_groq(text):
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():
48
- try:
49
- recognizer = sr.Recognizer()
50
- with sr.Microphone() as source:
51
- st.write("Listening... Speak now!")
52
- audio = recognizer.listen(source, timeout=5)
53
- return recognizer.recognize_google(audio)
54
- except (AttributeError, OSError):
55
- st.warning("Microphone not found or PyAudio not installed. Please type your input instead.")
56
- return st.text_input("Enter your sentence:")
57
 
58
  # Streamlit interface
59
  st.title("Spoken English Practice")
60
- st.write("Speak into your mic, or type your sentence if the mic is unavailable.")
 
61
  # Button to start speech
62
  if st.button("Speak Now"):
63
  user_input = get_speech()
64
  if user_input:
65
  st.write(f"You said: {user_input}")
66
 
67
- # Grammar correction using Groq API
68
- corrected = correct_grammar_with_groq(user_input)
69
- st.write(f"Corrected: {corrected}")
70
 
71
  # Speak corrected text
72
- speak_text(corrected)
73
 
74
- st.write("Ready to practice? Click 'Speak Now'!")
 
 
 
1
  import os
2
+ import sounddevice as sd
3
+ import queue
4
+ import vosk
5
+ import json
6
  from gtts import gTTS
7
  import tempfile
8
  import playsound
9
+ from groq import Groq
10
+ import streamlit as st
11
 
12
+ # Set Groq API key
13
  GROQ_API_KEY = "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" # Replace with your Groq API key
14
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
15
  client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
16
 
17
+ # Initialize Vosk model
18
+ vosk_model_path = "vosk-model-small-en-us-0.15" # Replace with your downloaded Vosk model path
19
+ if not os.path.exists(vosk_model_path):
20
+ raise FileNotFoundError(f"Vosk model not found at {vosk_model_path}. Download it first.")
21
+ model = vosk.Model(vosk_model_path)
22
+ q = queue.Queue()
23
+
24
+ # Function to handle audio input
25
+ def audio_callback(indata, frames, time, status):
26
+ if status:
27
+ print(status)
28
+ q.put(bytes(indata))
29
+
30
+ # Function to capture speech
31
+ def get_speech():
32
+ with sd.RawInputStream(samplerate=16000, blocksize=8000, dtype="int16",
33
+ channels=1, callback=audio_callback):
34
+ recognizer = vosk.KaldiRecognizer(model, 16000)
35
+ st.write("Listening... Speak now!")
36
+ while True:
37
+ data = q.get()
38
+ if recognizer.AcceptWaveform(data):
39
+ result = json.loads(recognizer.Result())
40
+ return result.get("text", "")
41
+
42
+ # Function to correct grammar using Groq
43
  def correct_grammar_with_groq(text):
44
  try:
45
  chat_completion = client.chat.completions.create(
 
58
 
59
  # Function to speak text using gTTS
60
  def speak_text(text):
61
+ tts = gTTS(text=text, lang='en')
62
+ with tempfile.NamedTemporaryFile(delete=True, suffix=".mp3") as fp:
63
+ tts.save(fp.name)
64
+ playsound.playsound(fp.name)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65
 
66
  # Streamlit interface
67
  st.title("Spoken English Practice")
68
+ st.write("Speak into your mic, and I'll help you improve your English.")
69
+
70
  # Button to start speech
71
  if st.button("Speak Now"):
72
  user_input = get_speech()
73
  if user_input:
74
  st.write(f"You said: {user_input}")
75
 
76
+ # Correct grammar using Groq API
77
+ corrected_text = correct_grammar_with_groq(user_input)
78
+ st.write(f"Corrected: {corrected_text}")
79
 
80
  # Speak corrected text
81
+ speak_text(corrected_text)
82
 
83
+ st.write("Ready to practice? Click 'Speak Now'!")