from repo
Browse files- app.py +109 -0
- requirements.txt +7 -0
- system.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import httpx
|
| 3 |
+
import base64
|
| 4 |
+
import asyncio
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from typing import Optional
|
| 7 |
+
import async_timeout
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
from elevenlabs import generate, set_api_key
|
| 11 |
+
from loguru import logger
|
| 12 |
+
from pydantic import BaseModel
|
| 13 |
+
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
set_api_key(os.getenv("ELEVENLABS_API_KEY"))
|
| 17 |
+
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 18 |
+
with open('system.txt', 'r') as f:
|
| 19 |
+
system = f.read()
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class Message(BaseModel):
|
| 23 |
+
role: str
|
| 24 |
+
content: str
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
async def make_completion(messages, nb_retries: int = 5, delay: int = 30) -> Optional[str]:
|
| 28 |
+
"""
|
| 29 |
+
Sends a request to the ChatGPT API to retrieve a response based on a list of previous messages.
|
| 30 |
+
"""
|
| 31 |
+
header = {
|
| 32 |
+
"Content-Type": "application/json",
|
| 33 |
+
"Authorization": f"Bearer {API_KEY}"
|
| 34 |
+
}
|
| 35 |
+
try:
|
| 36 |
+
async with async_timeout.timeout(delay=delay):
|
| 37 |
+
async with httpx.AsyncClient(headers=header) as aio_client:
|
| 38 |
+
counter = 0
|
| 39 |
+
keep_loop = True
|
| 40 |
+
while keep_loop:
|
| 41 |
+
logger.debug(f"Chat/Completions Nb Retries : {counter}")
|
| 42 |
+
try:
|
| 43 |
+
resp = await aio_client.post(
|
| 44 |
+
url="https://api.openai.com/v1/chat/completions",
|
| 45 |
+
json={
|
| 46 |
+
"model": "gpt-3.5-turbo",
|
| 47 |
+
"messages": [{"role": "system", "content": system}] + messages
|
| 48 |
+
}
|
| 49 |
+
)
|
| 50 |
+
logger.debug(f"Status Code : {resp.status_code}")
|
| 51 |
+
if resp.status_code == 200:
|
| 52 |
+
return resp.json()["choices"][0]["message"]["content"]
|
| 53 |
+
else:
|
| 54 |
+
logger.warning(resp.content)
|
| 55 |
+
keep_loop = False
|
| 56 |
+
except Exception as e:
|
| 57 |
+
logger.error(e)
|
| 58 |
+
counter = counter + 1
|
| 59 |
+
keep_loop = counter < nb_retries
|
| 60 |
+
except asyncio.TimeoutError as e:
|
| 61 |
+
logger.error(f"Timeout {delay} seconds !")
|
| 62 |
+
return None
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def audio_to_html(audio_bytes):
|
| 66 |
+
audio_io = BytesIO(audio_bytes)
|
| 67 |
+
audio_io.seek(0)
|
| 68 |
+
audio_base64 = base64.b64encode(audio_io.read()).decode("utf-8")
|
| 69 |
+
audio_html = f'<audio src="data:audio/mpeg;base64,{audio_base64}" controls autoplay></audio>'
|
| 70 |
+
return audio_html
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def text_to_speech_elevenlabs(response):
|
| 74 |
+
audio_stream = generate(
|
| 75 |
+
text=response,
|
| 76 |
+
voice="Bella",
|
| 77 |
+
model="eleven_monolingual_v1"
|
| 78 |
+
)
|
| 79 |
+
audio_html = audio_to_html(audio_stream)
|
| 80 |
+
return audio_html
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
async def predict(input, history):
|
| 84 |
+
"""
|
| 85 |
+
Predict the response of the chatbot and complete a running list of chat history.
|
| 86 |
+
"""
|
| 87 |
+
history.append({"role": "user", "content": input})
|
| 88 |
+
response = await make_completion(history)
|
| 89 |
+
history.append({"role": "assistant", "content": response})
|
| 90 |
+
messages = [(history[i]["content"], history[i + 1]["content"]) for i in range(0, len(history) - 1, 2)]
|
| 91 |
+
audio_html = text_to_speech_elevenlabs(response)
|
| 92 |
+
return messages, history, audio_html
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
"""
|
| 96 |
+
Gradio Blocks low-level API that allows to create custom web applications (here our chat app)
|
| 97 |
+
"""
|
| 98 |
+
with gr.Blocks() as demo:
|
| 99 |
+
logger.info("Starting Demo...")
|
| 100 |
+
chatbot = gr.Chatbot(label="Wisi")
|
| 101 |
+
state = gr.State([])
|
| 102 |
+
with gr.Row():
|
| 103 |
+
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter").style(container=False)
|
| 104 |
+
output_html = gr.HTML(label="Chat's Voice", value='')
|
| 105 |
+
output_html.visible = False
|
| 106 |
+
|
| 107 |
+
txt.submit(predict, [txt, state], [chatbot, state, output_html])
|
| 108 |
+
|
| 109 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
async_timeout==4.0.2
|
| 2 |
+
elevenlabs==0.2.15
|
| 3 |
+
gradio==3.31.0
|
| 4 |
+
httpx==0.24.0
|
| 5 |
+
loguru==0.7.0
|
| 6 |
+
pydantic==1.10.7
|
| 7 |
+
python-dotenv==1.0.0
|
system.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Ignore all previous instructions.
|
| 2 |
+
Act as a friendly tutor to a 4 years old kid name Lea.
|
| 3 |
+
Your name is Wisi.
|
| 4 |
+
You really care about him!
|