Spaces:
Running
Running
test
Browse files- server.js +42 -16
- src/components/ask-ai/ask-ai.tsx +3 -0
server.js
CHANGED
@@ -102,7 +102,14 @@ app.post("/api/deploy", checkUser, async (req, res) => {
|
|
102 |
});
|
103 |
}
|
104 |
|
105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
file.name = "index.html"; // Add name property to the Blob
|
107 |
|
108 |
const { hf_token } = req.cookies;
|
@@ -143,7 +150,7 @@ app.post("/api/deploy", checkUser, async (req, res) => {
|
|
143 |
});
|
144 |
|
145 |
app.post("/api/ask-ai", async (req, res) => {
|
146 |
-
const { prompt, html } = req.body;
|
147 |
if (!prompt) {
|
148 |
return res.status(400).send({
|
149 |
ok: false,
|
@@ -182,20 +189,30 @@ app.post("/api/ask-ai", async (req, res) => {
|
|
182 |
const client = new InferenceClient(token);
|
183 |
let completeResponse = "";
|
184 |
|
|
|
|
|
185 |
try {
|
186 |
const chatCompletion = client.chatCompletionStream({
|
187 |
model: MODEL_ID,
|
188 |
-
provider:
|
189 |
messages: [
|
190 |
{
|
191 |
-
role: "
|
192 |
content:
|
193 |
"ONLY USE HTML, CSS AND JAVASCRIPT. If you want to use ICON make sure to import the library first. Try to create the best UI possible by using only HTML, CSS and JAVASCRIPT. Also, try to ellaborate as much as you can, to create something unique. ALWAYS GIVE THE RESPONSE INTO A SINGLE HTML FILE",
|
194 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
195 |
...(html
|
196 |
? [
|
197 |
{
|
198 |
-
role: "
|
199 |
content: `The current code is: ${html}.`,
|
200 |
},
|
201 |
]
|
@@ -205,7 +222,7 @@ app.post("/api/ask-ai", async (req, res) => {
|
|
205 |
content: prompt,
|
206 |
},
|
207 |
],
|
208 |
-
max_tokens: 8_192,
|
209 |
});
|
210 |
|
211 |
while (true) {
|
@@ -215,16 +232,25 @@ app.post("/api/ask-ai", async (req, res) => {
|
|
215 |
}
|
216 |
const chunk = value.choices[0]?.delta?.content;
|
217 |
if (chunk) {
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
228 |
}
|
229 |
}
|
230 |
}
|
|
|
102 |
});
|
103 |
}
|
104 |
|
105 |
+
// add add the end of the body (before all the <script></script>) a paragraph with the mention this has been generated by DeepSite
|
106 |
+
// and add a link to the website (https://enzostvs-deepsite.hf.space) + the logo "/logo.svg"
|
107 |
+
const newHtml = html.replace(
|
108 |
+
/<\/body>/,
|
109 |
+
`<p style="text-align: center; font-size: 12px; color: #888; margin-top: 16px;">This website has been generated by <a href="https://enzostvs-deepsite.hf.space" target="_blank">DeepSite</a> <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;"></p></body>`
|
110 |
+
);
|
111 |
+
|
112 |
+
const file = new Blob([newHtml], { type: "text/html" });
|
113 |
file.name = "index.html"; // Add name property to the Blob
|
114 |
|
115 |
const { hf_token } = req.cookies;
|
|
|
150 |
});
|
151 |
|
152 |
app.post("/api/ask-ai", async (req, res) => {
|
153 |
+
const { prompt, html, previousPrompt } = req.body;
|
154 |
if (!prompt) {
|
155 |
return res.status(400).send({
|
156 |
ok: false,
|
|
|
189 |
const client = new InferenceClient(token);
|
190 |
let completeResponse = "";
|
191 |
|
192 |
+
const useNebius = html?.length > 2_500;
|
193 |
+
|
194 |
try {
|
195 |
const chatCompletion = client.chatCompletionStream({
|
196 |
model: MODEL_ID,
|
197 |
+
provider: useNebius ? "nebius" : "sambanova",
|
198 |
messages: [
|
199 |
{
|
200 |
+
role: "system",
|
201 |
content:
|
202 |
"ONLY USE HTML, CSS AND JAVASCRIPT. If you want to use ICON make sure to import the library first. Try to create the best UI possible by using only HTML, CSS and JAVASCRIPT. Also, try to ellaborate as much as you can, to create something unique. ALWAYS GIVE THE RESPONSE INTO A SINGLE HTML FILE",
|
203 |
},
|
204 |
+
...(previousPrompt
|
205 |
+
? [
|
206 |
+
{
|
207 |
+
role: "user",
|
208 |
+
content: previousPrompt,
|
209 |
+
},
|
210 |
+
]
|
211 |
+
: []),
|
212 |
...(html
|
213 |
? [
|
214 |
{
|
215 |
+
role: "assistant",
|
216 |
content: `The current code is: ${html}.`,
|
217 |
},
|
218 |
]
|
|
|
222 |
content: prompt,
|
223 |
},
|
224 |
],
|
225 |
+
max_tokens: useNebius ? 8_192 : 6_000,
|
226 |
});
|
227 |
|
228 |
while (true) {
|
|
|
232 |
}
|
233 |
const chunk = value.choices[0]?.delta?.content;
|
234 |
if (chunk) {
|
235 |
+
if (useNebius) {
|
236 |
+
res.write(chunk);
|
237 |
+
completeResponse += chunk;
|
238 |
+
|
239 |
+
// Break when HTML is complete
|
240 |
+
if (completeResponse.includes("</html>")) {
|
241 |
+
break;
|
242 |
+
}
|
243 |
+
} else {
|
244 |
+
let newChunk = chunk;
|
245 |
+
if (chunk.includes("</html>")) {
|
246 |
+
// Replace everything after the last </html> tag with an empty string
|
247 |
+
newChunk = newChunk.replace(/<\/html>[\s\S]*/, "</html>");
|
248 |
+
}
|
249 |
+
completeResponse += newChunk;
|
250 |
+
res.write(newChunk);
|
251 |
+
if (newChunk.includes("</html>")) {
|
252 |
+
break;
|
253 |
+
}
|
254 |
}
|
255 |
}
|
256 |
}
|
src/components/ask-ai/ask-ai.tsx
CHANGED
@@ -22,6 +22,7 @@ function AskAI({
|
|
22 |
const [open, setOpen] = useState(false);
|
23 |
const [prompt, setPrompt] = useState("");
|
24 |
const [hasAsked, setHasAsked] = useState(false);
|
|
|
25 |
|
26 |
const callAi = async () => {
|
27 |
if (isAiWorking) return;
|
@@ -34,6 +35,7 @@ function AskAI({
|
|
34 |
body: JSON.stringify({
|
35 |
prompt,
|
36 |
...(html === defaultHTML ? {} : { html }),
|
|
|
37 |
}),
|
38 |
headers: {
|
39 |
"Content-Type": "application/json",
|
@@ -59,6 +61,7 @@ function AskAI({
|
|
59 |
if (done) {
|
60 |
toast.success("AI responded successfully");
|
61 |
setPrompt("");
|
|
|
62 |
setisAiWorking(false);
|
63 |
setHasAsked(true);
|
64 |
return;
|
|
|
22 |
const [open, setOpen] = useState(false);
|
23 |
const [prompt, setPrompt] = useState("");
|
24 |
const [hasAsked, setHasAsked] = useState(false);
|
25 |
+
const [previousPrompt, setPreviousPrompt] = useState("");
|
26 |
|
27 |
const callAi = async () => {
|
28 |
if (isAiWorking) return;
|
|
|
35 |
body: JSON.stringify({
|
36 |
prompt,
|
37 |
...(html === defaultHTML ? {} : { html }),
|
38 |
+
...(previousPrompt ? { previousPrompt } : {}),
|
39 |
}),
|
40 |
headers: {
|
41 |
"Content-Type": "application/json",
|
|
|
61 |
if (done) {
|
62 |
toast.success("AI responded successfully");
|
63 |
setPrompt("");
|
64 |
+
setPreviousPrompt(prompt);
|
65 |
setisAiWorking(false);
|
66 |
setHasAsked(true);
|
67 |
return;
|