Skip to content

Commit

Permalink
Update app to use Serval creds rather than passcode. Add footnote abo…
Browse files Browse the repository at this point in the history
…ut IETF tags.
  • Loading branch information
Enkidu93 committed Oct 6, 2023
1 parent 38e332f commit 8c6f692
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 75 deletions.
151 changes: 82 additions & 69 deletions samples/ServalApp/serval_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,83 +5,96 @@
from sqlalchemy.orm import sessionmaker
from db import Build
from time import sleep

serval_auth = ServalBearerAuth()
client = RemoteCaller(url_prefix="http://localhost",auth=serval_auth)
engine = create_engine("sqlite:///builds.db")
Session = sessionmaker(bind=engine)
session = Session()

def submit():
engine = json.loads(client.translation_engines_create(TranslationEngineConfig(source_language=st.session_state['source_language'],target_language=st.session_state['target_language'],type='Nmt',name=f'serval_app_engine:{st.session_state["email"]}')))
source_file = json.loads(client.data_files_create(st.session_state['source_file'], format="Text"))
target_file = json.loads(client.data_files_create(st.session_state['target_file'], format="Text"))
corpus = json.loads(client.translation_engines_add_corpus(
engine['id'],
TranslationCorpusConfig(
source_files=[TranslationCorpusFileConfig(file_id=source_file['id'], text_id=st.session_state['source_file'].name)],
target_files=[TranslationCorpusFileConfig(file_id=target_file['id'], text_id=st.session_state['source_file'].name)],
source_language=st.session_state['source_language'],
target_language=st.session_state['target_language']
serval_auth = None
if not st.session_state.get('authorized',False):
with st.form(key="Authorization Form"):
st.session_state['client_id'] = st.text_input(label='Client ID')
st.session_state['client_secret'] = st.text_input(label='Client Secret', type='password')
if st.form_submit_button("Authorize"):
try:
serval_auth = ServalBearerAuth(client_id=st.session_state['client_id'] if st.session_state['client_id'] != "" else "<invalid>",client_secret=st.session_state['client_secret'] if st.session_state['client_secret'] != "" else "<invalid>")
st.session_state['authorized'] = True
st.rerun()
except ValueError:
st.error('Unable to authorize - please check your credentials')
else:
client = RemoteCaller(url_prefix="http://localhost",auth=serval_auth)
engine = create_engine("sqlite:///builds.db")
Session = sessionmaker(bind=engine)
session = Session()

def submit():
engine = json.loads(client.translation_engines_create(TranslationEngineConfig(source_language=st.session_state['source_language'],target_language=st.session_state['target_language'],type='Nmt',name=f'serval_app_engine:{st.session_state["email"]}')))
source_file = json.loads(client.data_files_create(st.session_state['source_file'], format="Text"))
target_file = json.loads(client.data_files_create(st.session_state['target_file'], format="Text"))
corpus = json.loads(client.translation_engines_add_corpus(
engine['id'],
TranslationCorpusConfig(
source_files=[TranslationCorpusFileConfig(file_id=source_file['id'], text_id=st.session_state['source_file'].name)],
target_files=[TranslationCorpusFileConfig(file_id=target_file['id'], text_id=st.session_state['source_file'].name)],
source_language=st.session_state['source_language'],
target_language=st.session_state['target_language']
)
)
)
)
build = json.loads(client.translation_engines_start_build(engine['id'], TranslationBuildConfig(pretranslate=[PretranslateCorpusConfig(corpus_id=corpus["id"], text_ids=[st.session_state['source_file'].name])])))
session.add(Build(build_id=build['id'],engine_id=engine['id'],email=st.session_state['email'],state=build['state'],corpus_id=corpus['id']))
session.commit()
build = json.loads(client.translation_engines_start_build(engine['id'], TranslationBuildConfig(pretranslate=[PretranslateCorpusConfig(corpus_id=corpus["id"], text_ids=[st.session_state['source_file'].name])])))
session.add(Build(build_id=build['id'],engine_id=engine['id'],email=st.session_state['email'],state=build['state'],corpus_id=corpus['id']))
session.commit()

def already_active_build_for(email:str):
return len(session.query(Build).where(Build.email == email).all()) > 0

def is_valid_passcode(passcode:str):
return passcode == os.environ.get('SERVAL_APP_PASSCODE')

def already_active_build_for(email:str):
return len(session.query(Build).where(Build.email == email).all()) > 0
st.subheader("Neural Machine Translation")

def is_valid_passcode(passcode:str):
return passcode == os.environ.get('SERVAL_APP_PASSCODE')
tried_to_submit = st.session_state.get('tried_to_submit', False)
with st.form(key="NmtTranslationForm"):
st.session_state['source_language'] = st.text_input(label="Source language tag*", placeholder="en")
if st.session_state.get('source_language','') == '' and tried_to_submit:
st.warning("Please enter a source language tag before submitting", icon='⬆️')

st.subheader("Neural Machine Translation")
st.session_state['source_file'] = st.file_uploader(label="Source File")
if st.session_state.get('source_file',None) is None and tried_to_submit:
st.warning("Please upload a source file before submitting", icon='⬆️')

tried_to_submit = st.session_state.get('tried_to_submit', False)
with st.form(key="NmtTranslationForm"):
st.session_state['source_language'] = st.text_input(label="Source language tag before submitting", placeholder="en")
if st.session_state.get('source_language','') == '' and tried_to_submit:
st.warning("Please enter a source language tag", icon='⬆️')
st.session_state['target_language'] = st.text_input(label="Target language tag*", placeholder="es")
if st.session_state.get('target_language','') == '' and tried_to_submit:
st.warning("Please enter a target language tag before submitting", icon='⬆️')

st.session_state['source_file'] = st.file_uploader(label="Source File")
if st.session_state.get('source_file',None) is None and tried_to_submit:
st.warning("Please upload a source file before submitting", icon='⬆️')
st.session_state['target_file'] = st.file_uploader(label="Target File")
if st.session_state.get('target_file',None) is None and tried_to_submit:
st.warning("Please upload a target file before submitting", icon='⬆️')

st.session_state['target_language'] = st.text_input(label="Target language tag", placeholder="es")
if st.session_state.get('target_language','') == '' and tried_to_submit:
st.warning("Please enter a target language tag before submitting", icon='⬆️')
st.session_state['email'] = st.text_input(label="Email", placeholder="[email protected]")
if st.session_state.get('email','') == '' and tried_to_submit:
st.warning("Please enter an email address", icon='⬆️')

st.session_state['target_file'] = st.file_uploader(label="Target File")
if st.session_state.get('target_file',None) is None and tried_to_submit:
st.warning("Please upload a target file before submitting", icon='⬆️')
st.session_state['passcode'] = st.text_input(label="Passcode", placeholder="")
if st.session_state.get('passcode','') == '' and tried_to_submit:
st.warning("Please enter the passcode", icon='⬆️')

st.session_state['email'] = st.text_input(label="Email", placeholder="[email protected]")
if st.session_state.get('email','') == '' and tried_to_submit:
st.warning("Please enter an email address", icon='⬆️')

st.session_state['passcode'] = st.text_input(label="Passcode", placeholder="")
if st.session_state.get('passcode','') == '' and tried_to_submit:
st.warning("Please enter the passcode", icon='⬆️')

if tried_to_submit:
st.error(st.session_state.get('error',"Something went wrong. Please try again in a moment."))
if st.form_submit_button("Generate translations"):
if not is_valid_passcode(st.session_state.get('passcode','')):
st.session_state['tried_to_submit'] = True
st.session_state['error'] = "The passcode was invalid."
st.rerun()
elif already_active_build_for(st.session_state['email']):
st.session_state['tried_to_submit'] = True
st.session_state['error'] = "There is already an a pending or active build associated with this email address. Please wait for the previous build to finish."
st.rerun()
elif st.session_state['source_language'] != '' and st.session_state['target_language'] != '' and st.session_state['source_file'] is not None and st.session_state['target_file'] is not None and st.session_state['email'] != '':
submit()
st.session_state['tried_to_submit'] = False
st.toast("Translations are on their way! You'll receive an email when your translation job has begun.")
sleep(4)
st.rerun()
else:
st.session_state['tried_to_submit'] = True
st.session_state['error'] = "Some required fields were left blank. Please fill in all fields above"
st.rerun()
if tried_to_submit:
st.error(st.session_state.get('error',"Something went wrong. Please try again in a moment."))
if st.form_submit_button("Generate translations"):
if not is_valid_passcode(st.session_state.get('passcode','')):
st.session_state['tried_to_submit'] = True
st.session_state['error'] = "The passcode was invalid."
st.rerun()
elif already_active_build_for(st.session_state['email']):
st.session_state['tried_to_submit'] = True
st.session_state['error'] = "There is already an a pending or active build associated with this email address. Please wait for the previous build to finish."
st.rerun()
elif st.session_state['source_language'] != '' and st.session_state['target_language'] != '' and st.session_state['source_file'] is not None and st.session_state['target_file'] is not None and st.session_state['email'] != '':
submit()
st.session_state['tried_to_submit'] = False
st.toast("Translations are on their way! You'll receive an email when your translation job has begun.")
sleep(4)
st.rerun()
else:
st.session_state['tried_to_submit'] = True
st.session_state['error'] = "Some required fields were left blank. Please fill in all fields above"
st.rerun()
st.markdown("<sub>\* Use IETF tags if possible. See [here](https://en.wikipedia.org/wiki/IETF_language_tag) for more information on IETF tags.</sub>", unsafe_allow_html=True)
12 changes: 6 additions & 6 deletions samples/ServalApp/serval_auth_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import time

class ServalBearerAuth(requests.auth.AuthBase):
def __init__(self):
self.__client_id = os.environ.get("SERVAL_CLIENT_ID")
def __init__(self, client_id="", client_secret=""):
self.__client_id = client_id if client_id != "" else os.environ.get("SERVAL_CLIENT_ID")
assert(self.__client_id is not None)
self.__client_secret = os.environ.get("SERVAL_CLIENT_SECRET")
self.__client_secret = client_secret if client_secret != "" else os.environ.get("SERVAL_CLIENT_SECRET")
assert(self.__client_secret is not None)
self.__auth_url = os.environ.get("SERVAL_AUTH_URL")
assert(self.__auth_url is not None)
Expand All @@ -18,7 +18,7 @@ def __call__(self, r):
self.update_token()
r.headers["authorization"] = "Bearer " + self.token
return r

def update_token(self):
data = {
"client_id": f"{self.__client_id}",
Expand All @@ -36,6 +36,6 @@ def update_token(self):
headers={"content-type": "application/json"}
)
self.token = r.json()['access_token'] if r is not None else None
except Exception as e:
raise ValueError(f"Token cannot be None. Failed to retrieve token from auth server; responded with {r.status}. Original exception: {e}")
except Exception as e:
raise ValueError(f"Token cannot be None. Failed to retrieve token from auth server; responded with {r.status_code if r is not None else '<unknown>'}. Original exception: {e}")

0 comments on commit 8c6f692

Please sign in to comment.