taranjeet / unofficial-chatgpt-api

This repo is unofficial ChatGPT api. It is based on Daniel Gross's WhatsApp GPT
https://embedchain.ai
MIT License
689 stars 141 forks source link

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

Open raphaelmansuy opened 1 year ago

raphaelmansuy commented 1 year ago

Thanks for this project.

vhanla commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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']"))