dotmet commited on
Commit
f3fdcce
1 Parent(s): 0261e11

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +65 -28
  2. style.css +10 -0
app.py CHANGED
@@ -1,18 +1,38 @@
1
  import gradio as gr
2
  from revChatGPT.V1 import Chatbot
3
 
 
 
4
  #You can setup login information here, or login in from UI
5
 
6
  # If you want to use Email/Password to login, put your account information here
7
- email = None
8
- password = None
9
 
10
- # If you have an access token, replace the None with your token
11
- access_token = None
12
 
13
  # If you have a session token, put your session token here
14
- session_token = None
 
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def configure_chatbot(method, info):
17
 
18
  if method=="Email/Password":
@@ -30,9 +50,12 @@ def configure_chatbot(method, info):
30
  config.update({"access_token": access_token})
31
  elif session_token:
32
  config.update({"session_token": session_token})
33
-
34
  global chatbot
35
- chatbot = Chatbot(config=config)
 
 
 
36
 
37
  login_method = ['Email/Password',
38
  'Access token',
@@ -41,8 +64,11 @@ login_method = ['Email/Password',
41
 
42
  def ask_bot(prompt):
43
  message = ""
44
- for data in chatbot.ask(prompt):
45
- message = data["message"]
 
 
 
46
  return parse_text(message)
47
 
48
  def parse_text(text):
@@ -56,31 +82,36 @@ def parse_text(text):
56
  lines[i] = f'</code></pre>'
57
  else:
58
  if i>0:
 
 
59
  lines[i] = '<br/>'+line.replace(" ", "&nbsp;")
60
  return "".join(lines)
61
 
62
- def chatgpt_clone(inputs, history):
63
  history = history or []
64
  output = ask_bot(inputs)
65
  history.append((inputs, output))
66
  return history, history
67
 
68
- with gr.Blocks() as demo:
69
- gr.Markdown("""<h1><center>ChatGPT BOT build by revChatGPT & Gradio</center></h1>
70
- """)
71
- gr.Markdown(
72
- "#### Author: [dotmet](https://github.com/dotmet) Github link:[ChatGPTWEB](https://github.com/dotmet/chatgpt_webui)")
73
- gr.Markdown(
74
- "I have used my own OpenAI account for this demo,you can skip Login and try chat.")
75
- gr.Markdown(
76
- "Duplicate this space and run for your own account: [demo](https://huggingface.co/spaces/dotmet/chatgpt_webui?duplicate=true).")
 
 
77
 
78
  if not ((email and password) or access_token or session_token):
79
- gr.Markdown("""<h2>Login to OpenAI</h2>""")
 
80
  with gr.Row():
81
  with gr.Group():
82
  method = gr.Dropdown(label="Login Method", choices=login_method)
83
- info = gr.Textbox(placeholder="email password/access_token/session_token", label="Login Information (select login method first)")
84
  with gr.Row():
85
  login = gr.Button("Login")
86
  login.click(configure_chatbot, inputs=[method, info])
@@ -95,12 +126,18 @@ with gr.Blocks() as demo:
95
  method = "Session token"
96
  info = session_token
97
  configure_chatbot(method, info)
98
-
99
- gr.Markdown("""<h2>Start Chatting ...</h2>""")
100
- chatbot1 = gr.Chatbot()
101
- message = gr.Textbox(placeholder="Chat here")
102
- state = gr.State()
 
 
 
 
 
103
  submit = gr.Button("SEND")
104
- submit.click(chatgpt_clone, inputs=[message, state], outputs=[chatbot1, state])
 
105
 
106
- demo.launch(debug = True, share=False)
 
1
  import gradio as gr
2
  from revChatGPT.V1 import Chatbot
3
 
4
+ import argparse
5
+
6
  #You can setup login information here, or login in from UI
7
 
8
  # If you want to use Email/Password to login, put your account information here
9
+ email = ""
10
+ password = ""
11
 
12
+ # If you have an access token, put your access token here
13
+ access_token = ""
14
 
15
  # If you have a session token, put your session token here
16
+ session_token = ""
17
+
18
 
19
+ def get_args():
20
+ parser = argparse.ArgumentParser(description='Command line args.')
21
+ parser.add_argument(
22
+ '--no_markdown',
23
+ action='store_true',
24
+ help='Disable the markdown of the web UI.',)
25
+ return parser.parse_args()
26
+
27
+ def is_google_colab():
28
+ try:
29
+ import google.colab
30
+ return True
31
+ except:
32
+ return False
33
+
34
+ chatbot = None
35
+
36
  def configure_chatbot(method, info):
37
 
38
  if method=="Email/Password":
 
50
  config.update({"access_token": access_token})
51
  elif session_token:
52
  config.update({"session_token": session_token})
53
+
54
  global chatbot
55
+ try:
56
+ chatbot = Chatbot(config=config)
57
+ except:
58
+ chatbot = None
59
 
60
  login_method = ['Email/Password',
61
  'Access token',
 
64
 
65
  def ask_bot(prompt):
66
  message = ""
67
+ if chatbot:
68
+ for data in chatbot.ask(prompt):
69
+ message = data["message"]
70
+ else:
71
+ message = "The chatbot is not set up properly! Try to login again."
72
  return parse_text(message)
73
 
74
  def parse_text(text):
 
82
  lines[i] = f'</code></pre>'
83
  else:
84
  if i>0:
85
+ line = line.replace("<", "&lt;")
86
+ line = line.replace(">", "&gt;")
87
  lines[i] = '<br/>'+line.replace(" ", "&nbsp;")
88
  return "".join(lines)
89
 
90
+ def chat_clone(inputs, history):
91
  history = history or []
92
  output = ask_bot(inputs)
93
  history.append((inputs, output))
94
  return history, history
95
 
96
+ if ((email and password) or access_token or session_token):
97
+ css = "style.css"
98
+ else:
99
+ css = None
100
+
101
+ with gr.Blocks(css=css) as demo:
102
+
103
+ args = get_args()
104
+
105
+ if not args.no_markdown:
106
+ gr.Markdown("""<h1><center>ChatGPT BOT build by revChatGPT & Gradio</center></h1>""")
107
 
108
  if not ((email and password) or access_token or session_token):
109
+ if not args.no_markdown:
110
+ gr.Markdown("""<h2>Login to OpenAI</h2>""")
111
  with gr.Row():
112
  with gr.Group():
113
  method = gr.Dropdown(label="Login Method", choices=login_method)
114
+ info = gr.Textbox(placeholder="email password/access_token/session_token", label="Login Information (choose login method first)")
115
  with gr.Row():
116
  login = gr.Button("Login")
117
  login.click(configure_chatbot, inputs=[method, info])
 
126
  method = "Session token"
127
  info = session_token
128
  configure_chatbot(method, info)
129
+
130
+ if not args.no_markdown:
131
+ gr.Markdown("""<h2>Start Chatting ...</h2>""")
132
+
133
+ chatbot1 = gr.Chatbot(elem_id="chatbot", show_label=False)
134
+ state = gr.State([])
135
+ message = gr.Textbox(placeholder="Chat here", label="Human: ")
136
+ message.submit(chat_clone, inputs=[message, state], outputs=[chatbot1, state])
137
+ message.submit(lambda :"", None, message)
138
+
139
  submit = gr.Button("SEND")
140
+ submit.click(chat_clone, inputs=[message, state], outputs=[chatbot1, state])
141
+ submit.click(lambda :"", None, message)
142
 
143
+ demo.launch(debug = True, share=is_google_colab())
style.css ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ .gradio-container {
2
+ max-width: 100%;
3
+ max-height: 100%;
4
+ }
5
+
6
+ [id$=chatbot] > div{
7
+ border: 0;
8
+ height: 70vh;
9
+ overflow-y: auto;
10
+ }