simonn8 commited on
Commit
b920afe
1 Parent(s): 7df184d

with sidebar

Browse files
Files changed (4) hide show
  1. app.py +31 -47
  2. poetry.lock +0 -0
  3. pyproject.toml +18 -0
  4. rate_results.jsonl +4 -0
app.py CHANGED
@@ -4,84 +4,68 @@ import random
4
  from pathlib import Path
5
  import huggingface_hub
6
  from huggingface_hub import Repository
 
7
 
8
 
9
- paths_to_html = [Path(filename) for filename in Path("./html_files/").glob("*.html")]
10
- # path_to_html = random.choice(paths_to_html)
11
-
12
-
13
- # Show in webpage
14
-
15
- # written_by = {}
16
-
17
- # st.header(f"Judge this email")
18
- # with open(path_to_html, "r") as f:
19
- # html_data = f.read()
20
- # st.components.v1.html(html_data, height=2100)
21
-
22
- # written_by[path_to_html.as_posix()] = st.sidebar.radio(
23
- # "Written by a human or generated by AI ?",
24
- # ["Human", "AI"],
25
- # horizontal=True,
26
- # index=None, #random.randint(0, 1),
27
- # key=path_to_html,
28
- # )
29
- # # col1, col2 = st.columns([1,1])
30
- # with col1:
31
- # if st.button(label="Human", key=path_to_html.as_posix()+"human"):
32
- # written_by[path_to_html.as_posix()] = "human"
33
- # with col2:
34
- # if st.button(label="GenAI", key=path_to_html.as_posix()+"ai"):
35
- # written_by[path_to_html.as_posix()] = "ai"
36
-
37
- # st.components.v1.html(
38
- # """<hr style="height:10px;border:none;color:#333;background-color:#333;" /> """
39
- # )
40
- # st.stop()
41
 
42
  label_to_number = {"Human":0, "AI":1}
43
 
 
 
 
 
44
  if "html_idx" not in st.session_state:
45
  st.session_state.html_idx = 0
46
 
 
 
 
47
  if "html_ratings" not in st.session_state:
48
  st.session_state.html_ratings = {}
49
 
50
  # Display the image
51
- path_to_html = paths_to_html[st.session_state.html_idx]
52
  with open(path_to_html, "r") as f:
53
  html_data = f.read()
54
  st.components.v1.html(html_data, height=2100)
55
 
56
- if st.session_state["html_idx"] in st.session_state.html_ratings:
57
- rating = st.session_state.html_ratings[st.session_state.html_idx]
58
  else:
59
  rating = ""
60
 
61
- if rating := st.radio(
62
  "Written by a human or generated by AI ?",
63
  ["Human", "AI"],
64
  horizontal=True,
65
  index=None if rating == "" else label_to_number[rating],
66
  key=path_to_html,
67
  ):
68
- st.session_state.html_ratings[st.session_state.html_idx] = rating
69
- st.info("Submitted!")
 
 
70
 
71
- if st.button("Next email", key="next"):
72
  idx = st.session_state.html_idx
73
- idx = (idx + 1) % len(paths_to_html)
74
  st.session_state.html_idx = idx
75
- st.experimental_rerun()
 
76
 
77
- elif st.button("Previous email", key="previous"):
78
  idx = st.session_state.html_idx
79
- idx = (idx - 1) % len(paths_to_html)
80
  st.session_state.html_idx = idx
81
- st.experimental_rerun()
 
82
  else:
83
- if st.button("Quit", key="quit"):
84
  # send email with data or append to jsonl file
85
- st.success(f"Thank you for rating {len(st.session_state.html_ratings)} emails, you can close the webpage.")
 
 
 
86
 
87
- # st.session_state
 
4
  from pathlib import Path
5
  import huggingface_hub
6
  from huggingface_hub import Repository
7
+ import json
8
 
9
 
10
+ paths_to_html = random.shuffle([Path(filename) for filename in Path("./html_files/").glob("*.html")])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  label_to_number = {"Human":0, "AI":1}
13
 
14
+ if "paths_to_html" not in st.session_state:
15
+ st.session_state.paths_to_html = [Path(filename) for filename in Path("./html_files/").glob("*.html")]
16
+ random.shuffle(st.session_state.paths_to_html)
17
+
18
  if "html_idx" not in st.session_state:
19
  st.session_state.html_idx = 0
20
 
21
+ if "html_key" not in st.session_state:
22
+ st.session_state.html_key = ""
23
+
24
  if "html_ratings" not in st.session_state:
25
  st.session_state.html_ratings = {}
26
 
27
  # Display the image
28
+ path_to_html = st.session_state.paths_to_html[st.session_state.html_idx]
29
  with open(path_to_html, "r") as f:
30
  html_data = f.read()
31
  st.components.v1.html(html_data, height=2100)
32
 
33
+ if st.session_state["html_key"] in st.session_state.html_ratings:
34
+ rating = st.session_state.html_ratings[st.session_state["html_key"]]
35
  else:
36
  rating = ""
37
 
38
+ if rating := st.sidebar.radio(
39
  "Written by a human or generated by AI ?",
40
  ["Human", "AI"],
41
  horizontal=True,
42
  index=None if rating == "" else label_to_number[rating],
43
  key=path_to_html,
44
  ):
45
+ key = st.session_state.paths_to_html[st.session_state.html_idx].stem
46
+ st.session_state.html_key = key
47
+ st.session_state.html_ratings[key] = rating
48
+ st.sidebar.info("Submitted!")
49
 
50
+ if st.sidebar.button("Next email", key="next"):
51
  idx = st.session_state.html_idx
52
+ idx = (idx + 1) % len(st.session_state.paths_to_html)
53
  st.session_state.html_idx = idx
54
+ st.session_state.html_key = st.session_state.paths_to_html[st.session_state.html_idx].stem
55
+ st.rerun()
56
 
57
+ elif st.sidebar.button("Previous email", key="previous"):
58
  idx = st.session_state.html_idx
59
+ idx = (idx - 1) % len(st.session_state.paths_to_html)
60
  st.session_state.html_idx = idx
61
+ st.session_state.html_key = st.session_state.paths_to_html[st.session_state.html_idx].stem
62
+ st.rerun()
63
  else:
64
+ if st.sidebar.button("Quit", key="quit"):
65
  # send email with data or append to jsonl file
66
+ with open("rate_results.jsonl", "a", encoding="utf-8") as jsonl:
67
+ rating_data = {"user": "simon.nachtergaele@gmail.com", "ratings": st.session_state.html_ratings}
68
+ jsonl.write(json.dumps(rating_data, ensure_ascii=False) + "\n")
69
+ st.sidebar.success(f"Thank you for rating {len(st.session_state.html_ratings)} emails, you can close the webpage.")
70
 
71
+ st.session_state
poetry.lock ADDED
The diff for this file is too large to render. See raw diff
 
pyproject.toml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [tool.poetry]
2
+ name = "compare-ab"
3
+ version = "0.1.0"
4
+ description = "compare versions of html files"
5
+ authors = ["Lode Nachtergaele <you@example.comlode.nachtergaele@gmail.com>"]
6
+ license = "MIT"
7
+ readme = "README.md"
8
+
9
+ [tool.poetry.dependencies]
10
+ python = "^3.12"
11
+ streamlit = "^1.33.0"
12
+ watchdog = "^4.0.0"
13
+ huggingface-hub = "^0.22.2"
14
+
15
+
16
+ [build-system]
17
+ requires = ["poetry-core"]
18
+ build-backend = "poetry.core.masonry.api"
rate_results.jsonl ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {"user": "simon.nachtergaele@gmail.com", "ratings": {"newsletter_5pm_240304": "AI", "newsletter_5pm_240304_generated": "AI", "newsletter_5pm_240306_generated": "AI", "newsletter_5pm_240307": "Human"}}
2
+ {"user": "simon.nachtergaele@gmail.com", "ratings": {"newsletter_5pm_240222": "Human", "newsletter_5pm_240228_generated": "AI"}}
3
+ {"user": "simon.nachtergaele@gmail.com", "ratings": {"newsletter_5pm_240304_generated": "AI", "newsletter_5pm_240228": "AI"}}
4
+ {"user": "simon.nachtergaele@gmail.com", "ratings": {"newsletter_5pm_240214": "AI", "newsletter_5pm_240215": "AI", "newsletter_5pm_240306_generated": "AI"}}