PedroC11 commited on
Commit
29cf9dd
·
verified ·
1 Parent(s): 1450ac1

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +46 -2
src/streamlit_app.py CHANGED
@@ -2,6 +2,41 @@ import altair as alt
2
  import numpy as np
3
  import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # ---- CONFIG ----
7
  st.set_page_config(page_title="Image Preference Study", layout="centered")
@@ -52,6 +87,15 @@ if st.button("Submit"):
52
 
53
  # st.json(results)
54
 
 
 
 
 
 
 
 
 
55
  # Save to file (you could also save to a database)
56
- with open("resp/responses.txt", "a") as f:
57
- f.write(str(results) + "\n")
 
 
2
  import numpy as np
3
  import pandas as pd
4
  import streamlit as st
5
+ #==
6
+ import uuid
7
+ from pathlib import Path
8
+ from huggingface_hub import CommitScheduler
9
+ import json
10
+ #==
11
+
12
+
13
+ #===
14
+ # Define the file where to save the data. Use UUID to make sure not to overwrite existing data from a previous run.
15
+ feedback_file = Path("data_pref/") / f"data_{uuid.uuid4()}.json"
16
+ feedback_folder = feedback_file.parent
17
+
18
+ # Schedule regular uploads. Remote repo and local folder are created if they don't already exist.
19
+ scheduler = CommitScheduler(
20
+ repo_id="data_pref",
21
+ repo_type="dataset",
22
+ folder_path=feedback_folder,
23
+ path_in_repo="data",
24
+ every=10,
25
+ )
26
+
27
+ # Define the function that will be called when the user submits its feedback (to be called in Gradio)
28
+ #def save_feedback(input_text:str, output_1: str, output_2:str, user_choice: int) -> None:
29
+ # """
30
+ # Append input/outputs and user feedback to a JSON Lines file using a thread lock to avoid concurrent writes from different users.
31
+ # """
32
+ # with scheduler.lock:
33
+ # with feedback_file.open("a") as f:
34
+ # f.write(json.dumps({"input": input_text, "output_1": output_1, "output_2": output_2, "user_choice": user_choice}))
35
+ # f.write("\n")
36
+ #====
37
+
38
+
39
+
40
 
41
  # ---- CONFIG ----
42
  st.set_page_config(page_title="Image Preference Study", layout="centered")
 
87
 
88
  # st.json(results)
89
 
90
+ #====
91
+ with scheduler.lock:
92
+ with feedback_file.open("a") as f:
93
+ f.write(json.dumps(results))
94
+ f.write("\n")
95
+ #====
96
+
97
+
98
  # Save to file (you could also save to a database)
99
+ #with open("resp/responses.txt", "a") as f:
100
+ # f.write(str(results) + "\n")
101
+