Advertisement
Guest User

Untitled

a guest
Jan 13th, 2025
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | Source Code | 0 0
  1. import sys
  2. import os
  3. sys.path.append(os.path.abspath(os.path.dirname(__file__)))
  4. import subprocess
  5. import keyboard
  6. import asyncio
  7. import webbrowser
  8. from dotenv import load_dotenv
  9. from livekit.agents import AutoSubscribe, JobContext, WorkerOptions, cli, llm
  10. from livekit.agents.voice_assistant import VoiceAssistant
  11. from livekit.plugins import openai, silero
  12. from flask import Flask
  13. from livekit import api
  14.  
  15.  
  16.  
  17. load_dotenv()
  18.  
  19. app = Flask(__name__)
  20.  
  21. u/app.route('/getToken')
  22. def getToken():
  23.   token = api.AccessToken(os.getenv('LIVEKIT_API_KEY'), os.getenv('LIVEKIT_API_SECRET')) \
  24.     .with_identity("identity") \
  25.     .with_name("my name") \
  26.     .with_grants(api.VideoGrants(
  27.         room_join=True,
  28.         room="my-room",
  29.     ))
  30.   return token.to_jwt()
  31.  
  32. async def entrypoint(ctx: JobContext):
  33.    initial_ctx = llm.ChatContext().append(
  34.        role="system",
  35.        text=(
  36.                       ""
  37.        ),
  38.     )
  39.    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
  40.  
  41.    assistant = VoiceAssistant(
  42.        vad=silero.VAD.load(),
  43.        stt=openai.STT(),
  44.        llm=openai.llm(),
  45.        tts=openai.TTS(),
  46.        chat_ctx=initial_ctx,
  47.    )
  48.    assistant.start(ctx.room)
  49.  
  50.  
  51. def stop_program():
  52.     raise StopIteration
  53.  
  54. async def main():
  55.     keyboard.add_hotkey('`', start_program_and_open_playground)  
  56.     keyboard.add_hotkey('~', stop_program)
  57.  
  58.  
  59. def start_program_and_open_playground():
  60.     webbrowser.open("https://agents-playground.livekit.io")
  61. while True:
  62.     if keyboard.read_key() == '`':
  63.         start_program_and_open_playground()
  64.         break
  65.  
  66.     if keyboard.read_key() == '~':
  67.         stop_program()
  68.         break
  69.  
  70. if __name__ == "__main__":
  71.     if len(sys.argv) > 1 and sys.argv[1] == "start":
  72.         try:
  73.             asyncio.run(main())
  74.         except StopIteration:
  75.             print("Program stopped.")
  76.     else:
  77.         loop = asyncio.new_event_loop()
  78.         asyncio.set_event_loop(loop)
  79.         cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement