import speech_recognition as sr from pydub import AudioSegment import gradio as gr from os import path import requests import openai prompt = "Type and press Enter" r = sr.Recognizer() def record_text(audio_file,prompt): if len(prompt) == 0: prompt = "Apply proper punctuations, upper case and lower case to the provided text." sound = audio_file sound_type = sound.split(".") if sound_type[-1] == 'mp3': input_file = sound output_file = "con_sound.wav" # convert mp3 file to wav file sound = AudioSegment.from_mp3(input_file) sound.export(output_file, format="wav") sound = "con_sound.wav" MyText = "" with sr.AudioFile(sound) as source: r.adjust_for_ambient_noise(source) print("Converting audio file to text..") audio2 = r.record(source, duration=None) # Use record instead of listen MyText = r.recognize_google(audio2, language="en-US", key=None, show_all=False) MyText = MyText.lower() return (MyText,prompt) def message_and_history(audio_file,input, history, api_key): history = history or [] input_text = "Type and press Enter" output_text = record_text(audio_file,input) if len(input_text) == 0: input_text = "Speech from the video." history.append((input_text, output_text)) else: history.append((input_text, output_text)) return history, history block = gr.Blocks(theme=gr.themes.Glass(primary_hue="slate")) with block: gr.Markdown("""