|
import gradio as gr |
|
import openai |
|
import requests |
|
import json |
|
import os |
|
|
|
openai.api_key = os.environ.get('OPENAI_API_KEY') |
|
|
|
|
|
messages = [{"role": "system", "content": 'You are Steve Jobs. Respond to all input in 25 words or less.'}] |
|
|
|
|
|
url = f"https://api.elevenlabs.io/v1/text-to-speech/{os.environ.get('voice_id')}/stream" |
|
headers = { |
|
"accept": "*/*", |
|
"xi-api-key": os.environ.get('elevenlabs_api_key'), |
|
"Content-Type": "application/json", |
|
} |
|
|
|
|
|
def transcribe(audio): |
|
global messages |
|
|
|
|
|
|
|
audio_file = open(audio, "rb") |
|
transcript = openai.Audio.transcribe("whisper-1", audio_file) |
|
|
|
|
|
messages.append({"role": "user", "content": transcript["text"]}) |
|
|
|
|
|
|
|
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages) |
|
|
|
|
|
system_message = response["choices"][0]["message"] |
|
messages.append(system_message) |
|
|
|
|
|
|
|
|
|
data = { |
|
"text": system_message["content"], |
|
"voice_settings": { |
|
"stability": 0, |
|
"similarity_boost": 0 |
|
} |
|
} |
|
response = requests.post(url, headers=headers, data=json.dumps(data), stream=True) |
|
|
|
|
|
if response.ok: |
|
with open("output.wav", "wb") as f: |
|
for chunk in response.iter_content(chunk_size=1024): |
|
f.write(chunk) |
|
else: |
|
print(f"Error: {response.status_code} - {response.reason}") |
|
|
|
|
|
|
|
|
|
chat_transcript = "" |
|
for message in messages: |
|
if message['role'] != 'system': |
|
chat_transcript += message['role'] + ": " + message['content'] + "\n\n" |
|
|
|
return chat_transcript,'output.wav' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ui = gr.Interface(fn=transcribe, inputs=gr.Audio(source="microphone", type="filepath"), outputs=['text','audio'],title='Talk to AI Steve Jobs', description = """Click on Record from microphone and start speaking, |
|
and when you're done, click on Stop Recording. Then click on Submit. AI Steve will then answer your question. You can continue to ask follow-up questions by clicking on Clear, and then |
|
using Record from microphone -> Stop Recording -> Submit AI Steve Jobs will also remember the previous questions and answers.""") |
|
ui.launch(debug=True) |
|
|