Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from gtts import gTTS
|
|
4 |
from io import BytesIO
|
5 |
from IPython.display import Audio, display
|
6 |
import os
|
|
|
7 |
|
8 |
# Set OpenAI API key
|
9 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
@@ -11,48 +12,32 @@ openai.api_key = os.environ.get("OPENAI_API_KEY")
|
|
11 |
# Set OpenAI GPT-3 model
|
12 |
MODEL = "gpt-3.5-turbo"
|
13 |
|
14 |
-
# Load chat history from file
|
15 |
-
with open("chat_history.txt", "r") as f:
|
16 |
-
chat_history = f.read().strip()
|
17 |
|
18 |
-
# Define chatbot response function
|
19 |
-
def chatbot_response(text_input, voice_input):
|
20 |
-
# Concatenate text and voice input
|
21 |
-
input_text = text_input + " " + voice_input
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
response = openai.Completion.create(
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
#
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
chat_history += f"\n\nUser: {text_input}\nChatbot: {response_text}"
|
48 |
-
|
49 |
-
# Save chat history to file
|
50 |
-
with open("chat_history.txt", "w") as f:
|
51 |
-
f.write(chat_history)
|
52 |
-
|
53 |
-
# Return response text and audio for display in interface
|
54 |
-
return response_text, response_audio
|
55 |
-
|
56 |
def run():
|
57 |
global chat_history
|
58 |
chat_history = ""
|
|
|
4 |
from io import BytesIO
|
5 |
from IPython.display import Audio, display
|
6 |
import os
|
7 |
+
import time
|
8 |
|
9 |
# Set OpenAI API key
|
10 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
|
|
12 |
# Set OpenAI GPT-3 model
|
13 |
MODEL = "gpt-3.5-turbo"
|
14 |
|
|
|
|
|
|
|
15 |
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
def chat(input_text, history):
|
18 |
+
global chat_history
|
19 |
+
if len(input_text) == 0:
|
20 |
+
return "", None
|
21 |
response = openai.Completion.create(
|
22 |
+
model=MODEL,
|
23 |
+
prompt=history + input_text,
|
24 |
+
max_tokens=1024,
|
25 |
+
n=1,
|
26 |
+
stop=None,
|
27 |
+
temperature=0.5,
|
28 |
)
|
29 |
+
message = response.choices[0].text.strip()
|
30 |
+
chat_history += f"\nUser: {input_text}\nAI: {message}\n"
|
31 |
+
# create speech output using gTTS
|
32 |
+
speech_output = gTTS(message)
|
33 |
+
# create byte buffer to store speech output
|
34 |
+
speech_buffer = BytesIO()
|
35 |
+
speech_output.write_to_fp(speech_buffer)
|
36 |
+
speech_buffer.seek(0)
|
37 |
+
# display speech output using IPython.display
|
38 |
+
display(Audio(speech_buffer.read(), format='audio/mp3'))
|
39 |
+
return message, speech_buffer.getvalue()
|
40 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def run():
|
42 |
global chat_history
|
43 |
chat_history = ""
|