shashwatIDR commited on
Commit
cb28c37
·
verified ·
1 Parent(s): 523255d

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +23 -5
server.js CHANGED
@@ -210,6 +210,9 @@ app.post('/api/generate', async (req, res) => {
210
 
211
  // Flux model generation endpoint using black-forest-labs/FLUX.1-dev Gradio client
212
  app.post('/api/generate/flux', async (req, res) => {
 
 
 
213
  try {
214
  const {
215
  prompt = '',
@@ -222,11 +225,18 @@ app.post('/api/generate/flux', async (req, res) => {
222
  } = req.body;
223
 
224
  if (!prompt) {
225
- return res.status(400).json({ success: false, error: 'Prompt is required' });
 
 
226
  }
227
 
 
 
 
 
 
228
  // Connect to FLUX.1-dev Gradio client
229
- const client = await Client.connect("black-forest-labs/FLUX.1-schnell");
230
  const result = await client.predict("/infer", {
231
  prompt,
232
  seed,
@@ -237,10 +247,18 @@ app.post('/api/generate/flux', async (req, res) => {
237
  num_inference_steps
238
  });
239
 
240
- // result.data: [imageUrl, seed]
241
- res.json({ success: true, image: result.data[0], seed: result.data[1] });
 
 
 
 
 
 
 
242
  } catch (err) {
243
- res.status(500).json({ success: false, error: err.message });
 
244
  }
245
  });
246
 
 
210
 
211
  // Flux model generation endpoint using black-forest-labs/FLUX.1-dev Gradio client
212
  app.post('/api/generate/flux', async (req, res) => {
213
+ res.setHeader('Content-Type', 'text/event-stream');
214
+ res.setHeader('Cache-Control', 'no-cache');
215
+ res.setHeader('Connection', 'keep-alive');
216
  try {
217
  const {
218
  prompt = '',
 
225
  } = req.body;
226
 
227
  if (!prompt) {
228
+ res.write(`data: ${JSON.stringify({ type: 'error', message: 'Prompt is required' })}\n\n`);
229
+ res.end();
230
+ return;
231
  }
232
 
233
+ // Simulate estimation and processing events for UI feedback
234
+ res.write(`data: ${JSON.stringify({ type: 'estimation', queueSize: 1, eta: 5 })}\n\n`);
235
+ await new Promise(resolve => setTimeout(resolve, 1000));
236
+ res.write(`data: ${JSON.stringify({ type: 'processing' })}\n\n`);
237
+
238
  // Connect to FLUX.1-dev Gradio client
239
+ const client = await Client.connect("black-forest-labs/FLUX.1-dev");
240
  const result = await client.predict("/infer", {
241
  prompt,
242
  seed,
 
247
  num_inference_steps
248
  });
249
 
250
+ // result.data: [imageObj, seed]
251
+ const imageObj = result.data[0];
252
+ const imageUrl = (imageObj && imageObj.url) ? imageObj.url : (typeof imageObj === 'string' ? imageObj : null);
253
+ if (imageUrl) {
254
+ res.write(`data: ${JSON.stringify({ type: 'success', imageUrl, seed: result.data[1] })}\n\n`);
255
+ } else {
256
+ res.write(`data: ${JSON.stringify({ type: 'error', message: 'No image URL returned' })}\n\n`);
257
+ }
258
+ res.end();
259
  } catch (err) {
260
+ res.write(`data: ${JSON.stringify({ type: 'error', message: err.message })}\n\n`);
261
+ res.end();
262
  }
263
  });
264