Aniquel commited on
Commit
f094adb
1 Parent(s): f1fd01a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -38
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
- # Concatenate input text with chat history
24
- input_text_with_history = input_text + "\n\n" + chat_history
25
-
26
- # Use OpenAI API to generate response
27
  response = openai.Completion.create(
28
- model=MODEL,
29
- prompt=input_text_with_history,
30
- max_tokens=3000,
31
- n=1,
32
- stop=None,
33
- temperature=0.5
34
  )
35
-
36
- # Extract response text from OpenAI API output
37
- response_text = response.choices[0].text.strip()
38
-
39
- # Convert response text to speech
40
- tts = gTTS(text=response_text)
41
- audio_bytes = BytesIO()
42
- tts.write_to_fp(audio_bytes)
43
- audio_bytes.seek(0)
44
- response_audio = Audio(audio_bytes, autoplay=True)
45
-
46
- # Update chat history with input and response text
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 = ""