Spaces:
Running
Running
import os | |
os.system("pip install openai") | |
os.system("pip install ipython") | |
import openai | |
import requests | |
import tempfile | |
import gradio as gr | |
from IPython.display import Audio | |
def get_chat_completion(prompt, openai_api_key, elevenlabs_api_key): | |
openai.api_key = openai_api_key | |
completion = openai.Completion.create( | |
engine="text-davinci-002", | |
prompt=prompt, | |
max_tokens=1024, | |
n=1, | |
stop=None, | |
temperature=0.7, | |
) | |
message = completion.choices[0].text.strip() | |
audio_path = text_to_audio(message, elevenlabs_api_key) | |
return message, audio_path | |
def text_to_audio(message,elevenlabs_api_key): | |
elevenLabs_url = 'https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM/stream' | |
elevenLabs_headers = { | |
'accept': '*/*', | |
'xi-api-key': elevenlabs_api_key, | |
'Content-Type': 'application/json' | |
} | |
elevenLabs_data = { | |
"text": message, | |
"voice_settings": { | |
"stability": 0, | |
"similarity_boost": 0 | |
} | |
} | |
response = requests.post(elevenLabs_url, headers=elevenLabs_headers, json=elevenLabs_data, stream=True) | |
if response.status_code == 200: | |
with tempfile.NamedTemporaryFile(delete=False) as f: | |
f.write(response.content) | |
audio_path = f.name | |
print("line 27 - fine") | |
return audio_path | |
else: | |
return None | |
input_text = gr.inputs.Textbox(label="Type your prompt. Example: Tell me a short story") | |
input_openAIKey = gr.inputs.Textbox(label="Please enter your OpenAI API key") | |
input_EleLabsAIKey = gr.inputs.Textbox(label="Please enter your Eleven Labs API key") | |
output_text = gr.outputs.Textbox(label="OpenAI response") | |
output_audio = gr.outputs.Audio(type="filepath", label="Eleven Labs audio") | |
title = "Audio style ChatGPT π" | |
article = gr.Interface(get_chat_completion, inputs=[input_text, input_openAIKey, input_EleLabsAIKey], outputs=[output_text, output_audio], title=title, cite="For feedback, please reach out to me on <a href='https://twitter.com/saurabhm6'>Twitter</a>.") | |
article.launch() |