Spaces:
Running
Running
File size: 3,878 Bytes
9c9e5d3 7da13d6 9c9e5d3 d4febae e781b23 9c9e5d3 e781b23 9c9e5d3 e781b23 9c9e5d3 296b17c 7510941 7da13d6 7510941 296b17c e781b23 9c9e5d3 e781b23 9c9e5d3 |
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 144 145 146 147 148 |
import express from 'express'
import { createSpace } from './createSpace.mts'
import { generateFiles } from './generateFiles.mts'
const app = express()
const port = 3000
const minPromptSize = 16 // if you change this, you will need to also change in public/index.html
const timeoutInSec = 15 * 60
console.log('timeout set to 30 minutes')
app.use(express.static('public'))
const pending: {
total: number;
queue: string[];
} = {
total: 0,
queue: [],
}
const endRequest = (id: string, reason: string) => {
if (!id || !pending.queue.includes(id)) {
return
}
pending.queue = pending.queue.filter(i => i !== id)
console.log(`request ${id} ended (${reason})`)
}
app.get('/debug', (req, res) => {
res.write(JSON.stringify({
nbTotal: pending.total,
nbPending: pending.queue.length,
queue: pending.queue,
}))
res.end()
})
app.get('/app', async (req, res) => {
if (`${req.query.prompt}`.length < minPromptSize) {
res.write(`prompt too short, please enter at least ${minPromptSize} characters`)
res.end()
return
}
const token = `${req.query.token}`
if (!token.startsWith("hf_")) {
res.write(`the provided token seems to be invalid`)
res.end()
return
}
/*
res.write(`<!doctype html>
<script src="/markdown-to-html.js"></script>
<div id="formatted-markdown"></div>
<script>
setInterval(
function fn() {
try {
var input = document.getElementById("raw-markdown-stream")
var output = document.getElementById("formatted-markdown")
output.innerHTML = MarkdownToHtml.parse(input.innerHTML)
} catch (err) {
console.error(err)
}
},
1000
)
</script>
<div id="raw-markdown-stream" style="display: none">
`)
*/
const id = `${pending.total++}`
console.log(`new request ${id}`)
pending.queue.push(id)
req.on('close', function() {
endRequest(id, 'browser asked to end the connection')
})
setTimeout(() => {
endRequest(id, `timed out after ${timeoutInSec}s`)
}, timeoutInSec * 1000)
let nbAttempts = 3
let files = []
while (nbAttempts-- > 0) {
files = await generateFiles(
`${req.query.prompt ||Β ""}`,
token,
(chunk: string) => {
res.write(chunk)
// return true here as long as our request is still valid
// but if the user disconnected, the id will be removed from the queue,
// and we will return false, indicating to generateFiles that we should abort
return pending.queue.includes(id)
})
if (files.length) {
console.log(`seems like we have ${files.length} files`)
break
}
}
if (files.length > 0) {
console.log("files:", JSON.stringify(files, null, 2))
const spaceInfo = await createSpace(files, token)
// Send the space URL back to the frontend
if (spaceInfo && spaceInfo.username && spaceInfo.slug) {
// Determine SDK type using the same logic as in createSpace.mts
let sdk;
if (files.some(file => file.path.includes("Dockerfile"))) {
sdk = "docker";
} else if (files.some(file => file.path.includes("app.py"))) {
sdk = "streamlit";
} else {
sdk = "static";
}
// Only static SDK spaces use the static.hf.space domain
const domain = sdk === "static" ? "static.hf.space" : "hf.space";
const spaceUrl = `https://${spaceInfo.username}-${spaceInfo.slug}.${domain}`
// Add clear markers and spacing to make the space URL easier to detect
res.write(`\n\n<!-- SPACE URL MARKER -->\n<space-url>${spaceUrl}</space-url>\n<!-- END SPACE URL MARKER -->\n\n`)
console.log(`Created ${sdk} space: ${spaceUrl}`)
}
}
// res.write(JSON.stringify(files, null, 2))
// res.write(`</div>`)
res.end()
})
app.listen(port, () => { console.log(`Open http://localhost:${port}`) })
|