Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,78 @@
|
|
|
|
|
|
|
|
|
|
1 |
import openai
|
2 |
import gradio as gr
|
3 |
from gtts import gTTS
|
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")
|
11 |
|
12 |
# Set OpenAI GPT-3 model
|
13 |
-
MODEL = "
|
14 |
|
15 |
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
response = openai.Completion.create(
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
# display speech output using IPython.display
|
38 |
-
display(Audio(speech_buffer.read(), format='audio/mp3'))
|
39 |
-
return message, speech_buffer.getvalue()
|
40 |
|
41 |
-
|
|
|
|
|
|
|
42 |
global chat_history
|
43 |
-
chat_history =
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
|
|
|
1 |
+
|
2 |
+
|
3 |
+
import os
|
4 |
+
import time
|
5 |
import openai
|
6 |
import gradio as gr
|
7 |
from gtts import gTTS
|
8 |
from io import BytesIO
|
9 |
from IPython.display import Audio, display
|
|
|
|
|
10 |
|
11 |
# Set OpenAI API key
|
12 |
openai.api_key = os.environ.get("OPENAI_API_KEY")
|
13 |
|
14 |
# Set OpenAI GPT-3 model
|
15 |
+
MODEL = "text-davinci-002"
|
16 |
|
17 |
|
18 |
+
# Initialize chat history as an empty list
|
19 |
+
chat_history = []
|
20 |
|
21 |
+
# Define function to generate speech from text using Google Text-to-Speech (gTTS)
|
22 |
+
def text_to_speech(text):
|
23 |
+
tts = gTTS(text=text)
|
24 |
+
mp3 = BytesIO()
|
25 |
+
tts.write_to_fp(mp3)
|
26 |
+
mp3.seek(0)
|
27 |
+
display(Audio(mp3, autoplay=True))
|
28 |
+
|
29 |
+
# Define function to get chatbot response
|
30 |
+
def chat(text):
|
31 |
+
# Append user input to chat history
|
32 |
+
chat_history.append(f"User: {text}")
|
33 |
+
|
34 |
+
# Use OpenAI's GPT-3.5 model to generate chatbot response
|
35 |
response = openai.Completion.create(
|
36 |
+
model=MODEL,
|
37 |
+
prompt=f"Conversation with user:\n{'\n'.join(chat_history)}\nChatbot:",
|
38 |
+
temperature=0.5,
|
39 |
+
max_tokens=1024,
|
40 |
+
n=1,
|
41 |
+
stop=None,
|
42 |
+
frequency_penalty=0,
|
43 |
+
presence_penalty=0
|
44 |
+
).choices[0].text.strip()
|
45 |
+
|
46 |
+
# Append chatbot response to chat history
|
47 |
+
chat_history.append(f"Chatbot: {response}")
|
48 |
+
|
49 |
+
# Generate speech from chatbot response
|
50 |
+
text_to_speech(response)
|
|
|
|
|
|
|
51 |
|
52 |
+
return response
|
53 |
+
|
54 |
+
# Define function to clear chat history
|
55 |
+
def clear_chat():
|
56 |
global chat_history
|
57 |
+
chat_history = []
|
58 |
+
|
59 |
+
# Define interface
|
60 |
+
interface = gr.Interface(
|
61 |
+
chat,
|
62 |
+
inputs=["text", gr.inputs.Voice()],
|
63 |
+
outputs=["text", gr.outputs.Voice()],
|
64 |
+
title="Chatbot with OpenAI's GPT-3.5 Model",
|
65 |
+
description="An interactive chatbot using OpenAI's GPT-3.5 model with chat persistence and voice inputs/outputs.",
|
66 |
+
theme="default",
|
67 |
+
layout="vertical",
|
68 |
+
allow_flagging=False,
|
69 |
+
allow_screenshot=False,
|
70 |
+
allow_download=False,
|
71 |
+
show_input=True,
|
72 |
+
show_output=True
|
73 |
+
)
|
74 |
+
|
75 |
+
# Run interface
|
76 |
+
interface.launch()
|
77 |
|
78 |
|