davidberenstein1957 HF staff commited on
Commit
7ccbe37
·
1 Parent(s): af275df

add language to dataset

Browse files
Files changed (1) hide show
  1. app/app.py +19 -18
app/app.py CHANGED
@@ -113,16 +113,18 @@ def _process_content(content) -> str | list[str]:
113
  return content
114
 
115
 
116
- def add_fake_like_data(history: list, session_id: str, liked: bool = False) -> None:
 
 
117
  data = {
118
  "index": len(history) - 1,
119
  "value": history[-1],
120
- "liked": True,
121
  }
122
  _, dataframe = wrangle_like_data(
123
  gr.LikeData(target=None, data=data), history.copy()
124
  )
125
- submit_conversation(dataframe, session_id)
126
 
127
 
128
  def respond_system_message(
@@ -205,7 +207,7 @@ def wrangle_like_data(x: gr.LikeData, history) -> DataFrame:
205
 
206
 
207
  def wrangle_edit_data(
208
- x: gr.EditData, history: list, dataframe: DataFrame, session_id: str
209
  ) -> list:
210
  """Edit the conversation and add negative feedback if assistant message is edited, otherwise regenerate the message
211
 
@@ -243,12 +245,12 @@ def wrangle_edit_data(
243
 
244
 
245
  def wrangle_retry_data(
246
- x: gr.RetryData, history: list, dataframe: DataFrame, session_id: str
247
  ) -> list:
248
  """Respond to the user message with a system message and add negative feedback on the original message
249
 
250
  Return the history with the new message"""
251
- add_fake_like_data(history, session_id)
252
 
253
  # Return the history without a new message
254
  history = respond_system_message(
@@ -259,10 +261,12 @@ def wrangle_retry_data(
259
  return history, update_dataframe(dataframe, history)
260
 
261
 
262
- def submit_conversation(dataframe, session_id):
263
  """ "Submit the conversation to dataset repo"""
264
- if dataframe.empty:
265
- gr.Info("No messages to submit because the conversation was empty")
 
 
266
  return (gr.Dataframe(value=None, interactive=False), [])
267
 
268
  dataframe["content"] = dataframe["content"].apply(_process_content)
@@ -271,9 +275,10 @@ def submit_conversation(dataframe, session_id):
271
  "timestamp": datetime.now().isoformat(),
272
  "session_id": session_id,
273
  "conversation_id": str(uuid.uuid4()),
 
274
  }
275
  save_feedback(input_object=conversation_data)
276
- gr.Info(f"Submitted {len(dataframe)} messages to the dataset")
277
  return (gr.Dataframe(value=None, interactive=False), [])
278
 
279
 
@@ -295,11 +300,7 @@ with gr.Blocks(css=css) as demo:
295
  )
296
 
297
  language = gr.Dropdown(
298
- label="Language",
299
- choices=LANGUAGES,
300
- value=LANGUAGES[0],
301
- interactive=True,
302
- max_choices=1,
303
  )
304
 
305
  chatbot = gr.Chatbot(
@@ -357,19 +358,19 @@ with gr.Blocks(css=css) as demo:
357
 
358
  chatbot.retry(
359
  fn=wrangle_retry_data,
360
- inputs=[chatbot, dataframe, session_id],
361
  outputs=[chatbot, dataframe],
362
  )
363
 
364
  chatbot.edit(
365
  fn=wrangle_edit_data,
366
- inputs=[chatbot, dataframe, session_id],
367
  outputs=[chatbot],
368
  ).then(update_dataframe, inputs=[dataframe, chatbot], outputs=[dataframe])
369
 
370
  submit_btn.click(
371
  fn=submit_conversation,
372
- inputs=[dataframe, session_id],
373
  outputs=[dataframe, chatbot],
374
  )
375
  demo.load(
 
113
  return content
114
 
115
 
116
+ def add_fake_like_data(
117
+ history: list, session_id: str, language: str, liked: bool = False
118
+ ) -> None:
119
  data = {
120
  "index": len(history) - 1,
121
  "value": history[-1],
122
+ "liked": liked,
123
  }
124
  _, dataframe = wrangle_like_data(
125
  gr.LikeData(target=None, data=data), history.copy()
126
  )
127
+ submit_conversation(dataframe, session_id, language)
128
 
129
 
130
  def respond_system_message(
 
207
 
208
 
209
  def wrangle_edit_data(
210
+ x: gr.EditData, history: list, dataframe: DataFrame, session_id: str, language: str
211
  ) -> list:
212
  """Edit the conversation and add negative feedback if assistant message is edited, otherwise regenerate the message
213
 
 
245
 
246
 
247
  def wrangle_retry_data(
248
+ x: gr.RetryData, history: list, dataframe: DataFrame, session_id: str, language: str
249
  ) -> list:
250
  """Respond to the user message with a system message and add negative feedback on the original message
251
 
252
  Return the history with the new message"""
253
+ add_fake_like_data(history, session_id, language)
254
 
255
  # Return the history without a new message
256
  history = respond_system_message(
 
261
  return history, update_dataframe(dataframe, history)
262
 
263
 
264
+ def submit_conversation(dataframe, session_id, language):
265
  """ "Submit the conversation to dataset repo"""
266
+ if "role" in dataframe.columns:
267
+ dataframe = dataframe[dataframe["role"] != "system"]
268
+ if dataframe.empty or len(dataframe) < 2:
269
+ gr.Info("No feedback to submit.")
270
  return (gr.Dataframe(value=None, interactive=False), [])
271
 
272
  dataframe["content"] = dataframe["content"].apply(_process_content)
 
275
  "timestamp": datetime.now().isoformat(),
276
  "session_id": session_id,
277
  "conversation_id": str(uuid.uuid4()),
278
+ "language": language,
279
  }
280
  save_feedback(input_object=conversation_data)
281
+ gr.Info("Submitted your feedback!")
282
  return (gr.Dataframe(value=None, interactive=False), [])
283
 
284
 
 
300
  )
301
 
302
  language = gr.Dropdown(
303
+ label="Language", choices=LANGUAGES, value=LANGUAGES[0], interactive=True
 
 
 
 
304
  )
305
 
306
  chatbot = gr.Chatbot(
 
358
 
359
  chatbot.retry(
360
  fn=wrangle_retry_data,
361
+ inputs=[chatbot, dataframe, session_id, language],
362
  outputs=[chatbot, dataframe],
363
  )
364
 
365
  chatbot.edit(
366
  fn=wrangle_edit_data,
367
+ inputs=[chatbot, dataframe, session_id, language],
368
  outputs=[chatbot],
369
  ).then(update_dataframe, inputs=[dataframe, chatbot], outputs=[dataframe])
370
 
371
  submit_btn.click(
372
  fn=submit_conversation,
373
+ inputs=[dataframe, session_id, language],
374
  outputs=[dataframe, chatbot],
375
  )
376
  demo.load(