Spaces:
Sleeping
Sleeping
Emmanuel Acheampong commited on
Commit Β·
65fe6bd
1
Parent(s): a8e4831
Add developer-friendly README with architecture docs, extension guides, and API reference
Browse files
README.md
CHANGED
|
@@ -10,4 +10,201 @@ pinned: false
|
|
| 10 |
short_description: Short demo for docs and code interacting with Managed AI
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
short_description: Short demo for docs and code interacting with Managed AI
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# π Crusoe Space Legal Docs
|
| 14 |
+
|
| 15 |
+
**Document analysis, codebase intelligence, and KV cache demo β powered by Crusoe Cloud Foundry**
|
| 16 |
+
|
| 17 |
+
A Gradio app showcasing Crusoe Foundry's long-context capabilities. Upload documents or code and interact with them using 7 open-source LLMs β no chunking, no embeddings, no RAG pipeline. Just raw context window power.
|
| 18 |
+
|
| 19 |
+
---
|
| 20 |
+
|
| 21 |
+
## Features
|
| 22 |
+
|
| 23 |
+
### 1. Document Analysis
|
| 24 |
+
Upload PDFs, TXT, MD, or DOCX files and ask questions about them. The entire document is passed to the model's context window β no chunking or retrieval needed. Responses cite relevant sections.
|
| 25 |
+
|
| 26 |
+
### 2. Codebase Intelligence
|
| 27 |
+
Upload source files (.py, .js, .ts, .go, .rs, .java) or paste code directly. Get architecture analysis, bug detection, API endpoint mapping, and error handling review β all from whole-codebase analysis.
|
| 28 |
+
|
| 29 |
+
### 3. MemoryAlloy Demo
|
| 30 |
+
Demonstrates KV cache sharing β set a large context once, then reuse it across multiple queries. Shows estimated token savings and cost reduction ($3/1M tokens).
|
| 31 |
+
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
## Architecture
|
| 35 |
+
|
| 36 |
+
```
|
| 37 |
+
crusoe-hf-space-legal/
|
| 38 |
+
βββ app.py # Full Gradio app β 3 tabs, 7 models, file upload + analysis
|
| 39 |
+
βββ requirements.txt
|
| 40 |
+
βββ README.md
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
### Single-file design
|
| 44 |
+
|
| 45 |
+
The entire app lives in `app.py` (~518 lines):
|
| 46 |
+
|
| 47 |
+
- **Lines 1-50:** Config β OpenAI client, model registry, API setup
|
| 48 |
+
- **Lines 50-100:** Utilities β token counting (tiktoken), file readers (PDF, TXT, MD)
|
| 49 |
+
- **Lines 100-250:** Tab 1 β Document Analysis with file upload + Q&A
|
| 50 |
+
- **Lines 250-380:** Tab 2 β Codebase Intelligence with code paste + file upload
|
| 51 |
+
- **Lines 380-500:** Tab 3 β MemoryAlloy KV cache demo
|
| 52 |
+
- **Lines 500-518:** Gradio app launch
|
| 53 |
+
|
| 54 |
+
## Models Available
|
| 55 |
+
|
| 56 |
+
| Model | ID |
|
| 57 |
+
|-------|----|
|
| 58 |
+
| Qwen3 235B | `Qwen/Qwen3-235B-A22B-Instruct-2507` |
|
| 59 |
+
| DeepSeek R1 | `deepseek-ai/DeepSeek-R1` |
|
| 60 |
+
| Kimi K2 | `moonshotai/Kimi-K2-Instruct` |
|
| 61 |
+
| DeepSeek V3 | `deepseek-ai/DeepSeek-V3-0324` |
|
| 62 |
+
| Llama 3.3 70B | `meta-llama/Llama-3.3-70B-Instruct` |
|
| 63 |
+
| GPT-OSS 120B | `openai/gpt-osse-120b` |
|
| 64 |
+
| Gemma 3 12B | `google/gemma-3-12b-it` |
|
| 65 |
+
|
| 66 |
+
---
|
| 67 |
+
|
| 68 |
+
## Quick Start
|
| 69 |
+
|
| 70 |
+
### Prerequisites
|
| 71 |
+
|
| 72 |
+
- Python 3.9+
|
| 73 |
+
- A [Crusoe Cloud](https://crusoe.ai) API key
|
| 74 |
+
|
| 75 |
+
### Local Development
|
| 76 |
+
|
| 77 |
+
```bash
|
| 78 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/crusoe-space-legal-docs
|
| 79 |
+
cd crusoe-space-legal-docs
|
| 80 |
+
|
| 81 |
+
pip install -r requirements.txt
|
| 82 |
+
export CRUSOE_API_KEY=your_key_here
|
| 83 |
+
python app.py
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
Open **http://localhost:7860**.
|
| 87 |
+
|
| 88 |
+
### Deploy to HuggingFace Spaces
|
| 89 |
+
|
| 90 |
+
1. Create a new Space at [huggingface.co/new-space](https://huggingface.co/new-space)
|
| 91 |
+
2. Select **Gradio** as the SDK
|
| 92 |
+
3. Add `CRUSOE_API_KEY` as a Space secret in Settings
|
| 93 |
+
4. Push:
|
| 94 |
+
|
| 95 |
+
```bash
|
| 96 |
+
git remote add hf https://huggingface.co/spaces/YOUR_USERNAME/crusoe-space-legal-docs
|
| 97 |
+
git push hf main
|
| 98 |
+
```
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
## Use as a Framework
|
| 103 |
+
|
| 104 |
+
### Add a new model
|
| 105 |
+
|
| 106 |
+
In `app.py`, add to the `MODELS` list:
|
| 107 |
+
|
| 108 |
+
```python
|
| 109 |
+
MODELS = [
|
| 110 |
+
...
|
| 111 |
+
"org/your-new-model",
|
| 112 |
+
]
|
| 113 |
+
```
|
| 114 |
+
|
| 115 |
+
### Add a new tab
|
| 116 |
+
|
| 117 |
+
Gradio's `gr.Tabs()` makes this straightforward:
|
| 118 |
+
|
| 119 |
+
```python
|
| 120 |
+
with gr.Tabs():
|
| 121 |
+
with gr.Tab("Your New Tab"):
|
| 122 |
+
# Your UI components here
|
| 123 |
+
input_box = gr.Textbox(label="Input")
|
| 124 |
+
output_box = gr.Textbox(label="Output")
|
| 125 |
+
btn = gr.Button("Analyze")
|
| 126 |
+
btn.click(fn=your_function, inputs=[input_box], outputs=[output_box])
|
| 127 |
+
```
|
| 128 |
+
|
| 129 |
+
### Swap the endpoint
|
| 130 |
+
|
| 131 |
+
Works with any OpenAI-compatible API:
|
| 132 |
+
|
| 133 |
+
```python
|
| 134 |
+
client = OpenAI(
|
| 135 |
+
base_url="https://your-endpoint.com/v1/",
|
| 136 |
+
api_key=os.environ.get("YOUR_API_KEY"),
|
| 137 |
+
)
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
### Add new file type support
|
| 141 |
+
|
| 142 |
+
Extend the file reader function in `app.py`:
|
| 143 |
+
|
| 144 |
+
```python
|
| 145 |
+
def read_file(file_path):
|
| 146 |
+
if file_path.endswith(".docx"):
|
| 147 |
+
# Add docx parsing
|
| 148 |
+
...
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
---
|
| 152 |
+
|
| 153 |
+
## API Reference (Crusoe Cloud Foundry)
|
| 154 |
+
|
| 155 |
+
All inference calls use the OpenAI-compatible SDK:
|
| 156 |
+
|
| 157 |
+
```python
|
| 158 |
+
from openai import OpenAI
|
| 159 |
+
|
| 160 |
+
client = OpenAI(
|
| 161 |
+
base_url="https://managed-inference-api-proxy.crusoecloud.com/v1/",
|
| 162 |
+
api_key="YOUR_CRUSOE_API_KEY",
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
response = client.chat.completions.create(
|
| 166 |
+
model="Qwen/Qwen3-235B-A22B-Instruct-2507",
|
| 167 |
+
messages=[{"role": "user", "content": "Analyze this document..."}],
|
| 168 |
+
temperature=1,
|
| 169 |
+
top_p=0.95,
|
| 170 |
+
)
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
```typescript
|
| 174 |
+
import OpenAI from 'openai';
|
| 175 |
+
|
| 176 |
+
const client = new OpenAI({
|
| 177 |
+
baseURL: 'https://managed-inference-api-proxy.crusoecloud.com/v1/',
|
| 178 |
+
apiKey: 'YOUR_CRUSOE_API_KEY',
|
| 179 |
+
});
|
| 180 |
+
|
| 181 |
+
const response = await client.chat.completions.create({
|
| 182 |
+
model: 'Qwen/Qwen3-235B-A22B-Instruct-2507',
|
| 183 |
+
messages: [{ role: 'user', content: 'Analyze this document...' }],
|
| 184 |
+
});
|
| 185 |
+
```
|
| 186 |
+
|
| 187 |
+
---
|
| 188 |
+
|
| 189 |
+
## Dependencies
|
| 190 |
+
|
| 191 |
+
```
|
| 192 |
+
gradio>=6.0.0
|
| 193 |
+
openai>=1.30.0
|
| 194 |
+
tiktoken>=0.7.0
|
| 195 |
+
pdfminer.six>=20221105
|
| 196 |
+
```
|
| 197 |
+
|
| 198 |
+
## Environment Variables
|
| 199 |
+
|
| 200 |
+
| Variable | Required | Description |
|
| 201 |
+
|----------|----------|-------------|
|
| 202 |
+
| `CRUSOE_API_KEY` | Yes | Your Crusoe Cloud Foundry API key |
|
| 203 |
+
|
| 204 |
+
---
|
| 205 |
+
|
| 206 |
+
## License
|
| 207 |
+
|
| 208 |
+
MIT β fork it, remix it, ship it.
|
| 209 |
+
|
| 210 |
+
Built by Crusoe AI Developer Relations Β· [crusoe.ai](https://crusoe.ai)
|