Spaces:
Running
Running
Update index.js
Browse files
index.js
CHANGED
@@ -7,6 +7,7 @@ import { FormData, Blob } from "formdata-node";
|
|
7 |
import { fileTypeFromBuffer } from "file-type";
|
8 |
import { Client } from "@gradio/client";
|
9 |
import { stablediff } from "./lib/diffusion.js"
|
|
|
10 |
|
11 |
const app = express();
|
12 |
app.set('json spaces', 4);
|
@@ -78,17 +79,45 @@ app.post('/api/img2img', async (req, res) => {
|
|
78 |
app.post('/api/openai/gpt4', async (req, res) => {
|
79 |
try {
|
80 |
console.log(req.body)
|
81 |
-
const { prompt, status } = req.body
|
82 |
if (!prompt) return res.json({ succese: false, message: 'Require an Promot text!'})
|
83 |
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
84 |
|
85 |
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
status: true,
|
90 |
result: response
|
91 |
-
|
|
|
92 |
} catch (e) {
|
93 |
console.log(e)
|
94 |
e = String(e)
|
@@ -152,58 +181,4 @@ async function processImage2Img(imgBuffer, prompt) {
|
|
152 |
reject(e.message);
|
153 |
}
|
154 |
});
|
155 |
-
}
|
156 |
-
|
157 |
-
async function askOpenGPT4o(prompt) {
|
158 |
-
try {
|
159 |
-
const session_hash = Math.random().toString(36).substring(2).slice(1);
|
160 |
-
|
161 |
-
const resPrompt = await axios.post('https://kingnish-opengpt-4o.hf.space/run/predict?__theme=light', {
|
162 |
-
data: [{ text: prompt, files: [] }],
|
163 |
-
event_data: null,
|
164 |
-
fn_index: 3,
|
165 |
-
trigger_id: 34,
|
166 |
-
session_hash,
|
167 |
-
});
|
168 |
-
|
169 |
-
const res = await axios.post('https://kingnish-opengpt-4o.hf.space/queue/join?__theme=light', {
|
170 |
-
data: [
|
171 |
-
null,
|
172 |
-
null,
|
173 |
-
'idefics2-8b-chatty',
|
174 |
-
'Top P Sampling',
|
175 |
-
0.5,
|
176 |
-
4096,
|
177 |
-
1,
|
178 |
-
0.9,
|
179 |
-
true,
|
180 |
-
],
|
181 |
-
event_data: null,
|
182 |
-
fn_index: 5,
|
183 |
-
trigger_id: 34,
|
184 |
-
session_hash,
|
185 |
-
});
|
186 |
-
|
187 |
-
const event_ID = res.data.event_id;
|
188 |
-
|
189 |
-
const anu = await axios.get(`https://kingnish-opengpt-4o.hf.space/queue/data?session_hash=${session_hash}`);
|
190 |
-
const lines = anu.data.split('\n');
|
191 |
-
const processStartsLine = lines.find(line => line.includes('process_completed'));
|
192 |
-
const processStartsData = JSON.parse(processStartsLine.replace('data: ', ''));
|
193 |
-
|
194 |
-
const processStartsLine_2 = lines.find(line => line.includes('process_generating'));
|
195 |
-
const processStartsData_2 = JSON.parse(processStartsLine_2.replace('data: ', ''));
|
196 |
-
|
197 |
-
|
198 |
-
if (processStartsData?.success) {
|
199 |
-
return processStartsData.output.data[0][0][1];
|
200 |
-
} else if (processStartsData_2?.success) {
|
201 |
-
return processStartsData_2.output.data[0][0][1];
|
202 |
-
}
|
203 |
-
|
204 |
-
|
205 |
-
} catch (error) {
|
206 |
-
console.error('Error occurred:', error);
|
207 |
-
return `Error: ${error.message}`;
|
208 |
-
}
|
209 |
-
}
|
|
|
7 |
import { fileTypeFromBuffer } from "file-type";
|
8 |
import { Client } from "@gradio/client";
|
9 |
import { stablediff } from "./lib/diffusion.js"
|
10 |
+
import { askOpenGPT4o } from "./lib/chatgpt.js"
|
11 |
|
12 |
const app = express();
|
13 |
app.set('json spaces', 4);
|
|
|
79 |
app.post('/api/openai/gpt4', async (req, res) => {
|
80 |
try {
|
81 |
console.log(req.body)
|
82 |
+
const { prompt, images status } = req.body
|
83 |
if (!prompt) return res.json({ succese: false, message: 'Require an Promot text!'})
|
84 |
if (!status) return res.json({ success: false, message: 'Required an status text!' })
|
85 |
|
86 |
if(status !== apikey) return res.json({ success: false, message: 'Invalid status!' })
|
87 |
+
if(images) {
|
88 |
+
if (/^(https?|http):\/\//i.test(images)) {
|
89 |
+
const data_img = await axios.request({
|
90 |
+
method: "GET",
|
91 |
+
url: images,
|
92 |
+
responseType: "arraybuffer"
|
93 |
+
})
|
94 |
+
const response = await askOpenGPT4o(prompt, data_img.data)
|
95 |
+
//const type_img = await fileTypeFromBuffer(response)
|
96 |
+
//res.setHeader('Content-Type', type_img.mime)
|
97 |
+
res.json({
|
98 |
+
status: true,
|
99 |
+
result: response
|
100 |
+
})
|
101 |
+
} else if (images && typeof images == 'string' && isBase64(images)) {
|
102 |
+
const response = await askOpenGPT4o(prompt, Buffer.from(images, "base64"))
|
103 |
+
//const type_img = await fileTypeFromBuffer(response)
|
104 |
+
//res.setHeader('Content-Type', type_img.mime)
|
105 |
+
res.json({
|
106 |
+
status: true,
|
107 |
+
result: response
|
108 |
+
})
|
109 |
+
} else {
|
110 |
+
res.json({
|
111 |
+
success: false, message: 'No url or base64 detected!!'
|
112 |
+
})
|
113 |
+
}
|
114 |
+
} else if(!images) {
|
115 |
+
const response await askOpenGPT4o(prompt)
|
116 |
+
res.json({
|
117 |
status: true,
|
118 |
result: response
|
119 |
+
});
|
120 |
+
}
|
121 |
} catch (e) {
|
122 |
console.log(e)
|
123 |
e = String(e)
|
|
|
181 |
reject(e.message);
|
182 |
}
|
183 |
});
|
184 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|