osbm commited on
Commit
a4ee4e7
·
1 Parent(s): 9606442
Files changed (1) hide show
  1. app.py +62 -14
app.py CHANGED
@@ -22,12 +22,6 @@ voters = [
22
 
23
  api = hfh.HfApi(token=os.environ.get("hf_token"))
24
 
25
- # login page
26
- with st.form("login"):
27
- username = st.selectbox("Select voter", voters)
28
- password = st.text_input("Password (get password from [email protected])", type="password")
29
- submitted = st.form_submit_button("Login")
30
-
31
 
32
  def get_list_of_images():
33
  files = api.list_repo_tree(repo_id="aifred-smart-life-coach/capstone-images", repo_type="dataset", recursive=True,)
@@ -63,8 +57,56 @@ def get_one_from_queue(voter: str):
63
  return {"image": images_not_voted[0]} if images_not_voted else False
64
 
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  print(get_one_from_queue("osman"))
67
 
 
 
 
 
 
 
 
 
 
 
 
68
  if submitted:
69
  if not password == os.environ.get("app_password"):
70
  st.error("The password you entered is incorrect")
@@ -74,13 +116,16 @@ if submitted:
74
  st.write("You are now logged in")
75
 
76
  with st.form("images"):
77
- queue = get_one_from_queue(username)
78
- if not queue:
79
  st.write("You have voted for all the images")
80
  st.stop()
81
  # https://huggingface.co/datasets/aifred-smart-life-coach/capstone-images/resolve/main/kaggle-human-segmentation-dataset/Women%20I/img/woman_image_200.jpg
82
- st.image(f"https://huggingface.co/datasets/aifred-smart-life-coach/capstone-images/resolve/main/{queue['image']}", width=300)
83
- gender = st.select([
 
 
 
84
  "Male",
85
  "Female",
86
  "Non-defining",
@@ -89,10 +134,13 @@ if submitted:
89
  fat_level = st.slider("How fat is this picture?", 0, 100, 50)
90
  muscle_level = st.slider("How muscular is this picture?", 0, 100, 50)
91
  # Every form must have a submit button.
92
- submitted = st.form_submit_button("Submit")
93
- if submitted:
94
- st.write("slideers", healthiness, fat_level, muscle_level)
95
- # push the data to the database
 
 
 
96
 
97
  st.write("Outside the form")
98
 
 
22
 
23
  api = hfh.HfApi(token=os.environ.get("hf_token"))
24
 
 
 
 
 
 
 
25
 
26
  def get_list_of_images():
27
  files = api.list_repo_tree(repo_id="aifred-smart-life-coach/capstone-images", repo_type="dataset", recursive=True,)
 
57
  return {"image": images_not_voted[0]} if images_not_voted else False
58
 
59
 
60
+ def update_vote(
61
+ voter: str,
62
+ image: str,
63
+ healthiness: int,
64
+ fat_level: int,
65
+ muscle_level: int,
66
+ ):
67
+ url = f"https://huggingface.co/datasets/aifred-smart-life-coach/labels/raw/main/{voter}.csv"
68
+
69
+ # fetch file and save it to the labels folder
70
+ file_path = f"labels/{voter}.csv"
71
+ req = requests.get(url)
72
+ with open(file_path, "wb") as file:
73
+ file.write(req.content)
74
+
75
+ df = pd.read_csv(file_path)
76
+ print(df)
77
+ num_past_votes = df.shape[0]
78
+ print("num_past_votes", num_past_votes)
79
+
80
+ # get the list of images that are not present in the csv file
81
+ images_not_voted = list(set(list_of_images) - set(df["image_path"].tolist()))
82
+ print("images_not_voted", len(images_not_voted))
83
+
84
+ # update the csv file with the new vote
85
+ new_row = {
86
+ "image_path": image,
87
+ "healthiness": healthiness,
88
+ "fat_level": fat_level,
89
+ "muscle_level": muscle_level,
90
+ }
91
+ df = df.append(new_row, ignore_index=True)
92
+ df.to_csv(file_path, index=False)
93
+
94
+ # push the file to the dataset
95
+ api.push_to_hub(file_path, repo_id="aifred-smart-life-coach/labels", repo_type="dataset", commit_message=f"Voted for {image}")
96
+
97
  print(get_one_from_queue("osman"))
98
 
99
+
100
+
101
+
102
+
103
+ # login page
104
+ with st.form("login"):
105
+ username = st.selectbox("Select voter", voters)
106
+ password = st.text_input("Password (get password from [email protected])", type="password")
107
+ submitted = st.form_submit_button("Login")
108
+
109
+
110
  if submitted:
111
  if not password == os.environ.get("app_password"):
112
  st.error("The password you entered is incorrect")
 
116
  st.write("You are now logged in")
117
 
118
  with st.form("images"):
119
+ image_path = get_one_from_queue(username)
120
+ if not image_path:
121
  st.write("You have voted for all the images")
122
  st.stop()
123
  # https://huggingface.co/datasets/aifred-smart-life-coach/capstone-images/resolve/main/kaggle-human-segmentation-dataset/Women%20I/img/woman_image_200.jpg
124
+ print(image_path)
125
+ image_url = f"https://huggingface.co/datasets/aifred-smart-life-coach/capstone-images/resolve/main/{image_path}"
126
+ print(image_url)
127
+ st.image(image_url, width=300)
128
+ gender = st.selectbox("Gender", [
129
  "Male",
130
  "Female",
131
  "Non-defining",
 
134
  fat_level = st.slider("How fat is this picture?", 0, 100, 50)
135
  muscle_level = st.slider("How muscular is this picture?", 0, 100, 50)
136
  # Every form must have a submit button.
137
+ submitted_second = st.form_submit_button("Submit")
138
+
139
+
140
+ if submitted_second:
141
+ update_vote(username, image_path, healthiness, fat_level, muscle_level)
142
+ st.write("Vote submitted")
143
+ # push the data to the database
144
 
145
  st.write("Outside the form")
146