File size: 6,098 Bytes
d87b56c f2b7155 d87b56c b9e27c7 f2b7155 d87b56c f2b7155 b9e27c7 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 d87b56c f2b7155 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
import os
import io
import random
import pandas as pd
import streamlit as st
from tempfile import NamedTemporaryFile
from streamlit_tags import st_tags
from datasets import load_dataset
from huggingface_hub import HfApi, Repository
HF_TOKEN = os.environ.get("HF_TOKEN")
API = HfApi()
def save_data():
st.session_state["user"]["part_two"] = {
"aesthetic": [aesthetic],
"real_buildings": [real_buildings],
"confidence": [confidence],
"expectation": [expectation],
"coherent": [coherent],
"coherent_others": [coherent_others],
"not_coherent": [not_coherent],
"not_coherent_others": [not_coherent_others],
"feedback": [feedback],
"storicity": [storicity],
"perspective": [perspective],
"image_idx": [image_idx]
}
st.session_state["n_annotated_images"] += 1
results = {
"user": [st.session_state["user"]["nickname"]],
**st.session_state["user"]["part_one"],
**st.session_state["user"]["part_two"]
}
user_results = pd.DataFrame.from_dict(results, orient='index').T
print(user_results)
updated_annotation = pd.concat([annotations, user_results])
print(updated_annotation)
# Create a NamedTemporaryFile
with NamedTemporaryFile(mode='w+', suffix='.csv') as tmp:
updated_annotation.to_csv(tmp.name, index=False)
tmp.seek(0)
API.upload_file(
path_or_fileobj=tmp.name,
path_in_repo="results.csv",
repo_id="Nicorb/annotation_results",
repo_type="dataset",
)
@st.cache_resource
def get_img_dataset(path):
return load_dataset(
path=path,
split="train",
token=HF_TOKEN
)
def get_dataset(path):
return load_dataset(
path=path,
split="train",
token=HF_TOKEN
)
images = get_img_dataset("Nicorb/test_img")
annotations = get_dataset("Nicorb/annotation_results").to_pandas()
if "n_annotated_images" not in st.session_state:
st.session_state["n_annotated_images"] = 0
if "selectbox_disable" not in st.session_state:
st.session_state["selectbox_disable"] = False
st.title("Studio sulla City Identity")
storicity = st.selectbox(
"Scegli un periodo storico:",
[
"Medioevo", "Ottocento", "Novecento"
],
index=None,
key="storicity",
disabled=st.session_state["selectbox_disable"]
)
perspective = st.selectbox(
"Scegli il punto di vista:",
[
"Frontale", "Prospettiva centrale", "Panoramico"
],
index=None,
key="perspective",
disabled=st.session_state["selectbox_disable"]
)
# GENERARE TRE IMMAGINI PER OGNI SCELTA
random_idxs = random.choices(list(range(len(images))), k=3)
with st.form("valutazione_immagine"):
if st.session_state["storicity"] and st.session_state["perspective"] and st.session_state["n_annotated_images"] < 3:
st.session_state["selectbox_disable"] = True
image_idx = st.session_state["n_annotated_images"]
st.image(
images[random_idxs[image_idx]]["image"]
)
aesthetic = st.radio(
"L'immagine mi piace",
[
"1 - Per nulla",
"2",
"3",
"4",
"5 - Totalmente"
],
horizontal=True
)
real_buildings = st.radio(
"Penso che gli edifici nell'immagine esistano realmente:",
[
"Si", "No"
]
)
confidence = st.radio(
"Sono convinto/a della mia risposta",
[
"1 - Per nulla",
"2",
"3",
"4",
"5 - Totalmente"
],
horizontal=True
)
expectation = st.radio(
"Gli edifici nell'immagine rispettano la mia aspettativa sulla città di Genova",
[
"1 - Per nulla d'accordo",
"2",
"3",
"4",
"5 - Pienamente d'accordo"
],
horizontal=True
)
coherent = st.multiselect(
"Penso che le seguenti caratteristiche dell'immagine siano coerenti con le mie aspettative:",
[
"colore", "nitidezza", "aspetto realistico", "aspetto surreale", "stile architettonico",
"difetti dell'immagine", "presenza del contesto", "correttezza del contesto (se presente)",
"coerenza con la città", "tratti caratteristici della città" # aggiungere ripetizione? troppi elementi?
],
placeholder="Seleziona gli elementi"
)
coherent_others = st_tags(
label="Altre caratteristiche:",
text="Premere invio per aggiungere un nuovo elemento",
value=[],
maxtags=-1,
key="coherent"
)
not_coherent = st.multiselect(
"Penso che le seguenti caratteristiche dell'immagine *NON* siano coerenti con le mie aspettative:",
[
"colore", "nitidezza", "aspetto realistico", "aspetto surreale", "stile architettonico",
"difetti dell'immagine", "presenza del contesto", "correttezza del contesto (se presente)",
"coerenza con la città", "tratti caratteristici della città" # aggiungere ripetizione? troppi elementi?
],
placeholder="Seleziona gli elementi"
)
not_coherent_others = st_tags(
label="Altre caratteristiche:",
text="Premere invio per aggiungere un nuovo elemento",
value=[],
maxtags=-1,
key="not_coherent"
)
feedback = st.text_area("Note, idee, suggerimenti e osservazioni")
submit = st.form_submit_button("Salva", on_click=save_data)
if submit:
st.success("Salvataggio completato")
st.page_link("pages/annotation.py", label="Continua")
else:
st.session_state["selectbox_disable"] = False
|