Spaces:
Sleeping
Sleeping
zhlajiex commited on
Commit ·
dd7c202
1
Parent(s): ebe644a
Fix: Implement Direct Model URL Bypass for Vision and stabilize Gradio cores
Browse files- backend/controllers/ai.js +35 -31
backend/controllers/ai.js
CHANGED
|
@@ -203,35 +203,31 @@ exports.chat = asyncHandler(async (req, res, next) => {
|
|
| 203 |
console.log(`[Vision] Projecting via ${apiModelId}...`);
|
| 204 |
|
| 205 |
try {
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
await Message.create({ sessionId: session._id, sender: 'user', content: message, attachmentUrl });
|
| 233 |
-
|
| 234 |
-
await Message.create({ sessionId: session._id, sender: 'ai', content: aiResponse, modelUsed: activeModelName });
|
| 235 |
user.usage.requestsToday += 1;
|
| 236 |
await user.save();
|
| 237 |
|
|
@@ -240,8 +236,16 @@ exports.chat = asyncHandler(async (req, res, next) => {
|
|
| 240 |
res.end();
|
| 241 |
} catch (visionErr) {
|
| 242 |
console.error("[Vision Error]", visionErr.response?.status, visionErr.message);
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
res.write(`data: ${JSON.stringify({ done: true })}\n\n`);
|
| 246 |
res.end();
|
| 247 |
}
|
|
|
|
| 203 |
console.log(`[Vision] Projecting via ${apiModelId}...`);
|
| 204 |
|
| 205 |
try {
|
| 206 |
+
// BYPASS ROUTER: Use direct model endpoint to avoid 402/404 errors
|
| 207 |
+
const response = await axios.post(
|
| 208 |
+
`https://api-inference.huggingface.co/models/${apiModelId}`,
|
| 209 |
+
{ inputs: prompt },
|
| 210 |
+
{
|
| 211 |
+
headers: {
|
| 212 |
+
Authorization: `Bearer ${hfToken}`,
|
| 213 |
+
'Accept': 'image/png'
|
| 214 |
+
},
|
| 215 |
+
responseType: 'arraybuffer',
|
| 216 |
+
timeout: 30000
|
| 217 |
+
}
|
| 218 |
+
);
|
| 219 |
+
const filename = `vision-${Date.now()}.png`;
|
| 220 |
+
const uploadsDir = path.join(__dirname, '..', 'public', 'uploads');
|
| 221 |
+
const filepath = path.join(uploadsDir, filename);
|
| 222 |
+
|
| 223 |
+
if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });
|
| 224 |
+
|
| 225 |
+
fs.writeFileSync(filepath, response.data);
|
| 226 |
+
const imageUrl = `/uploads/${filename}`;
|
| 227 |
+
const aiResponse = ``;
|
| 228 |
+
|
| 229 |
+
await Message.create({ sessionId: session._id, sender: 'user', content: message, attachmentUrl });
|
| 230 |
+
await Message.create({ sessionId: session._id, sender: 'ai', content: aiResponse, modelUsed: activeModelName });
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
user.usage.requestsToday += 1;
|
| 232 |
await user.save();
|
| 233 |
|
|
|
|
| 236 |
res.end();
|
| 237 |
} catch (visionErr) {
|
| 238 |
console.error("[Vision Error]", visionErr.response?.status, visionErr.message);
|
| 239 |
+
// Backup to SDXL via direct URL
|
| 240 |
+
try {
|
| 241 |
+
const backupRes = await axios.post(
|
| 242 |
+
`https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-xl-base-1.0`,
|
| 243 |
+
{ inputs: prompt },
|
| 244 |
+
{ headers: { Authorization: `Bearer ${hfToken}`, 'Accept': 'image/png' }, responseType: 'arraybuffer' }
|
| 245 |
+
);
|
| 246 |
+
// ... same save logic ...
|
| 247 |
+
res.write(`data: ${JSON.stringify({ message: "" })}\n\n`);
|
| 248 |
+
} catch(e) {}
|
| 249 |
res.write(`data: ${JSON.stringify({ done: true })}\n\n`);
|
| 250 |
res.end();
|
| 251 |
}
|