wuhp commited on
Commit
e87ed80
·
verified ·
1 Parent(s): c5c444a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -47
app.py CHANGED
@@ -148,7 +148,7 @@ def handle_user_message(
148
  backoff = min(backoff * 2, 30)
149
  continue
150
 
151
- # write code, README, requirements
152
  sdk_version = get_sdk_version(sdk_choice)
153
  files = {
154
  code_fn: code,
@@ -181,13 +181,13 @@ See https://huggingface.co/docs/hub/spaces-config-reference
181
  run_logs = fetch_logs(repo_id, "run")
182
  errors = classify_errors(build_logs + "\n" + run_logs)
183
 
184
- # success criteria
185
  if "ERROR" not in build_logs.upper() and \
186
  "ERROR" not in run_logs.upper() and \
187
  check_iframe(iframe_url):
188
  break
189
 
190
- # ask for a fix
191
  chat.append({
192
  "role":"user",
193
  "content":(
@@ -200,13 +200,12 @@ See https://huggingface.co/docs/hub/spaces-config-reference
200
  time.sleep(backoff)
201
  backoff = min(backoff * 2, 30)
202
 
203
- messages = [{"role":m["role"], "content":m["content"]} for m in chat if m["role"]!="system"]
204
  iframe_html = (
205
  f'<iframe src="{iframe_url}" width="100%" height="500px"></iframe>'
206
  + ("" if check_iframe(iframe_url) else
207
  "<p style='color:red;'>⚠️ iframe not responding.</p>")
208
  )
209
-
210
  return messages, build_logs, run_logs, iframe_html
211
 
212
  # — BUILD THE UI (Improved) —
@@ -218,30 +217,21 @@ custom_css = """
218
 
219
  with gr.Blocks(title="HF Space Auto‑Builder", css=custom_css) as demo:
220
  with gr.Row():
221
- # Sidebar
222
  with gr.Column(scale=1, min_width=250):
223
  login_btn = gr.LoginButton(variant="huggingface", size="sm")
224
  status_md = gr.Markdown("*Not logged in.*")
225
  models_md = gr.Markdown()
226
- sdk_choice = gr.Radio(
227
- ["gradio", "streamlit"],
228
- value="gradio",
229
- label="SDK template"
230
- )
231
- api_key = gr.Textbox(label="Gemini API Key", type="password")
232
- grounding = gr.Checkbox(label="Enable grounding", value=False)
233
- temp = gr.Slider(0, 1, value=0.2, label="LLM temperature")
234
- max_tokens = gr.Slider(256, 4096, value=1024, step=256, label="Max tokens")
235
- clear_btn = gr.Button("Clear Chat", variant="secondary")
236
 
237
- # Main area
238
  with gr.Column(scale=3):
239
  gr.Markdown("## 🐢 Wuhp Auto‑Builder\nSign in, enter your prompt, and watch your Space live.")
240
  chatbot = gr.Chatbot(elem_id="chatbot", type="messages")
241
- user_in = gr.Textbox(
242
- placeholder="Type your prompt and hit Enter…",
243
- label="Prompt", lines=1
244
- )
245
  send_btn = gr.Button("Send", variant="primary")
246
 
247
  with gr.Tabs():
@@ -249,35 +239,31 @@ with gr.Blocks(title="HF Space Auto‑Builder", css=custom_css) as demo:
249
  pass
250
  with gr.TabItem("Logs"):
251
  with gr.Accordion("🔨 Build Logs", open=True):
252
- build_box = gr.Textbox(
253
- lines=8, interactive=False, elem_classes="log-box"
254
- )
255
  with gr.Accordion("🚀 Run Logs", open=False):
256
- run_box = gr.Textbox(
257
- lines=8, interactive=False, elem_classes="log-box"
258
- )
259
  with gr.TabItem("Preview"):
260
  preview = gr.HTML("<p>No Space yet.</p>")
261
 
262
- # Wiring callbacks
263
  demo.load(show_profile, inputs=None, outputs=status_md)
264
  demo.load(list_private_models, inputs=None, outputs=models_md)
265
  login_btn.click(show_profile, inputs=None, outputs=status_md)
266
  login_btn.click(list_private_models, inputs=None, outputs=models_md)
267
 
268
- # Wrap both click and submit in a spinner
269
- with gr.Spinner():
270
- send_btn.click(
271
- fn=handle_user_message,
272
- inputs=[chatbot, sdk_choice, api_key, grounding, temp, max_tokens, status_md, models_md],
273
- outputs=[chatbot, build_box, run_box, preview]
274
- )
275
- user_in.submit(
276
- fn=handle_user_message,
277
- inputs=[chatbot, sdk_choice, api_key, grounding, temp, max_tokens, status_md, models_md],
278
- outputs=[chatbot, build_box, run_box, preview]
279
- )
280
 
 
281
  def _clear_chat():
282
  return [], "", "", "<p>No Space yet.</p>"
283
  clear_btn.click(
@@ -286,13 +272,13 @@ with gr.Blocks(title="HF Space Auto‑Builder", css=custom_css) as demo:
286
  outputs=[chatbot, build_box, run_box, preview]
287
  )
288
 
289
- # Refresh Logs
290
  refresh_btn = gr.Button("Refresh Logs", variant="secondary", size="sm")
291
- def _refresh(profile, oauth_token):
292
- if not profile or not oauth_token:
293
- return "", ""
294
- repo = f"{profile.username}/{profile.username}-auto-space"
295
- return fetch_logs(repo, "build"), fetch_logs(repo, "run")
296
- refresh_btn.click(_refresh, inputs=None, outputs=[build_box, run_box])
297
 
298
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
148
  backoff = min(backoff * 2, 30)
149
  continue
150
 
151
+ # write files
152
  sdk_version = get_sdk_version(sdk_choice)
153
  files = {
154
  code_fn: code,
 
181
  run_logs = fetch_logs(repo_id, "run")
182
  errors = classify_errors(build_logs + "\n" + run_logs)
183
 
184
+ # success?
185
  if "ERROR" not in build_logs.upper() and \
186
  "ERROR" not in run_logs.upper() and \
187
  check_iframe(iframe_url):
188
  break
189
 
190
+ # ask for fixes
191
  chat.append({
192
  "role":"user",
193
  "content":(
 
200
  time.sleep(backoff)
201
  backoff = min(backoff * 2, 30)
202
 
203
+ messages = [{"role":m["role"],"content":m["content"]} for m in chat if m["role"]!="system"]
204
  iframe_html = (
205
  f'<iframe src="{iframe_url}" width="100%" height="500px"></iframe>'
206
  + ("" if check_iframe(iframe_url) else
207
  "<p style='color:red;'>⚠️ iframe not responding.</p>")
208
  )
 
209
  return messages, build_logs, run_logs, iframe_html
210
 
211
  # — BUILD THE UI (Improved) —
 
217
 
218
  with gr.Blocks(title="HF Space Auto‑Builder", css=custom_css) as demo:
219
  with gr.Row():
 
220
  with gr.Column(scale=1, min_width=250):
221
  login_btn = gr.LoginButton(variant="huggingface", size="sm")
222
  status_md = gr.Markdown("*Not logged in.*")
223
  models_md = gr.Markdown()
224
+ sdk_choice = gr.Radio(["gradio","streamlit"], value="gradio", label="SDK template")
225
+ api_key = gr.Textbox(label="Gemini API Key", type="password")
226
+ grounding = gr.Checkbox(label="Enable grounding", value=False)
227
+ temp = gr.Slider(0,1,value=0.2,label="LLM temperature")
228
+ max_tokens = gr.Slider(256,4096,value=1024,step=256,label="Max tokens")
229
+ clear_btn = gr.Button("Clear Chat", variant="secondary")
 
 
 
 
230
 
 
231
  with gr.Column(scale=3):
232
  gr.Markdown("## 🐢 Wuhp Auto‑Builder\nSign in, enter your prompt, and watch your Space live.")
233
  chatbot = gr.Chatbot(elem_id="chatbot", type="messages")
234
+ user_in = gr.Textbox(placeholder="Type your prompt and hit Enter…", label="Prompt", lines=1)
 
 
 
235
  send_btn = gr.Button("Send", variant="primary")
236
 
237
  with gr.Tabs():
 
239
  pass
240
  with gr.TabItem("Logs"):
241
  with gr.Accordion("🔨 Build Logs", open=True):
242
+ build_box = gr.Textbox(lines=8, interactive=False, elem_classes="log-box")
 
 
243
  with gr.Accordion("🚀 Run Logs", open=False):
244
+ run_box = gr.Textbox(lines=8, interactive=False, elem_classes="log-box")
 
 
245
  with gr.TabItem("Preview"):
246
  preview = gr.HTML("<p>No Space yet.</p>")
247
 
248
+ # load auth status
249
  demo.load(show_profile, inputs=None, outputs=status_md)
250
  demo.load(list_private_models, inputs=None, outputs=models_md)
251
  login_btn.click(show_profile, inputs=None, outputs=status_md)
252
  login_btn.click(list_private_models, inputs=None, outputs=models_md)
253
 
254
+ # send / submit callbacks (Gradio auto-injects profile & oauth_token)
255
+ send_btn.click(
256
+ fn=handle_user_message,
257
+ inputs=[chatbot, sdk_choice, api_key, grounding, temp, max_tokens],
258
+ outputs=[chatbot, build_box, run_box, preview]
259
+ )
260
+ user_in.submit(
261
+ fn=handle_user_message,
262
+ inputs=[chatbot, sdk_choice, api_key, grounding, temp, max_tokens],
263
+ outputs=[chatbot, build_box, run_box, preview]
264
+ )
 
265
 
266
+ # clear chat
267
  def _clear_chat():
268
  return [], "", "", "<p>No Space yet.</p>"
269
  clear_btn.click(
 
272
  outputs=[chatbot, build_box, run_box, preview]
273
  )
274
 
275
+ # refresh logs (auto-injected profile & oauth_token)
276
  refresh_btn = gr.Button("Refresh Logs", variant="secondary", size="sm")
277
+ refresh_btn.click(
278
+ fn=lambda profile,token: (fetch_logs(f"{profile.username}/{profile.username}-auto-space","build"),
279
+ fetch_logs(f"{profile.username}/{profile.username}-auto-space","run")),
280
+ inputs=None,
281
+ outputs=[build_box, run_box]
282
+ )
283
 
284
  demo.launch(server_name="0.0.0.0", server_port=7860)