builder / docs /ARCHITECTURE.md
mgbam's picture
Update docs/ARCHITECTURE.md
7b12d1a verified

A newer version of the Gradio SDK is available: 5.42.0

Upgrade

Shasha AI β€” Architecture Overview

A high‑level view of the components and data flow in the Shasha AI code‑generation platform.

1. Core Layers

1.1 Frontend (Gradio UI)

  • app.py: Defines the Gradio Blocks UI β€” input panels (prompt, file, website, image), model selector, language selector, buttons, and output panes (Code, Preview, History).
  • static/: Holds index.html, index.js, style.css for any transformers.js demos and custom styling.
  • assets/: Images, logos, and other static assets.

1.2 Backend Services

1.2.1 Inference Routing

  • hf_client.py

    • get_inference_client(model_id, provider)
    • Wraps HF/OpenAI/Gemini/Groq providers into a unified interface.
    • Automatically selects/falls back based on model prefix and available credentials.
  • inference.py

    • chat_completion(...) and stream_chat_completion(...)
    • Encapsulates request/response logic, streaming vs. blocking, and error handling.

1.2.2 Model Registry

  • models.py
    • ModelInfo dataclass & AVAILABLE_MODELS registry
    • Central source of truth for supported models, identifiers, descriptions, and default providers.
    • Helper find_model() for lookup by name or ID.

1.2.3 Prompt & History Management

  • constants.py
    • System prompts for HTML/transformers.js modes, search/replace tokens, Gradio language support.
  • utils.py
    • history_to_messages(), remove_code_block(), multimodal image encoding, parsing transformers.js outputs, search/replace utilities.

1.2.4 Deployment & Project Import

  • deploy.py
    • send_to_sandbox() β†’ live HTML preview in iframe
    • load_project_from_url() β†’ import existing HF Spaces (app.py/index.html)
    • deploy_to_spaces*() β†’ create/update HF Space via Hub API

2. Extensions & Plugins

  • plugins.py (future)

    • Plugin discovery/loading for community‑created extensions (e.g., DB runners, snippet libraries).
  • auth.py (future)

    • OAuth flows for GitHub, Google Drive, Slack β€” enable private file loading.

3. Project Structure

. β”œβ”€β”€ app.py # Gradio application β”œβ”€β”€ constants.py # System prompts & app‑wide constants β”œβ”€β”€ hf_client.py # Inference client factory β”œβ”€β”€ models.py # Model registry & metadata β”œβ”€β”€ inference.py # Chat completion wrappers β”œβ”€β”€ utils.py # Helpers: history, code parsing, multimodal β”œβ”€β”€ deploy.py # Sandbox preview & HF Spaces import/deploy β”œβ”€β”€ plugins.py # (planned) Plugin architecture β”œβ”€β”€ auth.py # (planned) OAuth integrations β”œβ”€β”€ notebooks/ # Demo Jupyter notebooks β”œβ”€β”€ tests/ # pytest suites & UI smoke tests β”œβ”€β”€ ci.yaml # CI pipeline for lint/test/deploy β”œβ”€β”€ docs/ β”‚ β”œβ”€β”€ QUICKSTART.md β”‚ β”œβ”€β”€ ARCHITECTURE.md β”‚ └── API_REFERENCE.md └── static/ # transformers.js demos & CSS/JS assets

yaml Copy Edit

4. Data Flow

  1. User Interaction: User enters prompt / uploads file / selects model & language.
  2. Preprocessing:
    • File β†’ extract_text_from_file()
    • Website β†’ extract_website_content()
    • Image β†’ process_image_for_model()
  3. Message Assembly:
    • System prompt + history β†’ OpenAI‑style message list
    • Enhanced via optional web search (Tavily)
  4. Inference Call:
    • get_inference_client() β†’ select provider & billing
    • chat_completion() or streaming
  5. Postprocessing:
    • Parse code blocks / transformers.js multi‑file output
    • Apply search/replace to existing code if editing
  6. UI Update:
    • Code pane
    • Live preview iframe (send_to_sandbox)
    • Chat history pane