Imane Momayiz
commited on
Commit
·
9481a42
1
Parent(s):
3905434
test
Browse files
app.py
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from datasets import load_dataset
|
3 |
+
import csv
|
4 |
+
import datetime as dt
|
5 |
+
import random
|
6 |
+
import os
|
7 |
+
from huggingface_hub import Repository
|
8 |
+
|
9 |
+
|
10 |
+
HF_API_KEY = os.environ.get("HF_TOKEN", None)
|
11 |
+
|
12 |
+
REPO_ID = "imomayiz/darija-english"
|
13 |
+
DATASET_REPO_URL = f"https://huggingface.co/datasets/{REPO_ID}"
|
14 |
+
DATA_FILE = os.path.join("submissions", "submissions.csv")
|
15 |
+
|
16 |
+
submissions_repo = Repository(
|
17 |
+
local_dir="submissions", clone_from=DATASET_REPO_URL, use_auth_token=HF_API_KEY
|
18 |
+
)
|
19 |
+
|
20 |
+
def load_data(repo_id):
|
21 |
+
dataset = load_dataset(f'{repo_id}', split='sentences')
|
22 |
+
return dataset
|
23 |
+
|
24 |
+
def fetch_sentence(dataset, column_name="darija_ar"):
|
25 |
+
|
26 |
+
# Get a random sentence
|
27 |
+
random_sentence_index = random.randint(0, len(dataset) - 1)
|
28 |
+
random_sentence = dataset[random_sentence_index][column_name]
|
29 |
+
|
30 |
+
return random_sentence
|
31 |
+
|
32 |
+
def store_submission(sentence: str, translation: str, translation_fr: str):
|
33 |
+
if sentence and (translation or translation_fr):
|
34 |
+
with open(DATA_FILE, "a") as csvfile:
|
35 |
+
writer = csv.DictWriter(csvfile,
|
36 |
+
fieldnames=["darija", "eng", "darija_ar", "time"])
|
37 |
+
writer.writerow(
|
38 |
+
{"darija_ar": sentence,
|
39 |
+
"eng": translation,
|
40 |
+
"darija": translation_fr,
|
41 |
+
"time": str(dt.datetime.now())}
|
42 |
+
)
|
43 |
+
commit_url = submissions_repo.push_to_hub(
|
44 |
+
repo_id=REPO_ID,
|
45 |
+
commit_message="Add new submission",
|
46 |
+
config_name="submissions",
|
47 |
+
token=HF_API_KEY
|
48 |
+
)
|
49 |
+
print(commit_url)
|
50 |
+
|
51 |
+
|
52 |
+
# Load the dataset
|
53 |
+
dataset = load_data(REPO_ID)
|
54 |
+
|
55 |
+
|
56 |
+
def main():
|
57 |
+
|
58 |
+
if "sentence" not in st.session_state:
|
59 |
+
st.session_state.sentence = fetch_sentence(dataset)
|
60 |
+
if 'translation_input' not in st.session_state:
|
61 |
+
st.session_state.translation_input = ""
|
62 |
+
if 'translation_input_fr' not in st.session_state:
|
63 |
+
st.session_state.translation_input_fr = ""
|
64 |
+
if 'display_new' not in st.session_state:
|
65 |
+
st.session_state.display_new = False
|
66 |
+
|
67 |
+
st.title("Translate From Arabic to English")
|
68 |
+
|
69 |
+
st.markdown(
|
70 |
+
"""This mini-app allows you to contribute to the **darija-english** dataset
|
71 |
+
as part of [DODa](https://darija-open-dataset.github.io/)
|
72 |
+
project. To contribute, simply translate the given sentence from Arabic to English.
|
73 |
+
The translated sentence will be submitted to the dataset
|
74 |
+
[here](https://huggingface.co/datasets/imomayiz/darija-english)."""
|
75 |
+
)
|
76 |
+
|
77 |
+
st.text("")
|
78 |
+
|
79 |
+
st.write(f"""
|
80 |
+
<div style="
|
81 |
+
padding: 5px;
|
82 |
+
border: 1px solid #000000;
|
83 |
+
border-radius: 5px;
|
84 |
+
">
|
85 |
+
<p style="font-size: 20px;">{st.session_state.sentence}.</p>
|
86 |
+
</div>""", unsafe_allow_html=True)
|
87 |
+
|
88 |
+
|
89 |
+
# Display new sentence button
|
90 |
+
st.session_state.display_new = st.button("New Sentence",
|
91 |
+
on_click=fetch_sentence,
|
92 |
+
args=(dataset,))
|
93 |
+
|
94 |
+
|
95 |
+
# Input field for translation
|
96 |
+
translation_input_placeholder = st.empty()
|
97 |
+
|
98 |
+
with translation_input_placeholder.container():
|
99 |
+
translation_input = st.text_input("Enter translation to english: ",
|
100 |
+
st.session_state.translation_input)
|
101 |
+
st.session_state.translation_input = translation_input
|
102 |
+
|
103 |
+
# Input field for translation
|
104 |
+
translation_input_placeholder_fr = st.empty()
|
105 |
+
|
106 |
+
with translation_input_placeholder_fr.container():
|
107 |
+
translation_input_fr = st.text_input(
|
108 |
+
"Enter translation to darija in latin characters: ",
|
109 |
+
st.session_state.translation_input_fr
|
110 |
+
)
|
111 |
+
st.session_state.translation_input_fr = translation_input_fr
|
112 |
+
|
113 |
+
# Submit button
|
114 |
+
if st.button("Submit Translation"):
|
115 |
+
if translation_input:
|
116 |
+
st.success("Translation submitted successfully!")
|
117 |
+
|
118 |
+
elif translation_input_fr:
|
119 |
+
st.success("Translation submitted successfully!")
|
120 |
+
|
121 |
+
else:
|
122 |
+
st.warning("Please enter a translation before submitting.")
|
123 |
+
|
124 |
+
|
125 |
+
store_submission(st.session_state.sentence,
|
126 |
+
st.session_state.translation_input,
|
127 |
+
st.session_state.translation_input_fr)
|
128 |
+
|
129 |
+
if __name__ == "__main__":
|
130 |
+
main()
|