Spaces:
Sleeping
Sleeping
File size: 3,375 Bytes
b463033 b920afe ca7a4e2 cfcf7a6 4bafcc6 b463033 ca7a4e2 b463033 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 b920afe b463033 ca7a4e2 4bafcc6 ca7a4e2 cfcf7a6 ca7a4e2 b463033 b920afe |
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 |
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
HF_TOKEN = os.environ.get("HF_TOKEN")
print("is none?", HF_TOKEN is None)
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("Quit", 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:
with open("rate_results.jsonl", "a", encoding="utf-8") as jsonl:
rating_data = {"user": 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 {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
|