Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -954,17 +954,6 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
|
|
954 |
# New State variable for grounding checkbox
|
955 |
use_grounding_state = gr.State(False)
|
956 |
|
957 |
-
# Define the inputs used for checking prerequisites using the RENAMED states
|
958 |
-
# Moved definition *after* the state variables are defined
|
959 |
-
send_button_interactive_binding_inputs = [
|
960 |
-
hf_profile,
|
961 |
-
hf_token,
|
962 |
-
gemini_api_key_state, # Use new state name
|
963 |
-
gemini_model_state # Use new state name
|
964 |
-
]
|
965 |
-
# Define the output for updating the send button interactivity
|
966 |
-
send_button_update_output = [send_btn]
|
967 |
-
|
968 |
|
969 |
with gr.Row():
|
970 |
# Sidebar column for inputs and status displays
|
@@ -1022,146 +1011,148 @@ with gr.Blocks(title="AI-Powered HF Space App Builder") as ai_builder_tab:
|
|
1022 |
# Define chatbot, user_input, send_btn before send_btn.click
|
1023 |
chatbot = gr.Chatbot(type='messages', label="AI Workflow Chat")
|
1024 |
user_input = gr.Textbox(placeholder="Type your message…", interactive=True)
|
1025 |
-
# Define send_btn
|
1026 |
-
# Initial interactive state will be handled by the load event chain
|
1027 |
send_btn = gr.Button("Send") # Starts disabled by default (interactive=False)
|
1028 |
|
1029 |
|
1030 |
-
# Define iframe, build_txt, run_txt
|
1031 |
# These are UI components, NOT State variables
|
1032 |
iframe = gr.HTML("<p>No Space created yet.</p>") # HTML element for the Space iframe
|
1033 |
build_txt = gr.Textbox(label="Build Logs", lines=10, interactive=False, value="", max_lines=20) # Set max_lines for scrollability
|
1034 |
run_txt = gr.Textbox(label="Container Logs", lines=10, interactive=False, value="", max_lines=20) # Set max_lines for scrollability
|
1035 |
|
1036 |
|
1037 |
-
|
1038 |
-
|
1039 |
-
# Define the inputs used for checking prerequisites using the RENAMED states
|
1040 |
-
# Moved definition *after* the state variables are defined
|
1041 |
-
send_button_interactive_binding_inputs = [
|
1042 |
-
hf_profile,
|
1043 |
-
hf_token,
|
1044 |
-
gemini_api_key_state, # Use new state name
|
1045 |
-
gemini_model_state # Use new state name
|
1046 |
-
]
|
1047 |
-
# Define the output for updating the send button interactivity
|
1048 |
-
send_button_update_output = [send_btn]
|
1049 |
-
|
1050 |
-
# Trigger check_send_button_ready whenever any prerequisite state changes
|
1051 |
-
hf_profile.change(
|
1052 |
-
check_send_button_ready,
|
1053 |
-
inputs=send_button_interactive_binding_inputs,
|
1054 |
-
outputs=send_button_update_output,
|
1055 |
-
# run_as_event=True # Added this as a precaution if .change doesn't trigger correctly
|
1056 |
-
)
|
1057 |
-
hf_token.change(
|
1058 |
-
check_send_button_ready,
|
1059 |
-
inputs=send_button_interactive_binding_inputs,
|
1060 |
-
outputs=send_button_update_output,
|
1061 |
-
# run_as_event=True # Added this as a precaution
|
1062 |
-
)
|
1063 |
-
gemini_api_key_state.change(
|
1064 |
-
check_send_button_ready,
|
1065 |
-
inputs=send_button_interactive_binding_inputs,
|
1066 |
-
outputs=send_button_update_output,
|
1067 |
-
# run_as_event=True # Added this as a precaution
|
1068 |
-
)
|
1069 |
-
gemini_model_state.change(
|
1070 |
-
check_send_button_ready,
|
1071 |
-
inputs=send_button_interactive_binding_inputs,
|
1072 |
-
outputs=send_button_update_output,
|
1073 |
-
# run_as_event=True # Added this as a precaution
|
1074 |
-
)
|
1075 |
-
|
1076 |
|
1077 |
-
|
1078 |
-
|
1079 |
-
|
1080 |
-
|
1081 |
-
|
1082 |
-
|
1083 |
-
|
1084 |
-
|
1085 |
-
|
1086 |
-
|
1087 |
-
|
1088 |
-
outputs=[gemini_api_key_state] # Updating this state will trigger its .change handler
|
1089 |
-
).then(
|
1090 |
-
# Use RENAMED state variables as inputs to configure_gemini
|
1091 |
-
configure_gemini,
|
1092 |
-
inputs=[gemini_api_key_state, gemini_model_state],
|
1093 |
-
outputs=[gemini_status] # Update Gemini status based on new key
|
1094 |
-
) # Removed .then(check_send_button_ready, ...)
|
1095 |
-
|
1096 |
-
# Handle Gemini Model Selector change: Update model state -> Configure Gemini status -> *Now rely on .change handlers to trigger check_send_button_ready*
|
1097 |
-
model_selector.change(
|
1098 |
-
lambda m: m,
|
1099 |
-
inputs=[model_selector],
|
1100 |
-
outputs=[gemini_model_state] # Updating this state will trigger its .change handler
|
1101 |
-
).then(
|
1102 |
-
# Use RENAMED state variables as inputs to configure_gemini
|
1103 |
-
configure_gemini,
|
1104 |
-
inputs=[gemini_api_key_state, gemini_model_state],
|
1105 |
-
outputs=[gemini_status] # Update Gemini status based on new model
|
1106 |
-
) # Removed .then(check_send_button_ready, ...)
|
1107 |
-
|
1108 |
-
|
1109 |
-
# Handle Grounding checkbox change: update grounding state
|
1110 |
-
use_grounding_checkbox.change(
|
1111 |
-
lambda v: v, inputs=use_grounding_checkbox, outputs=use_grounding_state
|
1112 |
-
)
|
1113 |
|
1114 |
-
# Handle SDK selector change: update sdk state
|
1115 |
-
sdk_selector.change(
|
1116 |
-
lambda s: s, inputs=sdk_selector, outputs=sdk_state
|
1117 |
-
)
|
1118 |
|
1119 |
-
|
1120 |
-
|
1121 |
-
|
1122 |
-
|
1123 |
-
|
1124 |
-
|
1125 |
-
|
1126 |
-
|
1127 |
-
|
1128 |
-
|
1129 |
-
|
1130 |
-
|
1131 |
-
|
1132 |
-
|
1133 |
-
|
1134 |
-
|
1135 |
-
|
1136 |
-
|
1137 |
-
|
1138 |
-
|
1139 |
-
|
1140 |
-
|
1141 |
-
|
1142 |
-
|
1143 |
-
|
1144 |
-
|
1145 |
-
|
1146 |
-
|
1147 |
-
|
1148 |
-
|
1149 |
-
|
1150 |
-
|
1151 |
-
|
1152 |
-
|
1153 |
-
|
1154 |
-
|
1155 |
-
|
1156 |
-
|
1157 |
-
|
1158 |
-
|
1159 |
-
|
1160 |
-
|
1161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1162 |
|
1163 |
|
1164 |
-
# --- Initial Load Event Chain (Defined INSIDE gr.Blocks, AFTER components and
|
1165 |
# This chain runs once when the app loads
|
1166 |
ai_builder_tab.load(
|
1167 |
# Action 1: Show profile (loads cached login if available)
|
|
|
954 |
# New State variable for grounding checkbox
|
955 |
use_grounding_state = gr.State(False)
|
956 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
957 |
|
958 |
with gr.Row():
|
959 |
# Sidebar column for inputs and status displays
|
|
|
1011 |
# Define chatbot, user_input, send_btn before send_btn.click
|
1012 |
chatbot = gr.Chatbot(type='messages', label="AI Workflow Chat")
|
1013 |
user_input = gr.Textbox(placeholder="Type your message…", interactive=True)
|
1014 |
+
# Define send_btn here, BEFORE it's used in send_button_update_output
|
|
|
1015 |
send_btn = gr.Button("Send") # Starts disabled by default (interactive=False)
|
1016 |
|
1017 |
|
1018 |
+
# Define iframe, build_txt, run_txt after send_btn
|
1019 |
# These are UI components, NOT State variables
|
1020 |
iframe = gr.HTML("<p>No Space created yet.</p>") # HTML element for the Space iframe
|
1021 |
build_txt = gr.Textbox(label="Build Logs", lines=10, interactive=False, value="", max_lines=20) # Set max_lines for scrollability
|
1022 |
run_txt = gr.Textbox(label="Container Logs", lines=10, interactive=False, value="", max_lines=20) # Set max_lines for scrollability
|
1023 |
|
1024 |
|
1025 |
+
# --- Define Event Handlers and Chains AFTER all components and required lists are defined ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1026 |
|
1027 |
+
# Define the inputs used for checking prerequisites using the RENAMED states
|
1028 |
+
# This list should be defined AFTER the state variables
|
1029 |
+
send_button_interactive_binding_inputs = [
|
1030 |
+
hf_profile,
|
1031 |
+
hf_token,
|
1032 |
+
gemini_api_key_state, # Use new state name
|
1033 |
+
gemini_model_state # Use new state name
|
1034 |
+
]
|
1035 |
+
# Define the output for updating the send button interactivity
|
1036 |
+
# FIXED: Moved definition to *after* send_btn is defined
|
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],
|
1079 |
+
outputs=[gemini_api_key_state] # Updating this state will trigger its .change handler
|
1080 |
+
).then(
|
1081 |
+
# Use RENAMED state variables as inputs to configure_gemini
|
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],
|
1091 |
+
outputs=[gemini_model_state] # Updating this state will trigger its .change handler
|
1092 |
+
).then(
|
1093 |
+
# Use RENAMED state variables as inputs to configure_gemini
|
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
|
1101 |
+
use_grounding_checkbox.change(
|
1102 |
+
lambda v: v, inputs=use_grounding_checkbox, outputs=use_grounding_state
|
1103 |
+
)
|
1104 |
+
|
1105 |
+
# Handle SDK selector change: update sdk state
|
1106 |
+
sdk_selector.change(
|
1107 |
+
lambda s: s, inputs=sdk_selector, outputs=sdk_state
|
1108 |
+
)
|
1109 |
+
|
1110 |
+
# Link Workflow State variable change to UI status display
|
1111 |
+
workflow.change(lambda s: s, inputs=workflow, outputs=status_text)
|
1112 |
+
|
1113 |
+
# Link Repo ID State variable change to UI status display
|
1114 |
+
repo_id.change(lambda r: r if r else "None", inputs=repo_id, outputs=repo_id_text)
|
1115 |
+
|
1116 |
+
|
1117 |
+
# The main event handler for the Send button
|
1118 |
+
# This .click() event triggers the ai_workflow_chat generator function
|
1119 |
+
# Inputs are read from UI components AND State variables
|
1120 |
+
# Outputs are updated by the values yielded from the generator
|
1121 |
+
# MODIFIED: Inputs use renamed gemini state variables
|
1122 |
+
# MODIFIED: Outputs include renamed gemini state variables
|
1123 |
+
# Added back *args, **kwargs to the generator function signature and yield for robustness,
|
1124 |
+
# as the previous attempt suggested this might be necessary for state consistency within the generator's lifecycle.
|
1125 |
+
send_btn.click(
|
1126 |
+
ai_workflow_chat, # The generator function to run
|
1127 |
+
inputs=[
|
1128 |
+
user_input, chatbot, # UI component inputs (message, current chat history)
|
1129 |
+
hf_profile, hf_token, # HF State variables
|
1130 |
+
# Pass RENAMED gemini state variables as inputs
|
1131 |
+
gemini_api_key_state, gemini_model_state,
|
1132 |
+
repo_id, workflow, sdk_state, # Workflow State variables
|
1133 |
+
# UI component inputs whose *current values* are needed by the generator
|
1134 |
+
iframe, run_txt, build_txt, # UI component inputs (current values)
|
1135 |
+
debug_attempts, app_description, repo_name_state, generated_code_state, # Other State variables
|
1136 |
+
use_grounding_state # Grounding state input
|
1137 |
+
],
|
1138 |
+
outputs=[
|
1139 |
+
chatbot, # Update Chatbot with new messages
|
1140 |
+
repo_id, workflow, # Update workflow State variables
|
1141 |
+
iframe, run_txt, build_txt, # Update UI component outputs
|
1142 |
+
debug_attempts, app_description, repo_name_state, generated_code_state, # Update other State variables
|
1143 |
+
use_grounding_state, # Update the grounding state output
|
1144 |
+
# Include RENAMED gemini state variables in outputs to ensure consistency
|
1145 |
+
gemini_api_key_state, gemini_model_state
|
1146 |
+
]
|
1147 |
+
).success( # Chain a .success() event to run *after* the .click() handler completes without error
|
1148 |
+
# Clear the user input textbox after the message is sent and processed
|
1149 |
+
lambda: gr.update(value=""),
|
1150 |
+
inputs=None,
|
1151 |
+
outputs=user_input # Update the user input textbox
|
1152 |
+
)
|
1153 |
|
1154 |
|
1155 |
+
# --- Initial Load Event Chain (Defined INSIDE gr.Blocks, AFTER components and required lists are defined) ---
|
1156 |
# This chain runs once when the app loads
|
1157 |
ai_builder_tab.load(
|
1158 |
# Action 1: Show profile (loads cached login if available)
|