-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
63 lines (37 loc) · 1.81 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import streamlit as st
import numpy as np
from ibm_watson import LanguageTranslatorV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator, authenticator
# setting up the api key configuration
api_key = st.secrets["api_key"]
url = st.secrets["url"]
authenticator = IAMAuthenticator(apikey=api_key)
langtranslator = LanguageTranslatorV3(
version='2021-05-19', authenticator=authenticator)
langtranslator.set_service_url(url)
st.title("Language-Translator")
# setting up the dropdown list of the languages
option = st.selectbox(
'Which language would you choose to type',
('English', 'Arabic', 'Thai', 'German', 'Spanish', 'Korean'))
option1 = st.selectbox('Which language would you like to translate to',
('English', 'Arabic', 'Thai', 'German', 'Spanish', 'Korean'))
sent = "Enter the text in "+option+" language in the text-area provided below"
# setting up the dictionary of languages to their keywords
language_lib = {'English': 'en', 'Arabic': 'ar',
'Thai': 'th', 'Spanish': 'es', 'German': 'de', 'Korean': 'ko'}
sentence = st.text_area(sent, height=250)
if st.button("Translate"):
try:
if option == option1:
st.write("Please Select different Language for Translation")
else:
translate_code = language_lib[option]+'-'+language_lib[option1]
translation = langtranslator.translate(
text=sentence, model_id=translate_code)
ans = translation.get_result()['translations'][0]['translation']
sent1 = 'Translated text in '+option1+' language is shown below'
st.markdown(sent1)
st.write(ans)
except:
st.write("Please do cross check if text-area is filled with sentences or not")