wuhp commited on
Commit
32f88a6
·
verified ·
1 Parent(s): b8c1a3d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -29
app.py CHANGED
@@ -319,7 +319,7 @@ def check_send_button_ready(
319
  gemini_key: str | None,
320
  gemini_model: str | None,
321
  workflow_state: WorkflowState # Also depend on workflow state
322
- ) -> gr.Button.update: # Use specific component update
323
  """Checks if HF login and Gemini configuration are complete and returns update for button interactivity."""
324
  # Button should NOT be interactive when workflow is running
325
  if workflow_state != STATE_IDLE and workflow_state != STATE_AWAITING_REPO_NAME:
@@ -342,16 +342,11 @@ def check_send_button_ready(
342
  # They take all necessary inputs from the main generator's arguments
343
  # and return the full tuple of outputs required by the generator's yield signature.
344
 
345
- WorkflowInputs = Tuple[
346
- str, List[Dict[str, str]], Optional[gr.OAuthProfile], Optional[gr.OAuthToken],
347
- Optional[str], Optional[str], Optional[str], WorkflowState, str, str, str, str,
348
- int, Optional[str], Optional[str], Optional[str], bool
349
- ]
350
 
351
- WorkflowOutputs = Tuple[
352
- List[Dict[str, str]], Optional[str], WorkflowState, str, str, str,
353
- int, Optional[str], Optional[str], Optional[str], bool, Optional[str], Optional[str]
354
- ]
355
 
356
  def package_workflow_outputs(
357
  history: List[Dict[str, str]],
@@ -394,7 +389,7 @@ def handle_idle(
394
  use_grounding: bool,
395
  *args, # Catch potential extra args
396
  **kwargs # Catch potential extra kwargs
397
- ) -> WorkflowOutputs:
398
  """Handles logic when in the IDLE state."""
399
  logging.info(f"Handling STATE_IDLE with message: {message[:50]}...")
400
 
@@ -509,7 +504,7 @@ def handle_awaiting_repo_name(
509
  use_grounding: bool,
510
  *args,
511
  **kwargs
512
- ) -> WorkflowOutputs:
513
  """Handles logic when in the AWAITING_REPO_NAME state."""
514
  logging.info(f"Handling STATE_AWAITING_REPO_NAME with message: {message[:50]}...")
515
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -560,7 +555,7 @@ def handle_creating_space(
560
  use_grounding: bool,
561
  *args,
562
  **kwargs
563
- ) -> WorkflowOutputs:
564
  """Handles logic when in the CREATING_SPACE state."""
565
  logging.info(f"Handling STATE_CREATING_SPACE for repo '{repo_name}'")
566
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -620,7 +615,7 @@ def handle_generating_code(
620
  use_grounding: bool,
621
  *args,
622
  **kwargs
623
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
624
  """Handles logic when in the GENERATING_CODE state."""
625
  logging.info("Handling STATE_GENERATING_CODE")
626
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -702,7 +697,7 @@ def handle_uploading_app_py(
702
  use_grounding: bool,
703
  *args,
704
  **kwargs
705
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
706
  """Handles logic when in the UPLOADING_APP_PY state."""
707
  logging.info("Handling STATE_UPLOADING_APP_PY")
708
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -771,7 +766,7 @@ def handle_generating_requirements(
771
  use_grounding: bool,
772
  *args,
773
  **kwargs
774
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
775
  """Handles logic when in the GENERATING_REQUIREMENTS state."""
776
  logging.info("Handling STATE_GENERATING_REQUIREMENTS")
777
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -846,7 +841,7 @@ def handle_uploading_requirements(
846
  use_grounding: bool,
847
  *args,
848
  **kwargs
849
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
850
  """Handles logic when in the UPLOADING_REQUIREMENTS state."""
851
  logging.info("Handling STATE_UPLOADING_REQUIREMENTS")
852
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -915,7 +910,7 @@ def handle_generating_readme(
915
  use_grounding: bool,
916
  *args,
917
  **kwargs
918
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
919
  """Handles logic when in the GENERATING_README state."""
920
  logging.info("Handling STATE_GENERATING_README")
921
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -981,7 +976,7 @@ def handle_uploading_readme(
981
  use_grounding: bool,
982
  *args,
983
  **kwargs
984
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
985
  """Handles logic when in the UPLOADING_README state."""
986
  logging.info("Handling STATE_UPLOADING_README")
987
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -1050,7 +1045,7 @@ def handle_checking_logs_build(
1050
  use_grounding: bool,
1051
  *args,
1052
  **kwargs
1053
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
1054
  """Handles logic when in the CHECKING_LOGS_BUILD state."""
1055
  logging.info(f"Handling STATE_CHECKING_LOGS_BUILD for repo '{repo_id}'")
1056
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -1117,7 +1112,7 @@ def handle_checking_logs_run(
1117
  use_grounding: bool,
1118
  *args,
1119
  **kwargs
1120
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
1121
  """Handles logic when in the CHECKING_LOGS_RUN state."""
1122
  logging.info(f"Handling STATE_CHECKING_LOGS_RUN for repo '{repo_id}'")
1123
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -1200,7 +1195,7 @@ def handle_debugging_code(
1200
  use_grounding: bool,
1201
  *args,
1202
  **kwargs
1203
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
1204
  """Handles logic when in the DEBUGGING_CODE state."""
1205
  logging.info(f"Handling STATE_DEBUGGING_CODE (attempt #{debug_attempts}) for repo '{repo_id}'")
1206
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -1287,7 +1282,7 @@ def handle_uploading_fixed_app_py(
1287
  use_grounding: bool,
1288
  *args,
1289
  **kwargs
1290
- ) -> Generator[WorkflowOutputs, None, WorkflowOutputs]: # Specify return type as Generator
1291
  """Handles logic when in the UPLOADING_FIXED_APP_PY state."""
1292
  logging.info(f"Handling STATE_UPLOADING_FIXED_APP_PY for repo '{repo_id}'")
1293
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -1358,7 +1353,7 @@ def handle_complete(
1358
  use_grounding: bool,
1359
  *args,
1360
  **kwargs
1361
- ) -> WorkflowOutputs:
1362
  """Handles logic when in the COMPLETE state."""
1363
  logging.info("Handling STATE_COMPLETE")
1364
  current_gemini_key = gemini_api_key # Use the input vars directly
@@ -1477,6 +1472,9 @@ def ai_workflow_chat(
1477
  logging.debug(f"Invoking handler for state: {state}")
1478
  # Call the state handler function, passing all necessary data
1479
  # Need to pass *all* inputs to the handler function
 
 
 
1480
  handler_output = handler(
1481
  message=message, history=history,
1482
  hf_profile=hf_profile, hf_token=hf_token,
@@ -1604,12 +1602,13 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
1604
 
1605
  # Use the pattern suggested in the feedback: wire each dependency change to the same handler
1606
  for state_comp in prerequisite_states_for_button:
 
 
 
 
 
 
1607
  state_comp.change(
1608
- # Lambda function receives the new value of the changed component first,
1609
- # followed by the values of the components in 'inputs'.
1610
- # We pass ALL prerequisite states (including the one that changed) to the lambda's inputs.
1611
- # The lambda then passes the *explicitly listed* input values to the target function, check_send_button_ready.
1612
- # This avoids relying on the order of implicit args.
1613
  lambda *args, states=prerequisite_states_for_button: check_send_button_ready(
1614
  states[0], states[1], states[2], states[3], states[4] # Pass values from the 'states' list closure
1615
  ),
 
319
  gemini_key: str | None,
320
  gemini_model: str | None,
321
  workflow_state: WorkflowState # Also depend on workflow state
322
+ ) -> gr.update: # Correct type hint for a Gradio update object
323
  """Checks if HF login and Gemini configuration are complete and returns update for button interactivity."""
324
  # Button should NOT be interactive when workflow is running
325
  if workflow_state != STATE_IDLE and workflow_state != STATE_AWAITING_REPO_NAME:
 
342
  # They take all necessary inputs from the main generator's arguments
343
  # and return the full tuple of outputs required by the generator's yield signature.
344
 
345
+ # Using Any for handler return type simplifies type hints since some yield and some return
346
+ # A more precise type would be Union[WorkflowOutputs, Generator[WorkflowOutputs, None, WorkflowOutputs]]
347
+ # But Gradio's type checking for generators is often loose anyway.
348
+ WorkflowHandlerReturn = Any
 
349
 
 
 
 
 
350
 
351
  def package_workflow_outputs(
352
  history: List[Dict[str, str]],
 
389
  use_grounding: bool,
390
  *args, # Catch potential extra args
391
  **kwargs # Catch potential extra kwargs
392
+ ) -> WorkflowHandlerReturn:
393
  """Handles logic when in the IDLE state."""
394
  logging.info(f"Handling STATE_IDLE with message: {message[:50]}...")
395
 
 
504
  use_grounding: bool,
505
  *args,
506
  **kwargs
507
+ ) -> WorkflowHandlerReturn:
508
  """Handles logic when in the AWAITING_REPO_NAME state."""
509
  logging.info(f"Handling STATE_AWAITING_REPO_NAME with message: {message[:50]}...")
510
  current_gemini_key = gemini_api_key # Use the input vars directly
 
555
  use_grounding: bool,
556
  *args,
557
  **kwargs
558
+ ) -> WorkflowHandlerReturn:
559
  """Handles logic when in the CREATING_SPACE state."""
560
  logging.info(f"Handling STATE_CREATING_SPACE for repo '{repo_name}'")
561
  current_gemini_key = gemini_api_key # Use the input vars directly
 
615
  use_grounding: bool,
616
  *args,
617
  **kwargs
618
+ ) -> WorkflowHandlerReturn:
619
  """Handles logic when in the GENERATING_CODE state."""
620
  logging.info("Handling STATE_GENERATING_CODE")
621
  current_gemini_key = gemini_api_key # Use the input vars directly
 
697
  use_grounding: bool,
698
  *args,
699
  **kwargs
700
+ ) -> WorkflowHandlerReturn:
701
  """Handles logic when in the UPLOADING_APP_PY state."""
702
  logging.info("Handling STATE_UPLOADING_APP_PY")
703
  current_gemini_key = gemini_api_key # Use the input vars directly
 
766
  use_grounding: bool,
767
  *args,
768
  **kwargs
769
+ ) -> WorkflowHandlerReturn:
770
  """Handles logic when in the GENERATING_REQUIREMENTS state."""
771
  logging.info("Handling STATE_GENERATING_REQUIREMENTS")
772
  current_gemini_key = gemini_api_key # Use the input vars directly
 
841
  use_grounding: bool,
842
  *args,
843
  **kwargs
844
+ ) -> WorkflowHandlerReturn:
845
  """Handles logic when in the UPLOADING_REQUIREMENTS state."""
846
  logging.info("Handling STATE_UPLOADING_REQUIREMENTS")
847
  current_gemini_key = gemini_api_key # Use the input vars directly
 
910
  use_grounding: bool,
911
  *args,
912
  **kwargs
913
+ ) -> WorkflowHandlerReturn:
914
  """Handles logic when in the GENERATING_README state."""
915
  logging.info("Handling STATE_GENERATING_README")
916
  current_gemini_key = gemini_api_key # Use the input vars directly
 
976
  use_grounding: bool,
977
  *args,
978
  **kwargs
979
+ ) -> WorkflowHandlerReturn:
980
  """Handles logic when in the UPLOADING_README state."""
981
  logging.info("Handling STATE_UPLOADING_README")
982
  current_gemini_key = gemini_api_key # Use the input vars directly
 
1045
  use_grounding: bool,
1046
  *args,
1047
  **kwargs
1048
+ ) -> WorkflowHandlerReturn:
1049
  """Handles logic when in the CHECKING_LOGS_BUILD state."""
1050
  logging.info(f"Handling STATE_CHECKING_LOGS_BUILD for repo '{repo_id}'")
1051
  current_gemini_key = gemini_api_key # Use the input vars directly
 
1112
  use_grounding: bool,
1113
  *args,
1114
  **kwargs
1115
+ ) -> WorkflowHandlerReturn:
1116
  """Handles logic when in the CHECKING_LOGS_RUN state."""
1117
  logging.info(f"Handling STATE_CHECKING_LOGS_RUN for repo '{repo_id}'")
1118
  current_gemini_key = gemini_api_key # Use the input vars directly
 
1195
  use_grounding: bool,
1196
  *args,
1197
  **kwargs
1198
+ ) -> WorkflowHandlerReturn:
1199
  """Handles logic when in the DEBUGGING_CODE state."""
1200
  logging.info(f"Handling STATE_DEBUGGING_CODE (attempt #{debug_attempts}) for repo '{repo_id}'")
1201
  current_gemini_key = gemini_api_key # Use the input vars directly
 
1282
  use_grounding: bool,
1283
  *args,
1284
  **kwargs
1285
+ ) -> WorkflowHandlerReturn:
1286
  """Handles logic when in the UPLOADING_FIXED_APP_PY state."""
1287
  logging.info(f"Handling STATE_UPLOADING_FIXED_APP_PY for repo '{repo_id}'")
1288
  current_gemini_key = gemini_api_key # Use the input vars directly
 
1353
  use_grounding: bool,
1354
  *args,
1355
  **kwargs
1356
+ ) -> WorkflowHandlerReturn:
1357
  """Handles logic when in the COMPLETE state."""
1358
  logging.info("Handling STATE_COMPLETE")
1359
  current_gemini_key = gemini_api_key # Use the input vars directly
 
1472
  logging.debug(f"Invoking handler for state: {state}")
1473
  # Call the state handler function, passing all necessary data
1474
  # Need to pass *all* inputs to the handler function
1475
+ # Note: The inputs passed here are the *current* values received by the generator,
1476
+ # which are the values of the UI components and State variables
1477
+ # at the moment the button was clicked, plus any previous yielded state.
1478
  handler_output = handler(
1479
  message=message, history=history,
1480
  hf_profile=hf_profile, hf_token=hf_token,
 
1602
 
1603
  # Use the pattern suggested in the feedback: wire each dependency change to the same handler
1604
  for state_comp in prerequisite_states_for_button:
1605
+ # Create a lambda that captures the list of states at the time it's defined
1606
+ # This lambda will be called by Gradio and receives the new value of the changed component first,
1607
+ # followed by the values of the components in 'inputs'.
1608
+ # We pass ALL prerequisite states (including the one that changed) to the lambda's inputs.
1609
+ # The lambda then passes the *explicitly listed* input values from the 'states' list closure
1610
+ # to the target function, check_send_button_ready.
1611
  state_comp.change(
 
 
 
 
 
1612
  lambda *args, states=prerequisite_states_for_button: check_send_button_ready(
1613
  states[0], states[1], states[2], states[3], states[4] # Pass values from the 'states' list closure
1614
  ),