wuhp commited on
Commit
0c740cf
·
verified ·
1 Parent(s): fa6da58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -17
app.py CHANGED
@@ -166,7 +166,6 @@ def add_bot_message(history: list[dict], bot_message: str) -> list[dict]:
166
  history.append({"role": "assistant", "content": bot_message})
167
  return history
168
 
169
- # Modified function signature to accept *args and **kwargs
170
  def ai_workflow_chat(
171
  message: str,
172
  history: list[dict],
@@ -225,15 +224,15 @@ def ai_workflow_chat(
225
  # --- State Machine Logic ---
226
 
227
  if state == STATE_IDLE:
228
- # Check prerequisites first
229
  if not (hf_profile and hf_token):
230
- history = add_bot_message(history, "Please log in to Hugging Face first.")
231
  yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
232
- return
233
  if not (gemini_api_key and gemini_model):
234
- history = add_bot_message(history, "Please enter your API key and select a Gemini model.")
235
  yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
236
- return
237
 
238
  # Look for commands
239
  reset_match = "reset" in message.lower()
@@ -379,7 +378,7 @@ Return **only** the python code block for app.py. Do not include any extra text,
379
 
380
  reqs_content = "\n".join(reqs_list) + "\n"
381
 
382
- history = add_bot_message(history, "✅ `requirements.txt` generated. Click 'Send' to upload.")
383
  state = STATE_UPLOADING_REQUIREMENTS
384
  generated_code = reqs_content
385
  yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
@@ -546,7 +545,6 @@ Return **only** the python code block for app.py. Do not include any extra text,
546
  yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
547
 
548
  elif state == STATE_COMPLETE:
549
- # Workflow finished. Stay in this state until reset.
550
  pass
551
 
552
 
@@ -613,7 +611,6 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
613
  inputs=[gemini_key, gemini_model],
614
  outputs=[gemini_status]
615
  )
616
- # These should trigger configure_gemini whenever the relevant state changes
617
  gemini_key.change(
618
  configure_gemini,
619
  inputs=[gemini_key, gemini_model],
@@ -625,7 +622,6 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
625
  outputs=[gemini_status]
626
  )
627
 
628
-
629
  gr.Markdown("## Space SDK")
630
  sdk_selector = gr.Radio(choices=["gradio","streamlit"], value="gradio", label="Template SDK", interactive=True)
631
  sdk_selector.change(lambda s: s, inputs=sdk_selector, outputs=sdk_state)
@@ -634,6 +630,11 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
634
  status_text = gr.Textbox(label="Current State", value=STATE_IDLE, interactive=False)
635
  repo_id_text = gr.Textbox(label="Current Space ID", value="None", interactive=False)
636
 
 
 
 
 
 
637
  # Main content
638
  with gr.Column(scale=3):
639
  chatbot = gr.Chatbot(type='messages', label="AI Workflow Chat")
@@ -641,44 +642,65 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
641
  send_btn = gr.Button("Send", interactive=False)
642
 
643
  # Logic to enable send button only when logged in and API key is set
644
- # Modified function signature to accept *args and **kwargs
645
  def update_send_button_state(profile: gr.OAuthProfile | None, token: gr.OAuthToken | None, key: str | None, model: str | None, *args, **kwargs):
646
  """
647
  Gradio sometimes passes extra positional/keyword args (old_value, event, etc.),
648
  so we absorb them with *args/**kwargs.
 
649
  """
650
  is_logged_in = profile is not None and token is not None
651
  is_gemini_ready = key is not None and model is not None
652
- return gr.update(interactive=is_logged_in and is_gemini_ready)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
653
 
654
  # Bind update_send_button_state to the *change* of the relevant state variables
655
  # This ensures it's called with the correct 4 state values plus any extras
656
  hf_profile.change(
657
  update_send_button_state,
658
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
659
- outputs=[send_btn]
660
  )
661
  hf_token.change(
662
  update_send_button_state,
663
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
664
- outputs=[send_btn]
665
  )
666
  gemini_key.change(
667
  update_send_button_state,
668
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
669
- outputs=[send_btn]
670
  )
671
  gemini_model.change(
672
  update_send_button_state,
673
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
674
- outputs=[send_btn]
675
  )
676
 
677
  # Keep the initial load trigger as well
678
  ai_builder_tab.load(
679
  update_send_button_state,
680
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
681
- outputs=[send_btn]
682
  )
683
 
684
 
 
166
  history.append({"role": "assistant", "content": bot_message})
167
  return history
168
 
 
169
  def ai_workflow_chat(
170
  message: str,
171
  history: list[dict],
 
224
  # --- State Machine Logic ---
225
 
226
  if state == STATE_IDLE:
227
+ # Check prerequisites first within the state logic as well
228
  if not (hf_profile and hf_token):
229
+ history = add_bot_message(history, "Workflow paused: Please log in to Hugging Face first.")
230
  yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
231
+ return # Stop workflow until login
232
  if not (gemini_api_key and gemini_model):
233
+ history = add_bot_message(history, "Workflow paused: Please enter your API key and select a Gemini model.")
234
  yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
235
+ return # Stop workflow until API key/model set
236
 
237
  # Look for commands
238
  reset_match = "reset" in message.lower()
 
378
 
379
  reqs_content = "\n".join(reqs_list) + "\n"
380
 
381
+ history = add_bot_message(history, "✅ `requirements.txt` generated. Click 'Send' to generate README.")
382
  state = STATE_UPLOADING_REQUIREMENTS
383
  generated_code = reqs_content
384
  yield history, repo_id, state, updated_preview, updated_run, updated_build, attempts, app_desc, repo_name, generated_code
 
545
  yield history, None, STATE_IDLE, updated_preview, updated_run, updated_build, 0, None, None, None
546
 
547
  elif state == STATE_COMPLETE:
 
548
  pass
549
 
550
 
 
611
  inputs=[gemini_key, gemini_model],
612
  outputs=[gemini_status]
613
  )
 
614
  gemini_key.change(
615
  configure_gemini,
616
  inputs=[gemini_key, gemini_model],
 
622
  outputs=[gemini_status]
623
  )
624
 
 
625
  gr.Markdown("## Space SDK")
626
  sdk_selector = gr.Radio(choices=["gradio","streamlit"], value="gradio", label="Template SDK", interactive=True)
627
  sdk_selector.change(lambda s: s, inputs=sdk_selector, outputs=sdk_state)
 
630
  status_text = gr.Textbox(label="Current State", value=STATE_IDLE, interactive=False)
631
  repo_id_text = gr.Textbox(label="Current Space ID", value="None", interactive=False)
632
 
633
+ # --- Added Debugging Indicator ---
634
+ gr.Markdown("## Prerequisite Status")
635
+ prereq_status = gr.Markdown("Checking...")
636
+
637
+
638
  # Main content
639
  with gr.Column(scale=3):
640
  chatbot = gr.Chatbot(type='messages', label="AI Workflow Chat")
 
642
  send_btn = gr.Button("Send", interactive=False)
643
 
644
  # Logic to enable send button only when logged in and API key is set
 
645
  def update_send_button_state(profile: gr.OAuthProfile | None, token: gr.OAuthToken | None, key: str | None, model: str | None, *args, **kwargs):
646
  """
647
  Gradio sometimes passes extra positional/keyword args (old_value, event, etc.),
648
  so we absorb them with *args/**kwargs.
649
+ Returns Gradio.update for the button's interactive state and a status string.
650
  """
651
  is_logged_in = profile is not None and token is not None
652
  is_gemini_ready = key is not None and model is not None
653
+
654
+ # Print states for debugging
655
+ print(f"update_send_button_state called:")
656
+ print(f" Profile: {profile.username if profile else None}")
657
+ print(f" Token: {'Set' if token else 'None'}")
658
+ print(f" API Key: {'Set' if key else 'None'}")
659
+ print(f" Model: {model}")
660
+ print(f" Logged in: {is_logged_in}, Gemini Ready: {is_gemini_ready}")
661
+
662
+ status_str = ""
663
+ if is_logged_in and is_gemini_ready:
664
+ status_str = "✅ Ready to send commands."
665
+ elif not is_logged_in:
666
+ status_str = "⚠️ Please log in to Hugging Face."
667
+ elif not is_gemini_ready:
668
+ if not key: status_str += "⚠️ Gemini API key not set. "
669
+ if not model: status_str += "⚠️ Gemini model not selected."
670
+ status_str = status_str.strip()
671
+
672
+
673
+ return gr.update(interactive=is_logged_in and is_gemini_ready), status_str
674
+
675
 
676
  # Bind update_send_button_state to the *change* of the relevant state variables
677
  # This ensures it's called with the correct 4 state values plus any extras
678
  hf_profile.change(
679
  update_send_button_state,
680
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
681
+ outputs=[send_btn, prereq_status] # Update button and status indicator
682
  )
683
  hf_token.change(
684
  update_send_button_state,
685
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
686
+ outputs=[send_btn, prereq_status]
687
  )
688
  gemini_key.change(
689
  update_send_button_state,
690
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
691
+ outputs=[send_btn, prereq_status]
692
  )
693
  gemini_model.change(
694
  update_send_button_state,
695
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
696
+ outputs=[send_btn, prereq_status]
697
  )
698
 
699
  # Keep the initial load trigger as well
700
  ai_builder_tab.load(
701
  update_send_button_state,
702
  inputs=[hf_profile, hf_token, gemini_key, gemini_model],
703
+ outputs=[send_btn, prereq_status] # Update button and status indicator
704
  )
705
 
706