Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
import json
|
4 |
+
from transformers import pipeline
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
# Initialize the translation pipeline
|
8 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
|
9 |
+
|
10 |
+
# Load the JSON data from the file
|
11 |
+
with open('language.json', 'r') as file:
|
12 |
+
language_data = json.load(file)
|
13 |
+
|
14 |
+
# Extract language names from the JSON data
|
15 |
+
language_names = [entry['Language'] for entry in language_data]
|
16 |
+
|
17 |
+
def get_FLORES_code_from_language(language):
|
18 |
+
for entry in language_data:
|
19 |
+
if entry['Language'].lower() == language.lower():
|
20 |
+
return entry['FLORES-200 code']
|
21 |
+
return None
|
22 |
+
|
23 |
+
def translate_text(text, destination_language):
|
24 |
+
dest_code = get_FLORES_code_from_language(destination_language)
|
25 |
+
if dest_code:
|
26 |
+
translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
|
27 |
+
return translation[0]["translation_text"]
|
28 |
+
else:
|
29 |
+
return "Destination language code not found."
|
30 |
+
|
31 |
+
# Custom CSS for styling
|
32 |
+
st.markdown("""
|
33 |
+
<style>
|
34 |
+
.main {
|
35 |
+
background-color: #f0f2f6;
|
36 |
+
color: #4f4f4f;
|
37 |
+
}
|
38 |
+
.title {
|
39 |
+
font-family: 'Arial', sans-serif;
|
40 |
+
color: #336699;
|
41 |
+
font-size: 32px;
|
42 |
+
}
|
43 |
+
.description {
|
44 |
+
font-family: 'Arial', sans-serif;
|
45 |
+
color: #4f4f4f;
|
46 |
+
font-size: 18px;
|
47 |
+
}
|
48 |
+
.footer {
|
49 |
+
font-family: 'Arial', sans-serif;
|
50 |
+
color: #999999;
|
51 |
+
font-size: 12px;
|
52 |
+
text-align: center;
|
53 |
+
}
|
54 |
+
.textbox {
|
55 |
+
font-family: 'Courier New', monospace;
|
56 |
+
color: #333333;
|
57 |
+
background-color: #ffffff;
|
58 |
+
}
|
59 |
+
</style>
|
60 |
+
""", unsafe_allow_html=True)
|
61 |
+
|
62 |
+
# App layout
|
63 |
+
st.markdown('<div class="title">🌐 Multi-language Translator</div>', unsafe_allow_html=True)
|
64 |
+
st.markdown('<div class="description">This application translates any English text to multiple languages with ease and efficiency.</div>', unsafe_allow_html=True)
|
65 |
+
|
66 |
+
# Display a header image
|
67 |
+
image = Image.open('translator.png')
|
68 |
+
st.image(image, use_column_width=True)
|
69 |
+
|
70 |
+
# Create a sidebar for navigation
|
71 |
+
st.sidebar.header("Translation Settings")
|
72 |
+
text_to_translate = st.sidebar.text_area("Input text to translate", height=150, key="input_text")
|
73 |
+
destination_language = st.sidebar.selectbox("Select Destination Language", language_names, key="language_select")
|
74 |
+
|
75 |
+
# Translate button
|
76 |
+
if st.sidebar.button("Translate", key="translate_button"):
|
77 |
+
if text_to_translate:
|
78 |
+
translated_text = translate_text(text_to_translate, destination_language)
|
79 |
+
st.markdown("### Translated text")
|
80 |
+
st.text_area("", value=translated_text, height=150, key="translated_text", help="This is the translated text.", class_="textbox")
|
81 |
+
else:
|
82 |
+
st.warning("Please enter text to translate")
|
83 |
+
|
84 |
+
# Footer
|
85 |
+
st.markdown('<div class="footer">Developed with ❤️ using Streamlit</div>', unsafe_allow_html=True)
|
86 |
+
|
87 |
+
# Instructions or any additional information
|
88 |
+
st.sidebar.write("Instructions")
|
89 |
+
st.sidebar.write("1. Enter the text you want to translate in the input box.")
|
90 |
+
st.sidebar.write("2. Select the desired language from the dropdown menu.")
|
91 |
+
st.sidebar.write("3. Click 'Translate' to get the translation.")
|