Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Speech to text program #194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/apps/pages/programs/SpeechToText/speechToText.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import streamlit as st
import streamlit.components.v1 as components


def speechToText():
st.title("Speech Recognition App")

# Embed the HTML file content in Streamlit
components.html("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Speech Recognition</title>
</head>
<body>
<button id="start" onclick="startRecognition()">Start Recognition</button>
<button id="end" onclick="stopRecognition()">Stop Recognition</button>
<p id="output" style="color: white;"></p> <!-- Text color set to white -->

<script>
const output = document.getElementById('output');
let recognition;
let finalTranscript = ''; // Store the final transcript separately
let isStopping = false; // Track if the stop button was pressed

function startRecognition() {
isStopping = false; // Reset the stop flag
recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
recognition.lang = 'en-US';
recognition.continuous = true;
recognition.interimResults = true;

recognition.onstart = function() {
console.log('Recognition started');
};

recognition.onresult = function(event) {
let interimTranscript = ''; // Store interim transcript for each result

for (let i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
finalTranscript += event.results[i][0].transcript + ' '; // Append final transcript
} else {
interimTranscript += event.results[i][0].transcript + ' '; // Collect interim transcript
}
}

// Update the output with both final and interim transcripts
output.textContent = finalTranscript + interimTranscript;
console.log('Interim:', interimTranscript);
console.log('Final:', finalTranscript);
};

recognition.onerror = function(event) {
console.error('Recognition error:', event.error);
};

recognition.onend = function() {
if (!isStopping) { // Only restart if the stop button wasn't pressed
console.log('Recognition ended, restarting...');
recognition.start(); // Restart recognition for continuous listening
}
};

recognition.start();
console.log('Recognition initiated');
}

function stopRecognition() {
isStopping = true; // Set the stop flag to true
if (recognition) {
recognition.stop();
console.log('Recognition stopped');
}
}
</script>
</body>
</html>
""", height=500)
31 changes: 31 additions & 0 deletions src/apps/pages/programs/speechToTextProgram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import os
import importlib
import streamlit as st
from src.helpers.getModules import getModules

MAIN_DIR = 'SpeechToText'
BASE_DIR = os.path.dirname(__file__)
COMMON_MODULE_PATH = os.path.join(BASE_DIR, MAIN_DIR)
MODULES = getModules(COMMON_MODULE_PATH)

def speechToTextProgram():
st.title('Speech To Text')
choice = st.selectbox('Select a program to execute', [None] + list(MODULES.keys()))
st.markdown('---')

if choice in MODULES:
module_name = MODULES[choice]
try:
module = importlib.import_module(f"src.apps.pages.programs.{MAIN_DIR}.{module_name}")
func = getattr(module, module_name)
func()
except ModuleNotFoundError:
st.error(f"Module '{module_name}.py' could not be found.")
except AttributeError:
st.error(f"Function '{module_name}' could not be found in '{module_name}.py'.")
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.info("Star this project on [GitHub](https://github.com/Avdhesh-Varshney/Jarvis), if you like it!", icon='⭐')

speechToTextProgram()
3 changes: 2 additions & 1 deletion src/utils/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def logout():
imagePrograms = st.Page("src/apps/pages/programs/imageProgram.py", title="Image Programs", icon=":material/image:")
games = st.Page("src/apps/pages/programs/games.py",title="Games",icon=":material/casino:")
studyPrograms = st.Page("src/apps/pages/programs/studyProgram.py", title="Study Programs", icon=":material/school:")
speechToTextPrograms = st.Page("src/apps/pages/programs/speechToTextProgram.py",title="speech To Text Programs",icon=":material/casino:")

# /apps/pages/adminTools/contributors
contributors = st.Page("src/apps/pages/adminTools/contributors.py", title="Contributors", icon=":material/people:")
Expand All @@ -58,7 +59,7 @@ def load_functions():
"Account": [logout_page],
"Automations": [websites, messenger],
"Models": [chatBotModels, healthCareModels, objectDetectionModels, recommendationModels],
"Programs": [apiPrograms, games, imagePrograms, simplePrograms, studyPrograms],
"Programs": [apiPrograms, games, imagePrograms, simplePrograms, studyPrograms, speechToTextPrograms],
}
user = st.session_state['user'].split(',')
if user[4] == "Admin" or user[4] == "Super Admin":
Expand Down