Raspberry Pi पर Agents Platform के साथ वॉइस असिस्टेंट बनाएं

Raspberry Pi पर Agents Platform के साथ वॉइस असिस्टेंट बनाएं।

ट्यूटोरियल · मानता है कि आपने ElevenAgents क्विकस्टार्ट पूरा कर लिया है और आपके पास Python इंस्टॉल किया हुआ Raspberry Pi है।

परिचय

इस ट्यूटोरियल में आप सीखेंगे कि Raspberry Pi पर चलने वाले Agents Platform के साथ वॉइस असिस्टेंट कैसे बनाएं। Amazon Echo पर Alexa, Google Home या Apple डिवाइस पर Siri जैसे पारंपरिक होम असिस्टेंट की तरह, आपका Eleven वॉइस असिस्टेंट एक हॉटवर्ड सुनेगा—हमारे मामले में “Hey Eleven”—और फिर यूज़र की मदद के लिए ElevenLabs Agents सेशन शुरू करेगा।

ज़रूरी चीज़ें

  • Raspberry Pi 5 या ऐसा ही कोई डिवाइस।
  • माइक्रोफ़ोन और स्पीकर।
  • आपकी मशीन पर Python 3.9 या उससे नया वर्ज़न इंस्टॉल होना चाहिए।
  • API key वाला ElevenLabs अकाउंट।

सेटअप

डिपेंडेंसीज़ इंस्टॉल करें

Debian-आधारित सिस्टम पर आप ये डिपेंडेंसीज़ इंस्टॉल कर सकते हैं:

sudo apt-get update
sudo apt-get install libportaudio2 libportaudiocpp0 portaudio19-dev libasound-dev libsndfile1-dev -y

प्रोजेक्ट बनाएं

अपने Raspberry Pi पर टर्मिनल खोलें और प्रोजेक्ट के लिए एक नई डायरेक्टरी बनाएं।

mkdir eleven-voice-assistant
cd eleven-voice-assistant

नया वर्चुअल एनवायरनमेंट बनाएं और डिपेंडेंसीज़ इंस्टॉल करें:

python -m venv .venv # Only required the first time you set up the project
source .venv/bin/activate

डिपेंडेंसीज़ इंस्टॉल करें:

pip install tflite-runtime
pip install librosa
pip install EfficientWord-Net
pip install elevenlabs
pip install "elevenlabs[pyaudio]"

अब hotword.py नाम की नई Python फ़ाइल बनाएं और इसमें यह कोड जोड़ें:

hotword.py
import os
import signal
import time
from eff_word_net.streams import SimpleMicStream
from eff_word_net.engine import HotwordDetector
from eff_word_net.audio_processing import Resnet50_Arc_loss
# from eff_word_net import samples_loc
from elevenlabs.client import ElevenLabs
from elevenlabs.conversational_ai.conversation import Conversation, ConversationInitiationData
from elevenlabs.conversational_ai.default_audio_interface import DefaultAudioInterface
convai_active = False
elevenlabs = ElevenLabs()
agent_id = os.getenv("ELEVENLABS_AGENT_ID")
api_key = os.getenv("ELEVENLABS_API_KEY")
dynamic_vars = {
'user_name': 'Thor',
'greeting': 'Hey'
}
config = ConversationInitiationData(
dynamic_variables=dynamic_vars
)
base_model = Resnet50_Arc_loss()
eleven_hw = HotwordDetector(
hotword="hey_eleven",
model = base_model,
reference_file=os.path.join("hotword_refs", "hey_eleven_ref.json"),
threshold=0.7,
relaxation_time=2
)
def create_conversation():
"""Create a new conversation instance"""
return Conversation(
# API client and agent ID.
elevenlabs,
agent_id,
config=config,
# Assume auth is required when API_KEY is set.
requires_auth=bool(api_key),
# Use the default audio interface.
audio_interface=DefaultAudioInterface(),
# Simple callbacks that print the conversation to the console.
callback_agent_response=lambda response: print(f"Agent: {response}"),
callback_agent_response_correction=lambda original, corrected: print(f"Agent: {original} -> {corrected}"),
callback_user_transcript=lambda transcript: print(f"User: {transcript}"),
# Uncomment if you want to see latency measurements.
# callback_latency_measurement=lambda latency: print(f"Latency: {latency}ms"),
)
def start_mic_stream():
"""Start or restart the microphone stream"""
global mic_stream
try:
# Always create a new stream instance
mic_stream = SimpleMicStream(
window_length_secs=1.5,
sliding_window_secs=0.75,
)
mic_stream.start_stream()
print("Microphone stream started")
except Exception as e:
print(f"Error starting microphone stream: {e}")
mic_stream = None
time.sleep(1) # Wait a bit before retrying
def stop_mic_stream():
"""Stop the microphone stream safely"""
global mic_stream
try:
if mic_stream:
# SimpleMicStream doesn't have a stop_stream method
# We'll just set it to None and recreate it next time
mic_stream = None
print("Microphone stream stopped")
except Exception as e:
print(f"Error stopping microphone stream: {e}")
# Initialize microphone stream
mic_stream = None
start_mic_stream()
print("Say Hey Eleven ")
while True:
if not convai_active:
try:
# Make sure we have a valid mic stream
if mic_stream is None:
start_mic_stream()
continue
frame = mic_stream.getFrame()
result = eleven_hw.scoreFrame(frame)
if result == None:
#no voice activity
continue
if result["match"]:
print("Wakeword uttered", result["confidence"])
# Stop the microphone stream to avoid conflicts
stop_mic_stream()
# Start ConvAI Session
print("Start ConvAI Session")
convai_active = True
try:
# Create a new conversation instance
conversation = create_conversation()
# Start the session
conversation.start_session()
# Set up signal handler for graceful shutdown
def signal_handler(sig, frame):
print("Received interrupt signal, ending session...")
try:
conversation.end_session()
except Exception as e:
print(f"Error ending session: {e}")
signal.signal(signal.SIGINT, signal_handler)
# Wait for session to end
conversation_id = conversation.wait_for_session_end()
print(f"Conversation ID: {conversation_id}")
except Exception as e:
print(f"Error during conversation: {e}")
finally:
# Cleanup
convai_active = False
print("Conversation ended, cleaning up...")
# Give some time for cleanup
time.sleep(1)
# Restart microphone stream
start_mic_stream()
print("Ready for next wake word...")
except Exception as e:
print(f"Error in wake word detection: {e}")
# Try to restart microphone stream if there's an error
mic_stream = None
time.sleep(1)
start_mic_stream()

एजेंट कॉन्फ़िगरेशन

1

ElevenLabs में साइन इन करें

elevenlabs.io पर जाएं और अपने अकाउंट में साइन इन करें।

2

नया एजेंट बनाएं

Agents Platform > Agents पर जाएं और ब्लैंक टेम्पलेट से नया एजेंट बनाएं।

3

पहला मैसेज सेट करें

पहला मैसेज सेट करें और प्लेटफ़ॉर्म के लिए डायनामिक वेरिएबल तय करें।

{{greeting}} {{user_name}}, Eleven here, what's up?
4

सिस्टम प्रॉम्प्ट सेट करें

सिस्टम प्रॉम्प्ट सेट करें। हमारी बेहतरीन प्रथाओं के दस्तावेज़ यहां मिलेंगे।

You are a helpful Agents Platform assistant with access to a weather tool. When users ask about
weather conditions, use the get_weather tool to fetch accurate, real-time data. The tool requires
a latitude and longitude - use your geographic knowledge to convert location names to coordinates
accurately.
Never ask users for coordinates - you must determine these yourself. Always report weather
information conversationally, referring to locations by name only. For weather requests:
1. Extract the location from the user's message
2. Convert the location to coordinates and call get_weather
3. Present the information naturally and helpfully
For non-weather queries, provide friendly assistance within your knowledge boundaries. Always be
concise, accurate, and helpful.
5

वेबहुक टूल सेट अप करें

हम एक आसान वेबहुक टूल सेट अप करेंगे, जो हमारे लिए मौसम का डेटा फ़ेच करेगा। टूल सेट अप करने के लिए यहां दिए गए चरणों का पालन करें।

ऐप चलाएं

ऐप चलाने के लिए पहले ज़रूरी एनवायरनमेंट वेरिएबल सेट करें:

export ELEVENLABS_API_KEY=YOUR_API_KEY
export ELEVENLABS_AGENT_ID=YOUR_AGENT_ID

फिर बस यह कमांड चलाएं:

python hotword.py

अब बातचीत शुरू करने के लिए “Hey Eleven” कहें। चैटिंग का आनंद लें!

[वैकल्पिक] अपना कस्टम हॉटवर्ड प्रशिक्षित करें

प्रशिक्षण ऑडियो जनरेट करें

हॉटवर्ड एम्बेडिंग्स जनरेट करने के लिए, आप ElevenLabs का उपयोग करके चार प्रशिक्षण सैंपल बना सकते हैं। अपने ElevenLabs ऐप में टेक्स्ट टू स्पीच पर जाएं और अपना हॉटवर्ड टाइप करें, जैसे “Hey Eleven”। एक वॉइस चुनें और “Generate” बटन पर क्लिक करें।

ऑडियो जनरेट होने के बाद, ऑडियो फ़ाइल डाउनलोड करें और उसे अपने प्रोजेक्ट की रूट में hotword_training_audio नाम के फ़ोल्डर में सेव करें। अलग-अलग वॉइस के साथ यही प्रक्रिया तीन बार और दोहराएं।

हॉटवर्ड प्रशिक्षित करें

वर्चुअल एनवायरनमेंट सक्रिय होने पर, अपने टर्मिनल में हॉटवर्ड प्रशिक्षित करने के लिए यह कमांड चलाएं:

python -m eff_word_net.generate_reference --input-dir hotword_training_audio --output-dir hotword_refs --wakeword hey_eleven --model-type resnet_50_arc

इससे hotword_refs फ़ोल्डर में hey_eleven_ref.json फ़ाइल जनरेट होगी। अब आपको बस hotword.py में HotwordDetector क्लास के reference_file पैरामीटर को नई रेफ़रेंस फ़ाइल पर पॉइंट करने के लिए अपडेट करना है, और आपका काम हो गया!

अगले चरण