chatty_vader / app.py
aar2dee2
pass audio file to transcriber directly
9e7aeb0
raw
history blame
4.7 kB
# # Import required libraries
import vocode
from vocode import getenv
import gradio as gr
import os
import logging
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
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(input_audio):
try:
# Initialize WhisperTranscriber
transcriber = WhisperTranscriber(api_key=getenv("OPENAI_API_KEY"))
except Exception as e:
logger.error("Failed to initialize WhisperTranscriber: %s", e)
return None
try:
# Initialize ChatGPTAgent
agent = ChatGPTAgent(
system_prompt=system_prompt,
initial_message="What up",
api_key=getenv("OPENAI_API_KEY"),
)
except Exception as e:
logger.error("Failed to initialize ChatGPTAgent: %s", e)
return None
try:
# Initialize CoquiSynthesizer
synthesizer = CoquiSynthesizer(
voice_id=os.getenv("COQUI_VOICE_ID"),
api_key=getenv("COQUI_API_KEY"),
)
except Exception as e:
logger.error("Failed to initialize CoquiSynthesizer: %s", e)
return None
print("Starting conversation. Press Ctrl+C to exit.")
while True:
try:
# Transcribe the input_audio using WhisperTranscriber
transcript = transcriber.transcribe(input_audio)
except Exception as e:
logger.error("Failed to transcribe audio: %s", e)
break
try:
# Generate response using ChatGPTAgent
response = agent.generate_response(transcript)
except Exception as e:
logger.error("Failed to generate response: %s", e)
break
try:
# Synthesize the response into audio using CoquiSynthesizer
output_audio = synthesizer.synthesize(response)
except Exception as e:
logger.error("Failed to synthesize response: %s", e)
break
return output_audio
demo = gr.Interface(fn=main, inputs="audio", outputs="audio")
demo.launch()