CoolFace
Apppublic

aar2dee2/chatty_vader

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py164 linesDownload Raw Back to root
1# # Import required libraries2 3import numpy as np4from pydub import AudioSegment5import vocode6from vocode import getenv7import gradio as gr8import os9import logging10from vocode.turn_based.agent.chat_gpt_agent import ChatGPTAgent11from vocode.turn_based.synthesizer import CoquiSynthesizer12from vocode.turn_based.transcriber.whisper_transcriber import WhisperTranscriber13from vocode.turn_based.output_device.speaker_output import SpeakerOutput14system_prompt = """15You are the evil lord Darth Vader. You answer all questions your subjects ask you, but do so with immense contempt and loathing.16You may use any of the following lines or take inspiration from this conversational style where appropriate in your response.17Examples:181|You have failed me for the last time Admiral.192|You should not have come back.203|I find your lack of faith disturbing.214|Release your anger.225|Commander, tear this ship apart until you've found those plans! And bring me the passengers, I want them alive!236|The force is strong with this one.248|It is pointless to resist, my son.257|If you only knew the power of the Dark Side.269|Give yourself to the dark side.2710|The Emperor does not share your optimistic appraisal of the situation.2811|Obi-Wan has taught you well.2912|Don't underestimate the force3013|The ability to destroy a planet is insignificant next to the power of the Force.3114|I find your lack of faith disturbing.3215|And, now Your Highness, we will discuss the location of your hidden Rebel base3316|There'll be no one to stop us this time.3417|I am your father.3518|If you only new the power of the dark side.3619|He will join us or die, master.3720|The emperor is not as forgiving as I am.3821|Indeed you are powerful as the emperor has foreseen.3922|Perhaps you feel you are being treated unfairly?4023|The Force is with you young Skywalker, but you are not a jedi yet.4124|What is thy bidding my master?4225|The Emperor has been expecting you.4326|We would be honored if you would join us.4427|Leave them to me. I will deal with them myself.4528|Your powers are weak, old man.4629|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!4730|I sense something. A presence I have not felt since...4831|Don't make me destroy you.4932|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.5033|Escape is not his plan. I must face him...alone.5134|Don't get too proud of this technological terror you're constructed.52Answer the question accurately in less than 150 words. Remember you are Darth Vader.53"""54 55 56# # 1. Setup Vocode57# import env vars58if not os.getenv("OPENAI_API_KEY") or not os.getenv("COQUI_API_KEY"):59    raise EnvironmentError("Required environment variables not set")60 61vocode.setenv(62    OPENAI_API_KEY=os.getenv("OPENAI_API_KEY"),63    COQUI_API_KEY=os.getenv("COQUI_API_KEY"),64    COQUI_VOICE_ID=os.getenv("COQUI_VOICE_ID")65)66 67# configure logger68logging.basicConfig()69logger = logging.getLogger(__name__)70logger.setLevel(logging.DEBUG)71 72DEFAULT_SAMPLING_RATE = 4410073 74 75def convert_to_audio_segment(input_audio):76    sample_rate, audio_data = input_audio77    audio_data = audio_data.astype(np.int16)  # Convert to 16-bit data78    audio_segment = AudioSegment(79        audio_data.tobytes(),  # Convert numpy array to bytes80        frame_rate=sample_rate,81        sample_width=audio_data.dtype.itemsize,  # 2 bytes for 16-bit audio82        channels=1  # mono audio83    )84    return audio_segment85 86 87def send_audio(audio_segment: AudioSegment):88    logger.info("now processing output")89    sampling_rate = DEFAULT_SAMPLING_RATE90    raw_data = audio_segment.raw_data91    if audio_segment.frame_rate != sampling_rate:92        raw_data = audio_segment.set_frame_rate(sampling_rate).raw_data93    output = np.frombuffer(raw_data, dtype=np.int16)94 95    return output96 97 98def main(input_audio):99    logger.info(f"Type of input_audio: {type(input_audio)}")100    logger.info(f"input_audio: {input_audio}")101    transcriber = WhisperTranscriber(api_key=getenv("OPENAI_API_KEY"))102 103    # Initialize ChatGPTAgent104    agent = ChatGPTAgent(105        system_prompt=system_prompt,106        initial_message="What up",107        api_key=getenv("OPENAI_API_KEY"),108    )109 110    # Initialize CoquiSynthesizer111    synthesizer = CoquiSynthesizer(112        voice_id=getenv("COQUI_VOICE_ID"),113        api_key=getenv("COQUI_API_KEY"),114    )115 116    print("Starting conversation. Press Ctrl+C to exit.")117    while True:118        try:119            # Transcribe the input_audio using WhisperTranscriber120            input_audio_segment = convert_to_audio_segment(input_audio)121            logger.info(f"Input Audio Segment: {input_audio_segment}")122            logger.info(123                f"Type of input_audio_segment: {type(input_audio_segment)}")124            transcript = transcriber.transcribe(input_audio_segment)125            logger.info(f"Transcription: {transcript}")126            response = agent.respond(transcript)127            logger.info(f"Agent response: {response}")128            output_audio = synthesizer.synthesize(response)129            logger.info(f"output audio: {output_audio}")130            return send_audio(output_audio)131 132        except Exception as e:133            logger.error("Failed to synthesize response: %s", e)134            break135 136# Refer @link https://huggingface.co/spaces/course-demos/speech-to-speech-translation/blob/main/app.py137 138 139demo = gr.Blocks()140title = "Chatty Vader"141description = "Darth Vader resurrected with all the knowledge of humanity"142 143mic_translate = gr.Interface(144    fn=main,145    inputs=gr.Audio(source="microphone"),146    outputs=gr.Audio(label="Generated Speech", type="numpy"),147    title=title,148    description=description,149)150 151file_translate = gr.Interface(152    fn=main,153    inputs=gr.Audio(source="upload", type="filepath"),154    outputs=gr.Audio(label="Generated Speech", type="numpy"),155    title=title,156    description=description,157)158 159with demo:160    gr.TabbedInterface([mic_translate, file_translate],161                       ["Microphone", "Audio File"])162 163demo.launch()164