File size: 5,722 Bytes
74e80c5
49c0458
 
d43a9e4
da950f4
acf368b
afce58f
 
 
 
da950f4
 
bbf4029
72bac2a
 
 
 
 
 
 
 
 
 
 
 
bbf4029
b0719e4
 
72bac2a
d43a9e4
ce2be4d
abe5285
fdf8211
c921aca
 
d43a9e4
 
e326f0c
 
 
da950f4
 
 
 
 
 
 
 
 
 
 
 
 
 
63005e4
da950f4
 
 
 
 
8072724
e326f0c
b0719e4
a4ee4e7
 
 
acf368b
a4ee4e7
 
 
5f0893b
a4ee4e7
 
 
 
 
 
ffed18a
 
 
a4ee4e7
 
 
 
 
 
e8505b6
 
 
 
 
acf368b
 
 
 
5f0893b
acf368b
 
 
 
5f0893b
acf368b
 
 
e8505b6
a4ee4e7
 
 
181e118
 
 
 
 
 
a4ee4e7
5f0893b
 
 
2a2a63d
 
 
 
 
 
 
 
 
 
 
 
 
 
32b8a64
2a2a63d
 
 
 
5f0893b
6567232
ffed18a
5f0893b
fabb286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78e3e3b
fabb286
 
 
 
 
 
 
 
 
a4ee4e7
 
acf368b
 
 
 
 
 
5f0893b
 
acf368b
59c8c74
0468b1a
fabb286
d4ec28a
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
import os
import streamlit as st
import pandas as pd
import huggingface_hub as hfh
import requests
import time
import uuid

def get_uuid():
    return str(uuid.uuid4())[:6]

os.makedirs("labels", exist_ok=True)

voters = [
    "osman",
    "eren",
    "robin",
    "mira",
    "bilal",
    "volunteer-1",
    "volunteer-2",
    "volunteer-3",
    "volunteer-4",
    "volunteer-5",
]

api = hfh.HfApi(token=os.environ.get("hf_token"))


def get_list_of_images():
    files = api.list_repo_tree(repo_id="aifred-smart-life-coach/capstone-images", repo_type="dataset", recursive=True,)
    files = [file.path for file in files if file.path.endswith((".png", ".jpg"))]
    return files


def get_one_from_queue(voter: str):
    # get an image for the voter or return False if no image is left
    # aifred-smart-life-coach/labels labels dataset
    # labels dataset multiple csv files named as [voter name].csv
    # each csv file has the image image path vote date, votes
    url = f"https://huggingface.co/datasets/aifred-smart-life-coach/labels/raw/main/{voter}.csv"

    # fetch file and save it to the labels folder
    file_path = f"labels/{voter}.csv"
    req = requests.get(url)
    with open(file_path, "wb") as file:
        file.write(req.content)

    df = pd.read_csv(file_path)
    print(df)
    num_past_votes = df.shape[0]
    print("num_past_votes", num_past_votes)
    list_of_images = get_list_of_images()
    print("list_of_images", len(list_of_images))
    st.write(f"You have voted for {num_past_votes} images out of {len(list_of_images)} images")

    # get the list of images that are not present in the csv file
    images_not_voted = list(set(list_of_images) - set(df["image_path"].tolist()))
    print("images_not_voted", len(images_not_voted))

    return images_not_voted[0] if images_not_voted else False


def update_vote(
    voter: str,
    image: str,
    gender: str,
    healthiness: int,
    fat_level: int,
    muscle_level: int,
    uuid_num: str,
):
    url = f"https://huggingface.co/datasets/aifred-smart-life-coach/labels/raw/main/{voter}.csv"

    # fetch file and save it to the labels folder
    file_path = f"labels/{voter}.csv"
    req = requests.get(url)
    # delete the file if it exists
    if os.path.exists(file_path):
        os.remove(file_path)
    with open(file_path, "wb") as file:
        file.write(req.content)

    df = pd.read_csv(file_path)
    print(df)

    # if the df has the image path of the image, raise an error
    if image in df["image_path"].tolist():
        st.error("You have already voted for this image")
        st.stop()

    new_row = pd.DataFrame(
        {
            "image_path": [image],
            "gender": [ gender ],
            "health": [healthiness],
            "fat_level": [fat_level],
            "muscle_level": [muscle_level],
            "vote_date": [int(time.time())],
            "voter_name": [voter],
            "uuid": [uuid_num],
        }
    )

    df = pd.concat([df, new_row], ignore_index=True)
    df.to_csv(file_path, index=False)

    # push the file to the dataset
    api.upload_file(
        path_or_fileobj=file_path,
        path_in_repo=f"{voter}.csv",
        repo_id="aifred-smart-life-coach/labels", repo_type="dataset",
        commit_message=f"{voter} voted for {image}"
    )




if 'loggedin' not in st.session_state: # user is not logged in
    with st.form("login"):
        username = st.selectbox("Select voter", voters)
        password = st.text_input("Password (get password from [email protected])", type="password")
        submitted = st.form_submit_button("Login")

    if submitted: # submitted password
        if not password == os.environ.get("app_password"): # password is incorrect
            st.error("The password you entered is incorrect")
            st.stop()
        else: # password is correct
            st.success("Welcome, " + username)
            st.write("You are now logged in")
            st.session_state['loggedin'] = username
            st.rerun()

else: # logged in
    username = st.session_state['loggedin']
    st.success(f"Welcome, {username}")
    image_path = get_one_from_queue(username)

    with st.form("images", clear_on_submit=True):

        col1, col2= st.columns(2)
        with col1:
            if not image_path:
                st.write("You have voted for all the images")
                st.stop()

            path = hfh.hf_hub_download(
                repo_id="aifred-smart-life-coach/capstone-images",
                repo_type="dataset",
                filename=image_path,
                token=os.environ.get("hf_token")
            )
            st.image(path, width=300)

        with col2:
            st.write(image_path)
            gender = st.radio("Gender", [
                "Male",
                "Female",
                "Non-defining",
            ])
            healthiness = st.slider("How healthy is this picture?", 0, 100, 50)
            fat_level = st.slider("How fat is this picture?", 0, 100, 50)
            muscle_level = st.slider("How muscular is this picture?", 0, 100, 50)
            uuid_num = get_uuid()
            submitted_second = st.form_submit_button("Submit")

    if submitted_second:
        update_vote(
            voter=username,
            image=image_path,
            gender=gender,
            healthiness=healthiness,
            fat_level=fat_level,
            muscle_level=muscle_level,
            uuid_num=uuid_num,
        )
        st.success(f"Vote submitted uuid: {uuid_num} for the image: {image_path} (in case you want to undo the vote)")
        with st.spinner("Wait for the upload of the vote", show_time=True):
            time.sleep(1)
            st.rerun()