mirror of
https://github.com/codecrafters-io/build-your-own-x
synced 2026-07-03 01:09:25 +00:00
Add Alexa Assistant project
This commit is contained in:
parent
1a851d83f7
commit
86ba9ff076
3 changed files with 82 additions and 0 deletions
61
alexa-assistant/AlexaProject.py
Normal file
61
alexa-assistant/AlexaProject.py
Normal file
|
|
@ -0,0 +1,61 @@
|
||||||
|
|
||||||
|
*(If PyAudio gives error, users can install system version.)*
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# ✅ 4. alexa.py (Core Code)
|
||||||
|
Paste this:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import speech_recognition as sr
|
||||||
|
import pyttsx3
|
||||||
|
import datetime
|
||||||
|
import webbrowser
|
||||||
|
|
||||||
|
engine = pyttsx3.init()
|
||||||
|
|
||||||
|
def speak(text):
|
||||||
|
engine.say(text)
|
||||||
|
engine.runAndWait()
|
||||||
|
|
||||||
|
def listen():
|
||||||
|
r = sr.Recognizer()
|
||||||
|
with sr.Microphone() as source:
|
||||||
|
print("Listening...")
|
||||||
|
r.pause_threshold = 1
|
||||||
|
audio = r.listen(source)
|
||||||
|
|
||||||
|
try:
|
||||||
|
print("Recognizing...")
|
||||||
|
command = r.recognize_google(audio, language="en-in")
|
||||||
|
print("User said:", command)
|
||||||
|
except:
|
||||||
|
return ""
|
||||||
|
return command.lower()
|
||||||
|
|
||||||
|
speak("Hello, I am Alexa. How can I help you?")
|
||||||
|
|
||||||
|
while True:
|
||||||
|
cmd = listen()
|
||||||
|
|
||||||
|
if "time" in cmd:
|
||||||
|
time = datetime.datetime.now().strftime("%H:%M")
|
||||||
|
speak(f"The time is {time}")
|
||||||
|
|
||||||
|
elif "date" in cmd:
|
||||||
|
date = datetime.datetime.now().strftime("%d %B %Y")
|
||||||
|
speak(f"Today's date is {date}")
|
||||||
|
|
||||||
|
elif "search" in cmd:
|
||||||
|
query = cmd.replace("search", "")
|
||||||
|
webbrowser.open(f"https://www.google.com/search?q={query}")
|
||||||
|
speak("Searching Google")
|
||||||
|
|
||||||
|
elif "open youtube" in cmd:
|
||||||
|
speak("Opening YouTube")
|
||||||
|
webbrowser.open("https://youtube.com")
|
||||||
|
|
||||||
|
elif "exit" in cmd or "stop" in cmd:
|
||||||
|
speak("Goodbye")
|
||||||
|
break
|
||||||
|
|
||||||
16
alexa-assistant/README.md
Normal file
16
alexa-assistant/README.md
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
# Build Your Own Alexa Assistant
|
||||||
|
|
||||||
|
This project shows how to build a basic Alexa-like voice assistant using Python. It listens to voice commands, answers questions, performs searches, tells the time, and can be extended easily.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
- Voice recognition (SpeechRecognition)
|
||||||
|
- Text-to-Speech (pyttsx3)
|
||||||
|
- Online search functionality
|
||||||
|
- Time and date responses
|
||||||
|
- Custom command support
|
||||||
|
- Extendable skills system
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
```bash
|
||||||
|
pip install -r requirements.txt
|
||||||
|
|
||||||
5
alexa-assistant/requirements.txt
Normal file
5
alexa-assistant/requirements.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
speechrecognition
|
||||||
|
pyttsx3
|
||||||
|
pyaudio
|
||||||
|
requests
|
||||||
|
openai
|
||||||
Loading…
Reference in a new issue