| 364 | |
| 365 | @staticmethod |
| 366 | def _preprocess(folder_path: str, |
| 367 | path_in_repo_url: str, |
| 368 | add_powered_by: bool = True) -> 'Tuple[str, str]': |
| 369 | |
| 370 | report_filename = 'report.md' |
| 371 | file_path = os.path.join(folder_path, report_filename) |
| 372 | file_path_hash: str = text_hash(text=file_path, keep_n_chars=8) |
| 373 | new_report_filename: str = f'report_{file_path_hash}.md' |
| 374 | current_cache_path: str = os.path.join(folder_path, '.cache') |
| 375 | os.makedirs(current_cache_path, exist_ok=True) |
| 376 | new_file_path = os.path.join(current_cache_path, new_report_filename) |
| 377 | |
| 378 | if not os.path.exists(file_path): |
| 379 | logger.warning( |
| 380 | f'The report file: {file_path} does not exist. Skipping preprocessing.' |
| 381 | ) |
| 382 | return '', '' |
| 383 | |
| 384 | try: |
| 385 | shutil.copy(file_path, new_file_path) |
| 386 | except Exception as e: |
| 387 | logger.error(f'Error copying the report file: {e}') |
| 388 | return '', '' |
| 389 | |
| 390 | try: |
| 391 | with open(file_path, 'r', encoding='utf-8') as f: |
| 392 | content = f.read() |
| 393 | if add_powered_by and not content.lstrip().startswith( |
| 394 | '<span style='): |
| 395 | content = """<span style="color: darkgreen; font-weight: bold; font-family: monospace; |
| 396 | ">Powered by [MS-Agent](https://github.com/modelscope/ms-agent) | |
| 397 | [DocResearch](https://github.com/modelscope/ms-agent/blob/main/projects/doc_research/README.md) |
| 398 | </span>""" + '\n\n' + content |
| 399 | |
| 400 | pattern = r'!\[(.*?)\]\((resources/.*?)\)' |
| 401 | replacement = rf'' |
| 402 | new_content, count = re.subn(pattern, replacement, content) |
| 403 | |
| 404 | if count > 0: |
| 405 | with open(file_path, 'w', encoding='utf-8') as f: |
| 406 | f.write(new_content) |
| 407 | logger.info( |
| 408 | f"Preprocessed {count} 'resources/' links in {file_path}.") |
| 409 | else: |
| 410 | logger.info( |
| 411 | f'No "resources/" links found in {file_path}. No changes made.' |
| 412 | ) |
| 413 | |
| 414 | except IOError as e: |
| 415 | logger.error(f'Error reading or writing the report file: {e}') |
| 416 | return '', '' |
| 417 | except Exception as e: |
| 418 | logger.error( |
| 419 | f'Unexpected error during preprocessing of PushToModelScope: {e}' |
| 420 | ) |
| 421 | return '', '' |
| 422 | |
| 423 | return file_path, new_file_path |