| import os | |
| # Get the directory of the current script | |
| script_dir = os.path.dirname(os.path.abspath(__file__)) | |
| # Define paths | |
| model_dir = os.path.join(script_dir, '..', 'models') | |
| markdown_file = os.path.join(script_dir, '..', 'README.md') | |
| # Define HTML comments as markers | |
| begin_marker = "<!-- BEGIN MODEL LINKS -->" | |
| end_marker = "<!-- END MODEL LINKS -->" | |
| # Generate links | |
| links = [] | |
| for file_name in os.listdir(model_dir): | |
| if file_name.endswith(".json"): # Check if the file is a JSON file | |
| base_name = os.path.splitext(file_name)[0] # Extract the filename without extension | |
| link = f" - [{base_name}](https://openmodeldb.info/models/{base_name})" # Create a markdown link | |
| links.append(link) | |
| # Read the markdown file content | |
| with open(markdown_file, 'r') as file: | |
| content = file.read() | |
| # Find the start and end of the marker section | |
| begin_idx = content.find(begin_marker) + len(begin_marker) | |
| end_idx = content.find(end_marker) | |
| if begin_idx != -1 and end_idx != -1: # Ensure markers are found | |
| # Replace the old content between the markers with the new links | |
| new_content = content[:begin_idx] + "\n" + "\n".join(links) + "\n" + content[end_idx:] | |
| # Write the updated content back to the markdown file | |
| with open(markdown_file, 'w') as file: | |
| file.write(new_content) | |
| print("Markdown file has been updated.") | |
| else: | |
| print("Markers not found.") |