temp / Capstone_v2 /test_audio.py
NEXAS's picture
Upload 15 files
3d981be verified
raw
history blame contribute delete
No virus
2.05 kB
import streamlit as st
import speech_recognition as sr
import pyttsx3
from streamlit.components.v1 import html
# Initialize the recognizer
recognizer = sr.Recognizer()
# Initialize the text-to-speech engine
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
def speech_to_text():
try:
# Use microphone as the audio source
with sr.Microphone() as source:
# Adjust for ambient noise
recognizer.adjust_for_ambient_noise(source, duration=0.2)
st.info("Speak something...")
# Listen for user input
audio = recognizer.listen(source)
# Use Google's Speech Recognition
text = recognizer.recognize_google(audio)
return text
except sr.RequestError as e:
st.error("Could not request results; {0}".format(e))
except sr.UnknownValueError:
st.error("Unknown error occurred")
def text_to_speech(text):
# Convert text to speech
engine.save_to_file(text, "output.mp3")
engine.runAndWait()
# Streamlit app layout
st.title("Speech-to-Text and Text-to-Speech App")
# Text input box for user input
user_input = st.text_input("Enter text:", "")
# Main content
if st.button("Convert to Speech"):
if user_input:
text_to_speech(user_input)
st.audio("output.mp3", format="audio/mp3", start_time=0)
st.markdown("<script>document.getElementsByTagName('audio')[0].play()</script>", unsafe_allow_html=True)
else:
st.warning("Please enter some text.")
import base64
def autoplay_audio(file_path: str):
with open(file_path, "rb") as f:
data = f.read()
b64 = base64.b64encode(data).decode()
md = f"""
<audio controls autoplay="true">
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
</audio>
"""
st.markdown(
md,
unsafe_allow_html=True,
)
st.write("# Auto-playing Audio!")
autoplay_audio("output.mp3")