davidberenstein1957 HF staff commited on
Commit
688e507
1 Parent(s): 0ae6dc4

fix: reset session per gradio screen load

Browse files
Files changed (3) hide show
  1. .gitignore +2 -1
  2. app.py +15 -2
  3. feedback.py +2 -4
.gitignore CHANGED
@@ -160,4 +160,5 @@ cython_debug/
160
  # and can be added to the global gitignore or merged into this file. For a more nuclear
161
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
  #.idea/
163
- .vscode
 
 
160
  # and can be added to the global gitignore or merged into this file. For a more nuclear
161
  # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
  #.idea/
163
+ .vscode
164
+ user_feedback
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  from datetime import datetime
2
 
3
  import gradio as gr
@@ -47,11 +48,13 @@ def wrangle_like_data(x: gr.LikeData, history) -> DataFrame:
47
  return history, DataFrame(data=output_data)
48
 
49
 
50
- def submit_conversation(dataframe):
51
  """ "Submit the conversation to dataset repo"""
52
  conversation_data = {
53
  "conversation": dataframe.to_dict(orient="records"),
54
  "timestamp": datetime.now().isoformat(),
 
 
55
  }
56
  save_feedback(input_object=conversation_data)
57
  gr.Info(f"Submitted {len(dataframe)} messages to the dataset")
@@ -79,6 +82,11 @@ with gr.Blocks(
79
  ##############################
80
  # Chatbot
81
  ##############################
 
 
 
 
 
82
 
83
  chatbot = gr.Chatbot(
84
  elem_id="chatbot",
@@ -120,8 +128,13 @@ with gr.Blocks(
120
  value="Submit conversation",
121
  ).click(
122
  fn=submit_conversation,
123
- inputs=[dataframe],
124
  outputs=[dataframe, chatbot],
125
  )
 
 
 
 
 
126
 
127
  demo.launch()
 
1
+ import uuid
2
  from datetime import datetime
3
 
4
  import gradio as gr
 
48
  return history, DataFrame(data=output_data)
49
 
50
 
51
+ def submit_conversation(dataframe, session_id):
52
  """ "Submit the conversation to dataset repo"""
53
  conversation_data = {
54
  "conversation": dataframe.to_dict(orient="records"),
55
  "timestamp": datetime.now().isoformat(),
56
+ "session_id": session_id,
57
+ "conversation_id": str(uuid.uuid4()),
58
  }
59
  save_feedback(input_object=conversation_data)
60
  gr.Info(f"Submitted {len(dataframe)} messages to the dataset")
 
82
  ##############################
83
  # Chatbot
84
  ##############################
85
+ session_id = gr.Textbox(
86
+ interactive=False,
87
+ value=str(uuid.uuid4()),
88
+ visible=False,
89
+ )
90
 
91
  chatbot = gr.Chatbot(
92
  elem_id="chatbot",
 
128
  value="Submit conversation",
129
  ).click(
130
  fn=submit_conversation,
131
+ inputs=[dataframe, session_id],
132
  outputs=[dataframe, chatbot],
133
  )
134
+ demo.load(
135
+ lambda: str(uuid.uuid4()),
136
+ inputs=[],
137
+ outputs=[session_id],
138
+ )
139
 
140
  demo.launch()
feedback.py CHANGED
@@ -4,9 +4,9 @@ from pathlib import Path
4
 
5
  from huggingface_hub import CommitScheduler
6
 
7
- SESSION_ID = str(uuid.uuid4())
8
 
9
- feedback_file = Path("user_feedback/") / f"data_{SESSION_ID}.json"
10
  feedback_folder = feedback_file.parent
11
 
12
  scheduler = CommitScheduler(
@@ -24,7 +24,5 @@ def save_feedback(input_object: dict) -> None:
24
  """
25
  with scheduler.lock:
26
  with feedback_file.open(mode="a") as f:
27
- input_object["session_id"] = SESSION_ID
28
- input_object["conversation_id"] = str(uuid.uuid4())
29
  f.write(json.dumps(obj=input_object))
30
  f.write("\n")
 
4
 
5
  from huggingface_hub import CommitScheduler
6
 
7
+ APP_INSTANCE_ID = str(uuid.uuid4())
8
 
9
+ feedback_file = Path("user_feedback/") / f"data_{APP_INSTANCE_ID}.json"
10
  feedback_folder = feedback_file.parent
11
 
12
  scheduler = CommitScheduler(
 
24
  """
25
  with scheduler.lock:
26
  with feedback_file.open(mode="a") as f:
 
 
27
  f.write(json.dumps(obj=input_object))
28
  f.write("\n")