Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,7 @@
|
|
|
|
|
|
|
|
|
|
1 |
"""
|
2 |
π£οΈ Translator - Translate text from one language to another.
|
3 |
Application file made with Streamlit.
|
@@ -15,23 +19,44 @@ from available_models import MODELS
|
|
15 |
|
16 |
st.set_page_config(page_title="Translator", page_icon="π£οΈ")
|
17 |
st.title("π£οΈ Translator")
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
st.write("To add a new model, hit me up! β¬οΈ")
|
20 |
|
21 |
with st.expander(label="β How does it work", expanded=True):
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
"Source Language", ["π«π· French"],
|
27 |
key="input_lang", index=1,
|
28 |
)
|
29 |
-
|
30 |
-
"Target Language", ["π¬π§ English"],
|
31 |
key="output_lang", index=0,
|
32 |
)
|
33 |
|
34 |
-
|
35 |
|
36 |
|
37 |
if selected_model[0] == None:
|
@@ -66,49 +91,9 @@ else:
|
|
66 |
|
67 |
st.success("π£οΈ Translated!")
|
68 |
st.write(f"**Translation:** {' '.join(text_to_translate)}")
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
import torch
|
78 |
-
from transformers import BartTokenizer, BartForConditionalGeneration
|
79 |
-
import gradio as gr
|
80 |
-
from transformers import AutoTokenizer, AutoModelWithLMHead, TranslationPipeline
|
81 |
-
from transformers import pipeline
|
82 |
-
|
83 |
-
pipe= pipeline('text2text-generation', model="SEBIS/legal_t5_small_trans_fr_en")
|
84 |
-
def generate_text(inp):
|
85 |
-
output=pipe(inp, forced_bos_token_id=pipe.tokenizer.get_lang_id('en'))
|
86 |
-
tln=output[0]
|
87 |
-
for item in tln:
|
88 |
-
result=tln[item]
|
89 |
-
return result
|
90 |
-
#Gradio Interface
|
91 |
-
|
92 |
-
model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
|
93 |
-
tokenizer = BartTokenizer.from_pretrained("facebook/bart-large-cnn")
|
94 |
-
|
95 |
-
def article_summarization(result):
|
96 |
-
input_ = str(result) + ' </s>'
|
97 |
-
# generate summary
|
98 |
-
input_ids = tokenizer.encode(result, return_tensors='pt')
|
99 |
-
summary_ids = model.generate(input_ids,
|
100 |
-
min_length=20,
|
101 |
-
max_length=12000)
|
102 |
-
|
103 |
-
summary_text = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
104 |
-
return(summary_text)
|
105 |
-
|
106 |
-
|
107 |
-
#iface = gr.Interface(fn=article_summarization,title="Summarization in English",description="facebook/bart-large-cnn for summarization in English", inputs=(lines=50,['text']), outputs=["text"])
|
108 |
-
|
109 |
-
iface = gr.Interface(fn=article_summarization,title="Summarization in English",description="facebook/bart-large-cnn for summarization in English", inputs=gr.inputs.Textbox(lines=50, placeholder="Enter newspaper article to be summarized"), outputs=["result"])
|
110 |
-
|
111 |
-
iface.launch()
|
112 |
-
|
113 |
-
|
114 |
-
'''
|
|
|
1 |
+
"raw
|
2 |
+
history
|
3 |
+
blame
|
4 |
+
5.44 kB
|
5 |
"""
|
6 |
π£οΈ Translator - Translate text from one language to another.
|
7 |
Application file made with Streamlit.
|
|
|
19 |
|
20 |
st.set_page_config(page_title="Translator", page_icon="π£οΈ")
|
21 |
st.title("π£οΈ Translator")
|
22 |
+
st.subheader("Translation made fast and easy.")
|
23 |
+
st.markdown("""
|
24 |
+
[](https://github.com/ChainYo)
|
25 |
+
[](https://huggingface.co/ChainYo)
|
26 |
+
[](https://www.linkedin.com/in/thomas-chaigneau-dev/)
|
27 |
+
[](https://discord.gg/)
|
28 |
+
""")
|
29 |
st.write("To add a new model, hit me up! β¬οΈ")
|
30 |
|
31 |
with st.expander(label="β How does it work", expanded=True):
|
32 |
+
st.markdown("""
|
33 |
+
**Translator** is a **simple tool** that allows you to **translate text** from one language to another.
|
34 |
+
**Translator** is powered by the [Transformers library](https://huggingface.co/transformers) and uses the
|
35 |
+
[Helsinki-NLP](https://huggingface.co/Helsinki-NLP) models.
|
36 |
+
Choose the **source language**, the **target language** and add some **text to translate**.
|
37 |
+
**Translator** will translate the text and **save the output in a text file**. It cuts the sentences by following
|
38 |
+
the punctuation marks.
|
39 |
+
The output file content will also be displayed in the browser to help you understand the translation and choose
|
40 |
+
if you want to download it.
|
41 |
+
There is **no limit to the number of characters** that can be translated.
|
42 |
+
The only limit is the time you are ready to wait! π€
|
43 |
+
*P.S. I have built this tool to help me start writing blog posts in different languages. I am a French native speaker
|
44 |
+
and I will use it to translate my potential future blog posts in English.*
|
45 |
+
*P.P.S. I am a **Junior ML Engineer** passionate about **machine learning** and **data science**. Reach out to me by
|
46 |
+
clicking on the socials badges above.*
|
47 |
+
""")
|
48 |
|
49 |
+
lang1, lang2 = st.columns(2)
|
50 |
+
lang1.selectbox(
|
51 |
+
"Source Language", ["π¬π§ English", "π«π· French", "π©πͺ German", "πͺπΈ Spanish", "π·πΊ Russian"],
|
52 |
key="input_lang", index=1,
|
53 |
)
|
54 |
+
lang2.selectbox(
|
55 |
+
"Target Language", ["π¬π§ English", "π«π· French", "π©πͺ German", "πͺπΈ Spanish", "π·πΊ Russian"],
|
56 |
key="output_lang", index=0,
|
57 |
)
|
58 |
|
59 |
+
selected_model = MODELS[f"{st.session_state['input_lang']}->{st.session_state['output_lang']}"]
|
60 |
|
61 |
|
62 |
if selected_model[0] == None:
|
|
|
91 |
|
92 |
st.success("π£οΈ Translated!")
|
93 |
st.write(f"**Translation:** {' '.join(text_to_translate)}")
|
94 |
+
st.download_button(
|
95 |
+
label="Download translated text",
|
96 |
+
data="\n".join(text_to_translate),
|
97 |
+
file_name=f"{st.session_state['input_lang']}-{st.session_state['output_lang']}-{datetime.now().strftime('%Y-%m-%d-%H-%M-%S')}.txt",
|
98 |
+
mime="text/plain"
|
99 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|