Spaces:
Runtime error
Runtime error
import os | |
import streamlit as st | |
import whisper | |
import time | |
from pydub import AudioSegment | |
import subprocess | |
# Function to check if ffmpeg is installed | |
def check_ffmpeg(): | |
try: | |
subprocess.run(['ffmpeg', '-version'], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
return True | |
except subprocess.CalledProcessError: | |
return False | |
# Run the setup script to install ffmpeg if not installed | |
if not check_ffmpeg(): | |
st.write("Installing ffmpeg...") | |
os.system('bash setup.sh') | |
st.write("ffmpeg installed successfully!") | |
# Load the Whisper model | |
model = whisper.load_model("base") | |
# Title of the Streamlit app | |
st.title("SEMA TRANSCRIPTION AI") | |
# Check if ffmpeg is installed | |
if check_ffmpeg(): | |
# Upload audio file | |
audio_file = st.file_uploader("Upload an audio file", type=["mp3", "wav", "m4a"]) | |
if audio_file is not None: | |
# Save the uploaded file to disk | |
with open("uploaded_audio.m4a", "wb") as f: | |
f.write(audio_file.getbuffer()) | |
# Convert audio to the required format | |
audio = AudioSegment.from_file("uploaded_audio.m4a") | |
audio.export("converted_audio.wav", format="wav") | |
# Transcribe the audio file | |
result = model.transcribe("converted_audio.wav") | |
# Split the transcription into sentences | |
sentences = result["text"].split(". ") | |
# Display each sentence with a delay | |
st.write("Transcription:") | |
for sentence in sentences: | |
if sentence.strip(): # Check if the sentence is not empty | |
st.write(sentence.strip() + ".") | |
time.sleep(2) # Add a short delay before displaying the next sentence | |
else: | |
st.error("ffmpeg is required but not found. Please install ffmpeg to use this app.") | |