Skip to content

Commit

Permalink
returning embedding is optional with return_embedding parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
pleary committed Dec 31, 2024
1 parent 3923de7 commit fa3062b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
8 changes: 6 additions & 2 deletions lib/inat_vision_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,23 @@ def score_image(self, form, file_path, lat, lng, iconic_taxon_id, geomodel):
return InatVisionAPIResponses.aggregated_tree_response(
aggregated_scores, self.inferrer
)
embedding = self.inferrer.signature_for_image(file_path) if \
form.return_embedding.data == "true" else None
return InatVisionAPIResponses.aggregated_object_response(
leaf_scores, aggregated_scores, self.inferrer,
embedding=self.inferrer.signature_for_image(file_path)
embedding=embedding
)

# legacy dict response
if geomodel != "true":
return InatVisionAPIResponses.legacy_dictionary_response(leaf_scores, self.inferrer)

if form.format.data == "object":
embedding = self.inferrer.signature_for_image(file_path) if \
form.return_embedding.data == "true" else None
return InatVisionAPIResponses.object_response(
leaf_scores, self.inferrer,
embedding=self.inferrer.signature_for_image(file_path)
embedding=embedding
)

return InatVisionAPIResponses.array_response(leaf_scores, self.inferrer)
Expand Down
16 changes: 10 additions & 6 deletions lib/inat_vision_api_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def array_response(leaf_scores, inferrer):
return InatVisionAPIResponses.array_response_columns(leaf_scores).to_dict(orient="records")

@staticmethod
def object_response(leaf_scores, inferrer, embedding):
def object_response(leaf_scores, inferrer, embedding=None):
leaf_scores = InatVisionAPIResponses.limit_leaf_scores_for_response(leaf_scores)
leaf_scores = InatVisionAPIResponses.update_leaf_scores_scaling(leaf_scores)
results = InatVisionAPIResponses.array_response_columns(
Expand All @@ -39,11 +39,13 @@ def object_response(leaf_scores, inferrer, embedding):
common_ancestor_frame
).to_dict(orient="records")[0]

return {
response = {
"common_ancestor": common_ancestor,
"results": results,
"embedding": embedding
}
if embedding is not None:
response["embedding"] = embedding
return response

@staticmethod
def aggregated_tree_response(aggregated_scores, inferrer):
Expand Down Expand Up @@ -74,7 +76,7 @@ def aggregated_tree_response(aggregated_scores, inferrer):
return "<pre>" + "<br/>".join(printable_tree) + "</pre>"

@staticmethod
def aggregated_object_response(leaf_scores, aggregated_scores, inferrer, embedding):
def aggregated_object_response(leaf_scores, aggregated_scores, inferrer, embedding=None):
top_leaf_combined_score = aggregated_scores.query(
"leaf_class_id.notnull()"
).sort_values(
Expand Down Expand Up @@ -117,11 +119,13 @@ def aggregated_object_response(leaf_scores, aggregated_scores, inferrer, embeddi
common_ancestor_frame
).to_dict(orient="records")[0]

return {
response = {
"common_ancestor": common_ancestor,
"results": final_results.to_dict(orient="records"),
"embedding": embedding
}
if embedding is not None:
response["embedding"] = embedding
return response

@staticmethod
def limit_leaf_scores_for_response(leaf_scores):
Expand Down
7 changes: 6 additions & 1 deletion lib/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ <h2>Slim vs Legacy Model</h2>
Lng: <input type="test" name="lng" value="-70">
<br/>
<select name="format">
<option value="object">Object</option>
<option value="json">JSON</option>
<option value="tree">Tree</option>
<option value="object">Object</option>
</select>
<br/>
<select name="geomodel">
Expand All @@ -38,6 +38,11 @@ <h2>Slim vs Legacy Model</h2>
<option value="true">Aggregated</option>
</select>
<br/>
<select name="return_embedding">
<option value="true">Return embedding</option>
<option value="false">Do not return embedding</option>
</select>
<br/>
<br/>
<button type="submit">Submit</button>
</form>
Expand Down
1 change: 1 addition & 0 deletions lib/web_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class ImageForm(FlaskForm):
taxon_id = StringField("taxon_id")
geomodel = StringField("geomodel")
aggregated = StringField("aggregated")
return_embedding = StringField("return_embedding")
format = StringField("format")

0 comments on commit fa3062b

Please sign in to comment.