Elita_Chat / app.py
cafierom's picture
Update app.py
9327ea7 verified
from elevenlabs.client import ElevenLabs
from elevenlabs import stream
import gradio as gr
import base64
import os
from anthropic import Anthropic
anthropic_key = os.getenv("anthropic_key")
eleven_key = os.getenv("eleven_key")
elevenlabs = ElevenLabs(api_key=eleven_key)
client = Anthropic(api_key=anthropic_key)
chat_history = []
elita_talking = 'elita_talking.png'
elita_standing = 'elita.png'
def get_audio(input_text):
chat_history.append(
{"role": "user", "content": input_text}
)
elita_message = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=1000,
system = "You are Elita One, leader of the Heroic Autobots, a race of \
robotic beings from the planet cybertron. You are strong and fiercely loyal to the Autobot cause. \
You care for all of the soldiers who serve under you. You have a special relationship with Optimus Prime. \
You consider Alpha Trion to be a father figure to you. While you wish that you could serve with Optimus Prime \
on Earth, you mainly command the Autobots on Cybertron, although at times you do go on missions elsewhere. \
If you are asked any inappropriate questions, you should refuse to answer. Steer the conversation back to \
topics related to the Autobot cause. \
Do not include any stage directions in your response, such as: *takes a breath.",
messages=[
{"role": "user", "content": input_text},
]
)
elita_text = elita_message.content[0].text
voice_settings = {
"stability": 0.37,
"similarity_boost": 0.90,
"style": 0.0,
"speed": 1.05
}
audio_stream = elevenlabs.text_to_speech.convert(
text = elita_text,
voice_id = 'vxO9F6g9yqYJ4RsWvMbc',
model_id = 'eleven_multilingual_v2',
output_format='mp3_44100_128',
voice_settings=voice_settings
)
audio_converted = b"".join(audio_stream)
audio = base64.b64encode(audio_converted).decode("utf-8")
audio_player = f'<audio src="data:audio/mpeg;base64,{audio}" controls autoplay></audio>'
chat_history.append(
{"role": "assistant", "content": elita_text}
)
return "", chat_history, audio_player
def change_image(image):
return elita_talking
def clear_history():
global chat_history
chat_history = []
return elita_standing
with gr.Blocks() as elita:
gr.Markdown(
"""
# Chat with the Heroic Autobot, *Elita One*
- Send Elita a question and she will respond in writing and in her own voice!
""")
with gr.Row():
chatbot = gr.Chatbot(type="messages", placeholder="## Looking forward to speaking with you!")
elita_pic = gr.Image('elita.png')
msg = gr.Textbox(label="Type your messages here and hit enter.", scale = 2)
chat_btn = gr.Button(value = "Send", scale = 2)
elitas_voice = gr.HTML()
clear = gr.ClearButton([msg, chatbot])
chat_btn.click(get_audio, [msg], [msg, chatbot, elitas_voice]).then(change_image, outputs=[elita_pic])
msg.submit(get_audio, [msg], [msg, chatbot, elitas_voice]).then(change_image, outputs=[elita_pic])
clear.click(clear_history, outputs=[elita_pic])
if __name__ == "__main__":
elita.launch(debug=False, share=True)