magika

googleApache-2.0活跃维护

用深度学习快速准确地识别文件真实类型,不依赖扩展名,支持 MIME 检测,Rust 实现,可嵌入各类应用做内容安全与文件分类。

Fast and accurate AI powered file content types detection

cli/v1.1.0发布于 2026-04-24 14:51 UTC18,785star +9 / 7 天1,189fork同步于 2026-11-10 11:24 UTC

怎么上手

由 README 提炼,执行前请确认
pip install magika
magika ./tests_data/basic/python/code.py --json
硬件要求按 README 说明

README 说明可在单个 CPU 上运行,模型仅几 MB,未提及 GPU 或显存要求。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

谷歌开源的 AI 文件类型识别工具,几毫秒内判断文件真实内容类型

用深度学习快速准确地识别文件真实类型,不依赖扩展名,支持 MIME 检测,Rust 实现,可嵌入各类应用做内容安全与文件分类。

核心能力

  • 用几 MB 的定制深度学习模型识别 200+ 内容类型
  • 单 CPU 上每文件推理约 5ms,模型加载后耗时近乎恒定
  • 按内容类型阈值决定是否回退到通用标签,可调置信度模式
  • 提供 Rust CLI、Python API 及 Rust/JS/Go 绑定
  • 支持递归扫描目录、标准输入和 JSON/JSONL 输出

容易踩的坑

首次使用需加载模型,存在一次性开销;CLI 通过 pipx 安装,Python 包用 pip 安装,两者不要混用;标准输入 - 只能使用一次。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本cli/v1.1.0
  1. cli-latestLatest CLI release2026-04-24 · 2026-04-24 14:55 UTC
    查看更新说明

    This moving release simply replicates the assets and commit hash of the latest CLI release.

    The latest CLI release is cli/v1.1.0.

  2. cli/v1.1.0最新正式版2026-04-24 · 2026-04-24 14:51 UTC

    Install magika-cli 1.1.0

    Install prebuilt binaries via shell script

    curl --proto '=https' --tlsv1.2 -LsSf https://github.com/google/magika/releases/download/cli/v1.1.0/magika-cli-installer.sh | sh
    

    Install prebuilt binaries via powershell script

    powershell -ExecutionPolicy Bypass -c "irm https://github.com/google/magika/releases/download/cli/v1.1.0/magika-cli-installer.ps1 | iex"
    

    Download magika-cli 1.1.0

    FilePlatformChecksum
    magika-cli-aarch64-apple-darwin.tar.xzApple Silicon macOSchecksum
    magika-cli-x86_64-pc-windows-msvc.zipx64 Windowschecksum
    magika-cli-aarch64-unknown-linux-gnu.tar.xzARM64 Linuxchecksum
    magika-cli-x86_64-unknown-linux-gnu.tar.xzx64 Linuxchecksum

    Verifying GitHub Artifact Attestations

    The artifacts in this release have attestations generated with GitHub Artifact Attestations. These can be verified by using the GitHub CLI:

    gh attestation verify <file-path of downloaded artifact> --repo google/magika
    

    You can also download the attestation from GitHub and verify against that directly:

    gh attestation verify <file-path of downloaded artifact> --bundle <file-path of downloaded attestation>
    
  3. python-v1.0.22026-02-27 · 2026-02-27 09:30 UTC
    查看更新说明

    Changelog:

    • Mark python 3.14 as supported.
    • Remove direct dependency on numpy.
    • Remove dependency on python-dotenv (note: .env files are no longer loaded automatically).
    • Remove onnxruntime<=1.20.1 Windows pin.

    See all CHANGELOGs here: https://github.com/google/magika/blob/python-v1.0.2/python/CHANGELOG.md#102---2026-02-25

  4. cli/v1.0.22026-02-25 · 2026-02-25 10:47 UTC
    查看更新说明

    Install magika 1.0.2

    Install prebuilt binaries via shell script

    curl --proto '=https' --tlsv1.2 -LsSf https://github.com/google/magika/releases/download/cli/v1.0.2/magika-installer.sh | sh
    

    Install prebuilt binaries via powershell script

    powershell -ExecutionPolicy Bypass -c "irm https://github.com/google/magika/releases/download/cli/v1.0.2/magika-installer.ps1 | iex"
    

    Download magika 1.0.2

    FilePlatformChecksum
    magika-aarch64-apple-darwin.tar.xzApple Silicon macOSchecksum
    magika-x86_64-pc-windows-msvc.zipx64 Windowschecksum
    magika-aarch64-unknown-linux-gnu.tar.xzARM64 Linuxchecksum
    magika-x86_64-unknown-linux-gnu.tar.xzx64 Linuxchecksum

    Verifying GitHub Artifact Attestations

    The artifacts in this release have attestations generated with GitHub Artifact Attestations. These can be verified by using the GitHub CLI:

    gh attestation verify <file-path of downloaded artifact> --repo google/magika
    

    You can also download the attestation from GitHub and verify against that directly:

    gh attestation verify <file-path of downloaded artifact> --bundle <file-path of downloaded attestation>
    
  5. python-v1.0.12025-10-31 · 2025-10-31 11:47 UTC
    查看更新说明

    This release marks the first stable release, and the end of the experimental phase.

    Updated documentation is at the new website: https://securityresearch.google/magika/

    See CHANGELOGs here: https://github.com/google/magika/blob/python-v1.0.1/python/CHANGELOG.md#101---2025-10-31.

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

    Magika

    image NPM Version image image Go Version

    OpenSSF Best Practices CodeQL Python Coverage Actions status PyPI Monthly Downloads PyPI Downloads

    Magika is a novel AI-powered file type detection tool that relies on the recent advance of deep learning to provide accurate detection. Under the hood, Magika employs a custom, highly optimized model that only weighs about a few MBs, and enables precise file identification within milliseconds, even when running on a single CPU. Magika has been trained and evaluated on a dataset of ~100M samples across 200+ content types (covering both binary and textual file formats), and it achieves an average ~99% accuracy on our test set.

    Here is an example of what Magika command line output looks like:

    Magika is used at scale to help improve Google users' safety by routing Gmail, Drive, and Safe Browsing files to the proper security and content policy scanners, processing hundreds billions samples on a weekly basis. Magika has also been integrated with VirusTotal (example) and abuse.ch (example).

    For more context you can read our initial announcement post on Google's OSS blog, you can consult Magika's website, and you can read more in our research paper, published at the IEEE/ACM International Conference on Software Engineering (ICSE) 2025.

    You can try Magika without installing anything by using our web demo, which runs locally in your browser!

    Highlights

    • Available as a command line tool written in Rust, a Python API, and additional bindings for Rust, JavaScript/TypeScript (with an experimental npm package, which powers our web demo), and GoLang (WIP).
    • Trained and evaluated on a dataset of ~100M files across 200+ content types.
    • On our test set, Magika achieves ~99% average precision and recall, outperforming existing approaches -- especially on textual content types.
    • After the model is loaded (which is a one-off overhead), the inference time is about 5ms per file, even when run on a single CPU.
    • You can invoke Magika with even thousands of files at the same time. You can also use -r for recursively scanning a directory.
    • Near-constant inference time, independently from the file size; Magika only uses a limited subset of the file's content.
    • Magika uses a per-content-type threshold system that determines whether to "trust" the prediction for the model, or whether to return a generic label, such as "Generic text document" or "Unknown binary data".
    • The tolerance to errors can be controlled via different prediction modes, such as high-confidence, medium-confidence, and best-guess.
    • The client and the bindings are already open source, and more is coming soon!

    Table of Contents

    1. Getting Started
      1. Installation
      2. Quick Start
    2. Documentation
    3. Security Vulnerabilities
    4. License
    5. Disclaimer

    Getting Started

    Installation

    Command Line Tool

    Magika ships a CLI written in Rust, and can be installed in several ways.

    Via magika python package:

    pipx install magika
    

    Via brew (macOS / Linux)

    brew install magika
    

    Via installer script:

    curl -LsSf https://securityresearch.google/magika/install.sh | sh
    

    or:

    powershell -ExecutionPolicy Bypass -c "irm https://securityresearch.google/magika/install.ps1 | iex"
    

    Via magika-cli Rust package:

    cargo install --locked magika-cli
    

    Python package

    pip install magika
    

    JavaScript package

    npm install magika
    

    Quick Start

    Here you can find a number of quick examples just to get you started.

    To learn about Magika's inner workings, see the Core Concepts section of Magika's website.

    Command Line Tool Examples

    % cd tests_data/basic && magika -r * | head
    asm/code.asm: Assembly (code)
    batch/simple.bat: DOS batch file (code)
    c/code.c: C source (code)
    css/code.css: CSS source (code)
    csv/magika_test.csv: CSV document (code)
    dockerfile/Dockerfile: Dockerfile (code)
    docx/doc.docx: Microsoft Word 2007+ document (document)
    docx/magika_test.docx: Microsoft Word 2007+ document (document)
    eml/sample.eml: RFC 822 mail (text)
    empty/empty_file: Empty file (inode)
    
    % magika ./tests_data/basic/python/code.py --json
    [
      {
        "path": "./tests_data/basic/python/code.py",
        "result": {
          "status": "ok",
          "value": {
            "dl": {
              "description": "Python source",
              "extensions": [
                "py",
                "pyi"
              ],
              "group": "code",
              "is_text": true,
              "label": "python",
              "mime_type": "text/x-python"
            },
            "output": {
              "description": "Python source",
              "extensions": [
                "py",
                "pyi"
              ],
              "group": "code",
              "is_text": true,
              "label": "python",
              "mime_type": "text/x-python"
            },
            "score": 0.996999979019165
          }
        }
      }
    ]
    
    % cat tests_data/basic/ini/doc.ini | magika -
    -: INI configuration file (text)
    
    % magika --help
    Determines file content types using AI
    
    Usage: magika [OPTIONS] [PATH]...
    
    Arguments:
      [PATH]...
              List of paths to the files to analyze.
    
              Use a dash (-) to read from standard input (can only be used once).
    
    Options:
      -r, --recursive
              Identifies files within directories instead of identifying the directory itself
    
          --no-dereference
              Identifies symbolic links as is instead of identifying their content by following them
    
          --colors
              Prints with colors regardless of terminal support
    
          --no-colors
              Prints without colors regardless of terminal support
    
      -s, --output-score
              Prints the prediction score in addition to the content type
    
      -i, --mime-type
              Prints the MIME type instead of the content type description
    
      -l, --label
              Prints a simple label instead of the content type description
    
          --json
              Prints in JSON format
    
          --jsonl
              Prints in JSONL format
    
          --format <CUSTOM>
              Prints using a custom format (use --help for details).
    
              The following placeholders are supported:
    
                %p  The file path
                %l  The unique label identifying the content type
                %d  The description of the content type
                %g  The group of the content type
                %m  The MIME type of the content type
                %e  Possible file extensions for the content type
                %s  The score of the content type for the file
                %S  The score of the content type for the file in percent
                %b  The model output if overruled (empty otherwise)
                %%  A literal %
    
      -h, --help
              Print help (see a summary with '-h')
    
      -V, --version
              Print version
    

    For more examples and documentation about the CLI, see https://crates.io/crates/magika-cli.

    Python Examples

    >>> from magika import Magika
    >>> m = Magika()
    >>> res = m.identify_bytes(b'function log(msg) {console.log(msg);}')
    >>> print(res.output.label)
    javascript
    
    >>> from magika import Magika
    >>> m = Magika()
    >>> res = m.identify_path('./tests_data/basic/ini/doc.ini')
    >>> print(res.output.label)
    ini
    
    >>> from magika import Magika
    >>> m = Magika()
    >>> with open('./tests_data/basic/ini/doc.ini', 'rb') as f:
    >>>     res = m.identify_stream(f)
    >>> print(res.output.label)
    ini
    

    For more examples and documentation about the Python module, see the Python Magika module section.

    Documentation

    Please consult Magika's website for detailed documentation about:

    • Core Concepts
      • How Magika works
      • Models & content types
      • Prediction modes
      • Understanding the output
    • CLI & Bindings (Python module, JavaScript module, ...)
    • Contributing
    • FAQ
    • ...

    Contributing

    We review pull requests only for issues labeled help wanted and for a short list of always-welcome changes; for anything else, open a concise issue first. See CONTRIBUTING.md and the contribution policy.

    Security Vulnerabilities

    Please contact us directly at [email protected].

    License

    Apache 2.0; see LICENSE for details.

    Disclaimer

    This project is not an official Google project. It is not supported by Google and Google specifically disclaims all warranties as to its quality, merchantability, or fitness for a particular purpose.