Showcase your DataScience project with a WebApp
Have you ever wondered that why you are not able to showcase your machine learning and data science knowledge? Despite knowing every algorithm out there, having knowledge about data Analysis, Machine learning, Neural Networks and Reinforcement Learning you must find a way to better depict your outcomes and results to others. What could be a better way than developing an interactive webapp that will depict the results.
In this series, I am going to develop a Data Science webapp without complex structures (Django and Flask) and implementing only with a few lines of additional codes with Streamlit. Therefore, read the complete article to bring that into your skillset arsenal. I am going to show only a few lines of code to perform machine translation with the help of Hugging Face Transformers and Streamlit.
The first thing you need to do is install Transformer Library, Time library as well as the Streamlit Library. You can do that by pip or conda or anyway else you like. The next step is to open an editor such as sublime text or VS code or any other you like to use and import all of these libraries.
import transformers
import streamlit as st
import time
After importing the libraries, the next step is to define the model. The model that I will be using is the Helsinki model and use the pipeline text2text generation. In Hugging Face, there is an AutoTokenizer, Model and atlast a pipeline if you want to use them.
EN_FR_MODEL = "Helsinki-NLP/opus-mt-en-fr
en_fr_tokenizer = transformers.AutoTokenizer.from_pretrained(EN_FR_MODEL)
en_fr_model = transformers.AutoModelForSeq2SeqLM.from_pretrained(EN_FR_MODEL)
en_fr_translator = transformers.pipeline("text2text-generation",
model=en_fr_model, tokenizer=en_fr_tokenizer, device=0)"
This part is for English to French conversion, I build a function with conversion between English and French. Let's see the complete function:
def load_models():
# English to French
EN_FR_MODEL = "Helsinki-NLP/opus-mt-en-fr"
en_fr_tokenizer = transformers.AutoTokenizer.from_pretrained(EN_FR_MODEL)
en_fr_model = transformers.AutoModelForSeq2SeqLM.from_pretrained(EN_FR_MODEL)
en_fr_translator = transformers.pipeline("text2text-generation",
model=en_fr_model, tokenizer=en_fr_tokenizer, device=0)
# French to English
FR_EN_MODEL = "Helsinki-NLP/opus-mt-fr-en"
fr_en_tokenizer = transformers.AutoTokenizer.from_pretrained(FR_EN_MODEL)
fr_en_model = transformers.AutoModelForSeq2SeqLM.from_pretrained(FR_EN_MODEL)
fr_en_translator = transformers.pipeline("text2text-generation",
model=fr_en_model, tokenizer=fr_en_tokenizer, device=0)
return en_fr_translator, fr_en_translator
en_fr_translator, fr_en_translator = load_models()
After defining the models for both conversions, next part is to enter the text that you want to convert. For that I used a simple function to show the translation part:
def en_to_fr(input_text):
input_text = input_text.strip()
if len(input_text) > 0:
transation = en_fr_translator(input_text)[0]["generated_text"]
return transation.strip()
else:
return ""
def fr_to_en(input_text):
input_text = input_text.strip()
if len(input_text) > 0:
transation = fr_en_translator(input_text)[0]["generated_text"]
return transation.strip()
else:
return ""
After that a little bit designing with Streamlit is done. In which I entered title, a selection box for two languages and input text area:
Recommended by LinkedIn
st.title("English-French Translation App"
direction = st.selectbox("Direction", ["English -> French", "French -> English"])
input_text = st.text_area("Input text", value=""))
After making these settings, we will just call a function according to language conversion and we can also calculate time for calculation of the translation.
start_time = time.time()
if direction == "English -> French":
translation = en_to_fr(input_text)
else:
translation = fr_to_en(input_text)
end_time = time.time()
time_taken = str(round(end_time-start_time,2))
Atlast, I have shown the translation and time taken on the Streamlit app
st.markdown("**Translation**: "+translation
st.markdown("Time taken: "+str(time_taken)+"s"))
In this way, you can develop your own simple Streamlit Machine Translation app. To run this you must first save the file as .py file and run it with the following command in the prompt:
streamlit run app.py
To learn more about these you should also check the original documentations for Hugging Face and Streamlit from their websites. I have attached a link to both at the end of the article.
Thank you for reading the article, I am data Science Consultant and I help my clients make sense of their data as well as visualize and present that data in the most interactive ways. Let me know what you thought about the article, I really appreciate your feedback. You can follow the newsletter for more articles like this and contact me on LinkedIn as well here: