File size: 1,267 Bytes
3ae932d
 
 
 
7ab37b6
3ae932d
 
7ab37b6
 
 
3ae932d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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)