Spaces:
Sleeping
Sleeping
Add image generation frontend: ming-image model in picker + route to /api/image/generate with rendered result
Browse files- patch_frontend_image_gen.py +191 -0
patch_frontend_image_gen.py
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Patch frontend ChatInput.tsx: add inclusionai/ming-image-0.1-design and route it
|
| 2 |
+
to the image endpoint.
|
| 3 |
+
|
| 4 |
+
Image models cannot be used with /chat/completions (OpenRouter 404s), so the
|
| 5 |
+
backend exposes POST /api/image/generate instead. When such a model is selected,
|
| 6 |
+
handleSend calls that route and renders the returned data: URL directly in the
|
| 7 |
+
composer area instead of starting a chat turn.
|
| 8 |
+
|
| 9 |
+
Chat models are untouched: the original onSend() path still runs for them.
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
import sys
|
| 13 |
+
|
| 14 |
+
FILE = "/source/frontend/src/components/Chat/ChatInput.tsx"
|
| 15 |
+
|
| 16 |
+
with open(FILE, "r", encoding="utf-8") as f:
|
| 17 |
+
c = f.read()
|
| 18 |
+
|
| 19 |
+
if "ming-image-0-1-design" in c:
|
| 20 |
+
print("OK: image model already present (idempotent)")
|
| 21 |
+
raise SystemExit(0)
|
| 22 |
+
|
| 23 |
+
# ---------------------------------------------------------------- 1. picker
|
| 24 |
+
anchor = (
|
| 25 |
+
" {\n"
|
| 26 |
+
" id: 'laguna-s21',\n"
|
| 27 |
+
" name: 'Laguna S 2.1:free',\n"
|
| 28 |
+
" modelPath: 'openai/poolside/laguna-s-2.1:free',\n"
|
| 29 |
+
" avatarUrl: 'https://huggingface.co/api/avatars/poolside',\n"
|
| 30 |
+
" recommended: true,\n"
|
| 31 |
+
" },\n"
|
| 32 |
+
)
|
| 33 |
+
image_entry = (
|
| 34 |
+
" {\n"
|
| 35 |
+
" id: 'ming-image-0-1-design',\n"
|
| 36 |
+
" name: 'inclusionai/ming-image-0.1-design (image)',\n"
|
| 37 |
+
" modelPath: 'openai/inclusionai/ming-image-0.1-design',\n"
|
| 38 |
+
" avatarUrl: 'https://huggingface.co/api/avatars/inclusionai',\n"
|
| 39 |
+
" recommended: true,\n"
|
| 40 |
+
" },\n"
|
| 41 |
+
)
|
| 42 |
+
if anchor not in c:
|
| 43 |
+
print("FAIL: could not find Laguna S 2.1 anchor")
|
| 44 |
+
sys.exit(1)
|
| 45 |
+
c = c.replace(anchor, anchor + image_entry, 1)
|
| 46 |
+
print("OK: added ming-image model to picker")
|
| 47 |
+
|
| 48 |
+
# ---------------------------------------------------------------- 2. helper
|
| 49 |
+
helper_anchor = "const getHfAvatarUrl = (modelId: string) => {"
|
| 50 |
+
helper = (
|
| 51 |
+
"// === PATCH: image-generation models ===\n"
|
| 52 |
+
"// Image models cannot use /chat/completions; they go through\n"
|
| 53 |
+
"// POST /api/image/generate and render a data: URL instead of chat bubbles.\n"
|
| 54 |
+
"const IMAGE_MODEL_PATH = 'openai/inclusionai/ming-image-0.1-design';\n"
|
| 55 |
+
"const isImageModelPath = (path: string): boolean => path === IMAGE_MODEL_PATH;\n\n"
|
| 56 |
+
)
|
| 57 |
+
if "const isImageModelPath" not in c:
|
| 58 |
+
if helper_anchor not in c:
|
| 59 |
+
print("FAIL: could not find getHfAvatarUrl anchor")
|
| 60 |
+
sys.exit(1)
|
| 61 |
+
c = c.replace(helper_anchor, helper + helper_anchor, 1)
|
| 62 |
+
print("OK: added isImageModelPath helper")
|
| 63 |
+
|
| 64 |
+
# ---------------------------------------------------------------- 3. state
|
| 65 |
+
state_anchor = " const [datasetUploadProgress, setDatasetUploadProgress] = useState<number | null>(null);\n"
|
| 66 |
+
state = (
|
| 67 |
+
" // === PATCH: image generation state ===\n"
|
| 68 |
+
" const [imageBusy, setImageBusy] = useState(false);\n"
|
| 69 |
+
" const [imageDataUrl, setImageDataUrl] = useState<string | null>(null);\n"
|
| 70 |
+
" const [imageError, setImageError] = useState<string | null>(null);\n"
|
| 71 |
+
)
|
| 72 |
+
if "const [imageBusy" not in c:
|
| 73 |
+
if state_anchor not in c:
|
| 74 |
+
print("FAIL: could not find datasetUploadProgress state anchor")
|
| 75 |
+
sys.exit(1)
|
| 76 |
+
c = c.replace(state_anchor, state_anchor + state, 1)
|
| 77 |
+
print("OK: added image generation state")
|
| 78 |
+
|
| 79 |
+
# ---------------------------------------------------------------- 4. handleSend
|
| 80 |
+
old_send = """ const handleSend = useCallback(() => {
|
| 81 |
+
if (input.trim() && !disabled && !isUploadingDataset) {
|
| 82 |
+
onSend(input);
|
| 83 |
+
setInput('');
|
| 84 |
+
}
|
| 85 |
+
}, [input, disabled, isUploadingDataset, onSend]);"""
|
| 86 |
+
new_send = """ // === PATCH: route image models to /api/image/generate ===
|
| 87 |
+
const generateImage = useCallback(
|
| 88 |
+
async (prompt: string, modelPath: string) => {
|
| 89 |
+
setImageBusy(true);
|
| 90 |
+
setImageError(null);
|
| 91 |
+
try {
|
| 92 |
+
const res = await apiFetch('/api/image/generate', {
|
| 93 |
+
method: 'POST',
|
| 94 |
+
body: JSON.stringify({ prompt, model: modelPath }),
|
| 95 |
+
});
|
| 96 |
+
const data = await res.json().catch(() => null);
|
| 97 |
+
if (!res.ok) {
|
| 98 |
+
setImageError(data?.detail || `Image generation failed (HTTP ${res.status})`);
|
| 99 |
+
return;
|
| 100 |
+
}
|
| 101 |
+
const first = data?.images?.[0];
|
| 102 |
+
if (first?.url) {
|
| 103 |
+
setImageDataUrl(first.url);
|
| 104 |
+
} else {
|
| 105 |
+
setImageError('No image was returned.');
|
| 106 |
+
}
|
| 107 |
+
} catch (err) {
|
| 108 |
+
setImageError(err instanceof Error ? err.message : 'Image generation failed.');
|
| 109 |
+
} finally {
|
| 110 |
+
setImageBusy(false);
|
| 111 |
+
}
|
| 112 |
+
},
|
| 113 |
+
[],
|
| 114 |
+
);
|
| 115 |
+
|
| 116 |
+
const handleSend = useCallback(() => {
|
| 117 |
+
if (!input.trim() || disabled || isUploadingDataset || imageBusy) return;
|
| 118 |
+
|
| 119 |
+
// Image models bypass the chat transport entirely.
|
| 120 |
+
if (isImageModelPath(selectedModelPath)) {
|
| 121 |
+
const prompt = input;
|
| 122 |
+
setInput('');
|
| 123 |
+
void generateImage(prompt, selectedModelPath);
|
| 124 |
+
return;
|
| 125 |
+
}
|
| 126 |
+
|
| 127 |
+
onSend(input);
|
| 128 |
+
setInput('');
|
| 129 |
+
}, [input, disabled, isUploadingDataset, imageBusy, onSend, selectedModelPath, generateImage]);"""
|
| 130 |
+
|
| 131 |
+
if old_send not in c:
|
| 132 |
+
print("FAIL: could not find handleSend block")
|
| 133 |
+
sys.exit(1)
|
| 134 |
+
c = c.replace(old_send, new_send, 1)
|
| 135 |
+
print("OK: patched handleSend to route image models")
|
| 136 |
+
|
| 137 |
+
# ---------------------------------------------------------------- 5. render
|
| 138 |
+
render_anchor = " return (\n"
|
| 139 |
+
render = """ // === PATCH: image generation result panel ===
|
| 140 |
+
const imagePanel = imageBusy || imageError || imageDataUrl ? (
|
| 141 |
+
<Box sx={{ mb: 2, p: 1.5, border: '1px solid', borderColor: 'divider', borderRadius: 2 }}>
|
| 142 |
+
{imageBusy && <Typography variant="body2">Đang tạo ảnh… (có thể mất 1-2 phút)</Typography>}
|
| 143 |
+
{imageError && (
|
| 144 |
+
<Typography variant="body2" color="error">
|
| 145 |
+
Lỗi tạo ảnh: {imageError}
|
| 146 |
+
</Typography>
|
| 147 |
+
)}
|
| 148 |
+
{imageDataUrl && !imageBusy && (
|
| 149 |
+
<Box sx={{ display: 'flex', flexDirection: 'column', gap: 1, alignItems: 'flex-start' }}>
|
| 150 |
+
<img
|
| 151 |
+
src={imageDataUrl}
|
| 152 |
+
alt="generated"
|
| 153 |
+
style={{ maxWidth: '100%', maxHeight: '420px', borderRadius: 8 }}
|
| 154 |
+
/>
|
| 155 |
+
<Button size="small" onClick={() => setImageDataUrl(null)}>
|
| 156 |
+
Xóa ảnh
|
| 157 |
+
</Button>
|
| 158 |
+
</Box>
|
| 159 |
+
)}
|
| 160 |
+
</Box>
|
| 161 |
+
) : null;
|
| 162 |
+
|
| 163 |
+
"""
|
| 164 |
+
if "image generation result panel" not in c:
|
| 165 |
+
idx = c.rindex(render_anchor)
|
| 166 |
+
c = c[:idx] + render + c[idx:]
|
| 167 |
+
print("OK: added image result panel before the main return")
|
| 168 |
+
|
| 169 |
+
# Mount the panel just BEFORE the send/stop ternary. It cannot go inside the
|
| 170 |
+
# ternary branches: `cond ? (<IconButton/>) : ( <IconButton/> )` takes exactly
|
| 171 |
+
# one expression per branch, so `{imagePanel}` there is a syntax error.
|
| 172 |
+
# Placing it as a preceding sibling is also outside any <IconButton>, which is
|
| 173 |
+
# required since a <Box> panel cannot be a child of a <button>.
|
| 174 |
+
jsx_anchor = " {isProcessing ? (\n <IconButton\n onClick={onStop}"
|
| 175 |
+
jsx_new = (
|
| 176 |
+
" {imagePanel}\n"
|
| 177 |
+
" {isProcessing ? (\n"
|
| 178 |
+
" <IconButton\n"
|
| 179 |
+
" onClick={onStop}"
|
| 180 |
+
)
|
| 181 |
+
if "{imagePanel}" not in c:
|
| 182 |
+
if jsx_anchor not in c:
|
| 183 |
+
print("FAIL: could not find send/stop ternary anchor")
|
| 184 |
+
sys.exit(1)
|
| 185 |
+
c = c.replace(jsx_anchor, jsx_new, 1)
|
| 186 |
+
print("OK: mounted image panel before the send/stop ternary")
|
| 187 |
+
|
| 188 |
+
with open(FILE, "w", encoding="utf-8") as f:
|
| 189 |
+
f.write(c)
|
| 190 |
+
print("OK: ChatInput.tsx patched with image generation support")
|
| 191 |
+
print("DONE: frontend image model patch applied")
|