File size: 10,456 Bytes
e4256ad
 
 
 
 
 
 
0880d9d
dfc3a58
0880d9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f826074
0880d9d
 
 
 
e4256ad
 
 
 
dfc3a58
 
e4256ad
dfc3a58
 
 
e4256ad
 
dfc3a58
 
e4256ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfc3a58
 
cb28c37
 
 
b568b38
 
dfc3a58
 
 
 
 
 
 
b568b38
 
dfc3a58
cb28c37
 
 
b568b38
 
cb28c37
 
 
 
 
dfc3a58
cb28c37
dfc3a58
b568b38
dfc3a58
 
b568b38
 
dfc3a58
 
b568b38
 
cb28c37
 
 
 
 
 
 
 
 
b568b38
cb28c37
 
b568b38
 
 
e4256ad
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import express from 'express';
import axios from 'axios';
import { Client } from '@gradio/client';
import dotenv from 'dotenv';

dotenv.config();

// --- Hugging Face Space Auto-Restarter ---
const HF_TOKEN = process.env.HF_TOKEN;
const SPACE_ID = process.env.HF_SPACE_ID || 'shashwatIDR/vision';
const HF_API_BASE = 'https://huggingface.co/api';

async function restartSpace() {
  console.log(`[πŸ”„ Attempting restart] ${SPACE_ID}`);
  try {
    const response = await axios.post(`${HF_API_BASE}/spaces/${SPACE_ID}/restart`, {}, {
      headers: {
        Authorization: `Bearer ${HF_TOKEN}`,
        'Content-Type': 'application/json'
      },
      timeout: 30000
    });
    console.log(`[βœ… Restarted] ${SPACE_ID} at ${new Date().toLocaleString()}`);
    if (response.data) {
      console.log('[πŸ“Š Response]', response.data);
    }
  } catch (err) {
    if (err.code === 'ENOTFOUND') {
      console.error('[❌ DNS Error] Cannot resolve huggingface.co - check your internet connection');
    } else if (err.code === 'ETIMEDOUT') {
      console.error('[❌ Timeout] Request timed out - try again later');
    } else if (err.response) {
      console.error(`[❌ HTTP ${err.response.status}]`, err.response.data || err.response.statusText);
      if (err.response.status === 401) {
        console.error('[πŸ”‘ Auth Error] Check your Hugging Face token');
      } else if (err.response.status === 404) {
        console.error('[πŸ” Not Found] Space may not exist or token lacks permissions');
      }
    } else {
      console.error('[❌ Failed to Restart]', err.message);
    }
  }
}

async function testHFAPI() {
  try {
    console.log('[πŸ” Testing Hugging Face API endpoint...]');
    const response = await axios.get(`${HF_API_BASE}/spaces`, {
      headers: { Authorization: `Bearer ${HF_TOKEN}` },
      timeout: 10000,
      params: { limit: 1 }
    });
    console.log('[βœ… API Test] Endpoint is working');
    return true;
  } catch (err) {
    console.error('[❌ API Test Failed]', err.response?.status || err.code || err.message);
    return false;
  }
}

// On server startup, test API and schedule restarts
(async () => {
  if (!HF_TOKEN) {
    console.error('[❌ No HF_TOKEN] Hugging Face token not set. Skipping auto-restart scheduling.');
    return;
  }
  console.log('[πŸš€ Starting] HuggingFace Space Auto-Restarter');
  const apiWorking = await testHFAPI();
  if (!apiWorking) {
    console.log('[⚠️ Warning] API test failed, but proceeding anyway...');
  }
  // Only schedule periodic restarts (no immediate restart on startup)
  setInterval(restartSpace, 5 * 60 * 1000);
  console.log('[⏰ Scheduled] Space will restart every 5 minutes');
})();

const app = express();
const port = process.env.PORT || 3000;
app.use(express.json());

// Serve static files from public
app.use(express.static('public'));

// Serve index.html at root
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/public/index.html');
});

// Image Generation endpoint (Heartsync)
app.post('/api/generate', async (req, res) => {
    res.setHeader('Content-Type', 'text/event-stream');
    res.setHeader('Cache-Control', 'no-cache');
    res.setHeader('Connection', 'keep-alive');

    try {
        const { prompt } = req.body;
        const sessionHash = Math.random().toString(36).substring(2, 15);

        // First, join the queue
        const joinResponse = await axios.post('https://heartsync-nsfw-uncensored.hf.space/gradio_api/queue/join', {
            data: [
                prompt,
                "text, talk bubble, low quality, watermark, signature",
                0,
                true,
                1024,
                1024,
                7,
                28
            ],
            event_data: null,
            fn_index: 2,
            trigger_id: 14,
            session_hash: sessionHash
        }, {
            headers: {
                'Content-Type': 'application/json',
                'Referer': 'https://heartsync-nsfw-uncensored.hf.space/'
            }
        });

        // Poll for results
        let attempts = 0;
        const maxAttempts = 60; // Increased attempts for potentially longer queues
        
        while (attempts < maxAttempts) {
            const dataResponse = await axios.get(
                `https://heartsync-nsfw-uncensored.hf.space/gradio_api/queue/data?session_hash=${encodeURIComponent(sessionHash)}`,
                {
                    headers: {
                        'Accept': 'text/event-stream',
                        'Referer': 'https://heartsync-nsfw-uncensored.hf.space/'
                    }
                }
            );

            const data = dataResponse.data;
            
            // Manually parse the event stream data
            const lines = data.split('\n');
            let eventData = '';
            let foundEvent = false;
            for (const line of lines) {
                if (line.startsWith('data: ')) {
                    eventData += line.substring(6);
                } else if (line === '') {
                    // Process eventData when an empty line is encountered
                    if (eventData) {
                        try {
                            const json = JSON.parse(eventData);
                            console.log(`[Image] Parsed event: ${json.msg}`);
                            
                            if (json.msg === 'process_completed' && json.output?.data?.[0]?.url) {
                                const imageUrl = json.output.data[0].url;
                                
                                // Send the original image URL back to the client as an SSE
                                res.write(`data: ${JSON.stringify({
                                    type: 'success',
                                    originalUrl: imageUrl
                                })}\n\n`);
                                res.end(); // End the connection after sending the final event
                                return; // Exit the function
                            }

                            if (json.msg === 'estimation') {
                                res.write(`data: ${JSON.stringify({ type: 'estimation', queueSize: json.queue_size, eta: json.rank_eta })}\n\n`);
                                foundEvent = true;
                            } else if (json.msg === 'process_starts') {
                                res.write(`data: ${JSON.stringify({ type: 'processing' })}\n\n`);
                                foundEvent = true;
                            } else if (json.msg === 'process_failed') {
                                console.log(`[Image] Process failed:`, json);
                                res.write(`data: ${JSON.stringify({ type: 'error', message: 'Generation failed on server' })}\n\n`);
                                res.end();
                                return;
                            }

                        } catch (error) {
                            console.error('[Image] Error parsing event data:', error, 'Raw data:', eventData);
                        }
                        eventData = ''; // Reset for the next event
                    }
                } else if (line.startsWith(':')) {
                    // Ignore comment lines
                    continue;
                } else if (line !== '') {
                     // If line is not data or comment, it might be part of a multi-line data chunk, append to eventData
                     eventData += line;
                }
            }

            // If loop finishes without 'process_completed', wait and try again
            await new Promise(resolve => setTimeout(resolve, 2000)); // Wait longer before next poll attempt
            attempts++;
        }

        // If the loop times out without success
        if (!foundEvent) {
            console.log(`[Image] No events found in response, raw data:`, data.substring(0, 500));
        }
        res.status(500).write(`data: ${JSON.stringify({ type: 'error', message: 'Generation timed out' })}\n\n`);
        res.end();

    } catch (error) {
        console.error('Generation endpoint error:', error);
        res.status(500).write(`data: ${JSON.stringify({ type: 'error', message: error.message || 'Failed to generate image' })}\n\n`);
        res.end();
    }
});

// Flux model generation endpoint using black-forest-labs/FLUX.1-dev Gradio client
app.post('/api/generate/flux', async (req, res) => {
    res.setHeader('Content-Type', 'text/event-stream');
    res.setHeader('Cache-Control', 'no-cache');
    res.setHeader('Connection', 'keep-alive');
    try {
        const {
            prompt = '',
            width = 1024,
            height = 1024,
            guidance_scale = 3.5,
            num_inference_steps = 28,
            seed = 0,
            randomize_seed = true
        } = req.body;

        if (!prompt) {
            res.write(`data: ${JSON.stringify({ type: 'error', message: 'Prompt is required' })}\n\n`);
            res.end();
            return;
        }

        // Simulate estimation and processing events for UI feedback
        res.write(`data: ${JSON.stringify({ type: 'estimation', queueSize: 1, eta: 5 })}\n\n`);
        await new Promise(resolve => setTimeout(resolve, 1000));
        res.write(`data: ${JSON.stringify({ type: 'processing' })}\n\n`);

        // Connect to FLUX.1-dev Gradio client
        const client = await Client.connect("black-forest-labs/FLUX.1-dev");
        const result = await client.predict("/infer", {
            prompt,
            seed,
            randomize_seed,
            width,
            height,
            guidance_scale,
            num_inference_steps
        });

        // result.data: [imageObj, seed]
        const imageObj = result.data[0];
        const imageUrl = (imageObj && imageObj.url) ? imageObj.url : (typeof imageObj === 'string' ? imageObj : null);
        if (imageUrl) {
            res.write(`data: ${JSON.stringify({ type: 'success', imageUrl, seed: result.data[1] })}\n\n`);
        } else {
            res.write(`data: ${JSON.stringify({ type: 'error', message: 'No image URL returned' })}\n\n`);
        }
        res.end();
    } catch (err) {
        res.write(`data: ${JSON.stringify({ type: 'error', message: err.message })}\n\n`);
        res.end();
    }
});

app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});