wuhp commited on
Commit
910909f
·
verified ·
1 Parent(s): 14c3932

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -26
app.py CHANGED
@@ -261,6 +261,15 @@ def check_send_button_ready(hf_profile: gr.OAuthProfile | None, hf_token: gr.OAu
261
  # FIXED: Call gr.update instead of gr.Button.update
262
  return gr.update(interactive=is_ready)
263
 
 
 
 
 
 
 
 
 
 
264
 
265
  # This is the main generator function for the workflow, triggered by the 'Send' button
266
  # NOTE: This function MUST accept ALL state variables as inputs that it might need to modify or pass through.
@@ -1037,42 +1046,41 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
1037
  send_button_update_output = [send_btn]
1038
 
1039
 
1040
- # Trigger check_send_button_ready whenever any prerequisite state changes
1041
- # These now directly call check_send_button_ready with the specific state variables
 
1042
  hf_profile.change(
1043
- check_send_button_ready,
1044
- inputs=send_button_interactive_binding_inputs,
1045
- outputs=send_button_update_output,
1046
- # run_as_event=True # Removed this unless necessary, simplifies behavior
1047
  )
1048
  hf_token.change(
1049
- check_send_button_ready,
1050
- inputs=send_button_interactive_binding_inputs,
1051
- outputs=send_button_update_output,
1052
- # run_as_event=True # Removed
1053
  )
 
1054
  gemini_api_key_state.change(
1055
- check_send_button_ready,
1056
- inputs=send_button_interactive_binding_inputs,
1057
- outputs=send_button_update_output,
1058
- # run_as_event=True # Removed
1059
  )
 
1060
  gemini_model_state.change(
1061
- check_send_button_ready,
1062
- inputs=send_button_interactive_binding_inputs,
1063
- outputs=send_button_update_output,
1064
- # run_as_event=True # Removed
1065
  )
1066
 
1067
 
1068
- # Handle login button click: Update profile/token state -> Their .change handlers trigger check_send_button_ready
1069
  login_btn.click(
1070
  lambda x: (x[0], x[1]),
1071
  inputs=[login_btn],
1072
  outputs=[hf_profile, hf_token] # Updating these states will trigger their .change handlers
1073
- ) # Removed .then(check_send_button_ready, ...)
1074
 
1075
- # Handle Gemini Key Input change: Update key state -> Configure Gemini status -> State .change handler triggers check_send_button_ready
1076
  gemini_input.change(
1077
  lambda k: k,
1078
  inputs=[gemini_input],
@@ -1082,9 +1090,9 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
1082
  configure_gemini,
1083
  inputs=[gemini_api_key_state, gemini_model_state],
1084
  outputs=[gemini_status] # Update Gemini status based on new key
1085
- ) # Removed .then(check_send_button_ready, ...)
1086
 
1087
- # Handle Gemini Model Selector change: Update model state -> Configure Gemini status -> State .change handler triggers check_send_button_ready
1088
  model_selector.change(
1089
  lambda m: m,
1090
  inputs=[model_selector],
@@ -1094,7 +1102,7 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
1094
  configure_gemini,
1095
  inputs=[gemini_api_key_state, gemini_model_state],
1096
  outputs=[gemini_status] # Update Gemini status based on new model
1097
- ) # Removed .then(check_send_button_ready, ...)
1098
 
1099
 
1100
  # Handle Grounding checkbox change: update grounding state
@@ -1166,8 +1174,18 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
1166
  configure_gemini,
1167
  inputs=[gemini_api_key_state, gemini_model_state], # Use RENAMED state variables as inputs
1168
  outputs=[gemini_status] # Update Gemini status display
1169
- ) # Removed .then(check_send_button_ready, ...). The state changes from LoginButton/Gemini init *should* trigger the .change handlers above.
1170
- # The greet message will be added automatically by the chat UI initialization.
 
 
 
 
 
 
 
 
 
 
1171
 
1172
 
1173
  # The main workflow function and other helper functions are correctly defined OUTSIDE the gr.Blocks context
 
261
  # FIXED: Call gr.update instead of gr.Button.update
262
  return gr.update(interactive=is_ready)
263
 
264
+ # --- New wrapper function to handle the state updates and call the checker ---
265
+ # This function takes the required state variables as inputs and calls the logic.
266
+ # This function will be called by the .change() events of the state variables.
267
+ def update_send_button_state(profile: gr.OAuthProfile | None, token: gr.OAuthToken | None, gemini_key: str | None, gemini_model: str | None) -> gr.update:
268
+ """Wrapper function to read states and update button interactivity."""
269
+ # Pass the received values directly to the core check function
270
+ return check_send_button_ready(profile, token, gemini_key, gemini_model)
271
+ # --- End of new wrapper function ---
272
+
273
 
274
  # This is the main generator function for the workflow, triggered by the 'Send' button
275
  # NOTE: This function MUST accept ALL state variables as inputs that it might need to modify or pass through.
 
1046
  send_button_update_output = [send_btn]
1047
 
1048
 
1049
+ # Trigger update_send_button_state whenever any prerequisite state changes
1050
+ # This uses the new wrapper function which expects exactly 4 inputs.
1051
+ # The inputs list ensures those 4 state values are passed.
1052
  hf_profile.change(
1053
+ update_send_button_state, # Call the wrapper function
1054
+ inputs=send_button_interactive_binding_inputs, # Pass all 4 prerequisite states
1055
+ outputs=send_button_update_output, # Update only the send button
 
1056
  )
1057
  hf_token.change(
1058
+ update_send_button_state, # Call the wrapper function
1059
+ inputs=send_button_interactive_binding_inputs, # Pass all 4 prerequisite states
1060
+ outputs=send_button_update_output, # Update only the send button
 
1061
  )
1062
+ # gemini_input.change updates gemini_api_key_state, which triggers gemini_api_key_state.change
1063
  gemini_api_key_state.change(
1064
+ update_send_button_state, # Call the wrapper function
1065
+ inputs=send_button_interactive_binding_inputs, # Pass all 4 prerequisite states
1066
+ outputs=send_button_update_output, # Update only the send button
 
1067
  )
1068
+ # model_selector.change updates gemini_model_state, which triggers gemini_model_state.change
1069
  gemini_model_state.change(
1070
+ update_send_button_state, # Call the wrapper function
1071
+ inputs=send_button_interactive_binding_inputs, # Pass all 4 prerequisite states
1072
+ outputs=send_button_update_output, # Update only the send button
 
1073
  )
1074
 
1075
 
1076
+ # Handle login button click: Update profile/token state -> Their .change handlers trigger update_send_button_state
1077
  login_btn.click(
1078
  lambda x: (x[0], x[1]),
1079
  inputs=[login_btn],
1080
  outputs=[hf_profile, hf_token] # Updating these states will trigger their .change handlers
1081
+ )
1082
 
1083
+ # Handle Gemini Key Input change: Update key state -> Configure Gemini status -> State .change handler triggers update_send_button_state
1084
  gemini_input.change(
1085
  lambda k: k,
1086
  inputs=[gemini_input],
 
1090
  configure_gemini,
1091
  inputs=[gemini_api_key_state, gemini_model_state],
1092
  outputs=[gemini_status] # Update Gemini status based on new key
1093
+ )
1094
 
1095
+ # Handle Gemini Model Selector change: Update model state -> Configure Gemini status -> State .change handler triggers update_send_button_state
1096
  model_selector.change(
1097
  lambda m: m,
1098
  inputs=[model_selector],
 
1102
  configure_gemini,
1103
  inputs=[gemini_api_key_state, gemini_model_state],
1104
  outputs=[gemini_status] # Update Gemini status based on new model
1105
+ )
1106
 
1107
 
1108
  # Handle Grounding checkbox change: update grounding state
 
1174
  configure_gemini,
1175
  inputs=[gemini_api_key_state, gemini_model_state], # Use RENAMED state variables as inputs
1176
  outputs=[gemini_status] # Update Gemini status display
1177
+ ).then(
1178
+ # Action 3: After initial load checks, update the button state based on initial states
1179
+ # Use the new wrapper function here as well.
1180
+ update_send_button_state,
1181
+ inputs=send_button_interactive_binding_inputs, # Pass all 4 prerequisite states
1182
+ outputs=send_button_update_output, # Update the send button
1183
+ ).then(
1184
+ # Action 4: Add the initial welcome message to the chat history
1185
+ greet,
1186
+ inputs=None,
1187
+ outputs=chatbot # Updates the chatbot display
1188
+ )
1189
 
1190
 
1191
  # The main workflow function and other helper functions are correctly defined OUTSIDE the gr.Blocks context