-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
46 lines (35 loc) · 1.22 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import openai
import streamlit as st
import requests
from streamlit.components.v1 import html
openai.api_key = st.secrets.api_credentials.openapi_key
def generate_response(prompt):
st.session_state['messages'].append({"role": "user", "content": prompt})
completion = openai.ChatCompletion.create(
model="gpt-4-turbo",
messages=st.session_state['messages']
)
response = completion.choices[0].message.content
st.session_state['messages'].append({"role": "assistant", "content": response})
return response
def speak(text: str):
chunk_size = 1024
url = "https://api.elevenlabs.io/v1/text-to-speech/kJwrKss0EAIKMz7FSU7r"
headers = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": st.secrets.api_credentials.elevenlabsapi_key
}
data = {
"text": text,
"model_id": "eleven_multilingual_v1",
"voice_settings": {
"stability": 0.7,
"similarity_boost": 0.7
}
}
response = requests.post(url, json=data, headers=headers)
with open('output.mp3', 'wb') as f:
for chunk in response.iter_content(chunk_size=chunk_size):
if chunk:
f.write(chunk)