jbilcke-hf HF Staff commited on
Commit
296b17c
Β·
1 Parent(s): ee753ca

working on an improved version

Browse files
CLAUDE.md ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # CLAUDE.md
2
+
3
+ This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4
+
5
+ ## Commands
6
+ - Build: `npm start` (executes `node --loader ts-node/esm src/index.mts`)
7
+ - Test: `npm test` (executes `node --loader ts-node/esm src/test.mts`)
8
+ - Docker: `npm run docker` (builds and runs the Docker image)
9
+
10
+ ## Code Style
11
+ - Use TypeScript with .mts extension for all files
12
+ - Import statements at the top, sorted alphabetically
13
+ - Use interface for type definitions with explicit types
14
+ - camelCase for variables and functions
15
+ - Use explicit types for function parameters
16
+ - Use arrow functions for callbacks and methods
17
+ - Prefer const over let, avoid var
18
+ - Handle errors with try/catch blocks
19
+ - Use template literals for string interpolation
20
+ - Document complex logic with inline comments
21
+ - Use early returns for guard clauses
22
+ - Filter/map/reduce for array operations instead of loops when possible
23
+ - Export functions and types using named exports
public/index.html CHANGED
@@ -87,20 +87,26 @@
87
  <iframe
88
  id="iframe"
89
  class="border-none w-full md:min-h-screen"
90
- :src="!open
91
- ? '/placeholder.html'
92
- : `/app?prompt=${encodeURIComponent(prompt)}&token=${encodeURIComponent(token)}`
93
- "
94
  ></iframe>
95
 
96
  <div
97
- x-show="state !== 'stopped'"
98
  class="flex w-full -mt-20 items-end justify-center pointer-events-none">
99
  <div class="flex flex-row py-3 px-8 text-center bg-stone-200 text-stone-600 rounded-md shadow-md">
100
  <div class="animate-bounce duration-150 mr-1">πŸ€–</div>
101
  <div>Generating your app..</div>
102
  </div>
103
  </div>
 
 
 
 
 
 
 
 
 
104
  </div>
105
  </div>
106
  <script>
@@ -155,16 +161,34 @@
155
  timeoutInSec: 15, // time before we determine something went wrong
156
  state: "stopped",
157
  lastTokenAt: +new Date(),
 
158
  saveToken(token) {
159
  localStorage.setItem(storageKey, token)
160
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
161
  init() {
162
  setInterval(() => {
163
  if (this.state === "stopped") {
164
  this.lastTokenAt = +new Date();
165
  return;
166
  }
167
- const html = document?.getElementById("iframe")?.contentWindow?.document?.documentElement?.outerHTML;
 
168
  const size = Number(html?.length); // count how many characters we have generated
169
 
170
  if (isNaN(size) || !isFinite(size)) {
@@ -181,6 +205,14 @@
181
 
182
  if (hasChanged) {
183
  this.lastTokenAt = now;
 
 
 
 
 
 
 
 
184
  }
185
 
186
  this.size = newSize;
 
87
  <iframe
88
  id="iframe"
89
  class="border-none w-full md:min-h-screen"
90
+ :src="getIframeSource()"
 
 
 
91
  ></iframe>
92
 
93
  <div
94
+ x-show="state !== 'stopped' && !spaceUrl"
95
  class="flex w-full -mt-20 items-end justify-center pointer-events-none">
96
  <div class="flex flex-row py-3 px-8 text-center bg-stone-200 text-stone-600 rounded-md shadow-md">
97
  <div class="animate-bounce duration-150 mr-1">πŸ€–</div>
98
  <div>Generating your app..</div>
99
  </div>
100
  </div>
101
+
102
+ <div
103
+ x-show="state === 'stopped' && spaceUrl"
104
+ class="flex w-full -mt-20 items-end justify-center pointer-events-none">
105
+ <div class="flex flex-row py-3 px-8 text-center bg-green-200 text-green-800 rounded-md shadow-md">
106
+ <div class="mr-1">πŸš€</div>
107
+ <div>Space created successfully!</div>
108
+ </div>
109
+ </div>
110
  </div>
111
  </div>
112
  <script>
 
161
  timeoutInSec: 15, // time before we determine something went wrong
162
  state: "stopped",
163
  lastTokenAt: +new Date(),
164
+ spaceUrl: null,
165
  saveToken(token) {
166
  localStorage.setItem(storageKey, token)
167
  },
168
+ getIframeSource() {
169
+ if (!this.open) {
170
+ return '/placeholder.html';
171
+ }
172
+
173
+ if (this.spaceUrl) {
174
+ return this.spaceUrl;
175
+ }
176
+
177
+ return `/app?prompt=${encodeURIComponent(this.prompt)}&token=${encodeURIComponent(this.token)}`;
178
+ },
179
+ checkForSpaceUrl(html) {
180
+ if (!html) return null;
181
+ const match = html.match(/<space-url>(.*?)<\/space-url>/);
182
+ return match ? match[1] : null;
183
+ },
184
  init() {
185
  setInterval(() => {
186
  if (this.state === "stopped") {
187
  this.lastTokenAt = +new Date();
188
  return;
189
  }
190
+ const iframeDocument = document?.getElementById("iframe")?.contentWindow?.document;
191
+ const html = iframeDocument?.documentElement?.outerHTML;
192
  const size = Number(html?.length); // count how many characters we have generated
193
 
194
  if (isNaN(size) || !isFinite(size)) {
 
205
 
206
  if (hasChanged) {
207
  this.lastTokenAt = now;
208
+
209
+ // Check if the space URL is in the response
210
+ const detectedSpaceUrl = this.checkForSpaceUrl(html);
211
+ if (detectedSpaceUrl) {
212
+ console.log("Space URL detected:", detectedSpaceUrl);
213
+ this.spaceUrl = detectedSpaceUrl;
214
+ this.state = "stopped";
215
+ }
216
  }
217
 
218
  this.size = newSize;
src/createSpace.mts CHANGED
@@ -57,4 +57,11 @@ export const createSpace = async (files: RepoFile[], token: string) => {
57
  // TODO we should keep track of the repo and delete it after 30 min
58
  // or delete it if we reached 20 repos
59
  // await deleteRepo({ repo, credentials })
 
 
 
 
 
 
 
60
  }
 
57
  // TODO we should keep track of the repo and delete it after 30 min
58
  // or delete it if we reached 20 repos
59
  // await deleteRepo({ repo, credentials })
60
+
61
+ // Return information about the created space
62
+ return {
63
+ username: username.toLowerCase(),
64
+ slug: slug.toLowerCase(),
65
+ title
66
+ }
67
  }
src/getFlutterApp.mts CHANGED
@@ -21,6 +21,8 @@ There is already a project with this structure:
21
  - pubspec.yaml
22
  - cooking_llama.iml
23
 
 
 
24
 
25
  Don't forget to write a README.md with the following header, or else you will be FIRED:
26
  \`\`\`
 
21
  - pubspec.yaml
22
  - cooking_llama.iml
23
 
24
+ DO NOT WRITE AN EXAMPLE! WRITE THE FULL CODE, NOT AN EXAMPLE.
25
+ You must not leave any TODO in the code.
26
 
27
  Don't forget to write a README.md with the following header, or else you will be FIRED:
28
  \`\`\`
src/getGradioApp.mts CHANGED
@@ -16,6 +16,9 @@ export function getGradioApp(prompt: string) {
16
  role: "user",
17
  content: `Please write, file by file, the source code for a Gradio project.
18
 
 
 
 
19
  You MUST use the following Python modules:
20
  - gradio (version 5.23.3)
21
  - torch (version 2.6.0)
 
16
  role: "user",
17
  content: `Please write, file by file, the source code for a Gradio project.
18
 
19
+ DO NOT WRITE AN EXAMPLE! WRITE THE FULL CODE, NOT AN EXAMPLE.
20
+ You must not leave any TODO in the code.
21
+
22
  You MUST use the following Python modules:
23
  - gradio (version 5.23.3)
24
  - torch (version 2.6.0)
src/getReactApp.mts CHANGED
@@ -63,6 +63,9 @@ Important rules:
63
  - you need to leave: "sdk: docker" as-is, but replace: "<APPNAME>" with an actual name, please.
64
  - Don't forget to write a valid package.json file!
65
 
 
 
 
66
  Remember, the app is about: ${prompt}.
67
 
68
  Remember: don't forget to define the README.md and the package.json file!`,
 
63
  - you need to leave: "sdk: docker" as-is, but replace: "<APPNAME>" with an actual name, please.
64
  - Don't forget to write a valid package.json file!
65
 
66
+ DO NOT WRITE AN EXAMPLE! WRITE THE FULL CODE, NOT AN EXAMPLE.
67
+ You must not leave any TODO in the code.
68
+
69
  Remember, the app is about: ${prompt}.
70
 
71
  Remember: don't forget to define the README.md and the package.json file!`,
src/getStreamlitApp.mts CHANGED
@@ -16,12 +16,19 @@ export function getStreamlitApp(prompt: string) {
16
  role: "user",
17
  content: `Please write, file by file, the source code for a Streamlit app.
18
 
19
- Please limit yourself to the following Python modules:
 
 
 
 
 
 
 
20
  - numpy
21
  - matplotlib
22
- - torch
23
  - diffusers
24
  - transformers
 
25
 
26
  Don't forget to write a README.md with the following header:
27
  \`\`\`
 
16
  role: "user",
17
  content: `Please write, file by file, the source code for a Streamlit app.
18
 
19
+ DO NOT WRITE AN EXAMPLE! WRITE THE FULL CODE, NOT AN EXAMPLE.
20
+ You must not leave any TODO in the code.
21
+
22
+ You MUST use the following Python modules:
23
+ - torch (version 2.6.0)
24
+ - accelerate (version 1.6.0)
25
+
26
+ You are free to use (if necessary) the following Python modules. In tha case, don't specify a version, just use them as-is so it uses the latest one. Make sure to add them to the requirements.txt:
27
  - numpy
28
  - matplotlib
 
29
  - diffusers
30
  - transformers
31
+ - huggingface_hub
32
 
33
  Don't forget to write a README.md with the following header:
34
  \`\`\`
src/getWebApp.mts CHANGED
@@ -32,7 +32,8 @@ Some remarks:
32
  - DO NOT USE VUE.JS
33
 
34
  Remember, you need to write the index.html but also the app.js and/or the style.css files!
35
- DO NOT WRITE AN EXAMPLE! WRITE THE FULL CODE!!
 
36
 
37
  Don't forget to write a README.md with the following header:
38
  \`\`\`
 
32
  - DO NOT USE VUE.JS
33
 
34
  Remember, you need to write the index.html but also the app.js and/or the style.css files!
35
+ DO NOT WRITE AN EXAMPLE! WRITE THE FULL CODE, NOT AN EXAMPLE.
36
+ You must not leave any TODO in the code.
37
 
38
  Don't forget to write a README.md with the following header:
39
  \`\`\`
src/index.mts CHANGED
@@ -113,7 +113,14 @@ app.get('/app', async (req, res) => {
113
  if (files.length > 0) {
114
  console.log("files:", JSON.stringify(files, null, 2))
115
 
116
- await createSpace(files, token)
 
 
 
 
 
 
 
117
  }
118
 
119
  // res.write(JSON.stringify(files, null, 2))
 
113
  if (files.length > 0) {
114
  console.log("files:", JSON.stringify(files, null, 2))
115
 
116
+ const spaceInfo = await createSpace(files, token)
117
+
118
+ // Send the space URL back to the frontend
119
+ if (spaceInfo && spaceInfo.username && spaceInfo.slug) {
120
+ const spaceUrl = `https://${spaceInfo.username}-${spaceInfo.slug}.hf.space`
121
+ res.write(`\n\n<space-url>${spaceUrl}</space-url>`)
122
+ console.log(`Created space: ${spaceUrl}`)
123
+ }
124
  }
125
 
126
  // res.write(JSON.stringify(files, null, 2))