davanstrien HF staff commited on
Commit
98127fe
Β·
1 Parent(s): dba78fd

chore: Modify log_blurb_and_vote function to handle already voted responses

Browse files
Files changed (1) hide show
  1. app.py +25 -10
app.py CHANGED
@@ -20,7 +20,6 @@ MODELS = [
20
  "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
21
  ]
22
 
23
-
24
  # Set up dataset storage
25
  dataset_folder = Path("dataset")
26
  dataset_folder.mkdir(exist_ok=True)
@@ -124,8 +123,17 @@ def generate_blurb(prompt):
124
  yield full_text
125
 
126
 
127
- # Function to log blurb and vote
128
- def log_blurb_and_vote(prompt, blurb, vote, user_info: gr.OAuthProfile | None, *args):
 
 
 
 
 
 
 
 
 
129
  user_id = user_info.username if user_info is not None else str(uuid.uuid4())
130
  log_entry = {
131
  "timestamp": datetime.now().isoformat(),
@@ -139,7 +147,7 @@ def log_blurb_and_vote(prompt, blurb, vote, user_info: gr.OAuthProfile | None, *
139
  with dataset_file.open("a") as f:
140
  f.write(json.dumps(log_entry) + "\n")
141
  gr.Info("Thank you for voting!")
142
- return f"Logged: {vote} by user {user_id}"
143
 
144
 
145
  # Create custom theme
@@ -161,6 +169,7 @@ with gr.Blocks(theme=tufte_theme) as demo:
161
 
162
  prompt_state = gr.State()
163
  blurb_output = gr.Markdown(label="Book blurb")
 
164
 
165
  with gr.Row(visible=False) as voting_row:
166
  upvote_btn = gr.Button("πŸ‘ would read")
@@ -169,15 +178,19 @@ with gr.Blocks(theme=tufte_theme) as demo:
169
  vote_output = gr.Textbox(label="Vote Status", interactive=False, visible=False)
170
 
171
  def generate_and_show(prompt):
172
- return "Generating...", gr.Row.update(visible=False)
173
 
174
  def show_voting_buttons(blurb):
175
- return blurb, gr.Row.update(visible=True)
176
 
177
  generate_btn.click(get_and_store_prompt, outputs=prompt_state).then(
178
- generate_and_show, inputs=prompt_state, outputs=[blurb_output, voting_row]
 
 
179
  ).then(generate_blurb, inputs=prompt_state, outputs=blurb_output).then(
180
- show_voting_buttons, inputs=blurb_output, outputs=[blurb_output, voting_row]
 
 
181
  )
182
 
183
  upvote_btn.click(
@@ -187,8 +200,9 @@ with gr.Blocks(theme=tufte_theme) as demo:
187
  blurb_output,
188
  gr.Textbox(value="upvote", visible=False),
189
  login_btn,
 
190
  ],
191
- outputs=vote_output,
192
  )
193
  downvote_btn.click(
194
  log_blurb_and_vote,
@@ -197,8 +211,9 @@ with gr.Blocks(theme=tufte_theme) as demo:
197
  blurb_output,
198
  gr.Textbox(value="downvote", visible=False),
199
  login_btn,
 
200
  ],
201
- outputs=vote_output,
202
  )
203
 
204
  if __name__ == "__main__":
 
20
  "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
21
  ]
22
 
 
23
  # Set up dataset storage
24
  dataset_folder = Path("dataset")
25
  dataset_folder.mkdir(exist_ok=True)
 
123
  yield full_text
124
 
125
 
126
+ # Modified function to log blurb and vote
127
+ def log_blurb_and_vote(
128
+ prompt, blurb, vote, user_info: gr.OAuthProfile | None, has_voted, *args
129
+ ):
130
+ if has_voted:
131
+ return (
132
+ "You've already voted on this response.",
133
+ has_voted,
134
+ gr.Row.update(visible=True),
135
+ )
136
+
137
  user_id = user_info.username if user_info is not None else str(uuid.uuid4())
138
  log_entry = {
139
  "timestamp": datetime.now().isoformat(),
 
147
  with dataset_file.open("a") as f:
148
  f.write(json.dumps(log_entry) + "\n")
149
  gr.Info("Thank you for voting!")
150
+ return f"Logged: {vote} by user {user_id}", True, gr.Row.update(visible=False)
151
 
152
 
153
  # Create custom theme
 
169
 
170
  prompt_state = gr.State()
171
  blurb_output = gr.Markdown(label="Book blurb")
172
+ has_voted = gr.State(False)
173
 
174
  with gr.Row(visible=False) as voting_row:
175
  upvote_btn = gr.Button("πŸ‘ would read")
 
178
  vote_output = gr.Textbox(label="Vote Status", interactive=False, visible=False)
179
 
180
  def generate_and_show(prompt):
181
+ return "Generating...", gr.Row.update(visible=False), False
182
 
183
  def show_voting_buttons(blurb):
184
+ return blurb, gr.Row.update(visible=True), False
185
 
186
  generate_btn.click(get_and_store_prompt, outputs=prompt_state).then(
187
+ generate_and_show,
188
+ inputs=prompt_state,
189
+ outputs=[blurb_output, voting_row, has_voted],
190
  ).then(generate_blurb, inputs=prompt_state, outputs=blurb_output).then(
191
+ show_voting_buttons,
192
+ inputs=blurb_output,
193
+ outputs=[blurb_output, voting_row, has_voted],
194
  )
195
 
196
  upvote_btn.click(
 
200
  blurb_output,
201
  gr.Textbox(value="upvote", visible=False),
202
  login_btn,
203
+ has_voted,
204
  ],
205
+ outputs=[vote_output, has_voted, voting_row],
206
  )
207
  downvote_btn.click(
208
  log_blurb_and_vote,
 
211
  blurb_output,
212
  gr.Textbox(value="downvote", visible=False),
213
  login_btn,
214
+ has_voted,
215
  ],
216
+ outputs=[vote_output, has_voted, voting_row],
217
  )
218
 
219
  if __name__ == "__main__":