File size: 4,427 Bytes
b463033
 
 
 
b920afe
ca7a4e2
cfcf7a6
4bafcc6
683ecba
f009353
b463033
ca7a4e2
 
11ee225
 
b463033
3d634a0
683ecba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca7a4e2
 
 
 
 
 
 
b463033
b920afe
ca7a4e2
 
 
b920afe
 
b463033
 
 
b920afe
 
 
b463033
 
 
 
b920afe
b463033
 
 
 
b920afe
 
b463033
 
 
b920afe
b463033
 
 
 
 
 
b920afe
 
 
 
b463033
b920afe
b463033
b920afe
b463033
ca7a4e2
 
 
b920afe
b463033
b920afe
b463033
b920afe
b463033
ca7a4e2
 
 
b920afe
b463033
9e2f58f
b463033
ca7a4e2
 
 
c87d818
 
ca7a4e2
11ee225
ca7a4e2
 
cfcf7a6
 
 
 
 
 
be4028c
cfcf7a6
ca7a4e2
 
 
 
 
b463033
7b8c9e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import streamlit as st
import streamlit.components.v1 as components
import random
from pathlib import Path
import json
import os
from huggingface_hub import upload_file
from datetime import datetime
from cryptography.fernet import Fernet as F
from typing import Union

HF_TOKEN = os.environ.get("HF_TOKEN")
print("is none?", HF_TOKEN is None)
FERNET_KEY = os.environ.get("FERNET_KEY")
print("FERNET_KEY is none?", FERNET_KEY is None)

# https://stackoverflow.com/questions/61607367/how-to-encrypt-json-in-python

def encrypt_message(key: str, msg: Union[str, bytes]) -> str:
    encoded_key = key if isinstance(key, bytes) else key.encode()
    handler = F(encoded_key)
    encoded_msg = msg if isinstance(msg, bytes) else msg.encode()
    treatment = handler.encrypt(encoded_msg)
    return str(treatment, 'utf-8')

def decrypt_message(key: str, msg: Union[str, bytes]) -> str:
    encoded_key = key if isinstance(key, bytes) else key.encode()
    handler = F(encoded_key)
    encoded_msg = msg if isinstance(msg, bytes) else msg.encode()
    treatment = handler.decrypt(encoded_msg)
    return str(treatment, 'utf-8')

user = st.sidebar.text_input("User", "your.email@provider.com")

paths_to_html = random.shuffle(
    [Path(filename) for filename in Path("./html_files/").glob("*.html")]
)

label_to_number = {"Human": 0, "AI": 1}

if "paths_to_html" not in st.session_state:
    st.session_state.paths_to_html = [
        Path(filename) for filename in Path("./html_files/").glob("*.html")
    ]
    random.shuffle(st.session_state.paths_to_html)

if "html_idx" not in st.session_state:
    st.session_state.html_idx = 0

if "html_key" not in st.session_state:
    st.session_state.html_key = ""

if "html_ratings" not in st.session_state:
    st.session_state.html_ratings = {}

# Display the image
path_to_html = st.session_state.paths_to_html[st.session_state.html_idx]
with open(path_to_html, "r") as f:
    html_data = f.read()
st.components.v1.html(html_data, height=2100)

if st.session_state["html_key"] in st.session_state.html_ratings:
    rating = st.session_state.html_ratings[st.session_state["html_key"]]
else:
    rating = ""

if rating := st.sidebar.radio(
    "Written by a human or generated by AI ?",
    ["Human", "AI"],
    horizontal=True,
    index=None if rating == "" else label_to_number[rating],
    key=path_to_html,
):
    key = st.session_state.paths_to_html[st.session_state.html_idx].stem
    st.session_state.html_key = key
    st.session_state.html_ratings[key] = rating
    st.sidebar.info("Submitted!")

if st.sidebar.button("Next email", key="next"):
    idx = st.session_state.html_idx
    idx = (idx + 1) % len(st.session_state.paths_to_html)
    st.session_state.html_idx = idx
    st.session_state.html_key = st.session_state.paths_to_html[
        st.session_state.html_idx
    ].stem
    st.rerun()

elif st.sidebar.button("Previous email", key="previous"):
    idx = st.session_state.html_idx
    idx = (idx - 1) % len(st.session_state.paths_to_html)
    st.session_state.html_idx = idx
    st.session_state.html_key = st.session_state.paths_to_html[
        st.session_state.html_idx
    ].stem
    st.rerun()
else:
    if st.sidebar.button("Submit all votes", key="quit"):
        # send email with data or append to jsonl file
        if user == "your.email@provider.com":
            st.sidebar.error(f"Please provide your email adress")
        else:
            encrypted_user = encrypt_message(FERNET_KEY, user)
            assert user == decrypt_message(FERNET_KEY, encrypted_user), "decoded is different from original"
            with open("rate_results.jsonl", "a", encoding="utf-8") as jsonl:
                rating_data = {"user": encrypted_user, "ratings": st.session_state.html_ratings, "timestamp": datetime.now().isoformat()}
                jsonl.write(json.dumps(rating_data, ensure_ascii=False))
                jsonl.write("\n")
            upload_file(
                path_or_fileobj="rate_results.jsonl",
                path_in_repo="rate_results.jsonl",
                repo_id="simonn8/email_rater",
                repo_type="space",
                token=HF_TOKEN,
                commit_message=f"ratings by {encrypted_user}"
            )
            st.sidebar.success(
                f"Thank you for rating {len(st.session_state.html_ratings)} emails. Registered for user {user}. You can close the webpage."
            )



# st.session_state