File size: 6,443 Bytes
190629a
 
 
 
29cf9dd
 
 
 
 
c61912e
e151653
 
bd5385b
 
c61912e
 
29cf9dd
 
e0b336d
29cf9dd
 
c61912e
f598826
 
 
 
 
 
 
29cf9dd
 
 
 
 
 
 
f598826
29cf9dd
 
 
 
 
 
 
 
 
 
 
 
 
7b3e79e
 
29cf9dd
190629a
e536da4
 
 
 
aef1164
 
 
38e3962
b760f2a
b63d0b4
e22a0cf
38e3962
 
e536da4
aef1164
e536da4
 
 
9102a40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e536da4
 
 
 
d7fd62b
d7fda29
d7fd62b
e536da4
 
 
27bbcfc
e536da4
 
b63d0b4
e536da4
b63d0b4
e536da4
 
 
b63d0b4
e536da4
 
 
 
 
d7fd62b
e536da4
dc3e18c
 
 
 
d7fd62b
7b3e79e
d6da8b3
d7fd62b
fd0dcc7
dc3e18c
7b3e79e
d7fd62b
7b3e79e
d7fd62b
 
 
 
 
 
 
 
 
 
 
 
 
 
dc3e18c
d7fd62b
 
 
 
 
e810c39
d7fd62b
 
 
 
 
 
 
 
29cf9dd
e536da4
29cf9dd
 
d7fd62b
29cf9dd
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
import altair as alt
import numpy as np
import pandas as pd
import streamlit as st
#==
import uuid
from pathlib import Path
from huggingface_hub import CommitScheduler
import json
#from huggingface_hub import login
from huggingface_hub import whoami
import os
#==
import datetime

user = whoami(token=os.environ.get("HF_TOKEN"))
#==

#st.set_page_config(layout="wide")

#===
#login()
## Define the file where to save the data. Use UUID to make sure not to overwrite existing data from a previous run.
#feedback_file = Path("PedroC11/data_pref/") / f"data_{uuid.uuid4()}.json"
#feedback_folder = feedback_file.parent

feedback_folder = Path("data_JSON")
feedback_folder.mkdir(parents=True, exist_ok=True)
feedback_file = feedback_folder / f"data_{uuid.uuid4()}.json"

# Schedule regular uploads. Remote repo and local folder are created if they don't already exist.
scheduler = CommitScheduler(
    repo_id="data_pref",
    repo_type="dataset",
    folder_path=feedback_folder,
    path_in_repo="data",
    #every=10,
)

# Define the function that will be called when the user submits its feedback (to be called in Gradio)
#def save_feedback(input_text:str, output_1: str, output_2:str, user_choice: int) -> None:
#    """
#    Append input/outputs and user feedback to a JSON Lines file using a thread lock to avoid concurrent writes from different users.
#    """
#    with scheduler.lock:
#        with feedback_file.open("a") as f:
#            f.write(json.dumps({"input": input_text, "output_1": output_1, "output_2": output_2, "user_choice": user_choice}))
#            f.write("\n")
#====

if 'cont_r' not in st.session_state:
        st.session_state.cont_r = 0


# ---- CONFIG ----
st.set_page_config(page_title="Image Preference Study", layout="centered")

st.title("Image Preference Study")

st.header("Instructions", divider=True)
#st.markdown(f"**Instructions:** 
st.markdown(f"You will be shown 25 pairs of images. For each pair, please select which image \"Option A\" (left) or \"Option B\" (right) you think is \
**more visually appealing** and has a **better quality** (for example, *sharp edges*, *clear object details/shapes/proportions*, etc.), regardless of the picture subject content.")
st.markdown(f"Please, evaluate each pair of images **independently** of the other pairs.")
st.markdown(f"If needed, please, look closer to the screen and/or zoom in the pictures.")
st.markdown(f"*Example: If Option A depicts a gray bunny and is a better quality picture than Option B which depicts a brown bunny, I would choose Option A despite \
the fact that I personally *like* brown bunnies more than gray bunnies.*")

st.header("Images", divider=True)
# ---- IMAGE PAIRS ----
# Replace with your actual image paths or URLs
image_pairs = [
("images/A_0.jpeg", "images/B_0.jpeg"),
("images/A_1.jpeg", "images/B_1.jpeg"),
("images/A_2.jpeg", "images/B_2.jpeg"),
("images/A_3.jpeg", "images/B_3.jpeg"),
("images/A_4.jpeg", "images/B_4.jpeg"),
("images/A_5.jpeg", "images/B_5.jpeg"),
("images/A_6.jpeg", "images/B_6.jpeg"),
("images/A_7.jpeg", "images/B_7.jpeg"),
("images/A_8.jpeg", "images/B_8.jpeg"),
("images/A_9.jpeg", "images/B_9.jpeg"),
("images/A_10.jpeg", "images/B_10.jpeg"),
("images/A_11.jpeg", "images/B_11.jpeg"),
("images/A_12.jpeg", "images/B_12.jpeg"),
("images/A_13.jpeg", "images/B_13.jpeg"),
("images/A_14.jpeg", "images/B_14.jpeg"),
("images/A_15.jpeg", "images/B_15.jpeg"),
("images/A_16.jpeg", "images/B_16.jpeg"),
("images/A_17.jpeg", "images/B_17.jpeg"),
("images/A_18.jpeg", "images/B_18.jpeg"),
("images/A_19.jpeg", "images/B_19.jpeg"),
("images/A_20.jpeg", "images/B_20.jpeg"),
("images/A_21.jpeg", "images/B_21.jpeg"),
("images/A_22.jpeg", "images/B_22.jpeg"),
("images/A_23.jpeg", "images/B_23.jpeg"),
("images/A_24.jpeg", "images/B_24.jpeg")   
]

results = {}


code_prov = st.text_input("Type the provided code in the box below:")

# ---- LOOP THROUGH PAIRS ----
for i, (imgA, imgB) in enumerate(image_pairs):
    st.markdown(f"### Pair {i+1}")
    col1, col2 = st.columns(2)

    with col1:
        st.image(imgA, caption="Option A", use_container_width=True)
    with col2:
        st.image(imgB, caption="Option B", use_container_width=True)

    choice = st.radio(
        f"Which image do you prefer for Pair {i+1}?",
        ("-","Option A (left)", "Option B (right)"),
        key=f"choice_{i}"
    )
    results[f"pair_{i+1}"] = choice

# ---- SUBMIT ----
if st.button("Submit"):

    cont = 0
    for key in results:
        if results[key] == "-":
            cont += 1
    
    if code_prov == "":
        st.error("Please, insert your provided code at the beginning of this form. Then, try submitting again.")
    elif cont != 0:
        st.error("It seems you have not evaluated all pairs. Please, check again and Submit. Click on Submit again if you think there is error.")    
    else:
        if st.session_state.cont_r == 0:     

            st.session_state.cont_r += 1 
            #st.success("Thank you! Your responses have been recorded.")
            #st.write("Thank you! Your selection has been recorded.")
            st.success("Thank you! Your selection has been recorded.")
            #st.write("Your final selection is in the green box below. Please, copy them and send them to my email. Thanks a lot!")
            #s_rp = "SELECTION: "
            #for i in range(len(image_pairs)):
            #    if i == (len(image_pairs) - 1):
            #        s_rp = s_rp + results[f"pair_{i+1}"] +  "."
            #    else:
            #        s_rp = s_rp + results[f"pair_{i+1}"] +  ", "
            ##st.badge(s_rp, width = "stretch")
            #st.success(s_rp)
            
            # st.json(results)
        
            #====
            current_time = datetime.datetime.now()
            results["day"] = str(current_time.day)
            results["time"] = str(current_time.hour) + ":" + str(current_time.minute) + ":" + str(current_time.second)
            results["code"] = code_prov
            results["page"] = "Im_2"
            
            with scheduler.lock:
                with feedback_file.open("a") as f:
                    f.write(json.dumps(results))
                    f.write("\n")
            #====
        else:
            st.success("Thanks. Only one submission is allowed.")
    
    # Save to file (you could also save to a database)
    #with open("resp/responses.txt", "a") as f:
    #    f.write(str(results) + "\n")