Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
chore: add prompt viz, add new display for classifier and adjust layo…
Browse files Browse the repository at this point in the history
…ut for home
  • Loading branch information
d116626 committed Feb 7, 2024
1 parent 677d60f commit 505ba08
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/Home.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# st.image("./data/logo/logo.png", width=300)

DEFAULT_OBJECT = "water_level"
st.markdown("# Mapa de Alagamentos | Vision AI")
st.markdown("# Identificações | Vision AI")

# get cameras
cameras = get_cameras(
Expand Down Expand Up @@ -84,7 +84,6 @@
"timestamp",
"object",
"label",
"label_explanation",
]
selected_row = get_agrid_table(
cameras_identifications_merged[selected_cols].reset_index()
Expand Down Expand Up @@ -113,6 +112,7 @@
)

with col1:
st.markdown("### 📍 Mapa")
st_folium(folium_map, key="fig1", height=600, width="100%")

# for camera_id in cameras_identifications_filter.index:
Expand Down
48 changes: 26 additions & 22 deletions app/pages/Classificador de Labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def get_translation(label):
},
{
"object": "water_in_road",
"title": "Há água na via?",
"title": "Há indício de chuva?",
"condition": "Se a resposta for 'Não', associe o rótulo ‘Baixa ou Indiferente’ à opção 3 e pule para 4.", # noqa
"explanation": " Inspeção visual para presença de água na pista, que pode variar desde uma leve umidade até condições de alagamento evidente.", # noqa
"labels": {
Expand Down Expand Up @@ -104,6 +104,14 @@ def get_translation(label):
pd.DataFrame(data=snapshots), "snapshot_identification"
) # noqa

objects_number = {
object_name: i + 1
for i, object_name in enumerate(
snapshots_objects["object"].unique().tolist()
) # noqa
}
snapshots_objects["question_number"] = snapshots_objects["object"].map(objects_number)


def put_selected_label(label, snapshots_options):
snapshots_to_put = snapshots_options.to_dict()
Expand Down Expand Up @@ -162,13 +170,11 @@ def buttom(
else:
# Get the current image from the DataFrame
row = snapshots_objects.iloc[st.session_state.row_index] # noqa
st.write(
f"INDEX: {st.session_state.row_index +1} / {len(snapshots_objects)}" # noqa
) # noqa
# Extract relevant information
name = row["object"]
translate_dict = get_translation(name)
snapshot_url = row["snapshot_url"]
question_number = row["question_number"]

labels_options = labels.loc[name]
choices = labels_options["value"].tolist()
Expand All @@ -177,33 +183,31 @@ def buttom(
choices = ["true", "false"]

# st.write"
col1, col2, col3 = st.columns(3)
col1, col2 = st.columns(2)
with col2:
st.image(snapshot_url)
with col1:
st.markdown(
f"### {translate_dict.get('title')}",
f"### {question_number}. {translate_dict.get('title')}",
)
st.markdown(
f"**Explicação:** {translate_dict.get('explanation')}",
)
# place labels in a grid of 2 columns
col1, col2, col3, col4, col5, col6 = st.columns(6)
for i, label in enumerate(choices):
label_translated = translate_dict.get("labels").get(label)

if i % 2 == 0:
with col3:
buttom(
label=label,
label_translated=label_translated,
row=row,
)
else:
with col4:
buttom(
label=label,
label_translated=label_translated,
row=row,
)
with col1:
buttom(
label=label,
label_translated=label_translated,
row=row,
)
# else:
# with col4:
# buttom(
# label=label,
# label_translated=label_translated,
# row=row,
# )

st.session_state.row_index += 1 # noqa
33 changes: 33 additions & 0 deletions app/pages/Visualizar Prompt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
import pandas as pd
import streamlit as st
from utils.utils import get_objects, get_objetcs_labels_df, get_prompts

st.set_page_config(layout="wide", initial_sidebar_state="collapsed")
# st.image("./data/logo/logo.png", width=300)

st.markdown("# Visualizar Prompt | Vision AI")

data = get_prompts()
objects = pd.DataFrame(get_objects())
labels = get_objetcs_labels_df(objects)

prompt_parameters = data[0]
prompt_text = prompt_parameters.get("prompt_text")
prompt_objects = prompt_parameters.get("objects")

selected_labels_cols = ["name", "criteria", "identification_guide", "value"]
labels = labels[selected_labels_cols]
labels = labels[labels["name"].isin(prompt_objects)]
objects_table_md = labels.to_markdown(index=False)


output_schema = """{\n "$defs": {\n "Object": {\n "properties": {\n "object": {\n"description": "The object from the objects table",\n"title": "Object",\n"type": "string"\n },\n "label_explanation": {\n"description": "Highly detailed visual description of the image given the object context",\n"title": "Label Explanation",\n"type": "string"\n },\n "label": {\n"anyOf": [\n {\n "type": "boolean"\n },\n {\n "type": "string"\n },\n {\n "type": "null"\n }\n],\n"description": "Label indicating the condition or characteristic of the object",\n"title": "Label"\n }\n },\n "required": [\n "object",\n "label_explanation",\n "label"\n ],\n "title": "Object",\n "type": "object"\n }\n },\n "properties": {\n "objects": {\n "items": {\n "$ref": "#/$defs/Object"\n },\n "title": "Objects",\n "type": "array"\n }\n },\n "required": [\n "objects"\n ],\n "title": "Output",\n "type": "object"\n}\n""" # noqa
output_example = """{\n "objects": [\n {\n "object": "<Object from objects table>",\n "label_explanation": "<Visual description of the image given the object context>",\n "label": "<Selected label from objects table>"\n }\n ]\n}\n""" # noqa

prompt_text = (
prompt_text.replace("{objects_table_md}", objects_table_md)
.replace("{output_schema}", output_schema)
.replace("{output_example}", output_example)
)
st.markdown(prompt_text)
28 changes: 22 additions & 6 deletions app/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ def callback_data():
# )


@st.cache_data(ttl=600 * 2, persist=False)
@st.cache_data(ttl=60 * 5, persist=False)
def get_cameras(
only_active=True,
use_mock_data=False,
update_mock_data=False,
page_size=100,
page_size=3000,
timeout=120,
):
mock_data_path = "./data/temp/mock_api_data.json"
Expand All @@ -86,7 +86,7 @@ def get_cameras(
return data


@st.cache_data(ttl=600 * 2, persist=False)
@st.cache_data(ttl=60 * 2, persist=False)
def get_objects(
page_size=100,
timeout=120,
Expand All @@ -97,6 +97,17 @@ def get_objects(
return data


@st.cache_data(ttl=60 * 60, persist=False)
def get_prompts(
page_size=100,
timeout=120,
):
data = vision_api._get_all_pages(
path="/prompts", page_size=page_size, timeout=timeout
)
return data


def treat_data(response):
cameras_aux = pd.read_csv("./data/database/cameras_aux.csv", dtype=str)
cameras_aux = cameras_aux.rename(columns={"id_camera": "id"}).set_index(
Expand Down Expand Up @@ -211,19 +222,24 @@ def display_camera_details(row, cameras_identifications):

st.markdown(f"### 📷 Camera snapshot") # noqa
st.markdown(f"Endereço: {camera_name}")
# st.markdown(f"Data Snapshot: {snapshot_timestamp}")
st.markdown(f"Data Snapshot: {snapshot_timestamp}")

# get cameras_attr url from selected row by id
if image_url is None:
st.markdown("Falha ao capturar o snapshot da câmera.")
else:
st.markdown(
f"""<img src='{image_url}' style='max-height: 600px;'> """,
f"""<img src='{image_url}' style='max-width: 100%; max-height: 100%;'> """, # noqa
unsafe_allow_html=True,
)
st.markdown("<br><br><br><br>", unsafe_allow_html=True)

st.markdown(f"### 📃 Identificações")

camera_identifications = cameras_identifications.loc[camera_id] # noqa
get_agrid_table(table=camera_identifications.reset_index())
selected_cols = ["object", "label", "label_explanation"]

st.dataframe(camera_identifications[selected_cols].reset_index())


def get_icon_color(label: Union[bool, None]):
Expand Down
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ streamlit-extras = "^0.3.5"
streamlit-autorefresh = "^1.0.1"
pillow = "^10.1.0"
streamlit-aggrid = "0.3.4.post3"
tabulate = "^0.9.0"


[build-system]
Expand Down

0 comments on commit 505ba08

Please sign in to comment.