hectorjelly commited on
Commit
0ef7602
1 Parent(s): ec764f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
  import time
3
 
@@ -8,10 +9,10 @@ openai.api_key = os.getenv("OPENAI_API_KEY")
8
 
9
  import gradio as gr
10
 
11
-
12
  messages = []
13
 
14
-
15
  def add_user_text(chat_history, user_text):
16
  print('user_text_from_typing: ', user_text)
17
 
@@ -21,36 +22,35 @@ def add_user_text(chat_history, user_text):
21
  chat_history = chat_history + [(user_text, None)]
22
  return chat_history, gr.update(value="", interactive=False)
23
 
24
-
25
  def bot_respond(chat_history, openai_gpt_key):
26
  global messages
27
 
28
  if openai_gpt_key is not "":
29
  openai.api_key = openai_gpt_key
 
 
30
  bot_response = openai.ChatCompletion.create(
31
- model = 'gpt-3.5-turbo',
32
- messages = messages,
33
  )
34
  bot_text = bot_response["choices"][0]["message"]["content"]
35
- print("bot_text: ",bot_text)
36
 
37
  messages = messages + [{"role":'assistant', 'content': bot_text}]
38
 
39
  print(chat_history)
40
  chat_history[-1][1] = ""
 
 
41
  for character in bot_text:
42
  chat_history[-1][1] += character
43
  time.sleep(0.02)
44
  yield chat_history
45
 
46
-
47
  with gr.Blocks() as demo:
48
- # radio = gr.Radio(
49
- # value='gpt-3.5-turbo',
50
- # choices=['gpt-3.5-turbo','gpt-4'],
51
- # label='Model Options')
52
-
53
- openai_gpt_key = gr.Textbox(label="OpenAI GPT API Key", value="", placeholder="sk..", info = "If Error raised, you have to provide your own GPT keys for this app to function properly",)
54
  clear_btn = gr.Button("Clear for Restart")
55
  chat_history = gr.Chatbot([], elem_id="chat_history").style(height=500)
56
 
@@ -59,7 +59,8 @@ with gr.Blocks() as demo:
59
  show_label=False,
60
  placeholder="Enter text and press enter",
61
  ).style(container=False)
62
-
 
63
  user_text.submit(
64
  add_user_text, [chat_history, user_text], [chat_history, user_text], queue=False).then(
65
  bot_respond, [chat_history, openai_gpt_key], chat_history).then(
@@ -67,7 +68,6 @@ with gr.Blocks() as demo:
67
 
68
  clear_btn.click(lambda: None, None, chat_history, queue=False)
69
 
70
-
71
  if __name__ == "__main__":
72
  demo.queue()
73
  demo.launch()
 
1
+ # Import necessary libraries
2
  import os
3
  import time
4
 
 
9
 
10
  import gradio as gr
11
 
12
+ # Create an empty list to store chat messages
13
  messages = []
14
 
15
+ # Function to add user's text to chat history
16
  def add_user_text(chat_history, user_text):
17
  print('user_text_from_typing: ', user_text)
18
 
 
22
  chat_history = chat_history + [(user_text, None)]
23
  return chat_history, gr.update(value="", interactive=False)
24
 
25
+ # Function for the bot to respond
26
  def bot_respond(chat_history, openai_gpt_key):
27
  global messages
28
 
29
  if openai_gpt_key is not "":
30
  openai.api_key = openai_gpt_key
31
+
32
+ # Generate response from OpenAI Chat API
33
  bot_response = openai.ChatCompletion.create(
34
+ model='gpt-3.5-turbo',
35
+ messages=messages,
36
  )
37
  bot_text = bot_response["choices"][0]["message"]["content"]
38
+ print("bot_text: ", bot_text)
39
 
40
  messages = messages + [{"role":'assistant', 'content': bot_text}]
41
 
42
  print(chat_history)
43
  chat_history[-1][1] = ""
44
+
45
+ # Yield the chat history with the bot's response character by character
46
  for character in bot_text:
47
  chat_history[-1][1] += character
48
  time.sleep(0.02)
49
  yield chat_history
50
 
51
+ # Create a Gradio interface
52
  with gr.Blocks() as demo:
53
+ openai_gpt_key = gr.Textbox(label="OpenAI GPT API Key", value="", placeholder="sk..", info="If an error is raised, you need to provide your own GPT keys for this app to function properly")
 
 
 
 
 
54
  clear_btn = gr.Button("Clear for Restart")
55
  chat_history = gr.Chatbot([], elem_id="chat_history").style(height=500)
56
 
 
59
  show_label=False,
60
  placeholder="Enter text and press enter",
61
  ).style(container=False)
62
+
63
+ # Handle user input and bot response
64
  user_text.submit(
65
  add_user_text, [chat_history, user_text], [chat_history, user_text], queue=False).then(
66
  bot_respond, [chat_history, openai_gpt_key], chat_history).then(
 
68
 
69
  clear_btn.click(lambda: None, None, chat_history, queue=False)
70
 
 
71
  if __name__ == "__main__":
72
  demo.queue()
73
  demo.launch()