tối ưu đoạn mã sau: import json
import time
from playwright.sync_api import sync_playwright

class ChatBot:
    def __init__(self):
        self.CAU_CU = 'trong'
        self.CAU_TONG = ''
        self.TUKHOA = 'ketthuc'
        self.isLoi = False
        self.playwright = None
        self.browser = None

    def daSuyNghiXong(self, page):
        daSuyNghi = False
        while not daSuyNghi:
            time.sleep(.5)
            try:
                input_element = page.locator("#prompt")

                # Lấy giá trị thuộc tính 'disabled'
                is_disabled = input_element.get_attribute("disabled")

                if is_disabled == '':
                    return False
                if is_disabled is None:
                    daSuyNghi = True
                    return True
            except:
                return False

    def doc(self, cau, page):
        time.sleep(1)
        LOI = 'something went wrong'
        # loi = False
        while 1:
            if self.daSuyNghiXong(page):
                # Sử dụng Selectors để tìm div có class là "message p-2"
                content_textss = page.locator('div.message.p-2').all()
                content_text = content_textss[-1] if len(content_textss) > 1 else content_textss[0]
                # Trích xuất nội dung của div
                content_texts = content_text.text_content()

                if LOI in content_texts:
                    self.isLoi = True
                    break
                else:
                    content_texts = content_texts.splitlines()
                    ket_qua = [i for i in content_texts if len(i) > 0]
                    ket_qua = ket_qua if len(ket_qua) <= 1 else ket_qua[:-1]

                    for kq in ket_qua:
                        print(kq.split(cau)[1]) if cau in kq else print(kq)


                    break
        return self.isLoi

    def inputs(self):
        print('NHẬP CÂU HỎI CỦA BẠN:')
        CAU = ''
        cau = ''
        while not cau == self.TUKHOA:
            try:
                cau = input('-> ')
                if CAU == '' and cau != '':
                    CAU = cau
                elif cau != '':
                    CAU = CAU + '. ' + cau

            except:
                pass
        self.CAU_TONG = self.CAU_TONG + CAU
        return 'bằng tiếng việt, trả lời '+CAU

    def nhap_cau_hoi(self):
        return self.inputs()

    def send_luna(self, cau, page):
        page.get_by_placeholder("Enter your question...").click()
        page.get_by_placeholder("Enter your question...").fill(cau)
        page.get_by_placeholder("Enter your question...").press("Enter")

    def reset_page(self, context, page):
        page.close()
        context.clear_cookies()
        context.clear_permissions()
        context.close()

    def run_0(self):

        while True:
            context = self.browser.new_context()
            page = context.new_page()
            page.goto("https://opnai.net/")
            time.sleep(3)
            with open(f'cookies.json',"a") as f:
                f.write(json.dumps(context.cookies()))
                f.write('\n')

            isLoi = False
            while True:
                if self.CAU_CU == 'trong':
                    cau = self.nhap_cau_hoi()
                else:
                    cau = self.CAU_CU
                    self.CAU_CU = 'trong'

                cau = cau.replace(self.TUKHOA, '')
                #
                # try:
                #     self.isLoi = self.doc(cau, page)
                # except:
                #     pass

                if cau.lower() != 'ket thuc.' and cau != '':
                    if not self.isLoi:
                        self.send_luna(cau=cau, page=page)
                        self.isLoi = self.doc(cau, page)

                        if self.isLoi:
                            self.CAU_CU = cau
                            break
                    else:
                        print('Đợi tôi một chút...')

                        self.CAU_CU = cau
                        break

            if self.isLoi:
                self.reset_page(context=context, page=page)
                self.CAU_CU = self.CAU_TONG.replace(self.TUKHOA, '')
                self.isLoi = False
                # self.CAU_TONG = ''

    def run(self):
        self.CAU_CU = 'trong'
        self.CAU_TONG = ''
        with sync_playwright() as self.playwright:
            self.browser = self.playwright.chromium.launch(headless=False)
            while True:
                self.run_0()
            # self.browser.close()


if __name__ == "__main__":
    chat_bot = ChatBot()
    chat_bot.run()