R-Kentaren commited on
Commit
733e738
·
verified ·
1 Parent(s): 028f29e

Create nsfw-text-to-image.js

Browse files
Files changed (1) hide show
  1. nsfw-text-to-image.js +132 -0
nsfw-text-to-image.js ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+
3
+ credits rynn buat kode nya
4
+
5
+ */
6
+
7
+
8
+ const axios = require('axios');
9
+
10
+ const ProxyAgent = require('@rynn-k/proxy-agent');
11
+
12
+ const proxy = new ProxyAgent('./proxies.txt', { random: true }); // Adjust proxy path
13
+
14
+
15
+
16
+ async function nsfwimage(prompt, options = {}) {
17
+
18
+ try {
19
+
20
+ const {
21
+
22
+ negative_prompt = 'lowres, bad anatomy, bad hands, text, error, missing finger, extra digits, fewer digits, cropped, worst quality, low quality, low score, bad score, average score, signature, watermark, username, blurry',
23
+
24
+ style = 'anime',
25
+
26
+ width = 1024,
27
+
28
+ height = 1024,
29
+
30
+ guidance_scale = 7,
31
+
32
+ inference_steps = 28
33
+
34
+ } = options;
35
+
36
+
37
+
38
+ const _style = ['anime', 'real', 'photo'];
39
+
40
+
41
+
42
+ if (!prompt) throw new Error('Prompt is required');
43
+
44
+ if (!_style.includes(style)) throw new Error(`Available styles: ${_style.join(', ')}`);
45
+
46
+ if (width < 256 || width > 1216) throw new Error('Min width: 256, Max width: 1216');
47
+
48
+ if (height < 256 || height > 1216) throw new Error('Min height: 256, Max height: 1216');
49
+
50
+ if (guidance_scale < 0 || guidance_scale > 20) throw new Error('Min guidance scale: 0, Max guidance scale: 20');
51
+
52
+ if (inference_steps < 1 || inference_steps > 28) throw new Error('Max inference steps: 20');
53
+
54
+
55
+
56
+ const agent = proxy.config();
57
+
58
+ const session_hash = Math.random().toString(36).substring(2);
59
+
60
+ const d = await axios.post(`https://heartsync-nsfw-uncensored${style !== 'anime' ? `-${style}` : ''}.hf.space/gradio_api/queue/join?`, {
61
+
62
+ data: [
63
+
64
+ prompt,
65
+
66
+ negative_prompt,
67
+
68
+ 0,
69
+
70
+ true,
71
+
72
+ width,
73
+
74
+ height,
75
+
76
+ guidance_scale,
77
+
78
+ inference_steps
79
+
80
+ ],
81
+
82
+ event_data: null,
83
+
84
+ fn_index: 2,
85
+
86
+ trigger_id: 16,
87
+
88
+ session_hash: session_hash
89
+
90
+ }, agent);
91
+
92
+
93
+
94
+ const { data } = await axios.get(`https://heartsync-nsfw-uncensored${style !== 'anime' ? `-${style}` : ''}.hf.space/gradio_api/queue/data?session_hash=${session_hash}`, agent);
95
+
96
+
97
+
98
+ let result;
99
+
100
+ const lines = data.split('\n\n');
101
+
102
+ for (const line of lines) {
103
+
104
+ if (line.startsWith('data:')) {
105
+
106
+ const d = JSON.parse(line.substring(6));
107
+
108
+ if (d.msg === 'process_completed') result = d.output.data[0].url;
109
+
110
+ }
111
+
112
+ }
113
+
114
+
115
+
116
+ return result;
117
+
118
+ } catch (error) {
119
+
120
+ throw new Error(error.message);
121
+
122
+ }
123
+
124
+ }
125
+
126
+
127
+
128
+ // Usage:
129
+
130
+ const resp = await nsfwimage('1girl, Shiroko (Blue Archive), take a bath', { style: 'anime', width: 1216, height: 720 });
131
+
132
+ console.log(resp);