Commit ·
fa79fcf
1
Parent(s): de994b5
Expose model parameters in Space UI
Browse files- README.md +7 -0
- app.py +6 -2
- inference.py +15 -0
- model.py +13 -0
- static/index.html +148 -0
README.md
CHANGED
|
@@ -31,3 +31,10 @@ curl -X POST "http://localhost:7860/translate" \
|
|
| 31 |
-H "Content-Type: application/json" \
|
| 32 |
-d '{"text":"How are you?"}'
|
| 33 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
-H "Content-Type: application/json" \
|
| 32 |
-d '{"text":"How are you?"}'
|
| 33 |
```
|
| 34 |
+
|
| 35 |
+
Model parameter names, tensor shapes, and sizes are available in the Space UI
|
| 36 |
+
with the **Model Parameters** button, or directly through:
|
| 37 |
+
|
| 38 |
+
```bash
|
| 39 |
+
curl "http://localhost:7860/model-parameters"
|
| 40 |
+
```
|
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from fastapi.responses import FileResponse
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
from inference import translate
|
| 8 |
|
| 9 |
app = FastAPI(
|
| 10 |
title="Transformer Translation API",
|
|
@@ -38,4 +38,8 @@ def translate_api(request: TranslationRequest):
|
|
| 38 |
return {
|
| 39 |
"input": request.text,
|
| 40 |
"translation": translated_text
|
| 41 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from pydantic import BaseModel
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
from inference import get_model_parameters, translate
|
| 8 |
|
| 9 |
app = FastAPI(
|
| 10 |
title="Transformer Translation API",
|
|
|
|
| 38 |
return {
|
| 39 |
"input": request.text,
|
| 40 |
"translation": translated_text
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
@app.get("/model-parameters")
|
| 44 |
+
def model_parameters_api():
|
| 45 |
+
return get_model_parameters()
|
inference.py
CHANGED
|
@@ -34,6 +34,15 @@ model.load_state_dict(
|
|
| 34 |
|
| 35 |
model.eval()
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
@torch.no_grad()
|
| 38 |
def translate(sentence, max_new_tokens=32):
|
| 39 |
|
|
@@ -72,4 +81,10 @@ def translate(sentence, max_new_tokens=32):
|
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
print(translate("How are you?"))
|
|
|
|
| 34 |
|
| 35 |
model.eval()
|
| 36 |
|
| 37 |
+
def get_model_parameters():
|
| 38 |
+
parameters = model.parameter_summary()
|
| 39 |
+
total_parameters = sum(item["num_parameters"] for item in parameters)
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
"total_parameters": total_parameters,
|
| 43 |
+
"parameters": parameters,
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
@torch.no_grad()
|
| 47 |
def translate(sentence, max_new_tokens=32):
|
| 48 |
|
|
|
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
|
| 84 |
+
for item in get_model_parameters()["parameters"]:
|
| 85 |
+
print(
|
| 86 |
+
f'{item["name"]}: shape={item["shape"]}, '
|
| 87 |
+
f'parameters={item["num_parameters"]}'
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
print(translate("How are you?"))
|
model.py
CHANGED
|
@@ -85,6 +85,19 @@ class Translation(nn.Module):
|
|
| 85 |
parameters = (p for p in parameters if p.requires_grad)
|
| 86 |
return sum(p.numel() for p in parameters)
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
def forward(self, src, tgt, targets=None):
|
| 89 |
B, T_src = src.shape
|
| 90 |
src_tok_emb = self.transformer.token_embedding_encoder(src)
|
|
|
|
| 85 |
parameters = (p for p in parameters if p.requires_grad)
|
| 86 |
return sum(p.numel() for p in parameters)
|
| 87 |
|
| 88 |
+
def parameter_summary(self, only_trainable=False):
|
| 89 |
+
summary = []
|
| 90 |
+
for name, parameter in self.named_parameters():
|
| 91 |
+
if only_trainable and not parameter.requires_grad:
|
| 92 |
+
continue
|
| 93 |
+
summary.append({
|
| 94 |
+
"name": name,
|
| 95 |
+
"shape": list(parameter.shape),
|
| 96 |
+
"num_parameters": parameter.numel(),
|
| 97 |
+
"trainable": parameter.requires_grad,
|
| 98 |
+
})
|
| 99 |
+
return summary
|
| 100 |
+
|
| 101 |
def forward(self, src, tgt, targets=None):
|
| 102 |
B, T_src = src.shape
|
| 103 |
src_tok_emb = self.transformer.token_embedding_encoder(src)
|
static/index.html
CHANGED
|
@@ -134,6 +134,17 @@
|
|
| 134 |
background: #e0e0e0;
|
| 135 |
}
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
.status {
|
| 138 |
text-align: center;
|
| 139 |
margin-top: 20px;
|
|
@@ -165,6 +176,68 @@
|
|
| 165 |
font-size: 0.95em;
|
| 166 |
}
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
@media (max-width: 768px) {
|
| 169 |
.translator-box {
|
| 170 |
grid-template-columns: 1fr;
|
|
@@ -227,10 +300,31 @@
|
|
| 227 |
<div class="button-container">
|
| 228 |
<button class="translate-btn" id="translateBtn">Translate</button>
|
| 229 |
<button class="clear-btn" id="clearBtn">Clear</button>
|
|
|
|
| 230 |
</div>
|
| 231 |
|
| 232 |
<div class="status" id="status"></div>
|
| 233 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 234 |
<div class="info-box">
|
| 235 |
ℹ️ Enter English text in the left box and click "Translate" to get the
|
| 236 |
Hindi translation on the right side.
|
|
@@ -242,7 +336,12 @@
|
|
| 242 |
const outputTextarea = document.getElementById("output");
|
| 243 |
const translateBtn = document.getElementById("translateBtn");
|
| 244 |
const clearBtn = document.getElementById("clearBtn");
|
|
|
|
| 245 |
const statusDiv = document.getElementById("status");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
// Translate button click event
|
| 248 |
translateBtn.addEventListener("click", async () => {
|
|
@@ -294,6 +393,55 @@
|
|
| 294 |
statusDiv.className = "status";
|
| 295 |
});
|
| 296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
// Allow Enter key to trigger translation (Ctrl+Enter or Cmd+Enter)
|
| 298 |
inputTextarea.addEventListener("keydown", (e) => {
|
| 299 |
if ((e.ctrlKey || e.metaKey) && e.key === "Enter") {
|
|
|
|
| 134 |
background: #e0e0e0;
|
| 135 |
}
|
| 136 |
|
| 137 |
+
.params-btn {
|
| 138 |
+
background: #333;
|
| 139 |
+
color: white;
|
| 140 |
+
flex: 1;
|
| 141 |
+
max-width: 240px;
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
.params-btn:hover:not(:disabled) {
|
| 145 |
+
background: #111;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
.status {
|
| 149 |
text-align: center;
|
| 150 |
margin-top: 20px;
|
|
|
|
| 176 |
font-size: 0.95em;
|
| 177 |
}
|
| 178 |
|
| 179 |
+
.parameters-panel {
|
| 180 |
+
display: none;
|
| 181 |
+
margin-top: 30px;
|
| 182 |
+
border: 1px solid #e0e0e0;
|
| 183 |
+
border-radius: 8px;
|
| 184 |
+
overflow: hidden;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
.parameters-panel.visible {
|
| 188 |
+
display: block;
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
.parameters-header {
|
| 192 |
+
background: #f9f9f9;
|
| 193 |
+
border-bottom: 1px solid #e0e0e0;
|
| 194 |
+
color: #333;
|
| 195 |
+
padding: 15px;
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
.parameters-header h2 {
|
| 199 |
+
font-size: 1.2em;
|
| 200 |
+
margin-bottom: 6px;
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
.parameters-header p {
|
| 204 |
+
color: #666;
|
| 205 |
+
font-size: 0.95em;
|
| 206 |
+
}
|
| 207 |
+
|
| 208 |
+
.parameters-table-wrap {
|
| 209 |
+
max-height: 360px;
|
| 210 |
+
overflow: auto;
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
.parameters-table {
|
| 214 |
+
border-collapse: collapse;
|
| 215 |
+
color: #333;
|
| 216 |
+
font-size: 0.9em;
|
| 217 |
+
min-width: 720px;
|
| 218 |
+
width: 100%;
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
.parameters-table th,
|
| 222 |
+
.parameters-table td {
|
| 223 |
+
border-bottom: 1px solid #eee;
|
| 224 |
+
padding: 10px 12px;
|
| 225 |
+
text-align: left;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
.parameters-table th {
|
| 229 |
+
background: white;
|
| 230 |
+
color: #555;
|
| 231 |
+
position: sticky;
|
| 232 |
+
top: 0;
|
| 233 |
+
}
|
| 234 |
+
|
| 235 |
+
.parameters-table code {
|
| 236 |
+
background: #f5f5f5;
|
| 237 |
+
border-radius: 4px;
|
| 238 |
+
padding: 2px 5px;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
@media (max-width: 768px) {
|
| 242 |
.translator-box {
|
| 243 |
grid-template-columns: 1fr;
|
|
|
|
| 300 |
<div class="button-container">
|
| 301 |
<button class="translate-btn" id="translateBtn">Translate</button>
|
| 302 |
<button class="clear-btn" id="clearBtn">Clear</button>
|
| 303 |
+
<button class="params-btn" id="paramsBtn">Model Parameters</button>
|
| 304 |
</div>
|
| 305 |
|
| 306 |
<div class="status" id="status"></div>
|
| 307 |
|
| 308 |
+
<section class="parameters-panel" id="parametersPanel">
|
| 309 |
+
<div class="parameters-header">
|
| 310 |
+
<h2>Model Parameters</h2>
|
| 311 |
+
<p id="parametersSummary"></p>
|
| 312 |
+
</div>
|
| 313 |
+
<div class="parameters-table-wrap">
|
| 314 |
+
<table class="parameters-table">
|
| 315 |
+
<thead>
|
| 316 |
+
<tr>
|
| 317 |
+
<th>Name</th>
|
| 318 |
+
<th>Shape</th>
|
| 319 |
+
<th>Size</th>
|
| 320 |
+
<th>Trainable</th>
|
| 321 |
+
</tr>
|
| 322 |
+
</thead>
|
| 323 |
+
<tbody id="parametersBody"></tbody>
|
| 324 |
+
</table>
|
| 325 |
+
</div>
|
| 326 |
+
</section>
|
| 327 |
+
|
| 328 |
<div class="info-box">
|
| 329 |
ℹ️ Enter English text in the left box and click "Translate" to get the
|
| 330 |
Hindi translation on the right side.
|
|
|
|
| 336 |
const outputTextarea = document.getElementById("output");
|
| 337 |
const translateBtn = document.getElementById("translateBtn");
|
| 338 |
const clearBtn = document.getElementById("clearBtn");
|
| 339 |
+
const paramsBtn = document.getElementById("paramsBtn");
|
| 340 |
const statusDiv = document.getElementById("status");
|
| 341 |
+
const parametersPanel = document.getElementById("parametersPanel");
|
| 342 |
+
const parametersSummary = document.getElementById("parametersSummary");
|
| 343 |
+
const parametersBody = document.getElementById("parametersBody");
|
| 344 |
+
let parametersLoaded = false;
|
| 345 |
|
| 346 |
// Translate button click event
|
| 347 |
translateBtn.addEventListener("click", async () => {
|
|
|
|
| 393 |
statusDiv.className = "status";
|
| 394 |
});
|
| 395 |
|
| 396 |
+
paramsBtn.addEventListener("click", async () => {
|
| 397 |
+
if (parametersPanel.classList.contains("visible")) {
|
| 398 |
+
parametersPanel.classList.remove("visible");
|
| 399 |
+
paramsBtn.textContent = "Model Parameters";
|
| 400 |
+
return;
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
parametersPanel.classList.add("visible");
|
| 404 |
+
paramsBtn.textContent = "Hide Parameters";
|
| 405 |
+
|
| 406 |
+
if (parametersLoaded) {
|
| 407 |
+
return;
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
+
paramsBtn.disabled = true;
|
| 411 |
+
parametersSummary.textContent = "Loading parameter details...";
|
| 412 |
+
|
| 413 |
+
try {
|
| 414 |
+
const response = await fetch("/model-parameters");
|
| 415 |
+
|
| 416 |
+
if (!response.ok) {
|
| 417 |
+
throw new Error(`HTTP error! status: ${response.status}`);
|
| 418 |
+
}
|
| 419 |
+
|
| 420 |
+
const data = await response.json();
|
| 421 |
+
parametersSummary.textContent =
|
| 422 |
+
`Total parameters: ${data.total_parameters.toLocaleString()} ` +
|
| 423 |
+
`across ${data.parameters.length} tensors`;
|
| 424 |
+
parametersBody.innerHTML = data.parameters
|
| 425 |
+
.map((item) => {
|
| 426 |
+
return `
|
| 427 |
+
<tr>
|
| 428 |
+
<td><code>${item.name}</code></td>
|
| 429 |
+
<td>[${item.shape.join(", ")}]</td>
|
| 430 |
+
<td>${item.num_parameters.toLocaleString()}</td>
|
| 431 |
+
<td>${item.trainable ? "Yes" : "No"}</td>
|
| 432 |
+
</tr>
|
| 433 |
+
`;
|
| 434 |
+
})
|
| 435 |
+
.join("");
|
| 436 |
+
parametersLoaded = true;
|
| 437 |
+
} catch (error) {
|
| 438 |
+
console.error("Error:", error);
|
| 439 |
+
parametersSummary.textContent = "Unable to load model parameters.";
|
| 440 |
+
} finally {
|
| 441 |
+
paramsBtn.disabled = false;
|
| 442 |
+
}
|
| 443 |
+
});
|
| 444 |
+
|
| 445 |
// Allow Enter key to trigger translation (Ctrl+Enter or Cmd+Enter)
|
| 446 |
inputTextarea.addEventListener("keydown", (e) => {
|
| 447 |
if ((e.ctrlKey || e.metaKey) && e.key === "Enter") {
|