Spaces:
Build error
Build error
Thomas Chaigneau
commited on
Commit
Β·
9592801
1
Parent(s):
954d57f
init app
Browse files- README.md +3 -3
- available_models.py +32 -0
- hugging.svg +19 -0
- main.py +88 -0
- requirements.txt +1 -0
README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
---
|
| 2 |
title: Translator
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: pink
|
| 6 |
sdk: streamlit
|
| 7 |
-
app_file:
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
|
|
|
| 1 |
---
|
| 2 |
title: Translator
|
| 3 |
+
emoji: π£οΈ
|
| 4 |
+
colorFrom: blue
|
| 5 |
colorTo: pink
|
| 6 |
sdk: streamlit
|
| 7 |
+
app_file: main.py
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
available_models.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""π£οΈ Translator - Translate text from one language to another.
|
| 2 |
+
|
| 3 |
+
MODELS list all available models.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
MODELS = {
|
| 7 |
+
"π¬π§ English->π¬π§ English": [0,],
|
| 8 |
+
"π¬π§ English->π«π· French": ["Helsinki-NLP/opus-mt-en-fr", "https://huggingface.co/Helsinki-NLP/opus-mt-en-fr"],
|
| 9 |
+
"π¬π§ English->π©πͺ German": ["Helsinki-NLP/opus-mt-en-de", "https://huggingface.co/Helsinki-NLP/opus-mt-en-de"],
|
| 10 |
+
"π¬π§ English->πͺπΈ Spanish": ["Helsinki-NLP/opus-mt-en-es", "https://huggingface.co/Helsinki-NLP/opus-mt-en-es"],
|
| 11 |
+
"π¬π§ English->π·πΊ Russian": ["Helsinki-NLP/opus-mt-en-ru", "https://huggingface.co/Helsinki-NLP/opus-mt-en-ru"],
|
| 12 |
+
"π«π· French->π«π· French": 0,
|
| 13 |
+
"π«π· French->π¬π§ English": ["Helsinki-NLP/opus-mt-fr-en", "https://huggingface.co/Helsinki-NLP/opus-mt-fr-en"],
|
| 14 |
+
"π«π· French->π©πͺ German": ["Helsinki-NLP/opus-mt-fr-de", "https://huggingface.co/Helsinki-NLP/opus-mt-fr-de"],
|
| 15 |
+
"π«π· French->πͺπΈ Spanish": ["Helsinki-NLP/opus-mt-fr-es", "https://huggingface.co/Helsinki-NLP/opus-mt-fr-es"],
|
| 16 |
+
"π«π· French->π·πΊ Russian": ["Helsinki-NLP/opus-mt-fr-ru", "https://huggingface.co/Helsinki-NLP/opus-mt-fr-ru"],
|
| 17 |
+
"πͺπΈ Spanish->πͺπΈ Spanish": 0,
|
| 18 |
+
"πͺπΈ Spanish->π¬π§ English": ["Helsinki-NLP/opus-mt-es-en", "https://huggingface.co/Helsinki-NLP/opus-mt-es-en"],
|
| 19 |
+
"πͺπΈ Spanish->π©πͺ German": ["Helsinki-NLP/opus-mt-es-de", "https://huggingface.co/Helsinki-NLP/opus-mt-es-de"],
|
| 20 |
+
"πͺπΈ Spanish->π«π· French": ["Helsinki-NLP/opus-mt-es-fr", "https://huggingface.co/Helsinki-NLP/opus-mt-es-fr"],
|
| 21 |
+
"πͺπΈ Spanish->π·πΊ Russian": ["Helsinki-NLP/opus-mt-es-ru", "https://huggingface.co/Helsinki-NLP/opus-mt-es-ru"],
|
| 22 |
+
"π©πͺ German->π©πͺ German": 0,
|
| 23 |
+
"π©πͺ German->π¬π§ English": ["Helsinki-NLP/opus-mt-de-en", "https://huggingface.co/Helsinki-NLP/opus-mt-de-en"],
|
| 24 |
+
"π©πͺ German->πͺπΈ Spanish": ["Helsinki-NLP/opus-mt-de-es", "https://huggingface.co/Helsinki-NLP/opus-mt-de-es"],
|
| 25 |
+
"π©πͺ German->π«π· French": ["Helsinki-NLP/opus-mt-de-fr", "https://huggingface.co/Helsinki-NLP/opus-mt-de-fr"],
|
| 26 |
+
"π©πͺ German->π·πΊ Russian": None,
|
| 27 |
+
"π·πΊ Russian->π·πΊ Russian": 0,
|
| 28 |
+
"π·πΊ Russian->π¬π§ English": ["Helsinki-NLP/opus-mt-ru-en", "https://huggingface.co/Helsinki-NLP/opus-mt-ru-en"],
|
| 29 |
+
"π·πΊ Russian->πͺπΈ Spanish": ["Helsinki-NLP/opus-mt-ru-es", "https://huggingface.co/Helsinki-NLP/opus-mt-ru-es"],
|
| 30 |
+
"π·πΊ Russian->π«π· French": ["Helsinki-NLP/opus-mt-ru-fr", "https://huggingface.co/Helsinki-NLP/opus-mt-ru-fr"],
|
| 31 |
+
"π·πΊ Russian->π©πͺ German": None,
|
| 32 |
+
}
|
hugging.svg
ADDED
|
|
main.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import streamlit as st
|
| 3 |
+
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from available_models import MODELS
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
st.set_page_config(page_title="Translator", page_icon="π£οΈ")
|
| 9 |
+
st.title("π£οΈ Translator")
|
| 10 |
+
st.markdown("""
|
| 11 |
+
[](https://github.com/ChainYo)
|
| 12 |
+
[](https://huggingface.co/ChainYo)
|
| 13 |
+
[](https://www.linkedin.com/in/thomas-chaigneau-dev/)
|
| 14 |
+
[](https://discord.gg/)
|
| 15 |
+
""")
|
| 16 |
+
st.subheader("Translation made fast and easy.")
|
| 17 |
+
|
| 18 |
+
with st.expander("β How does it work"):
|
| 19 |
+
st.markdown("""
|
| 20 |
+
**Translator** is a **simple tool** that allows you to **translate text** from one language to another.
|
| 21 |
+
|
| 22 |
+
**Translator** is powered by the [Transformers library](https://huggingface.co/transformers) and uses the
|
| 23 |
+
[Helsinki-NLP](https://huggingface.co/Helsinki-NLP) models.
|
| 24 |
+
|
| 25 |
+
Choose the **source language** and the **target language** and the **text to translate**.
|
| 26 |
+
|
| 27 |
+
**Translator** will translate the text and **save the output in a text file**. It cuts the text following
|
| 28 |
+
punctuation marks.
|
| 29 |
+
|
| 30 |
+
The output file content will also be displayed in the browser to help you to understand the translation and choose
|
| 31 |
+
if you want to download it.
|
| 32 |
+
|
| 33 |
+
There is **no limit to the number of characters** that can be translated.
|
| 34 |
+
The only limit is the time you are ready to wait! π€
|
| 35 |
+
|
| 36 |
+
*P.S. I have built this tool to help me to start writing blog posts in different languages. I am a French native speaker
|
| 37 |
+
and I will use it to translate my potential future blog posts in English.*
|
| 38 |
+
|
| 39 |
+
*P.P.S. I am an **AI developer** passionate about **machine learning** and **data science**. Reach me by clicking on
|
| 40 |
+
the socials badges above.*
|
| 41 |
+
""")
|
| 42 |
+
|
| 43 |
+
lang1, lang2 = st.columns(2)
|
| 44 |
+
lang1.selectbox(
|
| 45 |
+
"Source Language", ["π¬π§ English", "π«π· French", "π©πͺ German", "πͺπΈ Spanish", "π·πΊ Russian"],
|
| 46 |
+
key="input_lang", index=1,
|
| 47 |
+
)
|
| 48 |
+
lang2.selectbox(
|
| 49 |
+
"Target Language", ["π¬π§ English", "π«π· French", "π©πͺ German", "πͺπΈ Spanish", "π·πΊ Russian"],
|
| 50 |
+
key="output_lang", index=0,
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
selected_model = MODELS[f"{st.session_state['input_lang']}->{st.session_state['output_lang']}"]
|
| 54 |
+
|
| 55 |
+
st.markdown(f"""
|
| 56 |
+
**Selected model:** [{selected_model[0]}]({selected_model[1]})
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
if selected_model[0] == None:
|
| 60 |
+
st.write("No model available for this pair.")
|
| 61 |
+
elif selected_model[0] == 0:
|
| 62 |
+
st.write("No translation necessary.")
|
| 63 |
+
else:
|
| 64 |
+
input_text = st.text_area("Enter text to translate:", height=400, key="input")
|
| 65 |
+
translate_text = st.button("Translate")
|
| 66 |
+
|
| 67 |
+
if translate_text:
|
| 68 |
+
with st.spinner(text="βοΈ Model loading..."):
|
| 69 |
+
task = pipeline(
|
| 70 |
+
"translation",
|
| 71 |
+
model=selected_model[0],
|
| 72 |
+
tokenizer=selected_model[0],
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
progress_bar = st.progress(0)
|
| 76 |
+
with st.spinner(text="π Translating..."):
|
| 77 |
+
text_to_translate = re.split('(?<=[.!?]) +', input_text)
|
| 78 |
+
total_progress = len(text_to_translate)
|
| 79 |
+
|
| 80 |
+
for i, text in enumerate(text_to_translate):
|
| 81 |
+
translation = task(text)
|
| 82 |
+
text_to_translate[i] = translation[0]["translation_text"]
|
| 83 |
+
progress_bar.progress((i + 1) / total_progress)
|
| 84 |
+
|
| 85 |
+
st.success("π£οΈ Translated!")
|
| 86 |
+
st.session_state["translation_count"] += 1
|
| 87 |
+
st.write(f"**Translation:** {' '.join(text_to_translate)}")
|
| 88 |
+
st.download_button("Download translated text", "\n".join(text_to_translate), "text/plain")
|
requirements.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.3.1
|