Upload 4 files
Browse files- Dockerfile +9 -0
- README.md +10 -10
- app.js +178 -0
- package.json +20 -0
Dockerfile
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM node:alpine
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY . /app
|
6 |
+
|
7 |
+
RUN npm install
|
8 |
+
|
9 |
+
CMD ["npm", "start"]
|
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
-
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
-
sdk: docker
|
7 |
-
pinned: false
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
1 |
+
---
|
2 |
+
title: sdxl
|
3 |
+
emoji: 👩🎨
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: yellow
|
6 |
+
sdk: docker
|
7 |
+
pinned: false
|
8 |
+
app_port: 3000
|
9 |
+
---
|
10 |
+
|
app.js
ADDED
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import express from "express";
|
2 |
+
import bodyParser from "body-parser";
|
3 |
+
import dotenv from "dotenv";
|
4 |
+
import fetch from "node-fetch";
|
5 |
+
dotenv.config();
|
6 |
+
|
7 |
+
const app = express();
|
8 |
+
app.use(bodyParser.json());
|
9 |
+
|
10 |
+
app.get("/", (req, res) => {
|
11 |
+
res.send(`
|
12 |
+
<html>
|
13 |
+
<head>
|
14 |
+
<title>OPENAI</title>
|
15 |
+
</head
|
16 |
+
<body>
|
17 |
+
<h1>OpenAI</h1>
|
18 |
+
<p>Congratulations! Your project has been successfully deployed.</p>
|
19 |
+
</body>
|
20 |
+
</html>
|
21 |
+
`);
|
22 |
+
});
|
23 |
+
|
24 |
+
app.post("/deem/v1/chat/completions", async (req, res) => {
|
25 |
+
let token; // 将 token 的定义提到函数的开头
|
26 |
+
const authHeader =
|
27 |
+
req.headers["authorization"] || req.headers["Authorization"];
|
28 |
+
if (!authHeader) {
|
29 |
+
return res.status(401).json({
|
30 |
+
code: 401,
|
31 |
+
errmsg: "Unauthorized.",
|
32 |
+
});
|
33 |
+
} else {
|
34 |
+
token = authHeader.split(" ")[1]; // 删除 const 定义
|
35 |
+
if (!token) {
|
36 |
+
return res.status(401).json({
|
37 |
+
code: 401,
|
38 |
+
errmsg: "Unauthorized.",
|
39 |
+
});
|
40 |
+
}
|
41 |
+
}
|
42 |
+
|
43 |
+
try {
|
44 |
+
const data = req.body;
|
45 |
+
const messages = data.messages;
|
46 |
+
const lastMessage = messages[messages.length - 1];
|
47 |
+
const messagesContent = lastMessage.content;
|
48 |
+
//const messagesContent = messages.map(m => m.content).join(' ');
|
49 |
+
|
50 |
+
//const resp = await fetch(`https://aihh.api.ecylt.top/?key=${token}&prompt=${messagesContent}`);
|
51 |
+
//const imgUrl = resp.url;//直接获取图片地址
|
52 |
+
//或者 imgUrl = https://aihh.api.ecylt.top/?key=${token}&prompt=${messagesContent}
|
53 |
+
|
54 |
+
const apiUrl = `https://aihh.api.ecylt.top/?key=${token}&prompt=${messagesContent}`
|
55 |
+
|
56 |
+
const apiResponse = await fetch(apiUrl);
|
57 |
+
if (!apiResponse.ok) {
|
58 |
+
throw new Error("文生图服务失效:" + apiResponse.status);
|
59 |
+
}
|
60 |
+
const imageBlob = await apiResponse.blob();
|
61 |
+
const formData = new FormData();
|
62 |
+
formData.append("file", imageBlob, "image.jpg");
|
63 |
+
|
64 |
+
const uploadResponse = await fetch("https://img.deem.love/upload", {
|
65 |
+
method: 'POST',
|
66 |
+
body: formData
|
67 |
+
});
|
68 |
+
|
69 |
+
if (!uploadResponse.ok) {
|
70 |
+
throw new Error("图片上传失败");
|
71 |
+
}
|
72 |
+
const uploadResult = await uploadResponse.json();
|
73 |
+
const imgUrl = "https://img.deem.love" + uploadResult[0].src;
|
74 |
+
|
75 |
+
console.log(imgUrl);
|
76 |
+
|
77 |
+
if (imgUrl) {
|
78 |
+
const chunkId = `chatcmpl-${Date.now()}`;
|
79 |
+
const chunkCreated = Math.floor(Date.now() / 1000);
|
80 |
+
|
81 |
+
res.write(
|
82 |
+
"data: " +
|
83 |
+
JSON.stringify({
|
84 |
+
id: chunkId,
|
85 |
+
object: "chat.completion.chunk",
|
86 |
+
created: chunkCreated,
|
87 |
+
model: req.body.model,
|
88 |
+
choices: [
|
89 |
+
{
|
90 |
+
index: 0,
|
91 |
+
delta: {
|
92 |
+
content: `![](${imgUrl})`,
|
93 |
+
},
|
94 |
+
finish_reason: null,
|
95 |
+
},
|
96 |
+
],
|
97 |
+
}) +
|
98 |
+
"\n\n"
|
99 |
+
);
|
100 |
+
res.write("data: [DONE]\n\n");
|
101 |
+
res.end();
|
102 |
+
} else {
|
103 |
+
res.status(500).json({ error: "地址获取失效" });
|
104 |
+
}
|
105 |
+
} catch (error) {
|
106 |
+
console.error("Error:", error);
|
107 |
+
}
|
108 |
+
});
|
109 |
+
|
110 |
+
app.post("/deem/v1/images/generations", async (req, res) => {
|
111 |
+
let token; // 将 token 的定义提到函数的开头
|
112 |
+
const authHeader =
|
113 |
+
req.headers["authorization"] || req.headers["Authorization"];
|
114 |
+
if (!authHeader) {
|
115 |
+
return res.status(401).json({
|
116 |
+
code: 401,
|
117 |
+
errmsg: "Unauthorized.",
|
118 |
+
});
|
119 |
+
} else {
|
120 |
+
token = authHeader.split(" ")[1]; // 删除 const 定义
|
121 |
+
if (!token) {
|
122 |
+
return res.status(401).json({
|
123 |
+
code: 401,
|
124 |
+
errmsg: "Unauthorized.",
|
125 |
+
});
|
126 |
+
}
|
127 |
+
}
|
128 |
+
|
129 |
+
try {
|
130 |
+
const data = req.body;
|
131 |
+
const messages = data.prompt;
|
132 |
+
|
133 |
+
const apiUrl = `https://aihh.api.ecylt.top/?key=${token}&prompt=${messages}`
|
134 |
+
|
135 |
+
const apiResponse = await fetch(apiUrl);
|
136 |
+
if (!apiResponse.ok) {
|
137 |
+
throw new Error("文生图服务失效:" + apiResponse.status);
|
138 |
+
}
|
139 |
+
const imageBlob = await apiResponse.blob();
|
140 |
+
const formData = new FormData();
|
141 |
+
formData.append("file", imageBlob, "image.jpg");
|
142 |
+
|
143 |
+
const uploadResponse = await fetch("https://img.deem.love/upload", {
|
144 |
+
method: 'POST',
|
145 |
+
body: formData
|
146 |
+
});
|
147 |
+
|
148 |
+
if (!uploadResponse.ok) {
|
149 |
+
throw new Error("图片上传失败");
|
150 |
+
}
|
151 |
+
const uploadResult = await uploadResponse.json();
|
152 |
+
const imgUrl = "https://img.deem.love" + uploadResult[0].src;
|
153 |
+
|
154 |
+
console.log(imgUrl);
|
155 |
+
|
156 |
+
if (imgUrl) {
|
157 |
+
const chunkCreated = Math.floor(Date.now() / 1000);
|
158 |
+
|
159 |
+
res.write(
|
160 |
+
JSON.stringify({
|
161 |
+
created: chunkCreated,
|
162 |
+
data: [
|
163 |
+
{
|
164 |
+
url: imgUrl,
|
165 |
+
},
|
166 |
+
],
|
167 |
+
})
|
168 |
+
);
|
169 |
+
res.end();
|
170 |
+
} else {
|
171 |
+
res.status(500).json({ error: "地址获取失效" });
|
172 |
+
}
|
173 |
+
} catch (error) {
|
174 |
+
console.error("Error:", error);
|
175 |
+
}
|
176 |
+
});
|
177 |
+
|
178 |
+
app.listen(process.env.PORT || 3000);
|
package.json
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "dify2openai",
|
3 |
+
"version": "1.0.0",
|
4 |
+
"description": "turn dify api into openai",
|
5 |
+
"main": "app.js",
|
6 |
+
"type": "module",
|
7 |
+
"scripts": {
|
8 |
+
"test": "echo \"Error: no test specified\" && exit 1",
|
9 |
+
"start": "node app.js"
|
10 |
+
},
|
11 |
+
"keywords": [],
|
12 |
+
"author": "fatwang2",
|
13 |
+
"license": "MIT",
|
14 |
+
"dependencies": {
|
15 |
+
"body-parser": "^1.20.2",
|
16 |
+
"dotenv": "^16.3.1",
|
17 |
+
"express": "^4.18.2",
|
18 |
+
"node-fetch": "^3.3.2"
|
19 |
+
}
|
20 |
+
}
|