Imane Momayiz commited on
Commit
ce49f76
·
2 Parent(s): 9481a42 443c352

Merge branch 'main' of https://huggingface.co/spaces/imomayiz/DODa

Browse files
Files changed (1) hide show
  1. app.py +128 -0
app.py CHANGED
@@ -128,3 +128,131 @@ def main():
128
 
129
  if __name__ == "__main__":
130
  main()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
 
129
  if __name__ == "__main__":
130
  main()
131
+ import streamlit as st
132
+ from datasets import load_dataset, Dataset
133
+ import csv
134
+ import datetime as dt
135
+ import random
136
+ import os
137
+ from huggingface_hub import Repository
138
+
139
+
140
+ HF_API_KEY = os.environ.get("HF_TOKEN", None)
141
+
142
+ REPO_ID = "imomayiz/darija-english"
143
+ DATASET_REPO_URL = f"https://huggingface.co/datasets/{REPO_ID}"
144
+ DATA_FILE = os.path.join("submissions", "submissions.csv")
145
+
146
+ submissions_repo = Repository(
147
+ local_dir="submissions", clone_from=DATASET_REPO_URL, use_auth_token=HF_API_KEY
148
+ )
149
+
150
+ def load_data(repo_id):
151
+ dataset = load_dataset(f'{repo_id}', split='sentences')
152
+ return dataset
153
+
154
+ def get_sentence_from_dataset(dataset, column_name="darija_ar"):
155
+
156
+ # Get a random sentence
157
+ random_sentence_index = random.randint(0, len(dataset) - 1)
158
+ random_sentence = dataset[random_sentence_index][column_name]
159
+
160
+ return random_sentence
161
+
162
+ def store_submission(sentence: str, translation: str, translation_fr: str):
163
+ if sentence and (translation or translation_fr):
164
+ with open(DATA_FILE, "a") as csvfile:
165
+ writer = csv.DictWriter(csvfile,
166
+ fieldnames=["darija", "eng", "darija_ar", "time"])
167
+ writer.writerow(
168
+ {"darija_ar": sentence,
169
+ "eng": translation,
170
+ "darija": translation_fr,
171
+ "time": str(dt.datetime.now())}
172
+ )
173
+ commit_url = submissions_repo.push_to_hub()
174
+ print(commit_url)
175
+
176
+
177
+ # Load the dataset
178
+ dataset = load_data(REPO_ID)
179
+
180
+ def fetch_sentence():
181
+ st.session_state.sentence = get_sentence_from_dataset(dataset)
182
+ st.session_state.translation_input = ""
183
+
184
+
185
+ def main():
186
+
187
+ if "sentence" not in st.session_state:
188
+ st.session_state.sentence = get_sentence_from_dataset(dataset)
189
+ if 'translation_input' not in st.session_state:
190
+ st.session_state.translation_input = ""
191
+ if 'translation_input_fr' not in st.session_state:
192
+ st.session_state.translation_input_fr = ""
193
+ if 'display_new' not in st.session_state:
194
+ st.session_state.display_new = False
195
+
196
+ st.title("Translate From Arabic to English")
197
+
198
+ st.markdown(
199
+ """This mini-app allows you to contribute to the **darija-english** dataset
200
+ as part of [DODa](https://darija-open-dataset.github.io/)
201
+ project. To contribute, simply translate the given sentence from Arabic to English.
202
+ The translated sentence will be submitted to the dataset
203
+ [here](https://huggingface.co/datasets/imomayiz/darija-english)."""
204
+ )
205
+
206
+ st.text("")
207
+
208
+ st.write(f"""
209
+ <div style="
210
+ padding: 5px;
211
+ border: 1px solid #000000;
212
+ border-radius: 5px;
213
+ ">
214
+ <p style="font-size: 20px;">{st.session_state.sentence}.</p>
215
+ </div>""", unsafe_allow_html=True)
216
+
217
+
218
+ # Display new sentence button
219
+ st.session_state.display_new = st.button("New Sentence",
220
+ on_click=fetch_sentence)
221
+
222
+
223
+ # Input field for translation
224
+ translation_input_placeholder = st.empty()
225
+
226
+ with translation_input_placeholder.container():
227
+ translation_input = st.text_input("Enter translation to english: ",
228
+ st.session_state.translation_input)
229
+ st.session_state.translation_input = translation_input
230
+
231
+ # Input field for translation
232
+ translation_input_placeholder_fr = st.empty()
233
+
234
+ with translation_input_placeholder_fr.container():
235
+ translation_input_fr = st.text_input(
236
+ "Enter translation to darija in latin characters: ",
237
+ st.session_state.translation_input
238
+ )
239
+ st.session_state.translation_input_fr = translation_input_fr
240
+
241
+ # Submit button
242
+ if st.button("Submit Translation"):
243
+ if translation_input:
244
+ st.success("Translation submitted successfully!")
245
+
246
+ elif translation_input_fr:
247
+ st.success("Translation submitted successfully!")
248
+
249
+ else:
250
+ st.warning("Please enter a translation before submitting.")
251
+
252
+
253
+ store_submission(st.session_state.sentence,
254
+ st.session_state.translation_input,
255
+ st.session_state.translation_input_fr)
256
+
257
+ if __name__ == "__main__":
258
+ main()