import whisper from langchain.chat_models import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.chains import LLMChain from transformers import AutoProcessor, AutoModelForSpeechSeq2Seq # Step 1: Load the Whisper model #model = whisper.load_model("base") processor = AutoProcessor.from_pretrained("openai/whisper-base") model = AutoModelForSpeechSeq2Seq.from_pretrained("openai/whisper-base") # Step 2: Function to convert speech to text def speech_to_text(audio_file_path): result = model.transcribe(audio_file_path) return result["text"] # Step 3: Use LangChain for further processing def process_text_with_langchain(text): # Define a simple prompt template and LLM prompt_template = "Translate the following text to French: {text}" prompt = PromptTemplate(input_variables=["text"], template=prompt_template) llm = ChatOpenAI(model="gpt-4") chain = LLMChain(llm=llm, prompt=prompt) # Generate output return chain.run(text) # Example usage audio_file_path = "path_to_your_audio_file.wav" text = speech_to_text(audio_file_path) print("Transcribed Text:", text) # Further processing with LangChain translated_text = process_text_with_langchain(text) print("Translated Text:", translated_text)