mgbam commited on
Commit
8ec22c6
·
verified ·
1 Parent(s): 375f19f

Update docs/ARCHITECTURE.md

Browse files
Files changed (1) hide show
  1. docs/ARCHITECTURE.md +194 -0
docs/ARCHITECTURE.md CHANGED
@@ -0,0 +1,194 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!-- ARCHITECTURE.md -->
2
+
3
+ # Shasha AI — Architecture Overview
4
+
5
+ A high‑level view of the components and data flow in the Shasha AI code‑generation platform.
6
+
7
+ ## 1. Core Layers
8
+
9
+ ### 1.1 Frontend (Gradio UI)
10
+ - **app.py**: Defines the Gradio Blocks UI — input panels (prompt, file, website, image), model selector, language selector, buttons, and output panes (Code, Preview, History).
11
+ - **static/**: Holds `index.html`, `index.js`, `style.css` for any transformers.js demos and custom styling.
12
+ - **assets/**: Images, logos, and other static assets.
13
+
14
+ ### 1.2 Backend Services
15
+
16
+ #### 1.2.1 Inference Routing
17
+ - **hf_client.py**
18
+ - `get_inference_client(model_id, provider)`
19
+ - Wraps HF/OpenAI/Gemini/Groq providers into a unified interface.
20
+ - Automatically selects/falls back based on model prefix and available credentials.
21
+
22
+ - **inference.py**
23
+ - `chat_completion(...)` and `stream_chat_completion(...)`
24
+ - Encapsulates request/response logic, streaming vs. blocking, and error handling.
25
+
26
+ #### 1.2.2 Model Registry
27
+ - **models.py**
28
+ - `ModelInfo` dataclass & `AVAILABLE_MODELS` registry
29
+ - Central source of truth for supported models, identifiers, descriptions, and default providers.
30
+ - Helper `find_model()` for lookup by name or ID.
31
+
32
+ #### 1.2.3 Prompt & History Management
33
+ - **constants.py**
34
+ - System prompts for HTML/transformers.js modes, search/replace tokens, Gradio language support.
35
+ - **utils.py**
36
+ - `history_to_messages()`, `remove_code_block()`, multimodal image encoding, parsing transformers.js outputs, search/replace utilities.
37
+
38
+ #### 1.2.4 Deployment & Project Import
39
+ - **deploy.py**
40
+ - `send_to_sandbox()` → live HTML preview in iframe
41
+ - `load_project_from_url()` → import existing HF Spaces (app.py/index.html)
42
+ - `deploy_to_spaces*()` → create/update HF Space via Hub API
43
+
44
+ ## 2. Extensions & Plugins
45
+ - **plugins.py** (future)
46
+ - Plugin discovery/loading for community‑created extensions (e.g., DB runners, snippet libraries).
47
+
48
+ - **auth.py** (future)
49
+ - OAuth flows for GitHub, Google Drive, Slack — enable private file loading.
50
+
51
+ ## 3. Project Structure
52
+
53
+ .
54
+ ├── app.py # Gradio application
55
+ ├── constants.py # System prompts & app‑wide constants
56
+ ├── hf_client.py # Inference client factory
57
+ ├── models.py # Model registry & metadata
58
+ ├── inference.py # Chat completion wrappers
59
+ ├── utils.py # Helpers: history, code parsing, multimodal
60
+ ├── deploy.py # Sandbox preview & HF Spaces import/deploy
61
+ ├── plugins.py # (planned) Plugin architecture
62
+ ├── auth.py # (planned) OAuth integrations
63
+ ├── notebooks/ # Demo Jupyter notebooks
64
+ ├── tests/ # pytest suites & UI smoke tests
65
+ ├── ci.yaml # CI pipeline for lint/test/deploy
66
+ ├── docs/
67
+ │ ├── QUICKSTART.md
68
+ │ ├── ARCHITECTURE.md
69
+ │ └── API_REFERENCE.md
70
+ └── static/ # transformers.js demos & CSS/JS assets
71
+
72
+ yaml
73
+ Copy
74
+ Edit
75
+
76
+ ## 4. Data Flow
77
+
78
+ 1. **User Interaction**: User enters prompt / uploads file / selects model & language.
79
+ 2. **Preprocessing**:
80
+ - File → `extract_text_from_file()`
81
+ - Website → `extract_website_content()`
82
+ - Image → `process_image_for_model()`
83
+ 3. **Message Assembly**:
84
+ - System prompt + history → OpenAI‑style message list
85
+ - Enhanced via optional web search (Tavily)
86
+ 4. **Inference Call**:
87
+ - `get_inference_client()` → select provider & billing
88
+ - `chat_completion()` or streaming
89
+ 5. **Postprocessing**:
90
+ - Parse code blocks / transformers.js multi‑file output
91
+ - Apply search/replace to existing code if editing
92
+ 6. **UI Update**:
93
+ - Code pane
94
+ - Live preview iframe (`send_to_sandbox`)
95
+ - Chat history pane
96
+
97
+ ---
98
+
99
+ ```markdown
100
+ <!-- API_REFERENCE.md -->
101
+
102
+ # Shasha AI — API Reference
103
+
104
+ This document describes the public interfaces provided by each module.
105
+
106
+ ---
107
+
108
+ ## `hf_client.py`
109
+
110
+ ### `get_inference_client(model_id: str, provider: str = "auto") → InferenceClient`
111
+ Creates and configures a Hugging Face Hub client for chat completions.
112
+
113
+ - **model_id**: HF model ID or external provider prefix (e.g. `"openai/gpt-4"`, `"gemini/pro"`, `"moonshotai/Kimi-K2-Instruct"`).
114
+ - **provider**: Override provider; one of `auto`, `groq`, `openai`, `gemini`, `fireworks`.
115
+ - **Returns**: `InferenceClient` instance with proper API key & billing target.
116
+
117
+ ---
118
+
119
+ ## `models.py`
120
+
121
+ ### `ModelInfo`
122
+ Dataclass representing model metadata.
123
+
124
+ - **name**: Human‑readable model name.
125
+ - **id**: Model identifier for API calls.
126
+ - **description**: Short description.
127
+ - **default_provider**: Preferred inference provider.
128
+
129
+ ### `AVAILABLE_MODELS: List[ModelInfo]`
130
+ Registry of all supported models.
131
+
132
+ ### `find_model(identifier: str) → Optional[ModelInfo]`
133
+ Lookup model by name (case‑insensitive) or ID.
134
+
135
+ ---
136
+
137
+ ## `inference.py`
138
+
139
+ ### `chat_completion(model_id: str, messages: List[Dict[str, str]], provider: str = None, max_tokens: int = 4096) → str`
140
+ Synchronously sends a chat completion request.
141
+
142
+ - **messages**: List of `{"role": "...", "content": "..."}`
143
+ - **provider**: Optional override; defaults to model’s `default_provider`.
144
+ - **Returns**: Response content string.
145
+
146
+ ### `stream_chat_completion(model_id: str, messages: List[Dict[str, str]], provider: str = None, max_tokens: int = 4096) → Generator[str]`
147
+ Streams a chat completion, yielding incremental content chunks.
148
+
149
+ ---
150
+
151
+ ## `utils.py`
152
+
153
+ ### `history_to_messages(history: History, system: str) → List[Dict]`
154
+ Converts internal history list to OpenAI‑style messages.
155
+
156
+ ### `remove_code_block(text: str) → str`
157
+ Strips markdown code fences from AI output and returns raw code.
158
+
159
+ ### `parse_transformers_js_output(text: str) → Dict[str, str]`
160
+ Extracts `index.html`, `index.js`, `style.css` from a multi‑file markdown output.
161
+
162
+ ### `format_transformers_js_output(files: Dict[str, str]) → str`
163
+ Formats a dict of file contents into a single combined string with section headers.
164
+
165
+ *(Other utilities: multimodal image processing, search/replace, history rendering)*
166
+
167
+ ---
168
+
169
+ ## `deploy.py`
170
+
171
+ ### `send_to_sandbox(code: str) → str`
172
+ Wraps HTML code in a base64 data‑URI iframe for live preview.
173
+
174
+ ### `load_project_from_url(url: str) → Tuple[str, str]`
175
+ Fetches `app.py` or `index.html` from a public HF Space URL.
176
+
177
+ *(Also: HF Spaces deploy helpers: `deploy_to_spaces()`, `deploy_to_spaces_static()`, `deploy_to_user_space()`)*
178
+
179
+ ---
180
+
181
+ ## `app.py`
182
+
183
+ ### `generation_code(query, image, file, website_url, _setting, _history, _current_model, enable_search, language, provider) → Tuple[str, History, str, List[Dict]]`
184
+ Main generation handler bound to the “Generate” button.
185
+
186
+ - **Returns**:
187
+ 1. `code_str`: Generated (or edited) source code
188
+ 2. `new_history`: Updated prompt/response history
189
+ 3. `sandbox_html`: Live preview HTML iframe string
190
+ 4. `chat_msgs`: Chatbot‑style history for the UI
191
+
192
+ ---
193
+
194
+ _For more examples, see the Jupyter notebooks in_ `notebooks/` and the quick‑start guide in `QUICKSTA