|
import streamlit as st |
|
import speech_recognition as sr |
|
import pyttsx3 |
|
from streamlit.components.v1 import html |
|
|
|
|
|
recognizer = sr.Recognizer() |
|
|
|
|
|
engine = pyttsx3.init() |
|
voices = engine.getProperty('voices') |
|
engine.setProperty('voice', voices[1].id) |
|
|
|
def speech_to_text(): |
|
try: |
|
|
|
with sr.Microphone() as source: |
|
|
|
recognizer.adjust_for_ambient_noise(source, duration=0.2) |
|
|
|
st.info("Speak something...") |
|
|
|
audio = recognizer.listen(source) |
|
|
|
|
|
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): |
|
|
|
engine.save_to_file(text, "output.mp3") |
|
engine.runAndWait() |
|
|
|
|
|
st.title("Speech-to-Text and Text-to-Speech App") |
|
|
|
|
|
user_input = st.text_input("Enter text:", "") |
|
|
|
|
|
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") |