Aniquel commited on
Commit
e61a4ec
1 Parent(s): 5be3769

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -41
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 = "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 = ""
44
- interface = gr.Interface(
45
- fn=chat,
46
- inputs=["text", gr.inputs.Dropdown(["en-US-Wavenet-A", "en-US-Wavenet-B", "en-US-Wavenet-C", "en-US-Wavenet-D"])],
47
- outputs=["text", "audio"],
48
- layout="vertical",
49
- title="OpenAI Chatbot with Speech Output",
50
- description="Chat with the OpenAI GPT-3 chatbot and get speech output of the bot's response."
51
- )
52
-
53
- interface.launch()
54
- while True:
55
- time.sleep(0.1)
56
- with open("chat_history.txt", "w") as f:
57
- f.write(chat_history)
 
 
 
 
 
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