Pref_Im_S1 / src /streamlit_app.py
PedroC11's picture
Update src/streamlit_app.py
1b8ec54 verified
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_s",
#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_1"
with scheduler.lock:
feedback_file = feedback_folder / f"data_{uuid.uuid4()}_{results["code"]}.json"
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")