import assemblyai as aai
from elevenlabs import generate, stream
from openai import OpenAI
class AI_Assistant:
def __init__(self):
aai.settings.api_key = "b1777ed8bdf24eea9767045727288eff"
self.openai_client = OpenAI(api_key = "sk-proj-WdLyvcLykynTjLPJsvqcT3BlbkFJuTSg6Lfc3sPXPqWQIWlU")
self.elevenlabs_api_key = "d8eaa1fbca3d2e1ee05f3f5c98066f51"
self.transcriber = None
# Prompt
self.full_transcript = [
{"role":"system", "content":"You are a receptionist at a dental clinic. Be resourceful and efficient."},
]
###### Step 2: Real-Time Transcription with AssemblyAI ######
def start_transcription(self):
self.transcriber = aai.RealtimeTranscriber(
sample_rate = 16000,
on_data = self.on_data,
on_error = self.on_error,
on_open = self.on_open,
on_close = self.on_close,
end_utterance_silence_threshold = 1000
)
self.transcriber.connect()
microphone_stream = aai.extras.MicrophoneStream(sample_rate =16000)
self.transcriber.stream(microphone_stream)
def stop_transcription(self):
if self.transcriber:
self.transcriber.close()
self.transcriber = None
def on_open(self, session_opened: aai.RealtimeSessionOpened):
#print("Session ID:", session_opened.session_id)
return
def on_data(self, transcript: aai.RealtimeTranscript):
if not transcript.text:
return
if isinstance(transcript, aai.RealtimeFinalTranscript):
self.generate_ai_response(transcript)
else:
print(transcript.text, end="r")
def on_error(self, error: aai.RealtimeError):
#print("An error occured:", error)
return
def on_close(self):
#print("Closing Session")
return
###### Step 3: Pass real-time transcript to OpenAI ######
def generate_ai_response(self, transcript):
self.stop_transcription()
self.full_transcript.append({"role":"user", "content": transcript.text})
print(f"nPatient: {transcript.text}", end="rn")
response = self.openai_client.chat.completions.create(
model = "gpt-3.5-turbo",
messages = self.full_transcript
)
ai_response = response.choices[0].message.content
self.generate_audio(ai_response)
self.start_transcription()
print(f"nReal-time transcription: ", end="rn")
###### Step 4: Generate audio with ElevenLabs ######
def generate_audio(self, text):
self.full_transcript.append({"role":"assistant", "content": text})
print(f"nAI Receptionist: {text}")
audio_stream = generate(
api_key = self.elevenlabs_api_key,
text = text,
voice = "Rachel",
stream = True
)
stream(audio_stream)
greeting = "Thank you for calling Durgadevi dental clinic. My name is Durga, how may I assist you?"
ai_assistant = AI_Assistant()
ai_assistant.generate_audio(greeting)
ai_assistant.start_transcription()
I am trying to create a ai voice bot where we can have real time conversation. The code runs perfectly but I am unable to give input though I am talking it is not taking input my mic is fine. Don’t know what to do? Please help me. All I see is a blinking line.
Thank you for calling Durgadevi dental clinic. My name is Durga, how may I assist you?
All I see is a blinking cursor not able to give input. Please help me. How should I solve this.