Spaces:
Running
Running
Update server.js
Browse files
server.js
CHANGED
@@ -570,11 +570,12 @@ app.post('/api/generate/anime-uc', authenticateToken, async (req, res) => {
|
|
570 |
}
|
571 |
});
|
572 |
|
573 |
-
// Flux Generation endpoint (
|
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 |
|
@@ -587,43 +588,57 @@ app.get('/api/generate/flux', authenticateToken, async (req, res) => {
|
|
587 |
}
|
588 |
|
589 |
const sessionHash = Math.random().toString(36).substring(2, 15);
|
|
|
590 |
|
591 |
// Join the queue using UNO-FLUX API
|
592 |
-
const
|
593 |
-
|
594 |
-
|
595 |
-
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
|
603 |
-
|
604 |
-
|
605 |
-
|
606 |
-
|
607 |
-
|
608 |
-
|
609 |
-
|
610 |
-
|
611 |
-
|
612 |
-
|
613 |
-
|
614 |
-
|
615 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
627 |
{
|
628 |
headers: {
|
629 |
'Accept': 'text/event-stream',
|
@@ -632,8 +647,8 @@ app.get('/api/generate/flux', authenticateToken, async (req, res) => {
|
|
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 = '';
|
@@ -644,21 +659,33 @@ app.get('/api/generate/flux', authenticateToken, async (req, res) => {
|
|
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 |
-
|
|
|
|
|
|
|
661 |
} catch (error) {
|
|
|
662 |
res.status(500).json({ success: false, error: error.message || 'Failed to generate image' });
|
663 |
}
|
664 |
});
|
|
|
570 |
}
|
571 |
});
|
572 |
|
573 |
+
// Flux Generation endpoint (UNO-FLUX direct API with detailed logging)
|
574 |
app.get('/api/generate/flux', authenticateToken, async (req, res) => {
|
575 |
try {
|
576 |
const { prompt, aspect } = req.query;
|
577 |
if (!prompt) {
|
578 |
+
console.error('[Flux] No prompt provided');
|
579 |
return res.status(400).json({ success: false, error: 'Prompt is required' });
|
580 |
}
|
581 |
|
|
|
588 |
}
|
589 |
|
590 |
const sessionHash = Math.random().toString(36).substring(2, 15);
|
591 |
+
console.log(`[Flux] Starting UNO-FLUX generation for prompt: "${prompt}" | aspect: ${aspect} | width: ${width} | height: ${height} | session: ${sessionHash}`);
|
592 |
|
593 |
// Join the queue using UNO-FLUX API
|
594 |
+
const joinPayload = {
|
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 |
+
console.log('[Flux] Sending queue/join payload:', JSON.stringify(joinPayload));
|
613 |
+
let joinResponse;
|
614 |
+
try {
|
615 |
+
joinResponse = await axios.post(
|
616 |
+
'https://bytedance-research-uno-flux.hf.space/gradio_api/queue/join?__theme=system',
|
617 |
+
joinPayload,
|
618 |
+
{
|
619 |
+
headers: {
|
620 |
+
'Content-Type': 'application/json',
|
621 |
+
'Referer': 'https://bytedance-research-uno-flux.hf.space/'
|
622 |
+
}
|
623 |
}
|
624 |
+
);
|
625 |
+
console.log('[Flux] queue/join response:', joinResponse.data);
|
626 |
+
} catch (err) {
|
627 |
+
console.error('[Flux] Error joining UNO-FLUX queue:', err.response?.data || err.message);
|
628 |
+
return res.status(500).json({ success: false, error: 'Failed to join UNO-FLUX queue' });
|
629 |
+
}
|
630 |
|
631 |
// Poll for results
|
632 |
let attempts = 0;
|
633 |
const maxAttempts = 60;
|
634 |
+
const pollUrl = `https://bytedance-research-uno-flux.hf.space/gradio_api/queue/data?session_hash=${encodeURIComponent(sessionHash)}`;
|
635 |
+
console.log(`[Flux] Polling for results at: ${pollUrl}`);
|
636 |
+
let found = false;
|
637 |
while (attempts < maxAttempts) {
|
638 |
+
attempts++;
|
639 |
try {
|
640 |
const dataResponse = await axios.get(
|
641 |
+
pollUrl,
|
642 |
{
|
643 |
headers: {
|
644 |
'Accept': 'text/event-stream',
|
|
|
647 |
timeout: 10000
|
648 |
}
|
649 |
);
|
|
|
650 |
const data = dataResponse.data;
|
651 |
+
console.log(`[Flux] Poll attempt ${attempts}, data length: ${data.length}`);
|
652 |
// Parse the event stream data
|
653 |
const lines = data.split('\n');
|
654 |
let eventData = '';
|
|
|
659 |
if (eventData) {
|
660 |
try {
|
661 |
const json = JSON.parse(eventData);
|
662 |
+
console.log(`[Flux] Event:`, json);
|
663 |
if (json.msg === 'process_completed' && json.output?.data?.[0]?.url) {
|
664 |
const imageUrl = json.output.data[0].url;
|
665 |
+
console.log(`[Flux] Success! Image URL: ${imageUrl}`);
|
666 |
+
found = true;
|
667 |
return res.json({ success: true, imageUrl });
|
668 |
+
} else if (json.msg === 'process_completed') {
|
669 |
+
console.warn('[Flux] process_completed but no image URL:', json);
|
670 |
}
|
671 |
+
} catch (error) {
|
672 |
+
console.error('[Flux] Error parsing event data:', error, 'Raw data:', eventData);
|
673 |
+
}
|
674 |
eventData = '';
|
675 |
}
|
676 |
}
|
677 |
}
|
678 |
+
} catch (pollError) {
|
679 |
+
console.error(`[Flux] Poll error on attempt ${attempts}:`, pollError.message);
|
680 |
+
}
|
681 |
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
|
682 |
}
|
683 |
+
if (!found) {
|
684 |
+
console.error(`[Flux] Generation timed out after ${maxAttempts} attempts`);
|
685 |
+
res.status(500).json({ success: false, error: 'Generation timed out' });
|
686 |
+
}
|
687 |
} catch (error) {
|
688 |
+
console.error('[Flux] Fatal error in endpoint:', error);
|
689 |
res.status(500).json({ success: false, error: error.message || 'Failed to generate image' });
|
690 |
}
|
691 |
});
|