Spaces:
Sleeping
Sleeping
rmm
commited on
Commit
·
53eb5bc
1
Parent(s):
4f8cbf1
fix: classifier tabs render their own content only
Browse filesrepair of problem 2 in the PR initial description. (note: the commit
contains 60 lines of changes but it is just adding two `with tab_x`
statements plus indenting the logic).
- src/pages/4_🔥_classifiers.py +121 -119
src/pages/4_🔥_classifiers.py
CHANGED
|
@@ -59,132 +59,134 @@ with st.sidebar:
|
|
| 59 |
# input elements (file upload, text input, etc)
|
| 60 |
setup_input()
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
st.session_state.workflow_fsm.complete_current_state()
|
| 67 |
-
# ->
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
if
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
df = pd.DataFrame([obs.to_dict() for obs in st.session_state.observations.values()])
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
#
|
| 85 |
-
#
|
| 86 |
-
# hmm, maybe it should actually just be "I'm done with data entry"
|
| 87 |
-
st.session_state.workflow_fsm.complete_current_state()
|
| 88 |
-
# -> data_entry_validated
|
| 89 |
-
|
| 90 |
-
if st.session_state.MODE_DEV_STATEFUL:
|
| 91 |
-
dbg_show_observation_hashes()
|
| 92 |
-
|
| 93 |
-
add_classifier_header()
|
| 94 |
-
# if we are before data_entry_validated, show the button, disabled.
|
| 95 |
-
if not st.session_state.workflow_fsm.is_in_state_or_beyond('data_entry_validated'):
|
| 96 |
-
tab_inference.button(":gray[*Identify with cetacean classifier*]", disabled=True,
|
| 97 |
-
help="Please validate inputs before proceeding",
|
| 98 |
-
key="button_infer_ceteans")
|
| 99 |
-
|
| 100 |
-
if st.session_state.workflow_fsm.is_in_state('data_entry_validated'):
|
| 101 |
-
# show the button, enabled. If pressed, we start the ML model (And advance state)
|
| 102 |
-
if tab_inference.button("Identify with cetacean classifier",
|
| 103 |
-
key="button_infer_ceteans"):
|
| 104 |
-
cetacean_classifier = AutoModelForImageClassification.from_pretrained(
|
| 105 |
-
classifier_name,
|
| 106 |
-
revision=classifier_revision,
|
| 107 |
-
trust_remote_code=True)
|
| 108 |
-
|
| 109 |
-
cetacean_just_classify(cetacean_classifier)
|
| 110 |
-
st.session_state.workflow_fsm.complete_current_state()
|
| 111 |
-
# trigger a refresh too (refreshhing the prog indicator means the script reruns and
|
| 112 |
-
# we can enter the next state - visualising the results / review)
|
| 113 |
-
# ok it doesn't if done programmatically. maybe interacting with teh button? check docs.
|
| 114 |
-
refresh_progress_display()
|
| 115 |
-
#TODO: validate this doesn't harm performance adversely.
|
| 116 |
-
st.rerun()
|
| 117 |
-
|
| 118 |
-
elif st.session_state.workflow_fsm.is_in_state('ml_classification_completed'):
|
| 119 |
-
# show the results, and allow manual validation
|
| 120 |
-
st.markdown("""### Inference results and manual validation/adjustment """)
|
| 121 |
-
if st.session_state.MODE_DEV_STATEFUL:
|
| 122 |
-
s = ""
|
| 123 |
-
for k, v in st.session_state.whale_prediction1.items():
|
| 124 |
-
s += f"* Image {k}: {v}\n"
|
| 125 |
-
|
| 126 |
-
st.markdown(s)
|
| 127 |
-
|
| 128 |
-
# add a button to advance the state
|
| 129 |
-
if st.button("I have looked over predictions and confirm correct species", icon= "👀",
|
| 130 |
-
type="primary",
|
| 131 |
-
help="Confirm that all species are selected correctly"):
|
| 132 |
-
st.session_state.workflow_fsm.complete_current_state()
|
| 133 |
-
# -> manual_inspection_completed
|
| 134 |
-
st.rerun()
|
| 135 |
-
|
| 136 |
-
cetacean_show_results_and_review()
|
| 137 |
-
|
| 138 |
-
elif st.session_state.workflow_fsm.is_in_state('manual_inspection_completed'):
|
| 139 |
-
# show the ML results, and allow the user to upload the observation
|
| 140 |
-
st.markdown("""### Inference Results (after manual validation) """)
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
if st.button("Upload all observations to THE INTERNET!", icon= "⬆️",
|
| 144 |
-
type="primary",):
|
| 145 |
-
# let this go through to the push_all func, since it just reports to log for now.
|
| 146 |
-
push_all_observations(enable_push=False)
|
| 147 |
-
st.session_state.workflow_fsm.complete_current_state()
|
| 148 |
-
# -> data_uploaded
|
| 149 |
-
st.rerun()
|
| 150 |
-
|
| 151 |
-
cetacean_show_results()
|
| 152 |
-
|
| 153 |
-
elif st.session_state.workflow_fsm.is_in_state('data_uploaded'):
|
| 154 |
-
# the data has been sent. Lets show the observations again
|
| 155 |
-
# but no buttons to upload (or greyed out ok)
|
| 156 |
-
st.markdown("""### Observation(s) uploaded - thank you!""")
|
| 157 |
-
cetacean_show_results()
|
| 158 |
-
|
| 159 |
-
st.divider()
|
| 160 |
-
df = pd.DataFrame([obs.to_dict() for obs in st.session_state.observations.values()])
|
| 161 |
-
st.table(df)
|
| 162 |
-
|
| 163 |
-
# didn't decide what the next state is here - I think we are in the terminal state.
|
| 164 |
-
#st.session_state.workflow_fsm.complete_current_state()
|
| 165 |
|
| 166 |
|
| 167 |
-
|
| 168 |
-
#
|
| 169 |
-
#
|
| 170 |
-
# -
|
| 171 |
-
|
| 172 |
-
tab_hotdogs.
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
pipeline_hot_dog = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
| 180 |
-
|
| 181 |
-
if st.session_state.image is None:
|
| 182 |
-
st.info("Please upload an image first.")
|
| 183 |
-
#st.info(str(observations.to_dict()))
|
| 184 |
-
|
| 185 |
-
else:
|
| 186 |
-
hotdog_classify(pipeline_hot_dog, tab_hotdogs)
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
|
| 189 |
# after all other processing, we can show the stage/state
|
| 190 |
refresh_progress_display()
|
|
|
|
| 59 |
# input elements (file upload, text input, etc)
|
| 60 |
setup_input()
|
| 61 |
|
| 62 |
+
with tab_inference:
|
| 63 |
+
if st.session_state.workflow_fsm.is_in_state('doing_data_entry'):
|
| 64 |
+
# can we advance state? - only when all inputs are set for all uploaded files
|
| 65 |
+
all_inputs_set = check_inputs_are_set(debug=True, empty_ok=False)
|
| 66 |
+
if all_inputs_set:
|
| 67 |
+
st.session_state.workflow_fsm.complete_current_state()
|
| 68 |
+
# -> data_entry_complete
|
| 69 |
+
else:
|
| 70 |
+
# button, disabled; no state change yet.
|
| 71 |
+
st.sidebar.button(":gray[*Validate*]", disabled=True, help="Please fill in all fields.")
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
if st.session_state.workflow_fsm.is_in_state('data_entry_complete'):
|
| 75 |
+
# can we advance state? - only when the validate button is pressed
|
| 76 |
+
if st.sidebar.button(":white_check_mark:[**Validate**]"):
|
| 77 |
+
# create a dictionary with the submitted observation
|
| 78 |
+
|
| 79 |
+
# TODO NEED TO ADAPT to multipage
|
| 80 |
+
#tab_log.info(f"{st.session_state.observations}")
|
| 81 |
+
|
| 82 |
+
df = pd.DataFrame([obs.to_dict() for obs in st.session_state.observations.values()])
|
| 83 |
+
#df = pd.DataFrame(st.session_state.observations, index=[0])
|
| 84 |
+
# with tab_coords:
|
| 85 |
+
# st.table(df)
|
| 86 |
+
# there doesn't seem to be any actual validation here?? TODO: find validator function (each element is validated by the input box, but is there something at the whole image level?)
|
| 87 |
+
# hmm, maybe it should actually just be "I'm done with data entry"
|
| 88 |
st.session_state.workflow_fsm.complete_current_state()
|
| 89 |
+
# -> data_entry_validated
|
| 90 |
+
|
| 91 |
+
if st.session_state.MODE_DEV_STATEFUL:
|
| 92 |
+
dbg_show_observation_hashes()
|
| 93 |
+
|
| 94 |
+
add_classifier_header()
|
| 95 |
+
# if we are before data_entry_validated, show the button, disabled.
|
| 96 |
+
if not st.session_state.workflow_fsm.is_in_state_or_beyond('data_entry_validated'):
|
| 97 |
+
tab_inference.button(":gray[*Identify with cetacean classifier*]", disabled=True,
|
| 98 |
+
help="Please validate inputs before proceeding",
|
| 99 |
+
key="button_infer_ceteans")
|
| 100 |
+
|
| 101 |
+
if st.session_state.workflow_fsm.is_in_state('data_entry_validated'):
|
| 102 |
+
# show the button, enabled. If pressed, we start the ML model (And advance state)
|
| 103 |
+
if tab_inference.button("Identify with cetacean classifier",
|
| 104 |
+
key="button_infer_ceteans"):
|
| 105 |
+
cetacean_classifier = AutoModelForImageClassification.from_pretrained(
|
| 106 |
+
classifier_name,
|
| 107 |
+
revision=classifier_revision,
|
| 108 |
+
trust_remote_code=True)
|
| 109 |
+
|
| 110 |
+
cetacean_just_classify(cetacean_classifier)
|
| 111 |
+
st.session_state.workflow_fsm.complete_current_state()
|
| 112 |
+
# trigger a refresh too (refreshhing the prog indicator means the script reruns and
|
| 113 |
+
# we can enter the next state - visualising the results / review)
|
| 114 |
+
# ok it doesn't if done programmatically. maybe interacting with teh button? check docs.
|
| 115 |
+
refresh_progress_display()
|
| 116 |
+
#TODO: validate this doesn't harm performance adversely.
|
| 117 |
+
st.rerun()
|
| 118 |
+
|
| 119 |
+
elif st.session_state.workflow_fsm.is_in_state('ml_classification_completed'):
|
| 120 |
+
# show the results, and allow manual validation
|
| 121 |
+
st.markdown("""### Inference results and manual validation/adjustment """)
|
| 122 |
+
if st.session_state.MODE_DEV_STATEFUL:
|
| 123 |
+
s = ""
|
| 124 |
+
for k, v in st.session_state.whale_prediction1.items():
|
| 125 |
+
s += f"* Image {k}: {v}\n"
|
| 126 |
+
|
| 127 |
+
st.markdown(s)
|
| 128 |
+
|
| 129 |
+
# add a button to advance the state
|
| 130 |
+
if st.button("I have looked over predictions and confirm correct species", icon= "👀",
|
| 131 |
+
type="primary",
|
| 132 |
+
help="Confirm that all species are selected correctly"):
|
| 133 |
+
st.session_state.workflow_fsm.complete_current_state()
|
| 134 |
+
# -> manual_inspection_completed
|
| 135 |
+
st.rerun()
|
| 136 |
+
|
| 137 |
+
cetacean_show_results_and_review()
|
| 138 |
|
| 139 |
+
elif st.session_state.workflow_fsm.is_in_state('manual_inspection_completed'):
|
| 140 |
+
# show the ML results, and allow the user to upload the observation
|
| 141 |
+
st.markdown("""### Inference Results (after manual validation) """)
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
if st.button("Upload all observations to THE INTERNET!", icon= "⬆️",
|
| 145 |
+
type="primary",):
|
| 146 |
+
# let this go through to the push_all func, since it just reports to log for now.
|
| 147 |
+
push_all_observations(enable_push=False)
|
| 148 |
+
st.session_state.workflow_fsm.complete_current_state()
|
| 149 |
+
# -> data_uploaded
|
| 150 |
+
st.rerun()
|
| 151 |
|
| 152 |
+
cetacean_show_results()
|
| 153 |
+
|
| 154 |
+
elif st.session_state.workflow_fsm.is_in_state('data_uploaded'):
|
| 155 |
+
# the data has been sent. Lets show the observations again
|
| 156 |
+
# but no buttons to upload (or greyed out ok)
|
| 157 |
+
st.markdown("""### Observation(s) uploaded - thank you!""")
|
| 158 |
+
cetacean_show_results()
|
| 159 |
+
|
| 160 |
+
st.divider()
|
| 161 |
df = pd.DataFrame([obs.to_dict() for obs in st.session_state.observations.values()])
|
| 162 |
+
st.table(df)
|
| 163 |
+
|
| 164 |
+
# didn't decide what the next state is here - I think we are in the terminal state.
|
| 165 |
+
#st.session_state.workflow_fsm.complete_current_state()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
|
| 168 |
+
with tab_hotdogs:
|
| 169 |
+
# inside the hotdog tab, on button press we call a 2nd model (totally unrelated at present, just for demo
|
| 170 |
+
# purposes, an hotdog image classifier) which will be run locally.
|
| 171 |
+
# - this model predicts if the image is a hotdog or not, and returns probabilities
|
| 172 |
+
# - the input image is the same as for the ceteacean classifier - defined in the sidebar
|
| 173 |
+
tab_hotdogs.title("Hot Dog? Or Not?")
|
| 174 |
+
tab_hotdogs.write("""
|
| 175 |
+
*Run alternative classifer on input images. Here we are using
|
| 176 |
+
a binary classifier - hotdog or not - from
|
| 177 |
+
huggingface.co/julien-c/hotdog-not-hotdog.*""")
|
| 178 |
+
|
| 179 |
+
if tab_hotdogs.button("Get Hotdog Prediction"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
+
pipeline_hot_dog = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")
|
| 182 |
+
|
| 183 |
+
if st.session_state.image is None:
|
| 184 |
+
st.info("Please upload an image first.")
|
| 185 |
+
#st.info(str(observations.to_dict()))
|
| 186 |
+
|
| 187 |
+
else:
|
| 188 |
+
hotdog_classify(pipeline_hot_dog, tab_hotdogs)
|
| 189 |
+
|
| 190 |
|
| 191 |
# after all other processing, we can show the stage/state
|
| 192 |
refresh_progress_display()
|