async def test_graph_special_characters(storage):
    """
    测试图数据库对特殊字符的处理:
    1. 测试节点名称和描述中包含单引号、双引号和反斜杠
    2. 测试边的描述中包含单引号、双引号和反斜杠
    3. 验证特殊字符是否被正确保存和检索
    """
    try:
        # 1. 测试节点名称中的特殊字符
        node1_id = "包含'单引号'的节点"
        node1_data = {
            "entity_id": node1_id,
            "description": "这个描述包含'单引号'、\"双引号\"和\\反斜杠",
            "keywords": "特殊字符,引号,转义",
            "entity_type": "测试节点",
        }
        print(f"插入包含特殊字符的节点1: {node1_id}")
        await storage.upsert_node(node1_id, node1_data)

        # 2. 测试节点名称中的双引号
        node2_id = '包含"双引号"的节点'
        node2_data = {
            "entity_id": node2_id,
            "description": "这个描述同时包含'单引号'和\"双引号\"以及\\反斜杠\\路径",
            "keywords": "特殊字符,引号,JSON",
            "entity_type": "测试节点",
        }
        print(f"插入包含特殊字符的节点2: {node2_id}")
        await storage.upsert_node(node2_id, node2_data)

        # 3. 测试节点名称中的反斜杠
        node3_id = "包含\\反斜杠\\的节点"
        node3_data = {
            "entity_id": node3_id,
            "description": "这个描述包含Windows路径C:\\Program Files\\和转义字符\\n\\t",
            "keywords": "反斜杠,路径,转义",
            "entity_type": "测试节点",
        }
        print(f"插入包含特殊字符的节点3: {node3_id}")
        await storage.upsert_node(node3_id, node3_data)

        # 4. 测试边描述中的特殊字符
        edge1_data = {
            "relationship": "特殊'关系'",
            "weight": 1.0,
            "description": "这个边描述包含'单引号'、\"双引号\"和\\反斜杠",
        }
        print(f"插入包含特殊字符的边: {node1_id} -> {node2_id}")
        await storage.upsert_edge(node1_id, node2_id, edge1_data)

        # 5. 测试边描述中的更复杂特殊字符组合
        edge2_data = {
            "relationship": '复杂"关系"\\类型',
            "weight": 0.8,
            "description": "包含SQL注入尝试: SELECT * FROM users WHERE name='admin'--",
        }
        print(f"插入包含复杂特殊字符的边: {node2_id} -> {node3_id}")
        await storage.upsert_edge(node2_id, node3_id, edge2_data)

        # 6. 验证节点特殊字符是否正确保存
        print("\n== 验证节点特殊字符")
        for node_id, original_data in [
            (node1_id, node1_data),
            (node2_id, node2_data),
            (node3_id, node3_data),
        ]:
            node_props = await storage.get_node(node_id)
            if node_props:
                print(f"成功读取节点: {node_id}")
                print(f"节点描述: {node_props.get('description', '无描述')}")

                # 验证节点ID是否正确保存
                assert (
                    node_props.get("entity_id") == node_id
                ), f"节点ID不匹配: 期望 {node_id}, 实际 {node_props.get('entity_id')}"

                # 验证描述是否正确保存
                assert (
                    node_props.get("description") == original_data["description"]
                ), f"节点描述不匹配: 期望 {original_data['description']}, 实际 {node_props.get('description')}"

                print(f"节点 {node_id} 特殊字符验证成功")
            else:
                print(f"读取节点属性失败: {node_id}")
                assert False, f"未能读取节点属性: {node_id}"

        # 7. 验证边特殊字符是否正确保存
        print("\n== 验证边特殊字符")
        edge1_props = await storage.get_edge(node1_id, node2_id)
        if edge1_props:
            print(f"成功读取边: {node1_id} -> {node2_id}")
            print(f"边关系: {edge1_props.get('relationship', '无关系')}")
            print(f"边描述: {edge1_props.get('description', '无描述')}")

            # 验证边关系是否正确保存
            assert (
                edge1_props.get("relationship") == edge1_data["relationship"]
            ), f"边关系不匹配: 期望 {edge1_data['relationship']}, 实际 {edge1_props.get('relationship')}"

            # 验证边描述是否正确保存
            assert (
                edge1_props.get("description") == edge1_data["description"]
            ), f"边描述不匹配: 期望 {edge1_data['description']}, 实际 {edge1_props.get('description')}"

            print(f"边 {node1_id} -> {node2_id} 特殊字符验证成功")
        else:
            print(f"读取边属性失败: {node1_id} -> {node2_id}")
            assert False, f"未能读取边属性: {node1_id} -> {node2_id}"

        edge2_props = await storage.get_edge(node2_id, node3_id)
        if edge2_props:
            print(f"成功读取边: {node2_id} -> {node3_id}")
            print(f"边关系: {edge2_props.get('relationship', '无关系')}")
            print(f"边描述: {edge2_props.get('description', '无描述')}")

            # 验证边关系是否正确保存
            assert (
                edge2_props.get("relationship") == edge2_data["relationship"]
            ), f"边关系不匹配: 期望 {edge2_data['relationship']}, 实际 {edge2_props.get('relationship')}"

            # 验证边描述是否正确保存
            assert (
                edge2_props.get("description") == edge2_data["description"]
            ), f"边描述不匹配: 期望 {edge2_data['description']}, 实际 {edge2_props.get('description')}"

            print(f"边 {node2_id} -> {node3_id} 特殊字符验证成功")
        else:
            print(f"读取边属性失败: {node2_id} -> {node3_id}")
            assert False, f"未能读取边属性: {node2_id} -> {node3_id}"

        print("\n特殊字符测试完成，数据已保留在数据库中")
        return True

    except Exception as e:
        ASCIIColors.red(f"测试过程中发生错误: {str(e)}")
        return False


    def roles(self):
        """Roles of the client processes"""
        return self._roles


def generator(prompt:str, model:str, llm_auth_token:str,llm_base_url:str, using_custom_llm=False,is_failed=False):
    if is_failed:
        yield "搜索失败，没有返回结果"
    else:
        total_token = ""
        for token in  free_ask_internet.chat(prompt=prompt,model=model,llm_auth_token=llm_auth_token,llm_base_url=llm_base_url,using_custom_llm=using_custom_llm,stream=True):
            total_token += token
            yield token


    def feature_stage(self):
        """Returns the stage of the feature flag.

        Returns:
            FEATURE_STAGES|None. The stage of the feature flag, None if the
            parameter isn't a feature flag.
        """
        return self._feature_stage


    def test_record_extensions(self):
        install_dir = self.mkdtemp()
        project_dir, dist = self.create_dist(ext_modules=[
            Extension('xx', ['xxmodule.c'])])
        os.chdir(project_dir)
        support.copy_xxmodule_c(project_dir)

        buildextcmd = build_ext(dist)
        support.fixup_build_ext(buildextcmd)
        buildextcmd.ensure_finalized()

        cmd = install(dist)
        dist.command_obj['install'] = cmd
        dist.command_obj['build_ext'] = buildextcmd
        cmd.root = install_dir
        cmd.record = os.path.join(project_dir, 'filelist')
        cmd.ensure_finalized()
        cmd.run()

        f = open(cmd.record)
        try:
            content = f.read()
        finally:
            f.close()

        found = [os.path.basename(line) for line in content.splitlines()]
        expected = [_make_ext_name('xx'),
                    'UNKNOWN-0.0.0-py%s.%s.egg-info' % sys.version_info[:2]]
        self.assertEqual(found, expected)


    def test_malformed_single(self):
        dg = Datagram()
        dg.add_uint16(0) # Datagram length
        dg.add_uint8(1) # Number of recipients
        dg.add_channel(12341234) # Recipient
        dg.add_channel(0) # Sender
        dg.add_string('Ex nihilo, alea iacta est. Tohuvabohu!')
        self.c1.s.send(dg.get_data())
        # The MD should drop this packet completely.
        self.expectNone(self.l1)
        # The MD should drop the misbehaving participant.
        self.assertRaises(EOFError, self.c1.recv_maybe())

        # Cleanup
        self.c1.close()
        self.__class__.c1 = self.connectToServer()
        self.l1.flush()


    def loop_waiting_for_flag(self, instance, timeout=5):
        timeout = float(timeout) / 100
        count = 100
        while asyncore.socket_map and count > 0:
            asyncore.loop(timeout=0.01, count=1, use_poll=self.use_poll)
            if instance.flag:
                return
            count -= 1
            time.sleep(timeout)
        self.fail("flag not set")


    def test_set(self):
        """Test that values can be set in the cache."""
        cache = TimedCache(datetime.timedelta(seconds=10))
        key, value = "test_key", "test_value"

        with patch("datetime.datetime") as mock_datetime:
            now = datetime.datetime(2023, 1, 1, tzinfo=datetime.timezone.utc)
            mock_datetime.now.return_value = now

            cache.set(key, value)

            # Check that the value is stored with the correct expiration
            assert key in cache.cache
            stored_value, expiration = cache.cache[key]
            assert stored_value == value
            assert expiration == now + datetime.timedelta(seconds=10)


def setup_directories(video_path: Path) -> dict:
    """
    Setup directories for storing processed files.

    Args:
        video_path (Path): Path to the video file.

    Returns:
        dict: A dictionary containing paths for various directories.
    """
    base_dir = video_path.parent.parent
    dirs = {
        "face_mask": base_dir / "face_mask",
        "sep_pose_mask": base_dir / "sep_pose_mask",
        "sep_face_mask": base_dir / "sep_face_mask",
        "sep_lip_mask": base_dir / "sep_lip_mask",
        "face_emb": base_dir / "face_emb",
        "audio_emb": base_dir / "audio_emb"
    }

    for path in dirs.values():
        path.mkdir(parents=True, exist_ok=True)

    return dirs


    def test_valid_json_array(self):
        """Test with valid JSON array"""
        content = '[1, 2, 3, "test"]'
        result = repair_json_output(content)
        expected = json.dumps([1, 2, 3, "test"], ensure_ascii=False)
        assert result == expected


    def __getitem__(self, index):

        # if _DEBUG:
        # index = 1

        # img_id = self.ids[index]

        im_path = self.annobase[index]['image']# os.path.join(self.root, img_id + '.jpg')
        img = Image.open(im_path).convert("RGB")
        # im = cv2.imread(im_path)
        anno = self.annobase[index]
        target = RBoxList(torch.from_numpy(anno["boxes"]), (anno['width'], anno['height']), mode="xywha")
        target.add_field("labels", torch.from_numpy(anno["gt_classes"]))
        target.add_field("difficult", torch.Tensor([0 for i in range(len(anno["gt_classes"]))]))

        masks = [np.array(mask).reshape(1, -1).tolist() for mask in anno["polys"]]
        # print('masks data:', masks)
        masks = SegmentationMask(masks, img.size)
        target.add_field("masks", masks)

        # target.add_field("masks", torch.from_numpy(np.array(anno["polys"]).reshape(-1)))

        target = target.clip_to_image(remove_empty=True)
        # print('target:', target, im_path)
        if self.transforms is not None:
            # off = int(self.num_samples * np.random.rand())
            # mix_index = (off + index) % self.num_samples
            # img_mix = Image.open(self.annobase[mix_index]['image']).convert("RGB")
            # img, target = self.mixup(img, img_mix, target)
            img, target = self.transforms(img, target)
        if _DEBUG:
            if not target is None:
                # print('target:', target, im_path)
                self.show_boxes(img, target)

        return img, target, index


    def format_record(record):
        # 获取日志记录中的文件全路径
        file_path = record["file"].path
        # 将绝对路径转换为相对于项目根目录的路径
        relative_path = os.path.relpath(file_path, root_dir)
        # 更新记录中的文件路径
        record["file"].path = f"./{relative_path}"
        # 返回修改后的格式字符串
        # 您可以根据需要调整这里的格式
        record["message"] = record["message"].replace(root_dir, ".")

        _format = (
            "<green>{time:%Y-%m-%d %H:%M:%S}</> | "
            + "<level>{level}</> | "
            + '"{file.path}:{line}":<blue> {function}</> '
            + "- <level>{message}</>"
            + "\n"
        )
        return _format


        def noop_no_sync():
            yield


    def print_text(text=None):
        if text is not None:
            print(text)


    def _add_write_service(self, ioi, write_data, data_type):
        """
        Add the write command stuff to the tagIOI
        """
        RequestPathSize = int(len(ioi)/2)
        RequestService = 0x4D
        write_service = pack('<BB', RequestService, RequestPathSize)
        write_service += ioi

        if data_type == 160:
            RequestNumberOfElements = self.StructIdentifier
            TypeCodeLen = 0x02
            write_service += pack('<BBHH', data_type, TypeCodeLen, self.StructIdentifier, len(write_data))
        else:
            TypeCodeLen = 0x00
            write_service += pack('<BBH', data_type, TypeCodeLen, len(write_data))

        for v in write_data:
            try:
                for i in range(len(v)):
                    el = v[i]
                    write_service += pack(self.CIPTypes[data_type][2], el)
            except Exception:
                write_service += pack(self.CIPTypes[data_type][2], v)

        return write_service


    def get_musa_bare_metal_version(self, musa_dir):
        raw_output = subprocess.run(
            [musa_dir + "/bin/mcc", "-v"], check=True,
            stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout.decode("utf-8")
        output = raw_output.split()
        release_idx = output.index("version") + 1
        bare_metal_version = parse(output[release_idx].split(",")[0])
        musa_version = f"{bare_metal_version.major}{bare_metal_version.minor}"
        return musa_version


        def execute(self):
            url, method, body, json_body, headers = (
                OAuth2Request(self.api).prepare_request(
                    self.method, self.path, self.parameters,
                    include_secret=self.include_secret))
            if self.as_generator:
                return self._paginator_with_url(url, method, body, headers)
            else:
                content, next = self._do_api_request(url, method, body,
                                                     json_body, headers)
            if self.paginates:
                return content, next
            else:
                return content


    def activate_restore(self):
        if not self.is_restore_active:
            for seq in self.RESTORE_SEQUENCES:
                self.text.event_add(self.RESTORE_VIRTUAL_EVENT_NAME, seq)
            self.is_restore_active = True


    def test_multiple_completions(self):
        """Test handling multiple completions at once."""
        completions = [
            [{"content": r"\boxed{\frac{63}{400}}"}],
            [{"content": r"\boxed{\frac{64}{400}}"}],
        ]
        solutions = [r"\frac{63}{400}", r"\frac{63}{400}"]

        rewards = accuracy_reward(completions, solutions)
        self.assertEqual(len(rewards), 2)
        self.assertEqual(rewards[0], 1.0)
        self.assertEqual(rewards[1], 0.0)


def identity(n, dtype=None):
    """
    Return the identity array.

    The identity array is a square array with ones on
    the main diagonal.

    Parameters
    ----------
    n : int
        Number of rows (and columns) in `n` x `n` output.
    dtype : data-type, optional
        Data-type of the output.  Defaults to ``float``.

    Returns
    -------
    out : ndarray
        `n` x `n` array with its main diagonal set to one,
        and all other elements 0.

    Examples
    --------
    >>> np.identity(3)
    array([[ 1.,  0.,  0.],
           [ 0.,  1.,  0.],
           [ 0.,  0.,  1.]])

    """
    from numpy import eye
    return eye(n, dtype=dtype)


def random_true_with_probability(probability):
    """return True with the requested probability, False otherwise."""
    return random.random() < probability


    def __call__(self, tokens):
        mergewords = self.mergewords
        mergenums = self.mergenums

        # This filter renumbers tokens as it expands them. New position
        # counter.
        newpos = None
        for t in tokens:
            text = t.text

            # If this is the first token we've seen, use it to set the new
            # position counter
            if newpos is None:
                if t.positions:
                    newpos = t.pos
                else:
                    # Token doesn't have positions, just use 0
                    newpos = 0

            if ((text.isalpha() and (text.islower() or text.isupper()))
                or text.isdigit()):
                # Short-circuit the common cases of no delimiters, no case
                # transitions, only digits, etc.
                t.pos = newpos
                yield t
                newpos += 1
            else:
                # Split the token text on delimiters, word and/or number
                # boundaries into a list of (text, pos, startchar, endchar)
                # tuples
                ranges = self._split(text)
                parts = [(text[sc:ec], i + newpos, sc, ec)
                         for i, (sc, ec) in enumerate(ranges)]

                # Did the split yield more than one part?
                if len(parts) > 1:
                    # If the options are set, merge consecutive runs of all-
                    # letters and/or all-numbers.
                    if mergewords or mergenums:
                        self._merge(parts)

                # Yield tokens for the parts
                chars = t.chars
                if chars:
                    base = t.startchar
                for text, pos, startchar, endchar in parts:
                    t.text = text
                    t.pos = pos
                    if t.chars:
                        t.startchar = base + startchar
                        t.endchar = base + endchar
                    yield t

                if parts:
                    # Set the new position counter based on the last part
                    newpos = parts[-1][1] + 1


def parse_results(
    result_file,
) -> tuple[defaultdict[str, defaultdict[str, dict[str, bool]]], defaultdict[str, set[str]], set[str], str]:
    """Parse a single test results file.

    Returns:
        Tuple containing:
        - parsed_results: DefaultDict[provider, DefaultDict[model, Dict[test_name, pass_status]]]
        - providers_in_file: DefaultDict[provider, Set[model]] found in this file.
        - tests_in_file: Set[test_name] found in this file.
        - run_timestamp: Timestamp when the test was run
    """
    if not os.path.exists(result_file):
        print(f"Results file does not exist: {result_file}")
        # Return empty defaultdicts/set matching the type hint
        return defaultdict(lambda: defaultdict(dict)), defaultdict(set), set(), ""

    with open(result_file) as f:
        results = json.load(f)

    # Initialize results dictionary with specific types
    parsed_results: defaultdict[str, defaultdict[str, dict[str, bool]]] = defaultdict(lambda: defaultdict(dict))
    providers_in_file: defaultdict[str, set[str]] = defaultdict(set)
    tests_in_file: set[str] = set()
    # Extract provider from filename (e.g., "openai.json" -> "openai")
    provider: str = result_file.stem

    # Extract run timestamp from the JSON data
    run_timestamp_unix = results.get("run_timestamp")
    run_timestamp_str = (
        time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(run_timestamp_unix))
        if run_timestamp_unix is not None
        else "Unknown"
    )

    # Debug: Print summary of test results
    print(f"Test results summary for {provider}:")
    print(f"Total tests: {results.get('summary', {}).get('total', 0)}")
    print(f"Passed: {results.get('summary', {}).get('passed', 0)}")
    print(f"Failed: {results.get('summary', {}).get('failed', 0)}")
    print(f"Error: {results.get('summary', {}).get('error', 0)}")
    print(f"Skipped: {results.get('summary', {}).get('skipped', 0)}")

    # Extract test results
    if "tests" not in results or not results["tests"]:
        print(f"No test results found in {result_file}")
        # Return empty defaultdicts/set matching the type hint
        return defaultdict(lambda: defaultdict(dict)), defaultdict(set), set(), ""

    # Process the tests
    for test in results["tests"]:
        test_id = test.get("nodeid", "")

        if not (call_phase := test.get("call")):
            continue
        call_outcome = call_phase.get("outcome")
        if call_outcome not in ("passed", "failed"):
            continue

        # --- Extract data from metadata ---
        metadata = test.get("metadata", {})
        model = metadata.get("model")
        case_id = metadata.get("case_id")  # String ID (if provided)
        case_index = metadata.get("case_index")  # Integer index (if no ID provided)

        # Check if we have a model and at least one case identifier
        if not model or (case_id is None and case_index is None):
            print(
                f"Warning: Missing 'model' or case identifier ('case_id'/'case_index') metadata for test: {test_id}. Skipping."
            )
            continue

        try:
            test_name_base = test_id.split("::")[1].split("[")[0]
        except (IndexError, ValueError) as e:
            print(f"Warning: Could not parse base test name for {test_id}. Error: {e}. Skipping.")
            continue

        # Construct detailed test name using ID or index
        if case_id is not None:
            detailed_test_name = f"{test_name_base} ({case_id})"
        elif case_index == 0:
            # If case_id is missing and index is 0, assume single case, use base name only
            detailed_test_name = test_name_base
        elif case_index is not None:  # case_index > 0
            # Use case_index for naming if case_id wasn't provided and index > 0
            detailed_test_name = f"{test_name_base} (case{case_index})"
        else:
            # This case should be prevented by the earlier check, but handle defensively
            print(f"Error: No case identifier found for test {test_id} after initial check. Skipping.")
            continue

        # Populate collections for this file
        tests_in_file.add(detailed_test_name)
        providers_in_file[provider].add(model)

        if call_outcome == "passed":
            parsed_results[provider][model][detailed_test_name] = True
        elif call_outcome == "failed":
            parsed_results[provider][model][detailed_test_name] = False

    # Final Summary Warning (Optional)
    if not parsed_results.get(provider):
        print(f"Warning: No valid test results parsed for provider {provider} from file {result_file}")

    return parsed_results, providers_in_file, tests_in_file, run_timestamp_str


    def get_prompt_for_current_round(self, content=None):
        """Get current round formatted question prompt during sft training"""
        if self.sep_style == SeparatorStyle.PLAIN:
            formatted_question = "<image>\n"
        elif self.sep_style == SeparatorStyle.DeepSeek:
            formatted_question = (
                f"{self.roles[0]}: " + content.strip() + self.sep + f"{self.roles[1]}:"
            )
        else:
            raise ValueError(f"Unsupported sep_style: {self.sep_style}")
        return formatted_question


    def digest(self):
        return msdcc2.encrypt(self._data[:64], user=self._user).decode('hex')


  def test_error(self):
    with run_configs.get().start(want_rgb=False) as controller:
      with self.assertRaises(remote_controller.RequestError):
        controller.create_game(sc_pb.RequestCreateGame())  # Missing map, etc.

      with self.assertRaises(protocol.ProtocolError):
        controller.join_game(sc_pb.RequestJoinGame())  # No game to join.


def test_add_metrics_missing_required_fields(tracer):
    """Test validation of required fields"""
    tracer.trace = {}
    metrics = [{"name": "metric1"}]  # Missing score

    with patch('ragaai_catalyst.tracers.agentic_tracing.tracers.base.logger') as mock_logger:
        tracer.add_metrics(metrics)
        mock_logger.error.assert_called_once_with(
            "Validation Error: Metric must contain 'name' and 'score' fields"
        )
    assert len(tracer.trace_metrics) == 0


    def test_query_provider(self):
        """
        wineventlog - Query by provider name (event source)
        """
        self.write_event_log("selected", source=self.otherAppName)
        self.write_event_log("filtered")
        evts = self.read_events(config={
            "event_logs": [
                {
                    "name": self.providerName,
                    "api": self.api,
                    "provider": [self.otherAppName]
                }
            ]
        })
        self.assertTrue(len(evts), 1)
        self.assertEqual(evts[0]["winlog.provider_name"], self.otherAppName)


def get_rrmgr_cmd(src, dst, compression=None, tcp_buf_size=None,
                  connections=None):
    """Returns rrmgr command for source and destination."""
    cmd = ['rrmgr', '-s', 'zfs']
    if compression:
        cmd.extend(['-c', six.text_type(compression)])
    cmd.append('-q')
    cmd.append('-e')
    if tcp_buf_size:
        cmd.extend(['-w', six.text_type(tcp_buf_size)])
    if connections:
        cmd.extend(['-n', six.text_type(connections)])
    cmd.extend([src, dst])
    return ' '.join(cmd)


    def test_1d_slicing3_npm(self):
        self.test_1d_slicing3(flags=Noflags)


    def test_basic_set_get(self):
        """Test basic set and get operations"""
        cache_instance = cache.TranslationCache("test_engine")

        # Test get with non-existent entry
        result = cache_instance.get("hello")
        self.assertIsNone(result)

        # Test set and get
        cache_instance.set("hello", "你好")
        result = cache_instance.get("hello")
        self.assertEqual(result, "你好")


def parse_arguments():
    """
    Parse command-line arguments such as model directory and device ID.
    """
    parser = argparse.ArgumentParser(description="Spark TTS Gradio server.")
    parser.add_argument(
        "--model_dir",
        type=str,
        default="pretrained_models/Spark-TTS-0.5B",
        help="Path to the model directory."
    )
    parser.add_argument(
        "--device",
        type=int,
        default=0,
        help="ID of the GPU device to use (e.g., 0 for cuda:0)."
    )
    parser.add_argument(
        "--server_name",
        type=str,
        default="0.0.0.0",
        help="Server host/IP for Gradio app."
    )
    parser.add_argument(
        "--server_port",
        type=int,
        default=7860,
        help="Server port for Gradio app."
    )
    return parser.parse_args()


    def test_unknown_eventlog_config(self):
        """
        wineventlog - Unknown config parameter
        """
        self.render_config_template(
            event_logs=[
                {
                    "name": self.providerName,
                    "api": self.api,
                    "forwarded": False,
                    "invalid": "garbage"}
            ]
        )
        self.start_beat().check_wait(exit_code=1)
        assert self.log_contains(
            "1 error: invalid event log key 'invalid' found.")


    def __init__(self, W, base_ring = QQ, prefix='u'):
        r"""
        Initiate the affine nil-Coxeter algebra corresponding to the Weyl
        group `W` over the base ring.

        EXAMPLES::

            sage: U = NilCoxeterAlgebra(WeylGroup(['A',3,1])); U
            The Nil-Coxeter Algebra of Type A3~ over Rational Field
            sage: TestSuite(U).run()

            sage: U = NilCoxeterAlgebra(WeylGroup(['C',3]), ZZ); U
            The Nil-Coxeter Algebra of Type C3 over Integer Ring
            sage: TestSuite(U).run()
        """

        self._W = W
        self._n = W.n
        self._base_ring = base_ring
        self._cartan_type = W.cartan_type()
        H = IwahoriHeckeAlgebra(W, 0, 0, base_ring=base_ring)
        super(IwahoriHeckeAlgebra.T,self).__init__(H, prefix=prefix)


def test_autodetect__when_no_django(env):
    assert not env.autodetect()


def get_env_value(key, provider):
    env_mappings = {
        "openai": {"api_key": "OPENAI_API_KEY", "base_url": "OPENAI_ENDPOINT"},
        "azure_openai": {"api_key": "AZURE_OPENAI_API_KEY", "base_url": "AZURE_OPENAI_ENDPOINT"},
        "google": {"api_key": "GOOGLE_API_KEY"},
        "deepseek": {"api_key": "DEEPSEEK_API_KEY", "base_url": "DEEPSEEK_ENDPOINT"},
        "mistral": {"api_key": "MISTRAL_API_KEY", "base_url": "MISTRAL_ENDPOINT"},
        "alibaba": {"api_key": "ALIBABA_API_KEY", "base_url": "ALIBABA_ENDPOINT"},
        "moonshot": {"api_key": "MOONSHOT_API_KEY", "base_url": "MOONSHOT_ENDPOINT"},
        "ibm": {"api_key": "IBM_API_KEY", "base_url": "IBM_ENDPOINT"}
    }

    if provider in env_mappings and key in env_mappings[provider]:
        return os.getenv(env_mappings[provider][key], "")
    return ""


def set_seed(seed: int):
    """Sets the random seed for reproducibility."""
    random.seed(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
    torch.backends.cudnn.deterministic = True
    torch.backends.cudnn.benchmark = False


    def mem(self):
        return self.value.mem


    def __exit__(self, *exc_details):
        received_exc = exc_details[0] is not None

        # We manipulate the exception state so it behaves as though
        # we were actually nesting multiple with statements
        frame_exc = sys.exc_info()[1]
        _fix_exception_context = _make_context_fixer(frame_exc)

        # Callbacks are invoked in LIFO order to match the behaviour of
        # nested context managers
        suppressed_exc = False
        pending_raise = False
        while self._exit_callbacks:
            cb = self._exit_callbacks.pop()
            try:
                if cb(*exc_details):
                    suppressed_exc = True
                    pending_raise = False
                    exc_details = (None, None, None)
            except:
                new_exc_details = sys.exc_info()
                # simulate the stack of exceptions by setting the context
                _fix_exception_context(new_exc_details[1], exc_details[1])
                pending_raise = True
                exc_details = new_exc_details
        if pending_raise:
            _reraise_with_existing_context(exc_details)
        return received_exc and suppressed_exc


    def declare(self):
        self._ass.mem_addr.usage_read()
        self._ass.mem_buf.usage_write()


    def __init__(self, client, config, serializer, deserializer):

        self._client = client
        self._serialize = serializer
        self._deserialize = deserializer
        self.api_version = "2019-09-01"

        self.config = config


    def _add_first_states(self, feed_dict, rollouts):
        """
        Add first state placeholders for the rollouts.
        """
        if isinstance(self.init_state_vars, tuple):
            for i, placeholder in enumerate(self.first_state_ph):
                first_states = [r.start_state[i][0] for r in rollouts]
                feed_dict[placeholder] = first_states
        else:
            first_states = [r.start_state[0] for r in rollouts]
            feed_dict[self.first_state_ph] = first_states


    def stateful(self):
        return True


def start_session(req: gr.Request):
    user_dir = os.path.join(TMP_DIR, str(req.session_hash))
    os.makedirs(user_dir, exist_ok=True)


def fold_batch_dim(data: 'DataProto', new_batch_size):
    """
    Fold a batch dim from [bsz, xxx] into [new_bsz, bsz // new_bsz, xxx]
    """
    batch_size = data.batch.batch_size[0]

    assert batch_size % new_batch_size == 0

    tensor: TensorDict = data.batch
    non_tensor = data.non_tensor_batch

    tensor = tensor.view(new_batch_size, -1)
    tensor.auto_batch_size_(batch_dims=1)

    for key, val in non_tensor.items():
        non_tensor[key] = np.reshape(val, newshape=(new_batch_size, -1, *val.shape[1:]))

    return DataProto(batch=tensor, non_tensor_batch=non_tensor, meta_info=data.meta_info)


    def __eq__(self, other):
        return other and self.__class__ is other.__class__\
        and self.__dict__ == other.__dict__


def parse_opt():
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', type=str, default=ROOT / 'yolo.pt', help='weights path')
    parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='inference size (pixels)')
    parser.add_argument('--batch-size', type=int, default=1, help='batch size')
    parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='dataset.yaml path')
    parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
    parser.add_argument('--test', action='store_true', help='test exports only')
    parser.add_argument('--pt-only', action='store_true', help='test PyTorch only')
    parser.add_argument('--hard-fail', nargs='?', const=True, default=False, help='Exception on error or < min metric')
    opt = parser.parse_args()
    opt.data = check_yaml(opt.data)  # check YAML
    print_args(vars(opt))
    return opt


    def __post_init__(self):
        # perform necessary checking
        self.check_consistency()


    def getAdhesionArea(self) -> Optional[Polygon]:
        """The polygon representing the 2D adhesion area.

        If no adhesion is used, the regular convex hull is returned
        """
        if self._node is None:
            return None

        hull = self._compute2DConvexHull()
        if hull is None:
            return None

        return self._add2DAdhesionMargin(hull)


    def generate(self,
                 input_prompt,
                 img,
                 max_area=720 * 1280,
                 frame_num=81,
                 shift=5.0,
                 sample_solver='unipc',
                 sampling_steps=40,
                 guide_scale=5.0,
                 n_prompt="",
                 seed=-1,
                 offload_model=True):
        r"""
        Generates video frames from input image and text prompt using diffusion process.

        Args:
            input_prompt (`str`):
                Text prompt for content generation.
            img (PIL.Image.Image):
                Input image tensor. Shape: [3, H, W]
            max_area (`int`, *optional*, defaults to 720*1280):
                Maximum pixel area for latent space calculation. Controls video resolution scaling
            frame_num (`int`, *optional*, defaults to 81):
                How many frames to sample from a video. The number should be 4n+1
            shift (`float`, *optional*, defaults to 5.0):
                Noise schedule shift parameter. Affects temporal dynamics
                [NOTE]: If you want to generate a 480p video, it is recommended to set the shift value to 3.0.
            sample_solver (`str`, *optional*, defaults to 'unipc'):
                Solver used to sample the video.
            sampling_steps (`int`, *optional*, defaults to 40):
                Number of diffusion sampling steps. Higher values improve quality but slow generation
            guide_scale (`float`, *optional*, defaults 5.0):
                Classifier-free guidance scale. Controls prompt adherence vs. creativity
            n_prompt (`str`, *optional*, defaults to ""):
                Negative prompt for content exclusion. If not given, use `config.sample_neg_prompt`
            seed (`int`, *optional*, defaults to -1):
                Random seed for noise generation. If -1, use random seed
            offload_model (`bool`, *optional*, defaults to True):
                If True, offloads models to CPU during generation to save VRAM

        Returns:
            torch.Tensor:
                Generated video frames tensor. Dimensions: (C, N H, W) where:
                - C: Color channels (3 for RGB)
                - N: Number of frames (81)
                - H: Frame height (from max_area)
                - W: Frame width from max_area)
        """
        img = TF.to_tensor(img).sub_(0.5).div_(0.5).to(self.device)

        F = frame_num
        h, w = img.shape[1:]
        aspect_ratio = h / w
        lat_h = round(
            np.sqrt(max_area * aspect_ratio) // self.vae_stride[1] //
            self.patch_size[1] * self.patch_size[1])
        lat_w = round(
            np.sqrt(max_area / aspect_ratio) // self.vae_stride[2] //
            self.patch_size[2] * self.patch_size[2])
        h = lat_h * self.vae_stride[1]
        w = lat_w * self.vae_stride[2]

        max_seq_len = ((F - 1) // self.vae_stride[0] + 1) * lat_h * lat_w // (
            self.patch_size[1] * self.patch_size[2])
        max_seq_len = int(math.ceil(max_seq_len / self.sp_size)) * self.sp_size

        seed = seed if seed >= 0 else random.randint(0, sys.maxsize)
        seed_g = torch.Generator(device=self.device)
        seed_g.manual_seed(seed)
        noise = torch.randn(
            16, (F - 1) // 4 + 1,
            lat_h,
            lat_w,
            dtype=torch.float32,
            generator=seed_g,
            device=self.device)

        msk = torch.ones(1, 81, lat_h, lat_w, device=self.device)
        msk[:, 1:] = 0
        msk = torch.concat([
            torch.repeat_interleave(msk[:, 0:1], repeats=4, dim=1), msk[:, 1:]
        ],
                           dim=1)
        msk = msk.view(1, msk.shape[1] // 4, 4, lat_h, lat_w)
        msk = msk.transpose(1, 2)[0]

        if n_prompt == "":
            n_prompt = self.sample_neg_prompt

        # preprocess
        if not self.t5_cpu:
            self.text_encoder.model.to(self.device)
            context = self.text_encoder([input_prompt], self.device)
            context_null = self.text_encoder([n_prompt], self.device)
            if offload_model:
                self.text_encoder.model.cpu()
        else:
            context = self.text_encoder([input_prompt], torch.device('cpu'))
            context_null = self.text_encoder([n_prompt], torch.device('cpu'))
            context = [t.to(self.device) for t in context]
            context_null = [t.to(self.device) for t in context_null]

        self.clip.model.to(self.device)
        clip_context = self.clip.visual([img[:, None, :, :]])
        if offload_model:
            self.clip.model.cpu()

        y = self.vae.encode([
            torch.concat([
                torch.nn.functional.interpolate(
                    img[None].cpu(), size=(h, w), mode='bicubic').transpose(
                        0, 1),
                torch.zeros(3, F - 1, h, w)
            ],
                         dim=1).to(self.device)
        ])[0]
        y = torch.concat([msk, y])

        @contextmanager
        def noop_no_sync():
            yield

        no_sync = getattr(self.model, 'no_sync', noop_no_sync)

        # evaluation mode
        with amp.autocast(dtype=self.param_dtype), torch.no_grad(), no_sync():

            if sample_solver == 'unipc':
                sample_scheduler = FlowUniPCMultistepScheduler(
                    num_train_timesteps=self.num_train_timesteps,
                    shift=1,
                    use_dynamic_shifting=False)
                sample_scheduler.set_timesteps(
                    sampling_steps, device=self.device, shift=shift)
                timesteps = sample_scheduler.timesteps
            elif sample_solver == 'dpm++':
                sample_scheduler = FlowDPMSolverMultistepScheduler(
                    num_train_timesteps=self.num_train_timesteps,
                    shift=1,
                    use_dynamic_shifting=False)
                sampling_sigmas = get_sampling_sigmas(sampling_steps, shift)
                timesteps, _ = retrieve_timesteps(
                    sample_scheduler,
                    device=self.device,
                    sigmas=sampling_sigmas)
            else:
                raise NotImplementedError("Unsupported solver.")

            # sample videos
            latent = noise

            arg_c = {
                'context': [context[0]],
                'clip_fea': clip_context,
                'seq_len': max_seq_len,
                'y': [y],
            }

            arg_null = {
                'context': context_null,
                'clip_fea': clip_context,
                'seq_len': max_seq_len,
                'y': [y],
            }

            if offload_model:
                torch.cuda.empty_cache()

            self.model.to(self.device)
            for _, t in enumerate(tqdm(timesteps)):
                latent_model_input = [latent.to(self.device)]
                timestep = [t]

                timestep = torch.stack(timestep).to(self.device)

                noise_pred_cond = self.model(
                    latent_model_input, t=timestep, **arg_c)[0].to(
                        torch.device('cpu') if offload_model else self.device)
                if offload_model:
                    torch.cuda.empty_cache()
                noise_pred_uncond = self.model(
                    latent_model_input, t=timestep, **arg_null)[0].to(
                        torch.device('cpu') if offload_model else self.device)
                if offload_model:
                    torch.cuda.empty_cache()
                noise_pred = noise_pred_uncond + guide_scale * (
                    noise_pred_cond - noise_pred_uncond)

                latent = latent.to(
                    torch.device('cpu') if offload_model else self.device)

                temp_x0 = sample_scheduler.step(
                    noise_pred.unsqueeze(0),
                    t,
                    latent.unsqueeze(0),
                    return_dict=False,
                    generator=seed_g)[0]
                latent = temp_x0.squeeze(0)

                x0 = [latent.to(self.device)]
                del latent_model_input, timestep

            if offload_model:
                self.model.cpu()
                torch.cuda.empty_cache()

            if self.rank == 0:
                videos = self.vae.decode(x0)

        del noise, latent
        del sample_scheduler
        if offload_model:
            gc.collect()
            torch.cuda.synchronize()
        if dist.is_initialized():
            dist.barrier()

        return videos[0] if self.rank == 0 else None


    def test_providing_channel(self):
        chan = Mock(name='channel')
        cs = compat.ConsumerSet(self.connection, channel=chan)
        assert cs._provided_channel
        assert cs.backend is chan

        cs.cancel = Mock(name='cancel')
        cs.close()
        chan.close.assert_not_called()


	def exit(self, save=True):
		""" Close the database and save all operations done to it if `save` is
		True. Write all contexts paths (NON fully-dotted) to the contexts file
		if at least one context was created or removed during operations. The
		contexts file exists for terminal auto-completion."""
		if save:
			self.connection.commit()
			if self.changed_contexts:
				c = self.connection.cursor()
				c.execute("""
					SELECT DISTINCT path FROM Context
					ORDER BY path
				""")
				data_ctx = op.join(DATA_DIR, DATA_CTX_NAME)
				with open(data_ctx, 'w') as ctx_file:
					for row in c:
						ctx = userify_context(row[0])
						ctx_file.write(ctx + '\n')
		self.connection.close()


def init_model(args):
    tokenizer = AutoTokenizer.from_pretrained('./model/')
    if args.load == 0:
        moe_path = '_moe' if args.use_moe else ''
        modes = {0: 'pretrain', 1: 'full_sft', 2: 'rlhf', 3: 'reason', 4: 'grpo'}
        ckp = f'./{args.out_dir}/{modes[args.model_mode]}_{args.hidden_size}{moe_path}.pth'

        model = MiniMindForCausalLM(MiniMindConfig(
            hidden_size=args.hidden_size,
            num_hidden_layers=args.num_hidden_layers,
            use_moe=args.use_moe
        ))

        model.load_state_dict(torch.load(ckp, map_location=args.device), strict=True)

        if args.lora_name != 'None':
            apply_lora(model)
            load_lora(model, f'./{args.out_dir}/lora/{args.lora_name}_{args.hidden_size}.pth')
    else:
        transformers_model_path = './MiniMind2'
        tokenizer = AutoTokenizer.from_pretrained(transformers_model_path)
        model = AutoModelForCausalLM.from_pretrained(transformers_model_path, trust_remote_code=True)
    print(f'MiniMind模型参数量: {sum(p.numel() for p in model.parameters() if p.requires_grad) / 1e6:.2f}M(illion)')
    return model.eval().to(args.device), tokenizer


def prompt_user_action() -> str:
    """
    Use inquirer to ask the user which action they want to perform.

    :return: Selected action.
    """
    try:
        questions = [
            inquirer.List(
                'action',
                message="Select the action you want to perform:",
                choices=[
                    "Generate Resume",
                    "Generate Resume Tailored for Job Description",
                    "Generate Tailored Cover Letter for Job Description",
                ],
            ),
        ]
        answer = inquirer.prompt(questions)
        if answer is None:
            print("No answer provided. The user may have interrupted.")
            return ""
        return answer.get('action', "")
    except Exception as e:
        print(f"An error occurred: {e}")
        return ""


    def __init__(
        self,
        tracing_enabled: bool = False,
        initial_output: list[TResponseOutputItem] | Exception | None = None,
    ):
        if initial_output is None:
            initial_output = []
        self.turn_outputs: list[list[TResponseOutputItem] | Exception] = (
            [initial_output] if initial_output else []
        )
        self.tracing_enabled = tracing_enabled
        self.last_turn_args: dict[str, Any] = {}
        self.hardcoded_usage: Usage | None = None


def test_search_with_metadata(setup_test_memory):
    """Test search with metadata"""
    video_file, index_file, chunks = setup_test_memory
    retriever = MemvidRetriever(video_file, index_file)

    results = retriever.search_with_metadata("blockchain", top_k=2)
    assert len(results) <= 2

    if results:
        result = results[0]
        assert "text" in result
        assert "score" in result
        assert "chunk_id" in result
        assert "frame" in result
        assert result["score"] > 0


    async def visit_page(self, channel_names: list[str] = None):
        tasks_config = [
            ("hotel_fofa", get_channels_by_fofa, "hotel_fofa_result"),
            ("multicast", get_channels_by_multicast, "multicast_result"),
            ("hotel_foodie", get_channels_by_hotel, "hotel_foodie_result"),
            ("subscribe", get_channels_by_subscribe_urls, "subscribe_result"),
            (
                "online_search",
                get_channels_by_online_search,
                "online_search_result",
            ),
            ("epg", get_epg, "epg_result"),
        ]

        for setting, task_func, result_attr in tasks_config:
            if (
                    setting == "hotel_foodie" or setting == "hotel_fofa"
            ) and config.open_hotel == False:
                continue
            if config.open_method[setting]:
                if setting == "subscribe":
                    subscribe_urls = get_urls_from_file(constants.subscribe_path)
                    whitelist_urls = get_urls_from_file(constants.whitelist_path)
                    if not os.getenv("GITHUB_ACTIONS") and config.cdn_url:
                        subscribe_urls = [join_url(config.cdn_url, url) if "raw.githubusercontent.com" in url else url
                                          for url in subscribe_urls]
                    task = asyncio.create_task(
                        task_func(subscribe_urls,
                                  names=channel_names,
                                  whitelist=whitelist_urls,
                                  callback=self.update_progress
                                  )
                    )
                elif setting == "hotel_foodie" or setting == "hotel_fofa":
                    task = asyncio.create_task(task_func(callback=self.update_progress))
                else:
                    task = asyncio.create_task(
                        task_func(channel_names, callback=self.update_progress)
                    )
                self.tasks.append(task)
                setattr(self, result_attr, await task)


    def test_include_xml(self):
        """
        wineventlog - Include raw XML event
        """
        msg = "Include raw XML event"
        self.write_event_log(msg)
        evts = self.read_events(config={
            "event_logs": [
                {
                    "name": self.providerName,
                    "api": self.api,
                    "include_xml": True,
                }
            ]
        })
        self.assertTrue(len(evts), 1)
        self.assert_common_fields(evts[0], msg=msg)
        self.assertTrue("event.original" in evts[0])
        original = evts[0]["event.original"]
        self.assertTrue(original.endswith('</Event>'),
                        'xml value should end with </Event>: "{}"'.format(original))


def encode_jpeg_handler(converter: TensorFlowConverter, tf_op: "tf.Operation"):
    raise NotImplementedError(f"[TensorFlowConverter] {tf_op.type} is not supported yet.")


    def __init__(self, open, high, low, close):
        pass


    def _fetch_thumbnail(self):
        url = get_thumbnail_url(self.data)
        if url is not None:
            self._msg = self._api.download_thumb(
                url, self.__message_done_cb)


async def test_get_file_size(computer):
    """Test get_file_size functionality."""
    tmp_path = "test_file_size.txt"
    test_content = "A" * 1000  # 1000 bytes

    await computer.interface.write_text(tmp_path, test_content)

    file_size = await computer.interface.get_file_size(tmp_path)
    assert file_size == 1000, f"File size should be 1000 bytes, got {file_size}"

    await computer.interface.delete_file(tmp_path)


        def _expanduser(path):
            return self.tmpdir


    def read(self):
        self.get_style_context().add_class('read')
        self._g('unread').props.visible = False


        def default_callback(self, *args, **kwargs):
            pass


    def __init__(self):
        self.root_path = config.root_path
        self.server_reference = config.mcp_config.server_reference
        self.agent = MCPAgent()


def convert_root_path():
    frame = inspect.currentframe().f_back
    caller_file = frame.f_code.co_filename
    caller_file = os.path.abspath(caller_file)
    result = os.path.join(os.path.dirname(caller_file), 'huggingface_space_mirror')
    return result + '/'


def setup_test_memory():
    """Create test video and index"""
    encoder = MemvidEncoder()
    chunks = [
        "Quantum computing uses qubits for parallel processing",
        "Machine learning models require large datasets",
        "Neural networks mimic brain structure",
        "Cloud computing provides scalable resources",
        "Blockchain ensures data immutability"
    ]
    encoder.add_chunks(chunks)

    with tempfile.TemporaryDirectory() as temp_dir:
        video_file = os.path.join(temp_dir, "test.mp4")
        index_file = os.path.join(temp_dir, "test_index.json")

        encoder.build_video(video_file, index_file, show_progress=False)

        yield video_file, index_file, chunks


def failed(message="Please, reload the task and try again.", name=None):
    """ Reports failure """
    if not name:
        name = sys._getframe().f_back.f_code.co_name
    print("#educational_plugin " + name + " FAILED + " + message)


def build(setup_kwargs):
    """
    This function is called by Poetry during the build process.
    `setup_kwargs` is a dictionary that will be passed to `setuptools.setup()`.
    """
    print('--- Running custom Poetry build script (build_vscode.py) ---')

    # Build the VS Code extension and place the .vsix file
    build_vscode_extension()

    # Poetry will handle including files based on pyproject.toml `include` patterns.
    # Ensure openhands/integrations/vscode/*.vsix is included there.

    print('--- Custom Poetry build script (build_vscode.py) finished ---')


async def get_search_refs(request: QueryRequest):

    global search_results
    search_results = []
    search_item_list = []
    if request.ask_type == "search":
        search_links,search_results = free_ask_internet.search_web_ref(request.query)
        for search_item in search_links:
            snippet = search_item.get("snippet")
            url = search_item.get("url")
            icon_url = search_item.get("icon_url")
            site_name = search_item.get("site_name")
            title = search_item.get("title")

            si = SearchItem(snippet=snippet,url=url,icon_url=icon_url,site_name=site_name,title=title)

            search_item_list.append(si)

    resp = SearchResp(code=0,msg="success",data=search_item_list)

    return  resp


    def test_create_dhparams(self, tmpdir):
        filename = str(tmpdir.join("dhparam.pem"))
        certs.CertStore.load_dhparam(filename)
        assert os.path.exists(filename)


        def on_update(www_auth):
            if not www_auth and 'www-authenticate' in self.headers:
                del self.headers['www-authenticate']
            elif www_auth:
                self.headers['WWW-Authenticate'] = www_auth.to_header()


def get_browser_session():
    project_name = request.args.get("project_name")
    agent_state = AgentState.get_latest_state(project_name)
    if not agent_state:
        return jsonify({"session": None})
    else:
        browser_session = agent_state["browser_session"]
        return jsonify({"session": browser_session})


    def _parseReply(self, tag_name, elements, data):
        """
        Gets the replies from the PLC
        In the case of BOOL arrays and bits of
            a word, we do some reformating
        """
        tag, base_tag, index = _parseTagName(tag_name, 0)
        data_type = self.KnownTags[base_tag][0]
        bit_count = self.CIPTypes[data_type][0] * 8

        # if bit of word was requested
        if BitofWord(tag_name):
            split_tag = tag_name.split('.')
            bit_pos = split_tag[len(split_tag)-1]
            bit_pos = int(bit_pos)

            word_count = _getWordCount(bit_pos, elements, bit_count)
            words = self._getReplyValues(tag_name, word_count, data)
            vals = self._wordsToBits(tag_name, words, count=elements)
        elif data_type == 211:
            word_count = _getWordCount(index, elements, bit_count)
            words = self._getReplyValues(tag_name, word_count, data)
            vals = self._wordsToBits(tag_name, words, count=elements)
        else:
            vals = self._getReplyValues(tag_name, elements, data)

        return vals


  def testExtensionScope(self):
    self.assertEqual(unittest_pb2.optional_int32_extension.extension_scope,
                     None)
    self.assertEqual(unittest_pb2.TestRequired.single.extension_scope,
                     unittest_pb2.TestRequired.DESCRIPTOR)


    def test_delitem_with_1_valid_key(self):
        d = {
            'a': 1,
        }
        b = KeypathDict(d)
        del b['a']
        with self.assertRaises(KeyError):
            del b['a']
        self.assertEqual(b.get('a'), None)


def main() -> None:
    asyncio.run(run())


    def fit(self, attribute_list, class_list, row_sampler, col_sampler, bin_structure):
        # when we start to fit a tree, we first conduct row and column sampling
        col_sampler.shuffle()
        row_sampler.shuffle()
        class_list.sampling(row_sampler.row_mask)

        # then we create the root node, initialize histogram(Gradient sum and Hessian sum)
        root_node = TreeNode(name=1, depth=1, feature_dim=attribute_list.feature_dim)
        root_node.Grad_setter(class_list.grad.sum())
        root_node.Hess_setter(class_list.hess.sum())
        self.root = root_node

        # every time a new node is created, we put it into self.name_to_node
        self.name_to_node[root_node.name] = root_node

        # put it into the alive_node, and fill the class_list, all data are assigned to root node initially
        self.alive_nodes.append(root_node)
        for i in range(class_list.dataset_size):
            class_list.corresponding_tree_node[i] = root_node

        # then build the tree util there is no alive tree_node to split
        self.build(attribute_list, class_list, col_sampler, bin_structure)
        self.clean_up()


def list_of_dict_to_dict_of_list(list_of_dict: list[dict]):
    if len(list_of_dict) == 0:
        return {}
    keys = list_of_dict[0].keys()
    output = {key: [] for key in keys}
    for data in list_of_dict:
        for key, item in data.items():
            assert key in output
            output[key].append(item)
    return output


    def __message_done_cb(self, pixbuf):
        self._msg = None
        self._g('preview').props.pixbuf = pixbuf
        self._g('preview-button').show()
        self._g('preview-button').connect('clicked', self.__image_clicked_cb)


    def _initial_read(self, tag, base_tag, data_type):
        """
        Store each unique tag read in a dict so that we can retreive the
        data type or data length (for STRING) later
        """
        # if a tag already exists, return True
        if base_tag in self.KnownTags:
            return tag, None, 0
        if data_type:
            self.KnownTags[base_tag] = (data_type, 0)
            return tag, None, 0

        ioi = self._buildTagIOI(base_tag, data_type)
        request = self._add_partial_read_service(ioi, 1)

        # send our tag read request
        status, ret_data = self.conn.send(request)

        # make sure it was successful
        if status == 0 or status == 6:
            data_type = unpack_from('<B', ret_data, 50)[0]
            data_len = unpack_from('<H', ret_data, 2)[0]
            self.KnownTags[base_tag] = (data_type, data_len)
            return tag, None, 0
        else:
            return tag, None, status


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('input', type=str, nargs='+',
                        help='Source file or directory.')
    parser.add_argument('output', type=str,
                        help='Destination directory.')
    parser.add_argument('-W', '--width', type=int, default=320,
                        help='width of output image (default:320)')
    parser.add_argument('-H', '--height', type=int, default=320,
                        help='height of output image (default:320)')
    parser.add_argument('-m', '--mode', default='trimming',
                        choices=['trimming', 'padding'],
                        help='shaping mode (trimming or padding)  (default:trimming)')
    parser.add_argument('-S', '--shuffle', choices=['True', 'False'],
                        help='shuffle mode if not specified, train:True, val:False.' +
                        ' Otherwise specified value will be used for both.')
    parser.add_argument('-N', '--file-cache-size', type=int, default=100,
                        help='num of data in cache file (default:100)')
    parser.add_argument('-C', '--cache-type', default='npy',
                        choices=['h5', 'npy'],
                        help='cache format (h5 or npy) (default:npy)')
    parser.add_argument('--thinning', type=int, default=1,
                        help='Thinning rate')

    args = parser.parse_args()
    ############################################################################
    # Analyze tar
    # If it consists only of members corresponding to regular expression
    # 'n[0-9]{8}\.tar', it is judged as train data archive.
    # If it consists only of members corresponding to regular expression
    # 'ILSVRC2012_val_[0-9]{8}\.JPEG', it is judged as validation data archive.

    archives = {'train': None, 'val': None}
    for inputarg in args.input:
        print('Checking input file [{}]'.format(inputarg))
        archive = tarfile.open(inputarg)
        is_train = False
        is_val = False
        names = []
        for name in archive.getnames():
            if re.match(r'n[0-9]{8}\.tar', name):
                if is_val:
                    print('Train data {} includes in validation tar'.format(name))
                    exit(-1)
                is_train = True
            elif re.match(r'ILSVRC2012_val_[0-9]{8}\.JPEG', name):
                if is_train:
                    print('Validation data {} includes in train tar'.format(name))
                    exit(-1)
                is_val = True
            else:
                print('Invalid member {} includes in tar file'.format(name))
                exit(-1)
            names.append(name)
        if is_train:
            if archives['train'] is None:
                archives['train'] = (archive, names)
            else:
                print('Please specify only 1 training tar archive.')
                exit(-1)
        if is_val:
            if archives['val'] is None:
                archives['val'] = (archive, names)
            else:
                print('Please specify only 1 validation tar archive.')
                exit(-1)

    # Read label of validation data, (Use ascending label of wordnet_id)
    validation_ground_truth = []
    g_file = VALIDATION_DATA_LABEL
    with open(g_file, 'r') as f:
        for l in f.readlines():
            validation_ground_truth.append(int(l.rstrip()))

    ############################################################################
    # Prepare logging
    tmpdir = tempfile.mkdtemp()
    logfilename = os.path.join(tmpdir, 'nnabla.log')

    # Temporarily chdir to tmpdir just before importing nnabla to reflect nnabla.conf.
    cwd = os.getcwd()
    os.chdir(tmpdir)
    with open('nnabla.conf', 'w') as f:
        f.write('[LOG]\n')
        f.write('log_file_name = {}\n'.format(logfilename))
        f.write('log_file_format = %(funcName)s : %(message)s\n')
        f.write('log_console_level = CRITICAL\n')

    from nnabla.config import nnabla_config
    os.chdir(cwd)

    ############################################################################
    # Data iterator setting
    nnabla_config.set('DATA_ITERATOR',
                      'cache_file_format', '.' + args.cache_type)
    nnabla_config.set('DATA_ITERATOR',
                      'data_source_file_cache_size', str(args.file_cache_size))
    nnabla_config.set('DATA_ITERATOR',
                      'data_source_file_cache_num_of_threads', '1')

    if not os.path.isdir(args.output):
        os.makedirs(args.output)

    ############################################################################
    # Prepare status monitor
    from nnabla.utils.progress import configure_progress
    configure_progress(None, _progress)

    ############################################################################
    # Converter

    try:
        if archives['train'] is not None:
            from nnabla.logger import logger
            logger.info('StartCreatingCache')
            archive, names = archives['train']
            output = os.path.join(args.output, 'train')
            if not os.path.isdir(output):
                os.makedirs(output)
            _create_train_cache(archive, output, names, args)
        if archives['val'] is not None:
            from nnabla.logger import logger
            logger.info('StartCreatingCache')
            archive, names = archives['val']
            output = os.path.join(args.output, 'val')
            if not os.path.isdir(output):
                os.makedirs(output)
            _create_validation_cache(
                archive, output, names, validation_ground_truth, args)
    except KeyboardInterrupt:
        shutil.rmtree(tmpdir, ignore_errors=True)

        # Even if CTRL-C is pressed, it does not stop if there is a running
        # thread, so it sending a signal to itself.
        os.kill(os.getpid(), 9)

    ############################################################################
    # Finish
    _finish = True
    shutil.rmtree(tmpdir, ignore_errors=True)


    def homogeneous_generator_noncommutative_variables(self, r):
        r"""
        Give the `r^{th}` homogeneous function inside the Nil-Coxeter algebra.
        In finite type `A` this is the sum of all decreasing elements of length `r`.
        In affine type `A` this is the sum of all cyclically decreasing elements of length `r`.
        This is only defined in finite type `A`, `B` and affine types `A^{(1)}`, `B^{(1)}`, `C^{(1)}`, `D^{(1)}`.

        INPUT:

        - ``r`` -- a positive integer at most the rank of the Weyl group

        EXAMPLES::

            sage: U = NilCoxeterAlgebra(WeylGroup(['A',3,1]))
            sage: U.homogeneous_generator_noncommutative_variables(2)
            u[1,0] + u[2,0] + u[0,3] + u[3,2] + u[3,1] + u[2,1]

            sage: U = NilCoxeterAlgebra(WeylGroup(['B',4]))
            sage: U.homogeneous_generator_noncommutative_variables(2)
            u[1,2] + u[2,1] + u[3,1] + u[4,1] + u[2,3] + u[3,2] + u[4,2] + u[3,4] + u[4,3]

            sage: U = NilCoxeterAlgebra(WeylGroup(['C',3]))
            sage: U.homogeneous_generator_noncommutative_variables(2)
            Traceback (most recent call last):
            ...
            AssertionError: Analogue of symmetric functions in noncommutative variables is not defined in type ['C', 3]

        TESTS::

            sage: U = NilCoxeterAlgebra(WeylGroup(['B',3,1]))
            sage: U.homogeneous_generator_noncommutative_variables(-1)
            0
            sage: U.homogeneous_generator_noncommutative_variables(0)
            1

        """
        assert (len(self._cartan_type) == 2 and self._cartan_type[0] in ['A','B']) or (len(self._cartan_type) == 3 and self._cartan_type[2] == 1), "Analogue of symmetric functions in noncommutative variables is not defined in type %s"%(self._cartan_type)
        if r >= self._n:
            return self.zero()
        return self.sum_of_monomials(w for w in self._W.pieri_factors() if w.length() == r)


    def _get_label(wordnet_id):
        for item in wid2ind:
            if item[1] == wordnet_id:
                return item[0]


async def main():
    # Parse command line arguments
    parser = argparse.ArgumentParser(description="Run Manus agent with a prompt")
    parser.add_argument(
        "--prompt", type=str, required=False, help="Input prompt for the agent"
    )
    args = parser.parse_args()

    # Create and initialize Manus agent
    agent = await Manus.create()
    try:
        # Use command line prompt if provided, otherwise ask for input
        prompt = args.prompt if args.prompt else input("Enter your prompt: ")
        if not prompt.strip():
            logger.warning("Empty prompt provided.")
            return

        logger.warning("Processing your request...")
        await agent.run(prompt)
        logger.info("Request processing completed.")
    except KeyboardInterrupt:
        logger.warning("Operation interrupted.")
    finally:
        # Ensure agent resources are cleaned up before exiting
        await agent.cleanup()


    def test_wait(self, n='test_wait'):

        class C(compat.Consumer):

            def iterconsume(self, limit=None):
                yield from range(limit)

        c = C(self.connection,
              queue=n, exchange=n, routing_key='rkey')
        assert c.wait(10) == list(range(10))
        c.close()


    def generate(
        self,
        prompt_tokens: List[List[int]],
        max_gen_len: int,
        temperature: float = 0.6,
        top_p: float = 0.9,
        logprobs: bool = False,
        echo: bool = False,
    ) -> Tuple[List[List[int]], Optional[List[List[float]]]]:
        """
        Generate text sequences based on provided prompts using the language generation model.

        Args:
            prompt_tokens (List[List[int]]): List of tokenized prompts, where each prompt is represented as a list of integers.
            max_gen_len (int): Maximum length of the generated text sequence.
            temperature (float, optional): Temperature value for controlling randomness in sampling. Defaults to 0.6.
            top_p (float, optional): Top-p probability threshold for nucleus sampling. Defaults to 0.9.
            logprobs (bool, optional): Flag indicating whether to compute token log probabilities. Defaults to False.
            echo (bool, optional): Flag indicating whether to include prompt tokens in the generated output. Defaults to False.

        Returns:
            Tuple[List[List[int]], Optional[List[List[float]]]]: A tuple containing generated token sequences and, if logprobs is True, corresponding token log probabilities.

        Note:
            This method uses the provided prompts as a basis for generating text. It employs nucleus sampling to produce text with controlled randomness.
            If logprobs is True, token log probabilities are computed for each generated token.

        """
        params = self.model.params
        bsz = len(prompt_tokens)
        assert bsz <= params.max_batch_size, (bsz, params.max_batch_size)

        min_prompt_len = min(len(t) for t in prompt_tokens)
        max_prompt_len = max(len(t) for t in prompt_tokens)
        assert max_prompt_len <= params.max_seq_len
        total_len = min(params.max_seq_len, max_gen_len + max_prompt_len)

        pad_id = self.tokenizer.pad_id
        tokens = torch.full((bsz, total_len), pad_id, dtype=torch.long, device="cuda")
        for k, t in enumerate(prompt_tokens):
            tokens[k, : len(t)] = torch.tensor(t, dtype=torch.long, device="cuda")
        if logprobs:
            token_logprobs = torch.zeros_like(tokens, dtype=torch.float)

        prev_pos = 0
        eos_reached = torch.tensor([False] * bsz, device="cuda")
        input_text_mask = tokens != pad_id
        if min_prompt_len == total_len:
            logits = self.model.forward(tokens, prev_pos)
            token_logprobs = -F.cross_entropy(
                input=logits.transpose(1, 2),
                target=tokens,
                reduction="none",
                ignore_index=pad_id,
            )

        stop_tokens = torch.tensor(list(self.tokenizer.stop_tokens))

        for cur_pos in range(min_prompt_len, total_len):
            logits = self.model.forward(tokens[:, prev_pos:cur_pos], prev_pos)
            if temperature > 0:
                probs = torch.softmax(logits[:, -1] / temperature, dim=-1)
                next_token = sample_top_p(probs, top_p)
            else:
                next_token = torch.argmax(logits[:, -1], dim=-1)

            next_token = next_token.reshape(-1)
            # only replace token if prompt has already been generated
            next_token = torch.where(
                input_text_mask[:, cur_pos], tokens[:, cur_pos], next_token
            )
            tokens[:, cur_pos] = next_token
            if logprobs:
                token_logprobs[:, prev_pos + 1 : cur_pos + 1] = -F.cross_entropy(
                    input=logits.transpose(1, 2),
                    target=tokens[:, prev_pos + 1 : cur_pos + 1],
                    reduction="none",
                    ignore_index=pad_id,
                )
            eos_reached |= (~input_text_mask[:, cur_pos]) & (
                torch.isin(next_token, stop_tokens)
            )
            prev_pos = cur_pos
            if all(eos_reached):
                break

        if logprobs:
            token_logprobs = token_logprobs.tolist()
        out_tokens, out_logprobs = [], []
        for i, toks in enumerate(tokens.tolist()):
            # cut to max gen len
            start = 0 if echo else len(prompt_tokens[i])
            toks = toks[start : len(prompt_tokens[i]) + max_gen_len]
            probs = None
            if logprobs:
                probs = token_logprobs[i][start : len(prompt_tokens[i]) + max_gen_len]
            # cut to after eos tok if any
            for stop_token in self.tokenizer.stop_tokens:
                try:
                    eos_idx = toks.index(stop_token)
                    toks = toks[:eos_idx]
                    probs = probs[:eos_idx] if logprobs else None
                except ValueError:
                    pass
            out_tokens.append(toks)
            out_logprobs.append(probs)
        return (out_tokens, out_logprobs if logprobs else None)


def check_node_version():
    """Check if Node.js version is sufficient for building the extension."""
    try:
        result = subprocess.run(
            ['node', '--version'], capture_output=True, text=True, check=True
        )
        version_str = result.stdout.strip()
        # Extract major version number (e.g., "v12.22.9" -> 12)
        major_version = int(version_str.lstrip('v').split('.')[0])
        return major_version >= 18  # Align with frontend actual usage (18.20.1)
    except (subprocess.CalledProcessError, FileNotFoundError, ValueError):
        return False


def mock_openai_client():
    m = MockOpenAIClient()
    m.set_response(
        create_mock_response({"role": "assistant", "content": DEFAULT_RESPONSE_CONTENT})
    )
    return m


    def testTobaccoPaperMissingParagraphs(self):
        local_pdf_path = os.path.join(os.path.dirname(__file__), "gnarly_pdfs", "tobacco_missed_tokens_pg1.pdf")

        anchor_text = get_anchor_text(local_pdf_path, 1, pdf_engine="pdfreport")

        print(anchor_text)
        print(len(anchor_text))
        self.assertLessEqual(len(anchor_text), 4000)


    def _percent_uncovered(key):
        (total, covered, percent) = covdata[key].coverage()
        if covered:
            return -1.0*covered/total
        else:
            return total or 1e6


def send_request(ques):
    # url = 'https://qanything-test.site.youdao.com/api/local_doc_qa/local_doc_chat'
    url = 'http://localhost:8777/api/local_doc_qa/local_doc_chat'
    headers = {
        'content-type': 'application/json'
    }
    data = {
        "user_id": "liujx_265",
        "kb_ids": ["KBf652e9e379c546f1894597dcabdc8e47"],
        "question": ques,
        "rerank": False,
        "history": []
    }
    try:
        start_time = time.time()
        response = requests.post(url=url, headers=headers, json=data, timeout=60)
        end_time = time.time()
        response_times.append(end_time - start_time)
        res = response.json()
        print(res['response'])
        print(f"响应状态码: {response.status_code}, 响应时间: {end_time - start_time}秒")
    except Exception as e:
        print(f"请求发送失败: {e}")


    def __init__(self, base_params={}, base_url='http://localhost:9001',
                       api_version='1.1', timeout=20):
        self.api_version = api_version
        self.base_params = base_params
        self.base_url = base_url
        self.timeout = timeout


def process_pdf(file) -> List:
    """Process PDF file and split into chunks with metadata."""
    try:
        with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
            tmp_file.write(file.getvalue())
            loader = PyPDFLoader(tmp_file.name)
            documents = loader.load()

            # Add source metadata
            for doc in documents:
                doc.metadata.update({
                    "source_type": "pdf",
                    "file_name": file.name,
                    "timestamp": datetime.now().isoformat()
                })

            text_splitter = RecursiveCharacterTextSplitter(
                chunk_size=1000,
                chunk_overlap=200
            )
            return text_splitter.split_documents(documents)
    except Exception as e:
        st.error(f"📄 PDF processing error: {str(e)}")
        return []


    def __getstate__(self):
        """Object.__getstate__.

        Returns:
            dict: the state to be pickled for the instance.

        """
        data = self.__dict__.copy()
        del data['_replay_buffers']
        del data['_context_replay_buffers']
        return data


def audio_highpass_filter(audio, sample_rate, highpass_cutoff_freq):
    """apply highpass fileter to audio

    Args:
        audio (np.ndarray):
        sample_rate (ind):
        highpass_cutoff_freq (int):
    """

    audio = torchaudio.functional.highpass_biquad(
        torch.from_numpy(audio), sample_rate, cutoff_freq=highpass_cutoff_freq
    )
    return audio.numpy()


def decode(x, canvas): # b * (10 + 3)
    x = x.view(-1, 10 + 3)
    stroke = 1 - Decoder(x[:, :10])
    stroke = stroke.view(-1, width, width, 1)
    color_stroke = stroke * x[:, -3:].view(-1, 1, 1, 3)
    stroke = stroke.permute(0, 3, 1, 2)
    color_stroke = color_stroke.permute(0, 3, 1, 2)
    stroke = stroke.view(-1, 5, 1, width, width)
    color_stroke = color_stroke.view(-1, 5, 3, width, width)
    res = []
    for i in range(5):
        canvas = canvas * (1 - stroke[:, i]) + color_stroke[:, i]
        res.append(canvas)
    return canvas, res


    def cmd_windows(self):
        """Return info for each client window"""
        return [
            i.info() for i in self.windows_map.values()
            if not isinstance(i, window.Internal)
        ]


def add_account(provider: str, account: dict) -> None:
    """
    Adds an account to the cache.

    Args:
        account (dict): The account to add

    Returns:
        None
    """
    if provider == "twitter":
        # Get the current accounts
        accounts = get_accounts("twitter")

        # Add the new account
        accounts.append(account)

        # Write the new accounts to the cache
        with open(get_twitter_cache_path(), 'w') as file:
            json.dump({
                "accounts": accounts
            }, file, indent=4)
    elif provider == "youtube":
        # Get the current accounts
        accounts = get_accounts("youtube")

        # Add the new account
        accounts.append(account)

        # Write the new accounts to the cache
        with open(get_youtube_cache_path(), 'w') as file:
            json.dump({
                "accounts": accounts
            }, file, indent=4)


    def save(self, fpath: Path):
        arg_dict = dict(
            t3=self.t3.__dict__,
            gen=self.gen
        )
        torch.save(arg_dict, fpath)


    def process_bind_param(self, value, dialect):
        return utils.to_json_str(value)


    def read_image_dims(self, image_data):
        """Read the image dimensions.

        Args:
            image_data: string of image data.
        Returns:
            image_height and image_width.
        """
        image = self.decode_image(image_data)
        return image.shape[:2]


    def __repr__(self):
        return f"ReplacementInfo(first_replaced_line={self.first_replaced_line}, n_search_lines={self.n_search_lines}, n_replace_lines={self.n_replace_lines}, n_replacements={self.n_replacements})"


    def testAnchorBase(self):
        local_pdf_path = os.path.join(os.path.dirname(__file__), "gnarly_pdfs", "pdftotext_two_column_issue.pdf")

        report = _pdf_report(local_pdf_path, 2)

        print(report)

        print(get_anchor_text(local_pdf_path, 2, pdf_engine="pdfreport"))


        def run_inference(image, video, model_id, image_size, conf_threshold, input_type):
            if input_type == "Image":
                return yolov10_inference(image, None, model_id, image_size, conf_threshold)
            else:
                return yolov10_inference(None, video, model_id, image_size, conf_threshold)


def test_add_metrics_list_input(tracer):
    """Test adding multiple metrics using list input"""
    tracer.trace = {}
    metrics = [
        {"name": "metric1", "score": 0.95},
        {"name": "metric2", "score": 0.85}
    ]
    tracer.add_metrics(metrics)

    assert len(tracer.trace_metrics) == 2
    assert tracer.trace_metrics[0]["name"] == "metric1"
    assert tracer.trace_metrics[1]["name"] == "metric2"


def non_max_suppression_v2_handler(converter: TensorFlowConverter, tf_op: "tf.Operation"):
    raise NotImplementedError(f"[TensorFlowConverter] {tf_op.type} is not supported yet.")


    def nudge(self):
        self.kill_process()
        self.paused = False
        self.task = self.run_task(self.get_agent().monologue)
        return self.task


	def __init__(self):
		RpcRequest.__init__(self, 'Domain', '2018-01-29', 'ListEmailVerification','domain')
		self.set_method('POST')
		if hasattr(self, "endpoint_map"):
			setattr(self, "endpoint_map", endpoint_data.getEndpointMap())
		if hasattr(self, "endpoint_regional"):
			setattr(self, "endpoint_regional", endpoint_data.getEndpointRegional())


  def testEnums(self):
    # We test only module-level enums here.
    # TODO(robinson): Examine descriptors directly to check
    # enum descriptor output.
    self.assertEqual(4, unittest_pb2.FOREIGN_FOO)
    self.assertEqual(5, unittest_pb2.FOREIGN_BAR)
    self.assertEqual(6, unittest_pb2.FOREIGN_BAZ)

    proto = unittest_pb2.TestAllTypes()
    self.assertEqual(1, proto.FOO)
    self.assertEqual(1, unittest_pb2.TestAllTypes.FOO)
    self.assertEqual(2, proto.BAR)
    self.assertEqual(2, unittest_pb2.TestAllTypes.BAR)
    self.assertEqual(3, proto.BAZ)
    self.assertEqual(3, unittest_pb2.TestAllTypes.BAZ)


def get_covrage():
    """get covrage"""
    # 发送请求获取网页内容
    html_content = open("htmlcov/index.html", "r", encoding="utf-8").read()
    soup = BeautifulSoup(html_content, 'html.parser')

    # 查找包含"pc_cov"的span标签
    pc_cov_span = soup.find('span', class_='pc_cov')

    # 提取百分比值
    percentage_value = pc_cov_span.text.strip()
    percentage_float = float(percentage_value.rstrip('%'))
    print ("percentage_float:", percentage_float)
    assert percentage_float >= 0.2


def pytest_collection_finish():
    print(f"\nTesting with {DEVICE=}")


    def _tokenize_segment(self, segment: Segment) -> Tuple[torch.Tensor, torch.Tensor]:
        """
        Returns:
            (seq_len, 33), (seq_len, 33)
        """
        text_tokens, text_masks = self._tokenize_text_segment(segment.text, segment.speaker)
        audio_tokens, audio_masks = self._tokenize_audio(segment.audio)

        return torch.cat([text_tokens, audio_tokens], dim=0), torch.cat([text_masks, audio_masks], dim=0)


def main(hf_ckpt_path, save_path, n_experts, mp):
    """
    Converts and saves model checkpoint files into a specified format.

    Args:
        hf_ckpt_path (str): Path to the directory containing the input checkpoint files.
        save_path (str): Path to the directory where the converted checkpoint files will be saved.
        n_experts (int): Total number of experts in the model.
        mp (int): Model parallelism factor.

    Returns:
        None
    """
    torch.set_num_threads(8)
    n_local_experts = n_experts // mp
    state_dicts = [{} for _ in range(mp)]

    for file_path in tqdm(glob(os.path.join(hf_ckpt_path, "*.safetensors"))):
        with safe_open(file_path, framework="pt", device="cpu") as f:
            for name in f.keys():
                if "model.layers.61" in name:
                    continue
                param: torch.Tensor = f.get_tensor(name)
                if name.startswith("model."):
                    name = name[len("model."):]
                name = name.replace("self_attn", "attn")
                name = name.replace("mlp", "ffn")
                name = name.replace("weight_scale_inv", "scale")
                name = name.replace("e_score_correction_bias", "bias")
                key = name.split(".")[-2]
                assert key in mapping, f"Key {key} not found in mapping"
                new_key, dim = mapping[key]
                name = name.replace(key, new_key)
                for i in range(mp):
                    new_param = param
                    if "experts" in name and "shared_experts" not in name:
                        idx = int(name.split(".")[-3])
                        if idx < i * n_local_experts or idx >= (i + 1) * n_local_experts:
                            continue
                    elif dim is not None:
                        assert param.size(dim) % mp == 0, f"Dimension {dim} must be divisible by {mp}"
                        shard_size = param.size(dim) // mp
                        new_param = param.narrow(dim, i * shard_size, shard_size).contiguous()
                    state_dicts[i][name] = new_param

    os.makedirs(save_path, exist_ok=True)

    for i in trange(mp):
        save_file(state_dicts[i], os.path.join(save_path, f"model{i}-mp{mp}.safetensors"))

    for file_path in glob(os.path.join(hf_ckpt_path, "*token*")):
        new_file_path = os.path.join(save_path, os.path.basename(file_path))
        shutil.copyfile(file_path, new_file_path)


def retain_logging_level():
    """Fixture to preserve the logging level between tests."""
    logger = get_logger("oumi")
    # Store the current log level
    log_level = logger.level
    yield
    # Rehydrate the log level
    logger.setLevel(log_level)


    def test_atomic_min_float(self):
        self.check_atomic_min(dtype=np.float32, lo=-65535, hi=65535)


    def __init__(self, name, toolpath=None, **kw):
        if toolpath is None:
            toolpath = []

        # Rename if there's a TOOL_ALIAS for this tool
        self.name = TOOL_ALIASES.get(name, name)
        self.toolpath = toolpath + DefaultToolpath
        # remember these so we can merge them into the call
        self.init_kw = kw

        module = self._tool_module()
        self.generate = module.generate
        self.exists = module.exists
        if hasattr(module, 'options'):
            self.options = module.options


def union_numpy_dict(tensor_dict1: dict[np.ndarray], tensor_dict2: dict[np.ndarray]) -> dict[np.ndarray]:
    for key, val in tensor_dict2.items():
        if key in tensor_dict1:
            assert isinstance(tensor_dict2[key], np.ndarray)
            assert isinstance(tensor_dict1[key], np.ndarray)
            assert np.all(tensor_dict2[key] == tensor_dict1[key]), \
                f'{key} in tensor_dict1 and tensor_dict2 are not the same object'
        tensor_dict1[key] = val

    return tensor_dict1


    def reduce(self, acc, val):
        # Either USE_MEM or empty string
        return val or acc


  def test_calculate_samples_count_per_handler_show_compile(self):
    counters = ipr.calculate_samples_count_per_handler([
      ["foo", "BytecodeHandler:bar"],
      ["foo", "BytecodeHandler:bar"],
      ["beep", "BytecodeHandler:bar"],
      ["hello", "v8::internal::Compiler", "[compiler]"],
    ])
    self.assertItemsEqual(counters, [
      ("bar", 3),
      ("[compiler]", 1)
    ])


def unregister(self, fileobj: _FileObject) -> SelectorKey: ...


def act_quant_kernel(x_ptr, y_ptr, s_ptr, BLOCK_SIZE: tl.constexpr):
    """
    Quantizes the input tensor `x_ptr` and stores the result in `y_ptr` and the scaling factor in `s_ptr`.

    Args:
        x_ptr (triton.Pointer): Pointer to the input tensor.
        y_ptr (triton.Pointer): Pointer to the output tensor where quantized values will be stored.
        s_ptr (triton.Pointer): Pointer to the output tensor where scaling factors will be stored.
        BLOCK_SIZE (tl.constexpr): The size of the block to be processed by each program instance.

    Returns:
        None
    """
    pid = tl.program_id(axis=0)
    offs = pid * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
    x = tl.load(x_ptr + offs).to(tl.float32)
    s = tl.max(tl.abs(x)) / 448.
    y = x / s
    y = y.to(y_ptr.dtype.element_ty)
    tl.store(y_ptr + offs, y)
    tl.store(s_ptr + pid, s)


def create_demo(stdout):
    """
    Create a database with demo data for developers to work in a realistic environment.
    The code is engineered to create a huge number of objects fast.
    """

    queue = BulkQueue(stdout)

    with Timeit(stdout, "Creating users"):
        name_size = int(math.sqrt(defaults.NB_OBJECTS["users"]))
        first_names = [fake.first_name() for _ in range(name_size)]
        last_names = [fake.last_name() for _ in range(name_size)]
        for i in range(defaults.NB_OBJECTS["users"]):
            first_name = random.choice(first_names)
            queue.push(
                models.User(
                    admin_email=f"user{i:d}@example.com",
                    email=f"user{i:d}@example.com",
                    password="!",
                    is_superuser=False,
                    is_active=True,
                    is_staff=False,
                    short_name=first_name,
                    full_name=f"{first_name:s} {random.choice(last_names):s}",
                    language=random.choice(settings.LANGUAGES)[0],
                )
            )
        queue.flush()

    users_ids = list(models.User.objects.values_list("id", flat=True))

    with Timeit(stdout, "Creating documents"):
        for i in range(defaults.NB_OBJECTS["docs"]):
            # pylint: disable=protected-access
            key = models.Document._int2str(i)  # noqa: SLF001
            padding = models.Document.alphabet[0] * (models.Document.steplen - len(key))
            queue.push(
                models.Document(
                    depth=1,
                    path=f"{padding}{key}",
                    creator_id=random.choice(users_ids),
                    title=fake.sentence(nb_words=4),
                    link_reach=models.LinkReachChoices.AUTHENTICATED
                    if random_true_with_probability(0.5)
                    else random.choice(models.LinkReachChoices.values),
                )
            )

        queue.flush()

    with Timeit(stdout, "Creating docs accesses"):
        docs_ids = list(models.Document.objects.values_list("id", flat=True))
        for doc_id in docs_ids:
            for user_id in random.sample(
                users_ids,
                random.randint(1, defaults.NB_OBJECTS["max_users_per_document"]),
            ):
                role = random.choice(models.RoleChoices.choices)
                queue.push(
                    models.DocumentAccess(
                        document_id=doc_id, user_id=user_id, role=role[0]
                    )
                )
        queue.flush()

    with Timeit(stdout, "Creating development users"):
        for dev_user in defaults.DEV_USERS:
            queue.push(
                models.User(
                    admin_email=dev_user["email"],
                    email=dev_user["email"],
                    sub=dev_user["email"],
                    password="!",
                    is_superuser=False,
                    is_active=True,
                    is_staff=False,
                    language=dev_user["language"]
                    or random.choice(settings.LANGUAGES)[0],
                )
            )

        queue.flush()

    with Timeit(stdout, "Creating docs accesses on development users"):
        for dev_user in defaults.DEV_USERS:
            docs_ids = list(models.Document.objects.values_list("id", flat=True))
            user_id = models.User.objects.get(email=dev_user["email"]).id

            for doc_id in docs_ids:
                role = random.choice(models.RoleChoices.choices)
                queue.push(
                    models.DocumentAccess(
                        document_id=doc_id, user_id=user_id, role=role[0]
                    )
                )

        queue.flush()

    with Timeit(stdout, "Creating Template"):
        with open(
            file="demo/data/template/code.txt", mode="r", encoding="utf-8"
        ) as text_file:
            code_data = text_file.read()

        with open(
            file="demo/data/template/css.txt", mode="r", encoding="utf-8"
        ) as text_file:
            css_data = text_file.read()

        queue.push(
            models.Template(
                id="baca9e2a-59fb-42ef-b5c6-6f6b05637111",
                title="Demo Template",
                description="This is the demo template",
                code=code_data,
                css=css_data,
                is_public=True,
            )
        )
        queue.flush()


def test_commands_create_demo():
    """The create_demo management command should create objects as expected."""
    call_command("create_demo")

    assert models.Template.objects.count() == 1
    assert models.User.objects.count() >= 10
    assert models.Document.objects.count() >= 10
    assert models.DocumentAccess.objects.count() > 10

    # assert dev users have doc accesses
    user = models.User.objects.get(email="impress@impress.world")
    assert models.DocumentAccess.objects.filter(user=user).exists()
    user = models.User.objects.get(email="user@webkit.test")
    assert models.DocumentAccess.objects.filter(user=user).exists()
    user = models.User.objects.get(email="user@firefox.test")
    assert models.DocumentAccess.objects.filter(user=user).exists()
    user = models.User.objects.get(email="user@chromium.test")
    assert models.DocumentAccess.objects.filter(user=user).exists()


def get_products() -> List[dict]:
    """
    Gets the products from the cache.

    Returns:
        products (List[dict]): The products
    """
    if not os.path.exists(get_afm_cache_path()):
        # Create the cache file
        with open(get_afm_cache_path(), 'w') as file:
            json.dump({
                "products": []
            }, file, indent=4)

    with open(get_afm_cache_path(), 'r') as file:
        parsed = json.load(file)

        # Get the products
        return parsed["products"]


    def create(cls, **kwargs) -> Union[bool, Any]:
        """Create a new record and save it the database."""
        instance = cls(**kwargs)
        return instance.save()


def construct_meta_info(frames_dir_path: Path) -> dict:
    """
    Construct meta information for a given frames directory.

    Args:
        frames_dir_path (Path): The path to the frames directory.

    Returns:
        dict: A dictionary containing the meta information for the frames directory, or None if the required files do not exist.
    """
    mask_path = str(frames_dir_path).replace("images", "face_mask") + ".png"
    face_emb_path = str(frames_dir_path).replace("images", "face_emb") + ".pt"

    if not os.path.exists(mask_path):
        print(f"Mask path not found: {mask_path}")
        return None

    if torch.load(face_emb_path) is None:
        print(f"Face emb is None: {face_emb_path}")
        return None

    return {
        "image_path": str(frames_dir_path),
        "mask_path": mask_path,
        "face_emb": face_emb_path,
    }


    def test_initializePoses(self):
        g2oFile = gtsam.findExampleDataFile("pose3example-grid")
        is3D = True
        inputGraph, expectedValues = gtsam.readG2o(g2oFile, is3D)
        priorModel = gtsam.noiseModel.Unit.Create(6)
        inputGraph.add(gtsam.PriorFactorPose3(0, Pose3(), priorModel))

        initial = gtsam.InitializePose3.initialize(inputGraph)
        # TODO(frank): very loose !!
        self.gtsamAssertEquals(initial, expectedValues, 0.1)


  def _cdf(self, x):
    return math_ops.igamma(self.alpha, self.beta * x)


    def repr_str(self, x, level):
        s = builtins.repr(x[:self.maxstring])
        if len(s) > self.maxstring:
            i = max(0, (self.maxstring-3)//2)
            j = max(0, self.maxstring-3-i)
            s = builtins.repr(x[:i] + x[len(x)-j:])
            s = s[:i] + '...' + s[len(s)-j:]
        return s


def union_numpy_dict(tensor_dict1: dict[str, np.ndarray], tensor_dict2: dict[str, np.ndarray]) -> dict[str, np.ndarray]:
    for key, val in tensor_dict2.items():
        if key in tensor_dict1:
            assert isinstance(tensor_dict2[key], np.ndarray)
            assert isinstance(tensor_dict1[key], np.ndarray)
            # to properly deal with nan and object type
            assert pd.DataFrame(tensor_dict2[key]).equals(pd.DataFrame(tensor_dict1[key])), (
                f"{key} in tensor_dict1 and tensor_dict2 are not the same object"
            )
        tensor_dict1[key] = val

    return tensor_dict1


    def __init__(self, open, high, low, close):
        pass


  def test_replay_a_replay(self):
    run_config = run_configs.get()
    with run_config.start(want_rgb=False) as controller:
      map_inst = maps.get("Flat64")
      map_data = map_inst.data(run_config)
      interface = sc_pb.InterfaceOptions(raw=True)

      # Play a quick game to generate a replay.
      create = sc_pb.RequestCreateGame(
          local_map=sc_pb.LocalMap(
              map_path=map_inst.path, map_data=map_data))
      create.player_setup.add(type=sc_pb.Participant)
      create.player_setup.add(type=sc_pb.Computer, race=sc_common.Terran,
                              difficulty=sc_pb.VeryEasy)
      join = sc_pb.RequestJoinGame(race=sc_common.Terran, options=interface)

      controller.create_game(create)
      controller.join_game(join)
      controller.step(100)
      obs = controller.observe()
      replay_data = controller.save_replay()

      # Run through the replay the first time, verifying that it finishes, but
      # wasn't recording a replay.
      start_replay = sc_pb.RequestStartReplay(
          replay_data=replay_data,
          map_data=map_data,
          options=interface,
          observed_player_id=1)

      controller.start_replay(start_replay)
      controller.step(1000)
      obs2 = controller.observe()
      self.assertEqual(obs.observation.game_loop, obs2.observation.game_loop)
      with self.assertRaises(protocol.ProtocolError):
        controller.save_replay()

      # Run through the replay a second time, verifying that it finishes, and
      # *was* recording a replay.
      start_replay.record_replay = True
      controller.start_replay(start_replay)
      controller.step(1000)
      obs2 = controller.observe()
      self.assertEqual(obs.observation.game_loop, obs2.observation.game_loop)
      replay_data2 = controller.save_replay()

      # Make sure the replay isn't too small. Variance is fine but empty is not.
      self.assertGreater(len(replay_data2), len(replay_data) * 0.8)

      # Run through the replay a third time, verifying that it finishes, but
      # still wasn't recording a replay.
      start_replay.record_replay = False
      controller.start_replay(start_replay)
      controller.step(1000)
      obs3 = controller.observe()
      self.assertEqual(obs.observation.game_loop, obs3.observation.game_loop)
      with self.assertRaises(protocol.ProtocolError):
        controller.save_replay()


def test_encoder_initialization():
    """Test encoder initialization"""
    encoder = MemvidEncoder()
    assert encoder.chunks == []
    assert encoder.index_manager is not None


    def __init__(self, session, action_dist, obs_vectorizer):
        """
        Construct a recurrent model.
        """
        super().__init__(session, action_dist, obs_vectorizer)

        self.seq_lens_ph = tf.placeholder(tf.int32, shape=(None,))
        self.is_init_state_ph = tf.placeholder(tf.bool, shape=(None,))
        self.mask_ph = tf.placeholder(tf.bool, (None, None))

        # Set this to a variable or a tuple of variables
        # for your model's initial state.
        self.init_state_vars = None

        # Set this to a placeholder for a batch of
        # observation sequences.
        self.obs_ph = None

        # Set this to a placeholder or a tuple of
        # placeholders for the first state in each
        # sequence.
        #
        # If a value in _is_init_state is True, then the
        # corresponding entry here is ignored.
        self.first_state_ph = None

        # Set these to the model outputs.
        self.actor_out = None
        self.critic_out = None
        self.states_out = None