shashwatIDR commited on
Commit
8051ba5
·
verified ·
1 Parent(s): b568b38

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +72 -31
server.js CHANGED
@@ -570,13 +570,14 @@ app.post('/api/generate/anime-uc', authenticateToken, async (req, res) => {
570
  }
571
  });
572
 
573
- // Flux Generation endpoint (now uses UNO-FLUX model)
574
  app.get('/api/generate/flux', authenticateToken, async (req, res) => {
575
  try {
576
  const { prompt, aspect } = req.query;
577
  if (!prompt) {
578
  return res.status(400).json({ success: false, error: 'Prompt is required' });
579
  }
 
580
  // Aspect ratio logic
581
  let width = 1280, height = 720;
582
  if (aspect === '9:16') {
@@ -585,39 +586,79 @@ app.get('/api/generate/flux', authenticateToken, async (req, res) => {
585
  width = 1024; height = 1024;
586
  }
587
 
588
- // Use a default image for all required image_prompt1-4
589
- const defaultImageUrl = 'https://raw.githubusercontent.com/gradio-app/gradio/main/test/test_files/bus.png';
590
- async function fetchImageBlob(url) {
591
- const response = await fetch(url);
592
- if (!response.ok) throw new Error('Failed to fetch image: ' + url);
593
- return await response.blob();
594
- }
595
- const [img1, img2, img3, img4] = await Promise.all([
596
- fetchImageBlob(defaultImageUrl),
597
- fetchImageBlob(defaultImageUrl),
598
- fetchImageBlob(defaultImageUrl),
599
- fetchImageBlob(defaultImageUrl)
600
- ]);
601
 
602
- // Connect to UNO-FLUX Gradio client
603
- const client = await Client.connect("bytedance-research/UNO-FLUX");
604
- const result = await client.predict("/gradio_generate", {
605
- prompt,
606
- width,
607
- height,
608
- guidance: 4,
609
- num_steps: 25,
610
- seed: -1,
611
- image_prompt1: img1,
612
- image_prompt2: img2,
613
- image_prompt3: img3,
614
- image_prompt4: img4
615
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
616
 
617
- // result.data[0] is the generated image URL
618
- res.json({ success: true, imageUrl: result.data[0] });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
619
  } catch (error) {
620
- console.error('[Flux] UNO-FLUX endpoint error:', error);
621
  res.status(500).json({ success: false, error: error.message || 'Failed to generate image' });
622
  }
623
  });
 
570
  }
571
  });
572
 
573
+ // Flux Generation endpoint (now uses UNO-FLUX queue API)
574
  app.get('/api/generate/flux', authenticateToken, async (req, res) => {
575
  try {
576
  const { prompt, aspect } = req.query;
577
  if (!prompt) {
578
  return res.status(400).json({ success: false, error: 'Prompt is required' });
579
  }
580
+
581
  // Aspect ratio logic
582
  let width = 1280, height = 720;
583
  if (aspect === '9:16') {
 
586
  width = 1024; height = 1024;
587
  }
588
 
589
+ const sessionHash = Math.random().toString(36).substring(2, 15);
 
 
 
 
 
 
 
 
 
 
 
 
590
 
591
+ // Join the queue using UNO-FLUX API
592
+ const joinResponse = await axios.post(
593
+ 'https://bytedance-research-uno-flux.hf.space/gradio_api/queue/join?__theme=system',
594
+ {
595
+ data: [
596
+ prompt,
597
+ width,
598
+ height,
599
+ 4, // guidance
600
+ 25, // num_steps
601
+ -1, // seed
602
+ null, // image_prompt1
603
+ null, // image_prompt2
604
+ null, // image_prompt3
605
+ null // image_prompt4
606
+ ],
607
+ event_data: null,
608
+ fn_index: 0,
609
+ trigger_id: 25,
610
+ session_hash: sessionHash
611
+ },
612
+ {
613
+ headers: {
614
+ 'Content-Type': 'application/json',
615
+ 'Referer': 'https://bytedance-research-uno-flux.hf.space/'
616
+ }
617
+ }
618
+ );
619
+
620
+ // Poll for results
621
+ let attempts = 0;
622
+ const maxAttempts = 60;
623
+ while (attempts < maxAttempts) {
624
+ try {
625
+ const dataResponse = await axios.get(
626
+ `https://bytedance-research-uno-flux.hf.space/gradio_api/queue/data?session_hash=${encodeURIComponent(sessionHash)}`,
627
+ {
628
+ headers: {
629
+ 'Accept': 'text/event-stream',
630
+ 'Referer': 'https://bytedance-research-uno-flux.hf.space/'
631
+ },
632
+ timeout: 10000
633
+ }
634
+ );
635
 
636
+ const data = dataResponse.data;
637
+ // Parse the event stream data
638
+ const lines = data.split('\n');
639
+ let eventData = '';
640
+ for (const line of lines) {
641
+ if (line.startsWith('data: ')) {
642
+ eventData += line.substring(6);
643
+ } else if (line === '') {
644
+ if (eventData) {
645
+ try {
646
+ const json = JSON.parse(eventData);
647
+ if (json.msg === 'process_completed' && json.output?.data?.[0]?.url) {
648
+ const imageUrl = json.output.data[0].url;
649
+ return res.json({ success: true, imageUrl });
650
+ }
651
+ } catch (error) {}
652
+ eventData = '';
653
+ }
654
+ }
655
+ }
656
+ } catch (pollError) {}
657
+ await new Promise(resolve => setTimeout(resolve, 2000));
658
+ attempts++;
659
+ }
660
+ res.status(500).json({ success: false, error: 'Generation timed out' });
661
  } catch (error) {
 
662
  res.status(500).json({ success: false, error: error.message || 'Failed to generate image' });
663
  }
664
  });