Imane Momayiz
commited on
Commit
·
100b4b0
1
Parent(s):
f044621
fix: app functioning
Browse files
app.py
CHANGED
@@ -4,7 +4,7 @@ import csv
|
|
4 |
import datetime as dt
|
5 |
import random
|
6 |
import os
|
7 |
-
from huggingface_hub import
|
8 |
|
9 |
|
10 |
HF_API_KEY = os.environ.get("HF_TOKEN", None)
|
@@ -15,9 +15,6 @@ REPO_ID = "imomayiz/darija-english"
|
|
15 |
DATASET_REPO_URL = f"https://huggingface.co/datasets/{REPO_ID}"
|
16 |
SUBMISSIONS_DATA_FILE = os.path.join("submissions", "submissions.csv")
|
17 |
|
18 |
-
submissions_repo = Repository(
|
19 |
-
local_dir="submissions", clone_from=DATASET_REPO_URL, use_auth_token=HF_API_KEY
|
20 |
-
)
|
21 |
|
22 |
def load_data(repo_id):
|
23 |
dataset = load_dataset(f'{repo_id}', name='sentences', split='sentences')
|
@@ -32,26 +29,28 @@ def fetch_sentence(dataset, column_name="darija_ar"):
|
|
32 |
return random_sentence
|
33 |
|
34 |
def store_submission(sentence: str, translation: str, translation_fr: str):
|
|
|
35 |
if sentence and (translation or translation_fr):
|
36 |
-
with open(SUBMISSIONS_DATA_FILE, "a") as csvfile:
|
37 |
-
writer = csv.DictWriter(csvfile,
|
38 |
-
fieldnames=["darija", "eng", "darija_ar", "time"])
|
39 |
-
writer.writerow(
|
40 |
-
{"darija_ar": sentence,
|
41 |
-
"eng": translation,
|
42 |
-
"darija": translation_fr,
|
43 |
-
"time": str(dt.datetime.now())}
|
44 |
-
)
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
repo_id=REPO_ID,
|
51 |
repo_type="dataset",
|
52 |
commit_message="New submission",
|
53 |
)
|
54 |
-
st.success(
|
|
|
|
|
|
|
55 |
|
56 |
|
57 |
# Load the dataset
|
@@ -117,19 +116,13 @@ def main():
|
|
117 |
|
118 |
# Submit button
|
119 |
if st.button("Submit Translation"):
|
120 |
-
if translation_input:
|
121 |
-
st.success("Translation submitted successfully!")
|
122 |
-
|
123 |
-
elif translation_input_fr:
|
124 |
-
st.success("Translation submitted successfully!")
|
125 |
-
|
126 |
-
else:
|
127 |
st.warning("Please enter a translation before submitting.")
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
|
134 |
if __name__ == "__main__":
|
135 |
main()
|
|
|
4 |
import datetime as dt
|
5 |
import random
|
6 |
import os
|
7 |
+
from huggingface_hub import HfApi
|
8 |
|
9 |
|
10 |
HF_API_KEY = os.environ.get("HF_TOKEN", None)
|
|
|
15 |
DATASET_REPO_URL = f"https://huggingface.co/datasets/{REPO_ID}"
|
16 |
SUBMISSIONS_DATA_FILE = os.path.join("submissions", "submissions.csv")
|
17 |
|
|
|
|
|
|
|
18 |
|
19 |
def load_data(repo_id):
|
20 |
dataset = load_dataset(f'{repo_id}', name='sentences', split='sentences')
|
|
|
29 |
return random_sentence
|
30 |
|
31 |
def store_submission(sentence: str, translation: str, translation_fr: str):
|
32 |
+
|
33 |
if sentence and (translation or translation_fr):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
api.upload_folder(
|
44 |
+
folder_path=folder_path,
|
45 |
+
path_in_repo=folder_path,
|
46 |
repo_id=REPO_ID,
|
47 |
repo_type="dataset",
|
48 |
commit_message="New submission",
|
49 |
)
|
50 |
+
st.success(
|
51 |
+
f"""Translation submitted successfully to
|
52 |
+
{DATASET_REPO_URL}/tree/main/{folder_path}"""
|
53 |
+
)
|
54 |
|
55 |
|
56 |
# Load the dataset
|
|
|
116 |
|
117 |
# Submit button
|
118 |
if st.button("Submit Translation"):
|
119 |
+
if not translation_input and translation_input_fr:
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
st.warning("Please enter a translation before submitting.")
|
121 |
+
else:
|
122 |
+
with st.spinner("Submitting translation..."):
|
123 |
+
store_submission(st.session_state.sentence,
|
124 |
+
st.session_state.translation_input,
|
125 |
+
st.session_state.translation_input_fr)
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
main()
|