Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tempfile
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
import re
|
| 6 |
+
from elevenlabs import TTS
|
| 7 |
+
import requests
|
| 8 |
+
import os
|
| 9 |
+
|
| 10 |
+
# OpenAI and ElevenLabs clients
|
| 11 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 12 |
+
tts = TTS(api_key=os.getenv("ELEVENLABS_API_KEY"))
|
| 13 |
+
|
| 14 |
+
def chatbot(persona, input):
|
| 15 |
+
|
| 16 |
+
completion = client.chat.completions.create(
|
| 17 |
+
model="gpt-3.5-turbo-1106",
|
| 18 |
+
messages=[
|
| 19 |
+
{"role": "system", "content": persona},
|
| 20 |
+
{"role": "user", "content": input}
|
| 21 |
+
]
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
message = str(completion.choices[0].message)
|
| 25 |
+
text_in_quotes = re.findall(r'"([^"]*)"', message)
|
| 26 |
+
input_text = str(text_in_quotes[0])
|
| 27 |
+
|
| 28 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 29 |
+
audio_file = os.path.join(tmpdir, "response.mp3")
|
| 30 |
+
|
| 31 |
+
# Text and voice settings for TTS request
|
| 32 |
+
data = {
|
| 33 |
+
"text": input_text,
|
| 34 |
+
"model_id": "eleven_multilingual_v2",
|
| 35 |
+
"voice_settings": {
|
| 36 |
+
"stability": 1.0,
|
| 37 |
+
"similarity_boost": 1.0,
|
| 38 |
+
"excitement": 0.9,
|
| 39 |
+
"speed": 1.1,
|
| 40 |
+
"volume": 80,
|
| 41 |
+
"pitch": 1.1,
|
| 42 |
+
"breathiness": 0.8,
|
| 43 |
+
"voice_id": "eIH76iW9yjOWnOKIkd6y"
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
response = tts.generate(text=input_text, voice=voice_id)
|
| 48 |
+
response.save(audio_file)
|
| 49 |
+
|
| 50 |
+
return open(audio_file, "rb")
|
| 51 |
+
|
| 52 |
+
iface = gr.Interface(
|
| 53 |
+
fn=chatbot,
|
| 54 |
+
inputs=[
|
| 55 |
+
"textbox",
|
| 56 |
+
"text"
|
| 57 |
+
],
|
| 58 |
+
outputs="audio",
|
| 59 |
+
capture_session=True)
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
iface.launch(share=True)
|