# xming521/weclone architecture

> Architecture diagram of the GitHub repository xming521/weclone, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/xming521/weclone
- Repository: https://github.com/xming521/weclone

## Overview

##### Project Type and Purpose
- This is a Python-based one-stop solution (“WeClone”) for creating a digital avatar from chat logs.
- It combines a CLI frontend (`weclone-cli`), a data-processing pipeline, model fine-tuning (SFT/LoRA/pt), inference services (vllm), a simple REST API, a browser demo, and an audio cloning submodule.

##### Main Components
A. CLI Entrypoint
- `weclone/cli.py` – defines commands: make-dataset, train-sft, train-pt, export-model, webchat-demo, server, test-model.
B. Data Pipeline
- Raw chat CSVs under `/dataset/csv` → cleaning & filtering (`weclone/data/clean/…`, `strategies.py`) → QA-pair generation (`weclone/data/qa_generator.py`).
C. Training Pipeline
- SFT training (`weclone/train/train_sft.py`)
- PT training (`weclone/train/train_pt.py`)
- Model export (`weclone/train/export_model.py`)
- Relies on external heavy-compute libs: PyTorch, vllm, deepspeed, flash-attn.
D. Inference Service
- vLLM-based infer module (`weclone/core/inference/vllm_infer.py`)
- CLI demo (`weclone/eval/cli_demo.py`) & web demo (`weclone/eval/web_demo.py`) call this.
E. REST API
- FastAPI-style or Flask in `weclone/server/api_service.py`
- Serves endpoints for inference, health checks.
F. Browser-based Demo
- Static/HTML + JS in `weclone/eval/web_demo.py`, talks to local API.
G. Audio Cloning Submodule
- Directory `weclone-audio/`: TTS inference (Llasa, Spark-TTS), a minimal server under `server未完工/`.
H. Configuration & Artifacts
- `settings.jsonc` – tuning, model paths, LoRA parameters.
- `ds_config.json` – data-split config.
- Local FS stores datasets, intermediate JSON, model checkpoints, logs.
I. External Dependencies
- LLM provider (local Qwen2.5/BLOOM, etc.)
- AstrBot (as a downstream consumer via OpenAI-compatible API).

##### Relationships and Data Flows
- CLI → invokes Data Pipeline → writes cleaned dataset.
- CLI → invokes Training Pipeline → reads cleaned dataset, calls external LLM frameworks → writes model artifacts.
- CLI/server → REST API loads model artifacts → on request calls Inference Service → returns JSON responses.
- Browser Demo ↔ REST API (HTTP).
- AstrBot or any OpenAI-compatible client ↔ REST API.
- Audio submodule server ↔ Inference Service (text output) → generates audio → returns WAV/stream.

##### Architectural Patterns & Principles
- Modular layer separation:
1. Presentation (CLI, Web Demo)
2. Application (Data, Train, Inference, API)
3. Infrastructure (File System, External LLM libs, GPU resources)
- CLI orchestrator pattern: single binary dispatches to submodules.
- Micro-services style for REST API and audio server (can be containerized).
- Config-driven behaviour via JSONC templates.

##### Essential Elements
- Details: note files or modules under each component (e.g., `weclone/data/clean/get_score.py` resides in Data Pipeline).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        CLI(("CLI Entrypoint")):::presentation
        CLIDemo(("CLI Demo\n(weclone/eval/cli_demo.py)")):::presentation
        WebDemo(("Web Demo\n(weclone/eval/web_demo.py)")):::presentation
    end

    %% Application Layer
    subgraph "Data Pipeline" 
        RawData[(Raw Chat CSVs\n/dataset/res_csv/)]:::data
        Clean1("get_score.py"):::compute
        Clean2("strategies.py"):::compute
        QA("qa_generator.py"):::compute
    end

    subgraph "Training Pipeline"
        SFT("train_sft.py"):::compute
        PT("train_pt.py"):::compute
        Export("export_model.py"):::compute
    end

    subgraph "Inference Service"
        Infer("vLLM Inference\n(vllm_infer.py)"):::compute
    end

    API(("REST API\n(api_service.py)")):::presentation

    %% Audio Submodule
    subgraph "Audio Submodule"
        AudioSrv("Audio Server\n(server未完工/server.py)"):::audio
        Llasa("Llasa TTS\ninfer.py/text_to_speech.py"):::audio
        Spark("Spark-TTS\nSparkTTS.py"):::audio
        Sample("Sample Audio\nget_sample_audio.py"):::audio
    end

    %% Infrastructure Layer
    ModelStore[(Model Store)]:::data
    ExtLibs(("External LLM Frameworks")):::external
    AstrBot(("AstrBot Client")):::external

    Config1(("settings.template.jsonc")):::external
    Config2(("ds_config.json")):::external

    %% Connections - Presentation to Application
    CLI -->|"invoke"| RawData
    CLI -->|"invoke"| SFT
    CLI -->|"invoke"| PT
    CLI -->|"invoke"| Export
    CLI -->|"invoke"| API

    CLIDemo -->|"calls"| Infer
    WebDemo -->|"HTTP"| API
    AstrBot -->|"OpenAI-compatible API"| API

    %% Data Pipeline Flow
    RawData -->|"raw files"| Clean1
    RawData --> Clean2
    Clean1 -->|"cleaned data"| QA
    Clean2 --> QA
    QA -->|"QA pairs"| ModelStore

    %% Training Pipeline Flow
    SFT -->|"reads dataset"| ModelStore
    PT --> ModelStore
    SFT -->|"uses"| ExtLibs
    PT --> ExtLibs
    Export -->|"reads checkpoints"| ModelStore
    Export --> ModelStore

    %% Inference Flow
    API -->|"RPC"| Infer
    Infer -->|"uses"| ExtLibs
    Infer -->|"generates text"| API

    %% Audio Flow
    Infer -->|"text output"| AudioSrv
    AudioSrv --> Llasa
    AudioSrv --> Spark
    AudioSrv --> Sample
    Llasa --> AudioSrv
    Spark --> AudioSrv
    Sample --> AudioSrv
    AudioSrv -->|"WAV/stream"| API

    %% Config Usage
    Config1 --> CLI
    Config1 --> SFT
    Config1 --> PT
    Config1 --> API
    Config2 --> QA

    %% Click Events
    click CLI "https://github.com/xming521/weclone/blob/master/weclone/cli.py"
    click RawData "https://github.com/xming521/weclone/tree/master/dataset/res_csv/"
    click Clean1 "https://github.com/xming521/weclone/blob/master/weclone/data/clean/get_score.py"
    click Clean2 "https://github.com/xming521/weclone/blob/master/weclone/data/clean/strategies.py"
    click QA "https://github.com/xming521/weclone/blob/master/weclone/data/qa_generator.py"
    click SFT "https://github.com/xming521/weclone/blob/master/weclone/train/train_sft.py"
    click PT "https://github.com/xming521/weclone/blob/master/weclone/train/train_pt.py"
    click Export "https://github.com/xming521/weclone/blob/master/weclone/train/export_model.py"
    click Infer "https://github.com/xming521/weclone/blob/master/weclone/core/inference/vllm_infer.py"
    click CLIDemo "https://github.com/xming521/weclone/blob/master/weclone/eval/cli_demo.py"
    click WebDemo "https://github.com/xming521/weclone/blob/master/weclone/eval/web_demo.py"
    click API "https://github.com/xming521/weclone/blob/master/weclone/server/api_service.py"
    click AudioSrv "https://github.com/xming521/weclone/blob/master/weclone-audio/src/server未完工/server.py"
    click Llasa "https://github.com/xming521/weclone/blob/master/weclone-audio/src/Llasa/infer.py"
    click Llasa "https://github.com/xming521/weclone/blob/master/weclone-audio/src/Llasa/text_to_speech.py"
    click Spark "https://github.com/xming521/weclone/blob/master/weclone-audio/src/Spark-TTS/SparkTTS.py"
    click Sample "https://github.com/xming521/weclone/blob/master/weclone-audio/src/get_sample_audio.py"

    %% Styles
    classDef presentation fill:#68bbe0,stroke:#333,stroke-width:1px
    classDef compute fill:#eb934b,stroke:#333,stroke-width:1px
    classDef data fill:#A3BE8C,stroke:#333,stroke-width:1px
    classDef audio fill:#b48ead,stroke:#333,stroke-width:1px
    classDef external fill:#c0c0c0,stroke:#333,stroke-width:1px
```
