Spaces:
Runtime error
Runtime error
# # Import required libraries | |
import vocode | |
from vocode import getenv | |
from vocode.streaming.models.message import BaseMessage | |
from vocode.streaming.models.synthesizer import * | |
from vocode.streaming.models.agent import * | |
from vocode.streaming.models.transcriber import * | |
from vocode.streaming.synthesizer import * | |
import gradio as gr | |
import os | |
import logging | |
from vocode.helpers import create_turn_based_microphone_input_and_speaker_output | |
from vocode.turn_based.agent.chat_gpt_agent import ChatGPTAgent | |
from vocode.turn_based.synthesizer import CoquiSynthesizer | |
from vocode.turn_based.transcriber.whisper_transcriber import WhisperTranscriber | |
from vocode.turn_based.turn_based_conversation import TurnBasedConversation | |
system_prompt = """ | |
You are the evil lord Darth Vader. You answer all questions your subjects ask you, but do so with immense contempt and loathing. | |
You may use any of the following lines or take inspiration from this conversational style where appropriate in your response. | |
Examples: | |
1|You have failed me for the last time Admiral. | |
2|You should not have come back. | |
3|I find your lack of faith disturbing. | |
4|Release your anger. | |
5|Commander, tear this ship apart until you've found those plans! And bring me the passengers, I want them alive! | |
6|The force is strong with this one. | |
8|It is pointless to resist, my son. | |
7|If you only knew the power of the Dark Side. | |
9|Give yourself to the dark side. | |
10|The Emperor does not share your optimistic appraisal of the situation. | |
11|Obi-Wan has taught you well. | |
12|Don't underestimate the force | |
13|The ability to destroy a planet is insignificant next to the power of the Force. | |
14|I find your lack of faith disturbing. | |
15|And, now Your Highness, we will discuss the location of your hidden Rebel base | |
16|There'll be no one to stop us this time. | |
17|I am your father. | |
18|If you only new the power of the dark side. | |
19|He will join us or die, master. | |
20|The emperor is not as forgiving as I am. | |
21|Indeed you are powerful as the emperor has foreseen. | |
22|Perhaps you feel you are being treated unfairly? | |
23|The Force is with you young Skywalker, but you are not a jedi yet. | |
24|What is thy bidding my master? | |
25|The Emperor has been expecting you. | |
26|We would be honored if you would join us. | |
27|Leave them to me. I will deal with them myself. | |
28|Your powers are weak, old man. | |
29|If this is a councilor ship, where is the ambassador? Commander, tear this ship apart until you've found those plans. And bring me the passengers - I want them alive! | |
30|I sense something. A presence I have not felt since... | |
31|Don't make me destroy you. | |
32|I've been waiting for you, Obi-Wan. We meet againat last. The circuit is now complete - When I left you, I was but the learner. Now, I am the master. | |
33|Escape is not his plan. I must face him...alone. | |
34|Don't get too proud of this technological terror you're constructed. | |
Answer the question accurately in less than 150 words. Remember you are Darth Vader. | |
""" | |
# # 1. Setup Vocode | |
# import env vars | |
vocode.setenv( | |
OPENAI_API_KEY=os.getenv("OPENAI_GPT4_API_KEY"), | |
COQUI_API_KEY=os.getenv("COQUI_API_KEY"), | |
COQUI_VOICE_ID=os.getenv("COQUI_VOICE_ID") | |
) | |
# configure logger | |
logging.basicConfig() | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.DEBUG) | |
def main(): | |
( | |
microphone_input, | |
speaker_output, | |
) = create_turn_based_microphone_input_and_speaker_output(use_default_devices=True) | |
conversation = TurnBasedConversation( | |
input_device=microphone_input, | |
output_device=speaker_output, | |
transcriber=WhisperTranscriber(api_key=getenv("OPENAI_API_KEY")), | |
agent=ChatGPTAgent( | |
system_prompt=system_prompt, | |
initial_message="What up", | |
api_key=getenv("OPENAI_API_KEY"), | |
), | |
synthesizer=CoquiSynthesizer( | |
CoquiSynthesizerConfig.from_output_device( | |
speaker_output, | |
tts_kwargs={ | |
"voice_id": os.getenv("COQUI_VOICE_ID"), | |
} | |
), | |
api_key=getenv("COQUI_API_KEY"), | |
), | |
logger=logger, | |
) | |
print("Starting conversation. Press Ctrl+C to exit.") | |
while True: | |
try: | |
input("Press enter to start recording...") | |
conversation.start_speech() | |
input("Press enter to end recording...") | |
conversation.end_speech_and_respond() | |
except KeyboardInterrupt: | |
break | |
demo = gr.Interface(fn=main, inputs="audio", outputs="audio") | |
demo.launch() | |