Update app.py
Browse files
app.py
CHANGED
@@ -1,33 +1,126 @@
|
|
1 |
-
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
try:
|
17 |
-
data = request.get_json()
|
18 |
-
text = data['text']
|
19 |
|
20 |
-
|
21 |
-
|
|
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import re
|
3 |
|
4 |
+
# Function to load model
|
5 |
+
@st.cache(allow_output_mutation=True)
|
6 |
+
def load_model():
|
7 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained("abdulwaheed1/english-to-urdu-translation-mbart")
|
9 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("abdulwaheed1/english-to-urdu-translation-mbart")
|
10 |
+
return tokenizer, model
|
11 |
|
12 |
+
# Custom CSS to style the GUI
|
13 |
+
st.markdown(
|
14 |
+
"""
|
15 |
+
<style>
|
16 |
+
body {
|
17 |
+
font-family: Arial, sans-serif;
|
18 |
+
background-color: #4b6cb7;
|
19 |
+
color: #fff;
|
20 |
+
margin: 0;
|
21 |
+
padding: 0;
|
22 |
+
}
|
23 |
+
.container {
|
24 |
+
max-width: 800px;
|
25 |
+
margin: 50px auto;
|
26 |
+
background-color: #fff;
|
27 |
+
padding: 30px;
|
28 |
+
border-radius: 15px;
|
29 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
|
30 |
+
}
|
31 |
+
h1 {
|
32 |
+
color: #007bff;
|
33 |
+
text-align: center;
|
34 |
+
}
|
35 |
+
label, .text-white {
|
36 |
+
color: #333;
|
37 |
+
font-weight: bold;
|
38 |
+
}
|
39 |
+
textarea {
|
40 |
+
width: 100%;
|
41 |
+
resize: none;
|
42 |
+
border: 1px solid #ced4da;
|
43 |
+
padding: 10px;
|
44 |
+
margin-top: 10px;
|
45 |
+
}
|
46 |
+
button {
|
47 |
+
width: 100%;
|
48 |
+
cursor: pointer;
|
49 |
+
transition: background-color 0.3s ease, box-shadow 0.3s ease;
|
50 |
+
padding: 12px;
|
51 |
+
font-size: 18px;
|
52 |
+
border-radius: 8px;
|
53 |
+
border: none;
|
54 |
+
background-color: #007bff;
|
55 |
+
color: #fff;
|
56 |
+
}
|
57 |
+
button:hover {
|
58 |
+
background-color: #0056b3;
|
59 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
|
60 |
+
}
|
61 |
+
#translation-result {
|
62 |
+
margin-top: 10px;
|
63 |
+
border: 1px solid #ced4da;
|
64 |
+
padding: 10px;
|
65 |
+
width: 100%;
|
66 |
+
color: #333;
|
67 |
+
}
|
68 |
+
footer {
|
69 |
+
text-align: center;
|
70 |
+
margin-top: 30px;
|
71 |
+
}
|
72 |
+
</style>
|
73 |
+
""",
|
74 |
+
unsafe_allow_html=True,
|
75 |
+
)
|
76 |
|
77 |
+
# Function to preprocess text
|
78 |
+
def preprocess_text(text):
|
79 |
+
# Convert text to lowercase
|
80 |
+
text = text.lower()
|
81 |
+
# Remove HTML tags
|
82 |
+
text = re.sub(r'<[^>]*>', '', text)
|
83 |
+
# Remove special characters and extra spaces
|
84 |
+
text = re.sub(r'[^a-zA-Z0-9\s]', '', text)
|
85 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
86 |
+
return text
|
87 |
|
88 |
+
# Create or load model
|
89 |
+
tokenizer, model = load_model()
|
|
|
|
|
|
|
90 |
|
91 |
+
# Creating a container for the main content
|
92 |
+
with st.container():
|
93 |
+
st.title("English to Urdu Translation")
|
94 |
|
95 |
+
# Display input field for English text
|
96 |
+
st.subheader("Enter English Text:")
|
97 |
+
english_text = st.text_area("", height=200)
|
98 |
|
99 |
+
# Translate button
|
100 |
+
if st.button("Translate"):
|
101 |
+
if english_text:
|
102 |
+
# Preprocess text
|
103 |
+
english_text = preprocess_text(english_text)
|
104 |
|
105 |
+
# Tokenize input text
|
106 |
+
inputs = tokenizer(english_text, return_tensors="pt", max_length=1024, truncation=True)
|
107 |
|
108 |
+
# Generate translation
|
109 |
+
translation_ids = model.generate(**inputs)
|
110 |
+
translation = tokenizer.batch_decode(translation_ids, skip_special_tokens=True)[0]
|
111 |
+
|
112 |
+
# Display translated text
|
113 |
+
st.subheader("Translation:")
|
114 |
+
st.text_area("", value=translation, height=200)
|
115 |
+
else:
|
116 |
+
st.warning("Please enter some text to translate.")
|
117 |
+
|
118 |
+
# Footer
|
119 |
+
st.markdown(
|
120 |
+
"""
|
121 |
+
<footer>
|
122 |
+
<p>© 2024 Translation App. All rights reserved.</p>
|
123 |
+
</footer>
|
124 |
+
""",
|
125 |
+
unsafe_allow_html=True,
|
126 |
+
)
|