add app.py file with tab3 section
Browse files
app.py
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
import time
|
5 |
+
import random
|
6 |
+
import huggingface_hub as hf
|
7 |
+
import datasets
|
8 |
+
from datasets import load_dataset
|
9 |
+
from huggingface_hub import login
|
10 |
+
import openai
|
11 |
+
|
12 |
+
|
13 |
+
# File Path
|
14 |
+
DATA_PATH = "Dr-En-space-test.csv"
|
15 |
+
DATA_REPO = "M-A-D/dar-en-space-test"
|
16 |
+
|
17 |
+
st.set_page_config(layout="wide")
|
18 |
+
|
19 |
+
api = hf.HfApi()
|
20 |
+
access_token_write = "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"
|
21 |
+
login(token=access_token_write)
|
22 |
+
|
23 |
+
# Load data
|
24 |
+
def load_data():
|
25 |
+
return pd.DataFrame(load_dataset(DATA_REPO,download_mode="force_redownload",split='test'))
|
26 |
+
|
27 |
+
def save_data(data):
|
28 |
+
data.to_csv(DATA_PATH, index=False)
|
29 |
+
# to_save = datasets.Dataset.from_pandas(data)
|
30 |
+
api.upload_file(
|
31 |
+
path_or_fileobj="./Dr-En-space-test.csv",
|
32 |
+
path_in_repo="Dr-En-space-test.csv",
|
33 |
+
repo_id=DATA_REPO,
|
34 |
+
repo_type="dataset",
|
35 |
+
)
|
36 |
+
# to_save.push_to_hub(DATA_REPO)
|
37 |
+
|
38 |
+
def skip_correction():
|
39 |
+
noncorrected_sentences = st.session_state.data[(st.session_state.data.translated == True) & (st.session_state.data.corrected == False)]['sentence'].tolist()
|
40 |
+
if noncorrected_sentences:
|
41 |
+
st.session_state.orig_sentence = random.choice(noncorrected_sentences)
|
42 |
+
st.session_state.orig_translation = st.session_state.data[st.session_state.data.sentence == st.session_state.orig_sentence]['translation']
|
43 |
+
else:
|
44 |
+
st.session_state.orig_sentence = "No more sentences to be corrected"
|
45 |
+
st.session_state.orig_translation = "No more sentences to be corrected"
|
46 |
+
|
47 |
+
st.title("Darija Translation Corpus Collection")
|
48 |
+
|
49 |
+
if "data" not in st.session_state:
|
50 |
+
st.session_state.data = load_data()
|
51 |
+
|
52 |
+
if "sentence" not in st.session_state:
|
53 |
+
untranslated_sentences = st.session_state.data[st.session_state.data['translated'] == False]['sentence'].tolist()
|
54 |
+
if untranslated_sentences:
|
55 |
+
st.session_state.sentence = random.choice(untranslated_sentences)
|
56 |
+
else:
|
57 |
+
st.session_state.sentence = "No more sentences to translate"
|
58 |
+
|
59 |
+
if "orig_translation" not in st.session_state:
|
60 |
+
noncorrected_sentences = st.session_state.data[(st.session_state.data.translated == True) & (st.session_state.data.corrected == False)]['sentence'].tolist()
|
61 |
+
noncorrected_translations = st.session_state.data[(st.session_state.data.translated == True) & (st.session_state.data.corrected == False)]['translation'].tolist()
|
62 |
+
|
63 |
+
if noncorrected_sentences:
|
64 |
+
st.session_state.orig_sentence = random.choice(noncorrected_sentences)
|
65 |
+
st.session_state.orig_translation = st.session_state.data.loc[st.session_state.data.sentence == st.session_state.orig_sentence]['translation'].values[0]
|
66 |
+
else:
|
67 |
+
st.session_state.orig_sentence = "No more sentences to be corrected"
|
68 |
+
st.session_state.orig_translation = "No more sentences to be corrected"
|
69 |
+
|
70 |
+
if "user_translation" not in st.session_state:
|
71 |
+
st.session_state.user_translation = ""
|
72 |
+
|
73 |
+
|
74 |
+
with st.sidebar:
|
75 |
+
st.subheader("About")
|
76 |
+
st.markdown("""This is app is designed to collect Darija translation corpus.""")
|
77 |
+
|
78 |
+
# tab1, tab2 = st.tabs(["Translation", "Correction"])
|
79 |
+
tab1, tab2, tab3 = st.tabs(["Translation", "Correction", "Auto-Translate"])
|
80 |
+
|
81 |
+
with tab1:
|
82 |
+
with st.container():
|
83 |
+
st.subheader("Original Text:")
|
84 |
+
|
85 |
+
st.write('<div style="height: 150px; overflow: auto; border: 2px solid #ddd; padding: 10px; border-radius: 5px;">{}</div>'.format(st.session_state.sentence), unsafe_allow_html=True)
|
86 |
+
|
87 |
+
|
88 |
+
st.subheader("Translation:")
|
89 |
+
st.session_state.user_translation = st.text_area("Enter your translation here:", value=st.session_state.user_translation)
|
90 |
+
|
91 |
+
if st.button("💾 Save"):
|
92 |
+
if st.session_state.user_translation:
|
93 |
+
st.session_state.data.loc[st.session_state.data['sentence'] == st.session_state.sentence, 'translation'] = st.session_state.user_translation
|
94 |
+
st.session_state.data.loc[st.session_state.data['sentence'] == st.session_state.sentence, 'translated'] = True
|
95 |
+
save_data(st.session_state.data)
|
96 |
+
|
97 |
+
st.session_state.user_translation = "" # Reset the input value after saving
|
98 |
+
|
99 |
+
# st.toast("Saved!", icon="👏")
|
100 |
+
st.success("Saved!")
|
101 |
+
|
102 |
+
# Update the sentence for the next iteration.
|
103 |
+
untranslated_sentences = st.session_state.data[st.session_state.data['translated'] == False]['sentence'].tolist()
|
104 |
+
if untranslated_sentences:
|
105 |
+
st.session_state.sentence = random.choice(untranslated_sentences)
|
106 |
+
|
107 |
+
else:
|
108 |
+
st.session_state.sentence = "No more sentences to translate"
|
109 |
+
|
110 |
+
time.sleep(0.5)
|
111 |
+
# Rerun the app
|
112 |
+
st.rerun()
|
113 |
+
|
114 |
+
with tab2:
|
115 |
+
with st.container():
|
116 |
+
st.subheader("Original Darija Text:")
|
117 |
+
st.write('<div style="height: 150px; overflow: auto; border: 2px solid #ddd; padding: 10px; border-radius: 5px;">{}</div>'.format(st.session_state.orig_sentence), unsafe_allow_html=True)
|
118 |
+
|
119 |
+
with st.container():
|
120 |
+
st.subheader("Original English Translation:")
|
121 |
+
st.write('<div style="height: 150px; overflow: auto; border: 2px solid #ddd; padding: 10px; border-radius: 5px;">{}</div>'.format(st.session_state.orig_translation), unsafe_allow_html=True)
|
122 |
+
|
123 |
+
st.subheader("Corrected Darija Translation:")
|
124 |
+
corrected_translation = st.text_area("Enter the corrected Darija translation here:")
|
125 |
+
|
126 |
+
if st.button("💾 Save Translation"):
|
127 |
+
if corrected_translation:
|
128 |
+
st.session_state.data.loc[st.session_state.data['sentence'] == st.session_state.orig_sentence, 'translation'] = corrected_translation
|
129 |
+
st.session_state.data.loc[st.session_state.data['sentence'] == st.session_state.orig_sentence, 'correction'] = corrected_translation
|
130 |
+
st.session_state.data.loc[st.session_state.data['sentence'] == st.session_state.orig_sentence, 'corrected'] = True
|
131 |
+
save_data(st.session_state.data)
|
132 |
+
|
133 |
+
st.success("Saved!")
|
134 |
+
|
135 |
+
# Update the sentence for the next iteration.
|
136 |
+
noncorrected_sentences = st.session_state.data[(st.session_state.data.translated == True) & (st.session_state.data.corrected == False)]['sentence'].tolist()
|
137 |
+
# noncorrected_sentences = st.session_state.data[st.session_state.data['corrected'] == False]['sentence'].tolist()
|
138 |
+
if noncorrected_sentences:
|
139 |
+
st.session_state.orig_sentence = random.choice(noncorrected_sentences)
|
140 |
+
st.session_state.orig_translation = st.session_state.data[st.session_state.data.sentence == st.session_state.orig_sentence]['translation']
|
141 |
+
|
142 |
+
else:
|
143 |
+
st.session_state.orig_translation = "No more sentences to be corrected"
|
144 |
+
|
145 |
+
corrected_translation = "" # Reset the input value after saving
|
146 |
+
|
147 |
+
st.button("⏩ Skip to the Next Pair", key="skip_button", on_click=skip_correction)
|
148 |
+
|
149 |
+
with tab3:
|
150 |
+
st.subheader("Auto-Translate")
|
151 |
+
|
152 |
+
# User input for OpenAI API key
|
153 |
+
openai_api_key = st.text_input("Paste your OpenAI API key:")
|
154 |
+
|
155 |
+
if st.button("Auto-Translate 10 Samples"):
|
156 |
+
if openai_api_key:
|
157 |
+
openai.api_key = openai_api_key
|
158 |
+
|
159 |
+
# Get 10 samples from the dataset for translation
|
160 |
+
samples_to_translate = st.session_state.data.sample(10)['sentence'].tolist()
|
161 |
+
|
162 |
+
# Perform automatic translation using OpenAI GPT-4 model
|
163 |
+
auto_translations = [openai.Completion.create(
|
164 |
+
engine="text-davinci-002", # Change engine if needed
|
165 |
+
prompt=sentence,
|
166 |
+
max_tokens=50 # Adjust max_tokens as needed
|
167 |
+
)['choices'][0]['text'] for sentence in samples_to_translate]
|
168 |
+
|
169 |
+
# Update the dataset with auto-translations
|
170 |
+
st.session_state.data.loc[
|
171 |
+
st.session_state.data['sentence'].isin(samples_to_translate),
|
172 |
+
'translation'
|
173 |
+
] = auto_translations
|
174 |
+
|
175 |
+
# Save the updated dataset
|
176 |
+
save_data(st.session_state.data)
|
177 |
+
|
178 |
+
st.success("Auto-Translations saved!")
|
179 |
+
|
180 |
+
else:
|
181 |
+
st.warning("Please paste your OpenAI API key.")
|