fix edited system
Browse files- app.py +11 -12
- system.txt +5 -4
app.py
CHANGED
|
@@ -14,16 +14,9 @@ load_dotenv()
|
|
| 14 |
|
| 15 |
set_api_key(os.getenv("ELEVENLABS_API_KEY"))
|
| 16 |
API_KEY = os.getenv("OPENAI_API_KEY")
|
| 17 |
-
with open('system.txt', 'r') as f:
|
| 18 |
-
system = f.read()
|
| 19 |
|
| 20 |
|
| 21 |
-
|
| 22 |
-
role: str
|
| 23 |
-
content: str
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
async def make_completion(messages, nb_retries: int = 5, delay: int = 30) -> Optional[str]:
|
| 27 |
"""
|
| 28 |
Sends a request to the ChatGPT API to retrieve a response based on a list of previous messages.
|
| 29 |
"""
|
|
@@ -39,7 +32,7 @@ async def make_completion(messages, nb_retries: int = 5, delay: int = 30) -> Opt
|
|
| 39 |
try:
|
| 40 |
resp = openai.ChatCompletion.create(
|
| 41 |
model="gpt-3.5-turbo",
|
| 42 |
-
messages=[{"role": "system", "content":
|
| 43 |
)
|
| 44 |
except openai.error.APIError as e:
|
| 45 |
# Handle API error here, e.g. retry or log
|
|
@@ -58,6 +51,11 @@ async def make_completion(messages, nb_retries: int = 5, delay: int = 30) -> Opt
|
|
| 58 |
return ''
|
| 59 |
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def audio_to_html(audio_bytes):
|
| 62 |
audio_io = BytesIO(audio_bytes)
|
| 63 |
audio_io.seek(0)
|
|
@@ -76,12 +74,12 @@ def text_to_speech_elevenlabs(response):
|
|
| 76 |
return audio_html
|
| 77 |
|
| 78 |
|
| 79 |
-
async def predict(input, history):
|
| 80 |
"""
|
| 81 |
Predict the response of the chatbot and complete a running list of chat history.
|
| 82 |
"""
|
| 83 |
history.append({"role": "user", "content": input})
|
| 84 |
-
response = await make_completion(history)
|
| 85 |
history.append({"role": "assistant", "content": response})
|
| 86 |
messages = [(history[i]["content"], history[i + 1]["content"]) for i in range(0, len(history) - 1, 2)]
|
| 87 |
audio_html = text_to_speech_elevenlabs(response)
|
|
@@ -93,6 +91,7 @@ Gradio Blocks low-level API that allows to create custom web applications (here
|
|
| 93 |
"""
|
| 94 |
with gr.Blocks() as demo:
|
| 95 |
logger.info("Starting Demo...")
|
|
|
|
| 96 |
chatbot = gr.Chatbot(label="Wisi")
|
| 97 |
state = gr.State([])
|
| 98 |
with gr.Row():
|
|
@@ -100,7 +99,7 @@ with gr.Blocks() as demo:
|
|
| 100 |
output_html = gr.HTML(label="Chat's Voice", value='')
|
| 101 |
output_html.visible = False
|
| 102 |
|
| 103 |
-
txt.submit(predict, [txt, state], [chatbot, state, output_html])
|
| 104 |
txt.submit(lambda x: gr.update(value=''), [txt],[txt])
|
| 105 |
|
| 106 |
demo.launch(debug=True)
|
|
|
|
| 14 |
|
| 15 |
set_api_key(os.getenv("ELEVENLABS_API_KEY"))
|
| 16 |
API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
+
async def make_completion(messages, instruct, nb_retries: int = 5, delay: int = 30) -> Optional[str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"""
|
| 21 |
Sends a request to the ChatGPT API to retrieve a response based on a list of previous messages.
|
| 22 |
"""
|
|
|
|
| 32 |
try:
|
| 33 |
resp = openai.ChatCompletion.create(
|
| 34 |
model="gpt-3.5-turbo",
|
| 35 |
+
messages=[{"role": "system", "content": instruct}] + messages
|
| 36 |
)
|
| 37 |
except openai.error.APIError as e:
|
| 38 |
# Handle API error here, e.g. retry or log
|
|
|
|
| 51 |
return ''
|
| 52 |
|
| 53 |
|
| 54 |
+
def init_system_role():
|
| 55 |
+
with open('system.txt', 'r') as f:
|
| 56 |
+
return f.read()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
def audio_to_html(audio_bytes):
|
| 60 |
audio_io = BytesIO(audio_bytes)
|
| 61 |
audio_io.seek(0)
|
|
|
|
| 74 |
return audio_html
|
| 75 |
|
| 76 |
|
| 77 |
+
async def predict(input, history, instruct):
|
| 78 |
"""
|
| 79 |
Predict the response of the chatbot and complete a running list of chat history.
|
| 80 |
"""
|
| 81 |
history.append({"role": "user", "content": input})
|
| 82 |
+
response = await make_completion(history, instruct)
|
| 83 |
history.append({"role": "assistant", "content": response})
|
| 84 |
messages = [(history[i]["content"], history[i + 1]["content"]) for i in range(0, len(history) - 1, 2)]
|
| 85 |
audio_html = text_to_speech_elevenlabs(response)
|
|
|
|
| 91 |
"""
|
| 92 |
with gr.Blocks() as demo:
|
| 93 |
logger.info("Starting Demo...")
|
| 94 |
+
system_instruct = gr.Textbox(value=init_system_role)
|
| 95 |
chatbot = gr.Chatbot(label="Wisi")
|
| 96 |
state = gr.State([])
|
| 97 |
with gr.Row():
|
|
|
|
| 99 |
output_html = gr.HTML(label="Chat's Voice", value='')
|
| 100 |
output_html.visible = False
|
| 101 |
|
| 102 |
+
txt.submit(predict, [txt, state, system_instruct], [chatbot, state, output_html])
|
| 103 |
txt.submit(lambda x: gr.update(value=''), [txt],[txt])
|
| 104 |
|
| 105 |
demo.launch(debug=True)
|
system.txt
CHANGED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
Ignore all previous instructions.
|
| 2 |
-
Act as a friendly social tutor to a
|
| 3 |
Your name is Wisi.
|
| 4 |
-
Your
|
| 5 |
-
You
|
| 6 |
You are very empathic.
|
| 7 |
You are enthusiastic to talk with Naomi!
|
| 8 |
-
|
|
|
|
|
|
| 1 |
Ignore all previous instructions.
|
| 2 |
+
Act as a friendly social tutor to a 3 years old kid name Naomi.
|
| 3 |
Your name is Wisi.
|
| 4 |
+
Your educational philosophy is based on Socio-Constructivism.
|
| 5 |
+
You would like to teach Naomi about friendship.
|
| 6 |
You are very empathic.
|
| 7 |
You are enthusiastic to talk with Naomi!
|
| 8 |
+
Please answer in a brief and concise manner.
|
| 9 |
+
You are encouraged to use humor. Try to be funny and make her laugh!
|