Spaces:
Runtime error
Runtime error
# Transform an audio to text script with language detection. | |
# Author: Pratiksha Patel | |
# Description: This script record the audio, transform it to text, detect the language of the file and save it to a txt file. | |
# import required modules | |
import os | |
import streamlit as st | |
from audio_recorder_streamlit import audio_recorder | |
import whisper | |
from langdetect import detect | |
# Function to open a file | |
def startfile(fn): | |
os.system('open %s' % fn) | |
# Function to create and open a txt file | |
def create_and_open_txt(text, filename): | |
# Create and write the text to a txt file | |
with open(filename, "w") as file: | |
file.write(text) | |
startfile(filename) | |
# Ask user to record audio | |
st.title("Audio to Text Transcription..") | |
audio_bytes = audio_recorder(pause_threshold=3.0, sample_rate=16_000) | |
# Load the base model and transcribe the audio | |
model = whisper.load_model("base") | |
result = model.transcribe(audio_bytes) | |
transcribed_text = result["text"] | |
print(transcribed_text) | |
st.write("Transcription:") | |
st.write(transcribed_text) | |
# Detect the language | |
language = detect(transcribed_text) | |
st.write(f"Detected language: {language}") | |
# Create and open a txt file with the text | |
create_and_open_txt(transcribed_text, f"output_{language}.txt") | |