Skip to content

Instantly share code, notes, and snippets.

@rowntreerob
Last active March 22, 2024 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rowntreerob/a0d29860aea8f66c0315648a2d8a1334 to your computer and use it in GitHub Desktop.
Save rowntreerob/a0d29860aea8f66c0315648a2d8a1334 to your computer and use it in GitHub Desktop.
streamlit - mp3-to-transcription
import streamlit as st
from streamlit_extras.stylable_container import stylable_container
from dotenv import load_dotenv
from deepgram import (
DeepgramClient,
ClientOptionsFromEnv,
PrerecordedOptions,
)
import os
st.title('audioToText+🫶')
if "url" in st.query_params:
AUDIO_URL = {"url": st.query_params["url"]}
st.text_input('transcribe:',AUDIO_URL)
else:
url_input = st.text_input('url of mp3 to transcribe ')
AUDIO_URL = {"url": url_input}
load_dotenv() #DEEPGRAM_API_KEY
def fetch(url):
try:
# STEP 1 Create a Deepgram client using the API key from environment variables
deepgram: DeepgramClient = DeepgramClient("", ClientOptionsFromEnv())
# STEP 2 Call the transcribe_url method on the prerecorded class
options: PrerecordedOptions = PrerecordedOptions(
model="nova-2",
smart_format=True,
paragraphs=True,
punctuate=True,
)
return deepgram.listen.prerecorded.v("1").transcribe_url(url, options)
# response['results']['channels'][0]['alternatives'][0]['paragraphs']['transcript']
except Exception as e:
print(f"Exception: {e}")
return("Error")
def text_layout(msg):
return f'<textarea cols="56" rows="20">{bucket}</textarea>'
# call dpgrm API, display the transcript in text-area using layout, markdown
if st.button('Enter'):
with st.spinner("Transcribing..."):
try:
data = fetch(AUDIO_URL)
bucket = data['results']['channels'][0]['alternatives'][0]['paragraphs']['transcript']
st.markdown( text_layout(bucket), unsafe_allow_html=True)
except Exception as e:
st.error(f"Error executing the query: {e}")
@rowntreerob
Copy link
Author

runs the server.
streamlit run transcribeMp3.py
Install
pip install the dependencies + streamlit
create an api key using the Deepgram Console
edit .env file with DEEPGRAM_API_KEY={your key}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment