Spaces:
Running on Zero
Running on Zero
update app
Browse files
app.py
CHANGED
|
@@ -166,10 +166,10 @@ def load_example_data(idx_str):
|
|
| 166 |
try:
|
| 167 |
idx = int(str(idx_str).strip())
|
| 168 |
except Exception:
|
| 169 |
-
return gr.update(value="")
|
| 170 |
|
| 171 |
if idx < 0 or idx >= len(image_examples):
|
| 172 |
-
return gr.update(value="")
|
| 173 |
|
| 174 |
ex = image_examples[idx]
|
| 175 |
img_b64 = file_to_data_url(ex["image"])
|
|
@@ -199,7 +199,7 @@ def b64_to_pil(b64_str):
|
|
| 199 |
return None
|
| 200 |
|
| 201 |
|
| 202 |
-
def
|
| 203 |
gpu_timeout = kwargs.get("gpu_timeout", None)
|
| 204 |
if gpu_timeout is None and args:
|
| 205 |
gpu_timeout = args[-1]
|
|
@@ -209,8 +209,8 @@ def calc_timeout_generic(*args, **kwargs):
|
|
| 209 |
return 60
|
| 210 |
|
| 211 |
|
| 212 |
-
@spaces.GPU(duration=
|
| 213 |
-
def generate_image(model_name, text, image, max_new_tokens
|
| 214 |
try:
|
| 215 |
if not model_name or model_name not in MODEL_MAP:
|
| 216 |
yield "[ERROR] Please select a valid model."
|
|
@@ -773,31 +773,21 @@ function init() {
|
|
| 773 |
setGradioValue('hidden-model-name', name);
|
| 774 |
}
|
| 775 |
|
| 776 |
-
function
|
| 777 |
-
|
| 778 |
-
|
| 779 |
-
previewImg.style.display = 'none';
|
| 780 |
-
previewWrap.style.display = 'none';
|
| 781 |
-
if (uploadPrompt) uploadPrompt.style.display = 'flex';
|
| 782 |
-
syncImageToGradio();
|
| 783 |
-
return;
|
| 784 |
-
}
|
| 785 |
-
|
| 786 |
previewWrap.style.display = 'flex';
|
| 787 |
if (uploadPrompt) uploadPrompt.style.display = 'none';
|
| 788 |
-
previewImg.src = imageState.preview || imageState.b64;
|
| 789 |
-
previewImg.style.display = 'block';
|
| 790 |
syncImageToGradio();
|
| 791 |
}
|
| 792 |
-
|
| 793 |
-
function setPreviewFromFileReader(b64, name) {
|
| 794 |
-
imageState = {b64, name: name || 'image', mode: 'image'};
|
| 795 |
-
renderPreview();
|
| 796 |
-
}
|
| 797 |
|
| 798 |
function clearPreview() {
|
| 799 |
imageState = null;
|
| 800 |
-
|
|
|
|
|
|
|
|
|
|
| 801 |
}
|
| 802 |
window.__clearPreview = clearPreview;
|
| 803 |
|
|
@@ -808,7 +798,7 @@ function init() {
|
|
| 808 |
return;
|
| 809 |
}
|
| 810 |
const reader = new FileReader();
|
| 811 |
-
reader.onload = (e) =>
|
| 812 |
reader.readAsDataURL(file);
|
| 813 |
}
|
| 814 |
|
|
@@ -847,7 +837,10 @@ function init() {
|
|
| 847 |
window.__activateModelTab = activateModelTab;
|
| 848 |
|
| 849 |
document.querySelectorAll('.model-tab[data-model]').forEach(btn => {
|
| 850 |
-
btn.addEventListener('click', () =>
|
|
|
|
|
|
|
|
|
|
| 851 |
});
|
| 852 |
|
| 853 |
activateModelTab('Nanonets-OCR2-3B');
|
|
@@ -969,24 +962,19 @@ function init() {
|
|
| 969 |
function applyExamplePayload(raw) {
|
| 970 |
try {
|
| 971 |
const data = JSON.parse(raw);
|
| 972 |
-
if (data.status
|
| 973 |
-
|
| 974 |
-
|
| 975 |
-
|
| 976 |
-
|
| 977 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 978 |
}
|
| 979 |
-
|
| 980 |
-
imageState = {
|
| 981 |
-
b64: data.image || '',
|
| 982 |
-
preview: data.image || '',
|
| 983 |
-
name: data.name || 'example.jpg',
|
| 984 |
-
mode: 'image'
|
| 985 |
-
};
|
| 986 |
-
renderPreview();
|
| 987 |
-
|
| 988 |
-
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 989 |
-
showToast('Example loaded', 'info');
|
| 990 |
} catch (e) {
|
| 991 |
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 992 |
}
|
|
@@ -1023,7 +1011,6 @@ function init() {
|
|
| 1023 |
|
| 1024 |
function writeIdxAndClick() {
|
| 1025 |
attempts += 1;
|
| 1026 |
-
|
| 1027 |
const ok1 = setGradioValue('example-idx-input', String(idx));
|
| 1028 |
setGradioValue('example-result-data', '');
|
| 1029 |
const currentVal = getValueFromContainer('example-idx-input');
|
|
@@ -1195,7 +1182,7 @@ with gr.Blocks() as demo:
|
|
| 1195 |
|
| 1196 |
<div id="single-preview-wrap" class="single-preview-wrap">
|
| 1197 |
<div class="single-preview-card">
|
| 1198 |
-
<img id="single-preview-img" src="" alt="Preview"
|
| 1199 |
<div class="preview-overlay-actions">
|
| 1200 |
<button id="preview-upload-btn" class="preview-action-btn" title="Replace">Upload</button>
|
| 1201 |
<button id="preview-clear-btn" class="preview-action-btn" title="Clear">Clear</button>
|
|
@@ -1326,14 +1313,12 @@ with gr.Blocks() as demo:
|
|
| 1326 |
const model = modelEl ? modelEl.getAttribute('data-model') : m;
|
| 1327 |
const promptEl = document.getElementById('custom-query-input');
|
| 1328 |
const promptVal = promptEl ? promptEl.value : p;
|
| 1329 |
-
|
| 1330 |
-
let imgVal = img;
|
| 1331 |
const imgContainer = document.getElementById('hidden-image-b64');
|
|
|
|
| 1332 |
if (imgContainer) {
|
| 1333 |
const inner = imgContainer.querySelector('textarea, input');
|
| 1334 |
if (inner) imgVal = inner.value;
|
| 1335 |
}
|
| 1336 |
-
|
| 1337 |
return [model, promptVal, imgVal, mnt, t, tp, tk, rp, gd];
|
| 1338 |
}""",
|
| 1339 |
)
|
|
|
|
| 166 |
try:
|
| 167 |
idx = int(str(idx_str).strip())
|
| 168 |
except Exception:
|
| 169 |
+
return gr.update(value=json.dumps({"status": "error", "message": "Invalid example index"}))
|
| 170 |
|
| 171 |
if idx < 0 or idx >= len(image_examples):
|
| 172 |
+
return gr.update(value=json.dumps({"status": "error", "message": "Example index out of range"}))
|
| 173 |
|
| 174 |
ex = image_examples[idx]
|
| 175 |
img_b64 = file_to_data_url(ex["image"])
|
|
|
|
| 199 |
return None
|
| 200 |
|
| 201 |
|
| 202 |
+
def calc_timeout_duration(*args, **kwargs):
|
| 203 |
gpu_timeout = kwargs.get("gpu_timeout", None)
|
| 204 |
if gpu_timeout is None and args:
|
| 205 |
gpu_timeout = args[-1]
|
|
|
|
| 209 |
return 60
|
| 210 |
|
| 211 |
|
| 212 |
+
@spaces.GPU(duration=calc_timeout_duration)
|
| 213 |
+
def generate_image(model_name, text, image, max_new_tokens, temperature, top_p, top_k, repetition_penalty, gpu_timeout):
|
| 214 |
try:
|
| 215 |
if not model_name or model_name not in MODEL_MAP:
|
| 216 |
yield "[ERROR] Please select a valid model."
|
|
|
|
| 773 |
setGradioValue('hidden-model-name', name);
|
| 774 |
}
|
| 775 |
|
| 776 |
+
function setPreview(b64, name) {
|
| 777 |
+
imageState = {b64, name: name || 'image'};
|
| 778 |
+
previewImg.src = b64;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 779 |
previewWrap.style.display = 'flex';
|
| 780 |
if (uploadPrompt) uploadPrompt.style.display = 'none';
|
|
|
|
|
|
|
| 781 |
syncImageToGradio();
|
| 782 |
}
|
| 783 |
+
window.__setPreview = setPreview;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 784 |
|
| 785 |
function clearPreview() {
|
| 786 |
imageState = null;
|
| 787 |
+
previewImg.src = '';
|
| 788 |
+
previewWrap.style.display = 'none';
|
| 789 |
+
if (uploadPrompt) uploadPrompt.style.display = 'flex';
|
| 790 |
+
syncImageToGradio();
|
| 791 |
}
|
| 792 |
window.__clearPreview = clearPreview;
|
| 793 |
|
|
|
|
| 798 |
return;
|
| 799 |
}
|
| 800 |
const reader = new FileReader();
|
| 801 |
+
reader.onload = (e) => setPreview(e.target.result, file.name);
|
| 802 |
reader.readAsDataURL(file);
|
| 803 |
}
|
| 804 |
|
|
|
|
| 837 |
window.__activateModelTab = activateModelTab;
|
| 838 |
|
| 839 |
document.querySelectorAll('.model-tab[data-model]').forEach(btn => {
|
| 840 |
+
btn.addEventListener('click', () => {
|
| 841 |
+
const model = btn.getAttribute('data-model');
|
| 842 |
+
activateModelTab(model);
|
| 843 |
+
});
|
| 844 |
});
|
| 845 |
|
| 846 |
activateModelTab('Nanonets-OCR2-3B');
|
|
|
|
| 962 |
function applyExamplePayload(raw) {
|
| 963 |
try {
|
| 964 |
const data = JSON.parse(raw);
|
| 965 |
+
if (data.status === 'ok') {
|
| 966 |
+
if (data.image) setPreview(data.image, data.name || 'example.jpg');
|
| 967 |
+
if (data.query) {
|
| 968 |
+
promptInput.value = data.query;
|
| 969 |
+
syncPromptToGradio();
|
| 970 |
+
}
|
| 971 |
+
if (data.model) activateModelTab(data.model);
|
| 972 |
+
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 973 |
+
showToast('Example loaded', 'info');
|
| 974 |
+
} else if (data.status === 'error') {
|
| 975 |
+
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 976 |
+
showToast(data.message || 'Failed to load example', 'error');
|
| 977 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 978 |
} catch (e) {
|
| 979 |
document.querySelectorAll('.example-card.loading').forEach(c => c.classList.remove('loading'));
|
| 980 |
}
|
|
|
|
| 1011 |
|
| 1012 |
function writeIdxAndClick() {
|
| 1013 |
attempts += 1;
|
|
|
|
| 1014 |
const ok1 = setGradioValue('example-idx-input', String(idx));
|
| 1015 |
setGradioValue('example-result-data', '');
|
| 1016 |
const currentVal = getValueFromContainer('example-idx-input');
|
|
|
|
| 1182 |
|
| 1183 |
<div id="single-preview-wrap" class="single-preview-wrap">
|
| 1184 |
<div class="single-preview-card">
|
| 1185 |
+
<img id="single-preview-img" src="" alt="Preview">
|
| 1186 |
<div class="preview-overlay-actions">
|
| 1187 |
<button id="preview-upload-btn" class="preview-action-btn" title="Replace">Upload</button>
|
| 1188 |
<button id="preview-clear-btn" class="preview-action-btn" title="Clear">Clear</button>
|
|
|
|
| 1313 |
const model = modelEl ? modelEl.getAttribute('data-model') : m;
|
| 1314 |
const promptEl = document.getElementById('custom-query-input');
|
| 1315 |
const promptVal = promptEl ? promptEl.value : p;
|
|
|
|
|
|
|
| 1316 |
const imgContainer = document.getElementById('hidden-image-b64');
|
| 1317 |
+
let imgVal = img;
|
| 1318 |
if (imgContainer) {
|
| 1319 |
const inner = imgContainer.querySelector('textarea, input');
|
| 1320 |
if (inner) imgVal = inner.value;
|
| 1321 |
}
|
|
|
|
| 1322 |
return [model, promptVal, imgVal, mnt, t, tp, tk, rp, gd];
|
| 1323 |
}""",
|
| 1324 |
)
|