Imane Momayiz
commited on
Commit
·
eeeb878
1
Parent(s):
bcb5974
test commitscheduler
Browse files
app.py
CHANGED
@@ -2,8 +2,24 @@ import streamlit as st
|
|
2 |
from datasets import load_dataset
|
3 |
import datetime as dt
|
4 |
import random
|
|
|
5 |
import os
|
6 |
-
from huggingface_hub import HfApi
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
HF_API_KEY = os.environ.get("HF_TOKEN", None)
|
@@ -13,6 +29,16 @@ api = HfApi(token=HF_API_KEY)
|
|
13 |
REPO_ID = "imomayiz/darija-english"
|
14 |
DATASET_REPO_URL = f"https://huggingface.co/datasets/{REPO_ID}"
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def load_data(repo_id):
|
18 |
dataset = load_dataset(f'{repo_id}', name='sentences', split='sentences')
|
@@ -30,39 +56,37 @@ def fetch_sentence(dataset, column_name="darija_ar"):
|
|
30 |
|
31 |
return random_sentence
|
32 |
|
33 |
-
def store_submission(api: HfApi, sentence: str, translation: str, translation_fr: str):
|
34 |
-
|
|
|
|
|
|
|
35 |
ts = dt.datetime.now().strftime("%Y-%m-%d_%H-%M-%S-%f")
|
36 |
-
folder_path = "submissions"
|
37 |
-
os.makedirs(folder_path, exist_ok=True)
|
38 |
-
filename = os.path.join(folder_path, f"submissions_{ts}.txt")
|
39 |
-
|
40 |
-
with open(filename, "w", encoding="utf-8") as f:
|
41 |
-
f.write(f"darija,eng,darija_ar\n{sentence},{translation},{translation_fr}")
|
42 |
-
|
43 |
-
print(filename)
|
44 |
-
with open(filename, 'r') as f:
|
45 |
-
print(f.read())
|
46 |
|
47 |
-
|
48 |
-
#
|
49 |
-
# folder_path=folder_path,
|
50 |
-
# path_in_repo=folder_path,
|
51 |
-
# repo_id=REPO_ID,
|
52 |
-
# repo_type="dataset",
|
53 |
-
# commit_message="New submission",
|
54 |
-
# )
|
55 |
|
56 |
-
api.upload_file(
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
st.success(
|
64 |
f"""Translation submitted successfully to
|
65 |
-
{DATASET_REPO_URL}/tree/main/{
|
66 |
)
|
67 |
|
68 |
|
@@ -89,7 +113,7 @@ The translated sentence will be submitted to the dataset
|
|
89 |
[here](https://huggingface.co/datasets/imomayiz/darija-english)."""
|
90 |
)
|
91 |
|
92 |
-
st.
|
93 |
|
94 |
st.write(f"""
|
95 |
<div style="
|
@@ -126,7 +150,7 @@ with translation_input_placeholder_fr.container():
|
|
126 |
st.session_state.translation_input_fr = translation_input_fr
|
127 |
|
128 |
if st.button("Submit Translation"):
|
129 |
-
if not st.session_state.translation_input_fr
|
130 |
st.warning("Please enter a translation before submitting.")
|
131 |
else:
|
132 |
store_submission(api,
|
|
|
2 |
from datasets import load_dataset
|
3 |
import datetime as dt
|
4 |
import random
|
5 |
+
import json
|
6 |
import os
|
7 |
+
from huggingface_hub import HfApi, CommitScheduler
|
8 |
+
import uuid
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
def save_feedback(input_text:str, output_1: str, output_2:str, user_choice: int) -> None:
|
13 |
+
"""
|
14 |
+
Append input/outputs and user feedback to a JSON Lines file
|
15 |
+
using a thread lock to avoid concurrent writes from different users.
|
16 |
+
"""
|
17 |
+
with scheduler.lock:
|
18 |
+
with submissions_file.open("a") as f:
|
19 |
+
f.write(json.dumps({"input": input_text, "output_1": output_1, "output_2": output_2, "user_choice": user_choice}))
|
20 |
+
f.write("\n")
|
21 |
+
|
22 |
+
|
23 |
|
24 |
|
25 |
HF_API_KEY = os.environ.get("HF_TOKEN", None)
|
|
|
29 |
REPO_ID = "imomayiz/darija-english"
|
30 |
DATASET_REPO_URL = f"https://huggingface.co/datasets/{REPO_ID}"
|
31 |
|
32 |
+
submissions_folder = "submissions"
|
33 |
+
submissions_file = os.path.join(submissions_folder, f"submissions_{uuid.uuid4()}.json")
|
34 |
+
|
35 |
+
scheduler = CommitScheduler(
|
36 |
+
repo_id=REPO_ID,
|
37 |
+
repo_type="dataset",
|
38 |
+
folder_path=submissions_folder,
|
39 |
+
path_in_repo="submissions",
|
40 |
+
every=10,
|
41 |
+
)
|
42 |
|
43 |
def load_data(repo_id):
|
44 |
dataset = load_dataset(f'{repo_id}', name='sentences', split='sentences')
|
|
|
56 |
|
57 |
return random_sentence
|
58 |
|
59 |
+
def store_submission(api: HfApi, sentence: str, translation: str, translation_fr: str):
|
60 |
+
"""
|
61 |
+
Append input/outputs and user feedback to a JSON Lines file
|
62 |
+
using a thread lock to avoid concurrent writes from different users.
|
63 |
+
"""
|
64 |
ts = dt.datetime.now().strftime("%Y-%m-%d_%H-%M-%S-%f")
|
65 |
+
# folder_path = "submissions"
|
66 |
+
# os.makedirs(folder_path, exist_ok=True)
|
67 |
+
# filename = os.path.join(folder_path, f"submissions_{ts}.txt")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
|
69 |
+
# with open(filename, "w", encoding="utf-8") as f:
|
70 |
+
# f.write(f"darija,eng,darija_ar\n{sentence},{translation},{translation_fr}")
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
|
72 |
+
# api.upload_file(
|
73 |
+
# path_or_fileobj=filename,
|
74 |
+
# path_in_repo=filename,
|
75 |
+
# repo_id=REPO_ID,
|
76 |
+
# repo_type="dataset",
|
77 |
+
# )
|
78 |
+
|
79 |
+
with scheduler.lock:
|
80 |
+
with submissions_file.open("a") as f:
|
81 |
+
f.write(json.dumps({
|
82 |
+
"darija": translation_fr,
|
83 |
+
"eng": translation,
|
84 |
+
"darija_ar": sentence}))
|
85 |
+
f.write("\n")
|
86 |
+
|
87 |
st.success(
|
88 |
f"""Translation submitted successfully to
|
89 |
+
{DATASET_REPO_URL}/tree/main/{submissions_folder}"""
|
90 |
)
|
91 |
|
92 |
|
|
|
113 |
[here](https://huggingface.co/datasets/imomayiz/darija-english)."""
|
114 |
)
|
115 |
|
116 |
+
st.divider()
|
117 |
|
118 |
st.write(f"""
|
119 |
<div style="
|
|
|
150 |
st.session_state.translation_input_fr = translation_input_fr
|
151 |
|
152 |
if st.button("Submit Translation"):
|
153 |
+
if not st.session_state.translation_input_fr or st.session_state.translation_input:
|
154 |
st.warning("Please enter a translation before submitting.")
|
155 |
else:
|
156 |
store_submission(api,
|