File size: 4,724 Bytes
8c40b3a
 
 
f3fdcce
 
8c40b3a
354893a
d335b54
f3fdcce
 
d335b54
f3fdcce
 
d335b54
 
f3fdcce
 
8c40b3a
f3fdcce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c40b3a
 
 
 
3168540
8c40b3a
3168540
8c40b3a
 
 
 
 
 
 
 
 
 
f3fdcce
8c40b3a
f3fdcce
1528664
 
f3fdcce
 
8c40b3a
 
 
 
 
 
 
 
f3fdcce
 
 
 
 
79f9eb4
 
 
 
 
 
 
 
 
 
 
 
 
f3fdcce
 
79f9eb4
 
8c40b3a
f3fdcce
8c40b3a
 
 
 
 
f3fdcce
 
 
 
 
 
 
 
 
 
 
776f935
 
 
8c40b3a
 
f3fdcce
 
8c40b3a
 
 
f3fdcce
8c40b3a
 
 
d335b54
 
 
 
 
 
 
 
 
 
 
f3fdcce
 
 
 
 
 
 
 
 
 
8c40b3a
f3fdcce
 
8c40b3a
f3fdcce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import gradio as gr
from revChatGPT.V1 import Chatbot

import argparse

#You can setup login information here, or login in from UI

# If you want to use Email/Password to login, put your account information here
email = ""
password = ""

# If you have an access token, put your access token here
access_token = ""

# If you have a session token, put your session token here
session_token = ""


def get_args():
    parser = argparse.ArgumentParser(description='Command line args.')
    parser.add_argument(
        '--no_markdown',
        action='store_true',
        help='Disable the markdown of the web UI.',)
    return parser.parse_args()

def is_google_colab():
    try:
        import google.colab
        return True
    except:
        return False
    
chatbot = None
    
def configure_chatbot(method, info):
    
    if method=="Email/Password":
        email, password = info.split()
    elif method=="Access token":
        access_token = info
    elif method=="Session token":
        session_token = info

    config = {}
    if email and password:
        config.update({"email": email,
                      "password": password})
    elif access_token:
        config.update({"access_token": access_token})
    elif session_token:
        config.update({"session_token": session_token})
    
    global chatbot
    try:
        # chatbot = Chatbot(config=config)
        chatbot = None
    except:
        chatbot = None

login_method = ['Email/Password',
                'Access token',
                'Session token',
                ]

def ask_bot(prompt):
    message = ""
    if chatbot:
        for data in chatbot.ask(prompt):
            message = data["message"]
    else:
        message = "The chatbot is not set up properly! Try to login again."
    return parse_text(message)

def parse_text(text):
    lines = text.split("\n")
    for i,line in enumerate(lines):
        if "```" in line:
            items = line.split('`')
            if items[-1]:
                lines[i] = f'<pre><code class="{items[-1]}">'
            else:
                lines[i] = f'</code></pre>'
        else:
            if i>0:
                line = line.replace("<", "&lt;")
                line = line.replace(">", "&gt;")
                lines[i] = '<br/>'+line.replace(" ", "&nbsp;")
    return "".join(lines)

def chat_clone(inputs, history):
    history = history or []
    output = ask_bot(inputs)
    history.append((inputs, output))
    return history, history

if ((email and password) or access_token or session_token):
    css = "style.css"
else:
    css = None

with gr.Blocks(css=css) as demo:
    
    args = get_args()
    
    if not args.no_markdown:
        gr.Markdown("""<h1><center>ChatGPT BOT build by revChatGPT & Gradio</center></h1>""")
        gr.Markdown("#### Author: [dotmet](https://github.com/dotmet)  Github link:[ChatGPTWEB](https://github.com/dotmet/chatgpt_webui)")
        gr.Markdown("I have used my own OpenAI account for this demo,you can skip Login and try chat.")
        gr.Markdown("Duplicate this space and run for your own account: [chat_gpt_web](https://huggingface.co/spaces/dotmet/chatgpt_webui?duplicate=true).")

    if not ((email and password) or access_token or session_token):
        if not args.no_markdown:
            gr.Markdown("""<h2>Login to OpenAI</h2>""")
        with gr.Row():
            with gr.Group():
                method = gr.Dropdown(label="Login Method", choices=login_method)
                info = gr.Textbox(placeholder="email password/access_token/session_token", label="Login Information (choose login method first)")
                with gr.Row():
                    login = gr.Button("Login")
                    login.click(configure_chatbot, inputs=[method, info])
    else:
        if email and password:
            method = "Email/Password"
            info = email + " " + password
        elif access_token:
            method = "Access token"
            info = access_token
        elif session_token:
            method = "Session token"
            info = session_token
        configure_chatbot(method, info)
    
    if not args.no_markdown:
        gr.Markdown("""<h2>Start Chatting ...</h2>""")
        
    chatbot1 = gr.Chatbot(elem_id="chatbot", show_label=False)
    state = gr.State([])
    message = gr.Textbox(placeholder="Chat here", label="Human: ")
    message.submit(chat_clone, inputs=[message, state], outputs=[chatbot1, state])
    message.submit(lambda :"", None, message)
    
    submit = gr.Button("SEND")
    submit.click(chat_clone, inputs=[message, state], outputs=[chatbot1, state])
    submit.click(lambda :"", None, message)

    demo.launch(debug = True, share=is_google_colab())