Spaces:
Sleeping
Sleeping
File size: 3,658 Bytes
af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 af2a264 ba5aa63 |
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 |
import express from "express";
import bodyParser from "body-parser";
import dotenv from "dotenv";
import fetch from "node-fetch";
import fs from "fs";
import path from "path";
dotenv.config();
const app = express();
app.use(bodyParser.json());
// 创建图片保存目录
const imageDir = path.join(process.cwd(), "images");
if (!fs.existsSync(imageDir)) {
fs.mkdirSync(imageDir);
}
// 静态文件服务
app.use("/images", express.static(imageDir));
app.get("/", (req, res) => {
res.send(`
<html>
<head>
<title>OPENAI</title>
</head>
<body>
<h1>OpenAI</h1>
<p>Congratulations! Your project has been successfully deployed.</p>
</body>
</html>
`);
});
app.post("/deem/v1/chat/completions", async (req, res) => {
try {
const data = req.body;
const messages = data.messages;
const lastMessage = messages[messages.length - 1];
const messagesContent = lastMessage.content;
const apiUrl = `https://api-flux1.api4gpt.com/?prompt=${messagesContent}`;
const apiResponse = await fetch(apiUrl);
if (!apiResponse.ok) {
throw new Error("文生图服务失效:" + apiResponse.status);
}
const imageBuffer = await apiResponse.buffer();
// 生成唯一的文件名
const fileName = `image_${Date.now()}.jpg`;
const filePath = path.join(imageDir, fileName);
// 保存图片到本地
fs.writeFileSync(filePath, imageBuffer);
// 构建本地图片URL
const imgUrl = `/images/${fileName}`;
console.log(imgUrl);
if (imgUrl) {
const chunkId = `chatcmpl-${Date.now()}`;
const chunkCreated = Math.floor(Date.now() / 1000);
res.write(
"data: " +
JSON.stringify({
id: chunkId,
object: "chat.completion.chunk",
created: chunkCreated,
model: req.body.model,
choices: [
{
index: 0,
delta: {
content: `![](${imgUrl})`,
},
finish_reason: null,
},
],
}) +
"\n\n"
);
res.write("data: [DONE]\n\n");
res.end();
} else {
res.status(500).json({ error: "图片保存失败" });
}
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: error.message });
}
});
app.post("/deem/v1/images/generations", async (req, res) => {
try {
const data = req.body;
const messages = data.prompt;
const apiUrl = `https://api-flux1.api4gpt.com/?prompt=${messages}`;
const apiResponse = await fetch(apiUrl);
if (!apiResponse.ok) {
throw new Error("文生图服务失效:" + apiResponse.status);
}
const imageBuffer = await apiResponse.buffer();
// 生成唯一的文件名
const fileName = `image_${Date.now()}.jpg`;
const filePath = path.join(imageDir, fileName);
// 保存图片到本地
fs.writeFileSync(filePath, imageBuffer);
// 构建本地图片URL
const imgUrl = `/images/${fileName}`;
console.log(imgUrl);
if (imgUrl) {
const chunkCreated = Math.floor(Date.now() / 1000);
res.json({
created: chunkCreated,
data: [
{
url: imgUrl,
},
],
});
} else {
res.status(500).json({ error: "图片保存失败" });
}
} catch (error) {
console.error("Error:", error);
res.status(500).json({ error: error.message });
}
});
app.listen(process.env.PORT || 3000);
|