wuhp commited on
Commit
680f26f
Β·
verified Β·
1 Parent(s): 0dfd0d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -5
app.py CHANGED
@@ -22,6 +22,8 @@ def show_profile(profile: gr.OAuthProfile | None) -> str:
22
  """Displays the logged-in Hugging Face profile username."""
23
  if profile is None:
24
  return "*Not logged in.*"
 
 
25
  return f"βœ… Logged in as **{profile.username}**"
26
 
27
  def list_private_models(
@@ -303,8 +305,10 @@ def ai_workflow_chat(
303
  if state == STATE_IDLE:
304
  # Check prerequisites before starting any workflow actions
305
  # Use the passed-in state variable values for checks
 
 
306
  if not (hf_profile and hf_token):
307
- history = add_bot_message(history, "Workflow paused: Please log in to Hugging Face first.")
308
  # Yield updated history and current state, then exit for this click
309
  yield (history, repo_id, state, updated_preview, updated_run, updated_build,
310
  attempts, app_desc, repo_name, generated_code, use_grounding)
@@ -312,12 +316,13 @@ def ai_workflow_chat(
312
 
313
  if not (gemini_api_key and gemini_model):
314
  # The button is *enabled* if HF is logged in, but we still need Gemini for the workflow
315
- history = add_bot_message(history, "Workflow cannot start: Please enter your API key and select a Gemini model.")
316
  # Yield updated history and current state, then exit for this click
317
  yield (history, repo_id, state, updated_preview, updated_run, updated_build,
318
  attempts, app_desc, repo_name, generated_code, use_grounding)
319
  return # Exit the generator for this click
320
 
 
321
  # Look for specific commands in the user's message
322
  reset_match = "reset" in message.lower()
323
  # Capture app description AND repo name using regex
@@ -525,7 +530,6 @@ Return **only** the python code block for `app.py`. Do not include any extra tex
525
  reqs_list = ["gradio"] if space_sdk == "gradio" else ["streamlit"]
526
  # Add essential libraries regardless of description keywords or grounding
527
  essential_libs = ["google-generativeai", "huggingface_hub"]
528
- # Only add if Gemini is actually needed for the app (determined by description or if key is present)
529
  # If we are here, key and model are available based on STATE_IDLE checks
530
  reqs_list.extend(essential_libs)
531
 
@@ -628,7 +632,7 @@ This Space was automatically generated by an AI workflow using Google Gemini and
628
  history = add_bot_message(history, "βœ… `README.md` generated. Click 'Send' to upload.")
629
  state = STATE_UPLOADING_README # Transition state
630
  generated_code = readme_content # Store README content
631
- # Yield updated state variables and history (pass UI outputs and other states through)
632
  yield (history, repo_id, state, updated_preview, updated_run, updated_build,
633
  attempts, app_desc, repo_name, generated_code, use_grounding)
634
  # No return needed
@@ -655,7 +659,7 @@ This Space was automatically generated by an AI workflow using Google Gemini and
655
  history = add_bot_message(history, "βœ… Uploaded `README.md`. All files uploaded. Space is now building. Click 'Send' to check build logs.")
656
  state = STATE_CHECKING_LOGS_BUILD # Transition to checking build logs
657
  generated_code = None # Clear content after use
658
- # Yield updated state variables and history (pass UI outputs and other states through)
659
  yield (history, repo_id, state, updated_preview, updated_run, updated_build,
660
  attempts, app_desc, repo_name, generated_code, use_grounding)
661
  # No return needed
@@ -906,6 +910,8 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
906
  status_text = gr.Textbox(label="Current State", value=STATE_IDLE, interactive=False)
907
  repo_id_text = gr.Textbox(label="Current Space ID", value="None", interactive=False)
908
 
 
 
909
 
910
  # Main content area column
911
  with gr.Column(scale=3):
@@ -925,6 +931,7 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
925
  # --- Define Event Handlers and Chains AFTER all components are defined ---
926
 
927
  # Helper function to update send button interactivity based on prereqs
 
928
  def update_send_button_interactivity(profile, token, api_key, model_name):
929
  is_logged_in = profile is not None and token is not None
930
  is_gemini_ready = api_key is not None and model_name is not None
@@ -935,11 +942,13 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
935
 
936
  # Handle login button click: Update profile/token state -> Update send button interactivity
937
  login_btn.click(
 
938
  lambda x: (x[0], x[1]),
939
  inputs=[login_btn],
940
  outputs=[hf_profile, hf_token] # Update HF State variables
941
  ).then(
942
  update_send_button_interactivity,
 
943
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
944
  outputs=[send_btn] # Update button interactivity
945
  )
@@ -951,6 +960,7 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
951
  configure_gemini, inputs=[gemini_key, gemini_model], outputs=[gemini_status] # Update Gemini status
952
  ).then(
953
  update_send_button_interactivity,
 
954
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
955
  outputs=[send_btn] # Update button interactivity
956
  )
@@ -962,6 +972,7 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
962
  configure_gemini, inputs=[gemini_key, gemini_model], outputs=[gemini_status] # Update Gemini status
963
  ).then(
964
  update_send_button_interactivity,
 
965
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
966
  outputs=[send_btn] # Update button interactivity
967
  )
 
22
  """Displays the logged-in Hugging Face profile username."""
23
  if profile is None:
24
  return "*Not logged in.*"
25
+ # More robust check: ensure token is also present for "logged in" status for actions
26
+ # However, the LoginButton itself updates profile/token state. Let's rely on the State check.
27
  return f"βœ… Logged in as **{profile.username}**"
28
 
29
  def list_private_models(
 
305
  if state == STATE_IDLE:
306
  # Check prerequisites before starting any workflow actions
307
  # Use the passed-in state variable values for checks
308
+ # Note: Button interactivity should prevent reaching here if HF login or Gemini isn't ready
309
+ # But this check is a safeguard.
310
  if not (hf_profile and hf_token):
311
+ history = add_bot_message(history, "Workflow paused: Hugging Face login is required.")
312
  # Yield updated history and current state, then exit for this click
313
  yield (history, repo_id, state, updated_preview, updated_run, updated_build,
314
  attempts, app_desc, repo_name, generated_code, use_grounding)
 
316
 
317
  if not (gemini_api_key and gemini_model):
318
  # The button is *enabled* if HF is logged in, but we still need Gemini for the workflow
319
+ history = add_bot_message(history, "Workflow cannot start: Google AI Studio / Gemini configuration is required.")
320
  # Yield updated history and current state, then exit for this click
321
  yield (history, repo_id, state, updated_preview, updated_run, updated_build,
322
  attempts, app_desc, repo_name, generated_code, use_grounding)
323
  return # Exit the generator for this click
324
 
325
+
326
  # Look for specific commands in the user's message
327
  reset_match = "reset" in message.lower()
328
  # Capture app description AND repo name using regex
 
530
  reqs_list = ["gradio"] if space_sdk == "gradio" else ["streamlit"]
531
  # Add essential libraries regardless of description keywords or grounding
532
  essential_libs = ["google-generativeai", "huggingface_hub"]
 
533
  # If we are here, key and model are available based on STATE_IDLE checks
534
  reqs_list.extend(essential_libs)
535
 
 
632
  history = add_bot_message(history, "βœ… `README.md` generated. Click 'Send' to upload.")
633
  state = STATE_UPLOADING_README # Transition state
634
  generated_code = readme_content # Store README content
635
+ # Yield updated state variables and history (pass UI outputs and states through)
636
  yield (history, repo_id, state, updated_preview, updated_run, updated_build,
637
  attempts, app_desc, repo_name, generated_code, use_grounding)
638
  # No return needed
 
659
  history = add_bot_message(history, "βœ… Uploaded `README.md`. All files uploaded. Space is now building. Click 'Send' to check build logs.")
660
  state = STATE_CHECKING_LOGS_BUILD # Transition to checking build logs
661
  generated_code = None # Clear content after use
662
+ # Yield updated state variables and history (pass UI outputs and states through)
663
  yield (history, repo_id, state, updated_preview, updated_run, updated_build,
664
  attempts, app_desc, repo_name, generated_code, use_grounding)
665
  # No return needed
 
910
  status_text = gr.Textbox(label="Current State", value=STATE_IDLE, interactive=False)
911
  repo_id_text = gr.Textbox(label="Current Space ID", value="None", interactive=False)
912
 
913
+ # Removed prereq_status markdown component
914
+
915
 
916
  # Main content area column
917
  with gr.Column(scale=3):
 
931
  # --- Define Event Handlers and Chains AFTER all components are defined ---
932
 
933
  # Helper function to update send button interactivity based on prereqs
934
+ # Defined outside Block but used inside event handlers
935
  def update_send_button_interactivity(profile, token, api_key, model_name):
936
  is_logged_in = profile is not None and token is not None
937
  is_gemini_ready = api_key is not None and model_name is not None
 
942
 
943
  # Handle login button click: Update profile/token state -> Update send button interactivity
944
  login_btn.click(
945
+ # The LoginButton outputs a tuple (OAuthProfile, OAuthToken) on success
946
  lambda x: (x[0], x[1]),
947
  inputs=[login_btn],
948
  outputs=[hf_profile, hf_token] # Update HF State variables
949
  ).then(
950
  update_send_button_interactivity,
951
+ # Pass required state variables to determine interactivity
952
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
953
  outputs=[send_btn] # Update button interactivity
954
  )
 
960
  configure_gemini, inputs=[gemini_key, gemini_model], outputs=[gemini_status] # Update Gemini status
961
  ).then(
962
  update_send_button_interactivity,
963
+ # Pass required state variables to determine interactivity
964
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
965
  outputs=[send_btn] # Update button interactivity
966
  )
 
972
  configure_gemini, inputs=[gemini_key, gemini_model], outputs=[gemini_status] # Update Gemini status
973
  ).then(
974
  update_send_button_interactivity,
975
+ # Pass required state variables to determine interactivity
976
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
977
  outputs=[send_btn] # Update button interactivity
978
  )