CoolFace
Apppublic

dotmet/chatgpt_webui

sourceHugging Facebsd-2-clauseupdated 4y agoView on Hugging Face
11likes
app.py148 linesDownload Raw Back to root
1import gradio as gr2from revChatGPT.V1 import Chatbot3 4import argparse5 6#You can setup login information here, or login in from UI7 8# If you want to use Email/Password to login, put your account information here9email = ""10password = ""11 12# If you have an access token, put your access token here13access_token = ""14 15# If you have a session token, put your session token here16session_token = ""17 18 19def 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 27def is_google_colab():28    try:29        import google.colab30        return True31    except:32        return False33    34chatbot = None35    36def configure_chatbot(method, info):37    38    if method=="Email/Password":39        email, password = info.split()40    elif method=="Access token":41        access_token = info42    elif method=="Session token":43        session_token = info44 45    config = {}46    if email and password:47        config.update({"email": email,48                      "password": password})49    elif access_token:50        config.update({"access_token": access_token})51    elif session_token:52        config.update({"session_token": session_token})53    54    global chatbot55    try:56        # chatbot = Chatbot(config=config)57        chatbot = None58    except:59        chatbot = None60 61login_method = ['Email/Password',62                'Access token',63                'Session token',64                ]65 66def ask_bot(prompt):67    message = ""68    if chatbot:69        for data in chatbot.ask(prompt):70            message = data["message"]71    else:72        message = "The chatbot is not set up properly! Try to login again."73    return parse_text(message)74 75def parse_text(text):76    lines = text.split("\n")77    for i,line in enumerate(lines):78        if "```" in line:79            items = line.split('`')80            if items[-1]:81                lines[i] = f'<pre><code class="{items[-1]}">'82            else:83                lines[i] = f'</code></pre>'84        else:85            if i>0:86                line = line.replace("<", "&lt;")87                line = line.replace(">", "&gt;")88                lines[i] = '<br/>'+line.replace(" ", "&nbsp;")89    return "".join(lines)90 91def chat_clone(inputs, history):92    history = history or []93    output = ask_bot(inputs)94    history.append((inputs, output))95    return history, history96 97if ((email and password) or access_token or session_token):98    css = "style.css"99else:100    css = None101 102with gr.Blocks(css=css) as demo:103    104    args = get_args()105    106    if not args.no_markdown:107        gr.Markdown("""<h1><center>ChatGPT BOT build by revChatGPT & Gradio</center></h1>""")108        gr.Markdown("#### Author: [dotmet](https://github.com/dotmet)  Github link:[ChatGPTWEB](https://github.com/dotmet/chatgpt_webui)")109        gr.Markdown("I have used my own OpenAI account for this demo,you can skip Login and try chat.")110        gr.Markdown("Duplicate this space and run for your own account: [chat_gpt_web](https://huggingface.co/spaces/dotmet/chatgpt_webui?duplicate=true).")111 112    if not ((email and password) or access_token or session_token):113        if not args.no_markdown:114            gr.Markdown("""<h2>Login to OpenAI</h2>""")115        with gr.Row():116            with gr.Group():117                method = gr.Dropdown(label="Login Method", choices=login_method)118                info = gr.Textbox(placeholder="email password/access_token/session_token", label="Login Information (choose login method first)")119                with gr.Row():120                    login = gr.Button("Login")121                    login.click(configure_chatbot, inputs=[method, info])122    else:123        if email and password:124            method = "Email/Password"125            info = email + " " + password126        elif access_token:127            method = "Access token"128            info = access_token129        elif session_token:130            method = "Session token"131            info = session_token132        configure_chatbot(method, info)133    134    if not args.no_markdown:135        gr.Markdown("""<h2>Start Chatting ...</h2>""")136        137    chatbot1 = gr.Chatbot(elem_id="chatbot", show_label=False)138    state = gr.State([])139    message = gr.Textbox(placeholder="Chat here", label="Human: ")140    message.submit(chat_clone, inputs=[message, state], outputs=[chatbot1, state])141    message.submit(lambda :"", None, message)142    143    submit = gr.Button("SEND")144    submit.click(chat_clone, inputs=[message, state], outputs=[chatbot1, state])145    submit.click(lambda :"", None, message)146 147    demo.launch(debug = True, share=is_google_colab())148