DODa / app.py
Imane Momayiz
update
d9514f5
raw
history blame
2.51 kB
import os
import streamlit as st
from huggingface_hub import HfApi, CommitScheduler
from src.components import (
load_data, fetch_sentence, store_submission,
REPO_ID, submissions_folder)
from src.layout import INTRO_TEXT
# setup
HF_API_KEY = os.environ.get("HF_TOKEN", None)
api = HfApi(token=HF_API_KEY)
os.makedirs(submissions_folder, exist_ok=True)
# Create a commit scheduler
scheduler = CommitScheduler(
token=HF_API_KEY,
hf_api=api,
repo_id=REPO_ID,
repo_type="dataset",
folder_path=submissions_folder,
path_in_repo=submissions_folder,
every=1,
)
# Load the dataset
dataset = load_data(REPO_ID)
# Initialize session state
if "sentence" not in st.session_state:
st.session_state.sentence = fetch_sentence(dataset)
if 'translation_input' not in st.session_state:
st.session_state.translation_input = ""
if 'translation_input_fr' not in st.session_state:
st.session_state.translation_input_fr = ""
if 'display_new' not in st.session_state:
st.session_state.display_new = False
st.title("Translate From Arabic to English")
st.markdown(INTRO_TEXT, unsafe_allow_html=True)
st.divider()
st.write(f"""
<div style="
padding: 5px;
border: 1px solid #000000;
border-radius: 5px;
">
<p style="font-size: 20px;">{st.session_state.sentence}.</p>
</div>""", unsafe_allow_html=True)
# Display new sentence button
st.session_state.display_new = st.button("New Sentence",
on_click=fetch_sentence,
args=(dataset,))
# Input field for translation
translation_input = st.text_input(
"Enter translation to english: ",
st.session_state.translation_input
)
st.session_state.translation_input = translation_input
# Input field for translation in latin characters
translation_input_fr = st.text_input(
"Enter translation to darija in latin characters: ",
st.session_state.translation_input_fr
)
st.session_state.translation_input_fr = translation_input_fr
# Submit button
if st.button("Submit Translation"):
if st.session_state.translation_input_fr or st.session_state.translation_input:
store_submission(api,
st.session_state.sentence,
st.session_state.translation_input,
st.session_state.translation_input_fr
)
else:
st.warning("Please enter a translation before submitting.")