Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update server.py // Better waiting algo to wait for a reply for ChatGPT, installation script and improved documentation #11

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

raphaelmansuy
Copy link

No description provided.

raphaelmansuy and others added 3 commits December 5, 2022 02:19
👉  I have improved the waiting algo to wait for a reply for ChatGPT
@raphaelmansuy raphaelmansuy changed the title Improve documentation and add installation script Update server.py // Better waiting algo to wait for a reply for ChatGPT, installation script and improved documentation Dec 4, 2022
@raphaelmansuy
Copy link
Author

Thanks for this project.

@vhanla
Copy link

vhanla commented Dec 5, 2022

Interesting, but it still fails waiting for it to end conversation. I'd suggest to check submit button svg child, since it is removed while chat is sending messages, and when it finishes, svg is restored.

def get_submit_button():
    """Get the submit button"""
    print ("Getting submit button")
    QUERY = PAGE.query_selector("div[class*='PromptTextarea__TextareaWrapper']")
    if QUERY is None:
        return None
    return QUERY.query_selector("button"
    
@APP.route("/chat", methods=["GET"])
def chat():
    message = flask.request.args.get("q")
    BUTTON = get_submit_button()
    if BUTTON is not None:
        if BUTTON.query_selector("svg") is None:
            return "Please wait for previous question to finish its answer!"

        print("Sending message: ", message)
        send_message(message)
        print("⏳ Waiting for response...")
        # Submit button has SVG when iddle, and it is removed while waiting for answer completion
        while True:
            time.sleep(1)
            if BUTTON.query_selector("svg") is None:
                break;
        # Wait until svg is restored, which means that chat has completed answering.
        while True:
            time.sleep(1)
            print(" ... waiting svg ...")
            if BUTTON.query_selector("svg"):
                break
        print("⏳ ✅ End waiting for response...")
        response = get_last_message()
        print("🤖 Response: ", response)
    else:
        response = "Update this script, since something changed in ChatGPT web UI."
    return response

@MendyLanda
Copy link

Interesting, but it still fails waiting for it to end conversation. I'd suggest to check submit button svg child, since it is removed while chat is sending messages, and when it finishes, svg is restored.

def get_submit_button():

    """Get the submit button"""

    print ("Getting submit button")

    QUERY = PAGE.query_selector("div[class*='PromptTextarea__TextareaWrapper']")

    if QUERY is None:

        return None

    return QUERY.query_selector("button"

    

@APP.route("/chat", methods=["GET"])

def chat():

    message = flask.request.args.get("q")

    BUTTON = get_submit_button()

    if BUTTON is not None:

        if BUTTON.query_selector("svg") is None:

            return "Please wait for previous question to finish its answer!"



        print("Sending message: ", message)

        send_message(message)

        print("⏳ Waiting for response...")

        # Submit button has SVG when iddle, and it is removed while waiting for answer completion

        while True:

            time.sleep(1)

            if BUTTON.query_selector("svg") is None:

                break;

        # Wait until svg is restored, which means that chat has completed answering.

        while True:

            time.sleep(1)

            print(" ... waiting svg ...")

            if BUTTON.query_selector("svg"):

                break

        print("⏳ ✅ End waiting for response...")

        response = get_last_message()

        print("🤖 Response: ", response)

    else:

        response = "Update this script, since something changed in ChatGPT web UI."

    return response
This can be solved with:

def is_loading_response() -> bool:
    """See if the send button is diabled, if it does, we're not loading"""
    return not PAGE.query_selector("button[class*='PromptTextarea__PositionSubmit']").is_enabled()

See PR #14 for more details

@ItsAditya-xyz
Copy link

Interesting, but it still fails waiting for it to end conversation. I'd suggest to check submit button svg child, since it is removed while chat is sending messages, and when it finishes, svg is restored.

def get_submit_button():

    """Get the submit button"""

    print ("Getting submit button")

    QUERY = PAGE.query_selector("div[class*='PromptTextarea__TextareaWrapper']")

    if QUERY is None:

        return None

    return QUERY.query_selector("button"

    

@APP.route("/chat", methods=["GET"])

def chat():

    message = flask.request.args.get("q")

    BUTTON = get_submit_button()

    if BUTTON is not None:

        if BUTTON.query_selector("svg") is None:

            return "Please wait for previous question to finish its answer!"



        print("Sending message: ", message)

        send_message(message)

        print("⏳ Waiting for response...")

        # Submit button has SVG when iddle, and it is removed while waiting for answer completion

        while True:

            time.sleep(1)

            if BUTTON.query_selector("svg") is None:

                break;

        # Wait until svg is restored, which means that chat has completed answering.

        while True:

            time.sleep(1)

            print(" ... waiting svg ...")

            if BUTTON.query_selector("svg"):

                break

        print("⏳ ✅ End waiting for response...")

        response = get_last_message()

        print("🤖 Response: ", response)

    else:

        response = "Update this script, since something changed in ChatGPT web UI."

    return response
This can be solved with:

def is_loading_response() -> bool:
    """See if the send button is diabled, if it does, we're not loading"""
    return not PAGE.query_selector("button[class*='PromptTextarea__PositionSubmit']").is_enabled()

See PR #14 for more details

This no longer works as they made changes to their frontend lol
using this now:

def is_loading_response() -> bool:
    # see if the send button svg exists
    return not bool(PAGE.query_selector("svg[class*='w-4 h-4 rotate-90']"))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants