-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6eef5b0
commit 60e397e
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
## Chapter 14: Speech Recognition and Synthesis | ||
You can use LangChain to create applications that incorporate speech recognition and speech synthesis. | ||
|
||
``` | ||
from langchain.llms import OpenAI | ||
from langchain.chains import LLMChain | ||
from langchain.prompts import PromptTemplate | ||
import speech_recognition as sr | ||
import pyttsx3 | ||
# | ||
recognizer = sr.Recognizer() | ||
# | ||
engine = pyttsx3.init() | ||
# LLM | ||
llm = OpenAI(temperature=0.7) | ||
prompt = PromptTemplate( | ||
input_variables=["query"], | ||
template="You are a helpful assistant. Answer the following query: {query}" | ||
) | ||
chain = LLMChain(llm=llm, prompt=prompt) | ||
# | ||
with sr.Microphone() as source: | ||
print("Speak your question:") | ||
audio = recognizer.listen(source) | ||
try: | ||
# | ||
query = recognizer.recognize_google(audio) | ||
print(f"You said: {query}") | ||
# LLM | ||
response = chain.run(query) | ||
print(f"AI response: {response}") | ||
# | ||
engine.say(response) | ||
engine.runAndWait() | ||
except sr.UnknownValueError: | ||
print("Sorry, I couldn't understand that.") | ||
except sr.RequestError as e: | ||
print(f"Could not request results; {e}") | ||
``` |