Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
|
| 4 |
-
from peft import PeftModel
|
| 5 |
|
| 6 |
# -----------------------------
|
| 7 |
# PAGE CONFIG
|
|
@@ -23,17 +21,35 @@ THEME = st.session_state.theme
|
|
| 23 |
bg = "#111111" if THEME == "dark" else "#ffffff"
|
| 24 |
fg = "#ffffff" if THEME == "dark" else "#000000"
|
| 25 |
card_bg = "rgba(255,255,255,0.07)" if THEME == "dark" else "rgba(0,0,0,0.05)"
|
|
|
|
|
|
|
| 26 |
|
| 27 |
st.markdown(
|
| 28 |
f"""<style>
|
| 29 |
-
|
| 30 |
background-color: {bg};
|
| 31 |
color: {fg};
|
| 32 |
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
.dots-loader {{
|
| 34 |
display: flex;
|
| 35 |
justify-content: center;
|
| 36 |
-
margin
|
| 37 |
}}
|
| 38 |
.dots-loader div {{
|
| 39 |
width: 12px;
|
|
@@ -56,6 +72,7 @@ body {{
|
|
| 56 |
font-size: 18px;
|
| 57 |
min-height: 120px;
|
| 58 |
word-wrap: break-word;
|
|
|
|
| 59 |
}}
|
| 60 |
</style>""",
|
| 61 |
unsafe_allow_html=True
|
|
@@ -64,31 +81,11 @@ body {{
|
|
| 64 |
# -----------------------------
|
| 65 |
# LOAD MODEL
|
| 66 |
# -----------------------------
|
| 67 |
-
BASE_MODEL = "facebook/nllb-200-distilled-600M"
|
| 68 |
-
LORA_REPO = "junaid17/nllb-kurdish-lora"
|
| 69 |
-
|
| 70 |
@st.cache_resource
|
| 71 |
-
def
|
| 72 |
-
|
| 73 |
-
base = AutoModelForSeq2SeqLM.from_pretrained(BASE_MODEL)
|
| 74 |
-
model = PeftModel.from_pretrained(base, LORA_REPO)
|
| 75 |
-
return model, tokenizer
|
| 76 |
|
| 77 |
-
model
|
| 78 |
-
|
| 79 |
-
# -----------------------------
|
| 80 |
-
# TRANSLATION FUNCTION
|
| 81 |
-
# -----------------------------
|
| 82 |
-
def translate(text, src, tgt):
|
| 83 |
-
translator = pipeline(
|
| 84 |
-
"translation",
|
| 85 |
-
model=model,
|
| 86 |
-
tokenizer=tokenizer,
|
| 87 |
-
src_lang=src,
|
| 88 |
-
tgt_lang=tgt,
|
| 89 |
-
max_length=256,
|
| 90 |
-
)
|
| 91 |
-
return translator(text)[0]["translation_text"]
|
| 92 |
|
| 93 |
# -----------------------------
|
| 94 |
# CALLBACK FUNCTIONS
|
|
@@ -102,10 +99,6 @@ def swap_languages():
|
|
| 102 |
def toggle_theme():
|
| 103 |
st.session_state.theme = "dark" if st.session_state.theme == "light" else "light"
|
| 104 |
|
| 105 |
-
def copy_to_clipboard():
|
| 106 |
-
if st.session_state.output_text:
|
| 107 |
-
st.toast("β Text copied to clipboard!", icon="β
")
|
| 108 |
-
|
| 109 |
# -----------------------------
|
| 110 |
# UI
|
| 111 |
# -----------------------------
|
|
@@ -151,13 +144,22 @@ if st.button("Translate", type="primary", use_container_width=True):
|
|
| 151 |
if not text.strip():
|
| 152 |
st.warning("Please enter some text.")
|
| 153 |
else:
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
# Display output if available
|
| 163 |
if st.session_state.output_text:
|
|
@@ -167,9 +169,6 @@ if st.session_state.output_text:
|
|
| 167 |
unsafe_allow_html=True
|
| 168 |
)
|
| 169 |
|
| 170 |
-
# Copy
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
# Display the output in a code block that users can easily select and copy
|
| 174 |
-
st.code(st.session_state.output_text, language=None)
|
| 175 |
-
st.caption("π Select and copy the text above (Ctrl+C / Cmd+C)")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from backend import load_model, translate
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# -----------------------------
|
| 5 |
# PAGE CONFIG
|
|
|
|
| 21 |
bg = "#111111" if THEME == "dark" else "#ffffff"
|
| 22 |
fg = "#ffffff" if THEME == "dark" else "#000000"
|
| 23 |
card_bg = "rgba(255,255,255,0.07)" if THEME == "dark" else "rgba(0,0,0,0.05)"
|
| 24 |
+
input_bg = "#1e1e1e" if THEME == "dark" else "#f5f5f5"
|
| 25 |
+
button_bg = "#2d2d2d" if THEME == "dark" else "#e0e0e0"
|
| 26 |
|
| 27 |
st.markdown(
|
| 28 |
f"""<style>
|
| 29 |
+
[data-testid="stAppViewContainer"] {{
|
| 30 |
background-color: {bg};
|
| 31 |
color: {fg};
|
| 32 |
}}
|
| 33 |
+
[data-testid="stHeader"] {{
|
| 34 |
+
background-color: {bg};
|
| 35 |
+
}}
|
| 36 |
+
[data-testid="stToolbar"] {{
|
| 37 |
+
background-color: {bg};
|
| 38 |
+
}}
|
| 39 |
+
.stTextArea textarea {{
|
| 40 |
+
background-color: {input_bg} !important;
|
| 41 |
+
color: {fg} !important;
|
| 42 |
+
}}
|
| 43 |
+
.stSelectbox {{
|
| 44 |
+
color: {fg};
|
| 45 |
+
}}
|
| 46 |
+
h1, h2, h3, p, label {{
|
| 47 |
+
color: {fg} !important;
|
| 48 |
+
}}
|
| 49 |
.dots-loader {{
|
| 50 |
display: flex;
|
| 51 |
justify-content: center;
|
| 52 |
+
margin: 20px 0;
|
| 53 |
}}
|
| 54 |
.dots-loader div {{
|
| 55 |
width: 12px;
|
|
|
|
| 72 |
font-size: 18px;
|
| 73 |
min-height: 120px;
|
| 74 |
word-wrap: break-word;
|
| 75 |
+
color: {fg};
|
| 76 |
}}
|
| 77 |
</style>""",
|
| 78 |
unsafe_allow_html=True
|
|
|
|
| 81 |
# -----------------------------
|
| 82 |
# LOAD MODEL
|
| 83 |
# -----------------------------
|
|
|
|
|
|
|
|
|
|
| 84 |
@st.cache_resource
|
| 85 |
+
def get_model():
|
| 86 |
+
return load_model()
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
model = get_model()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# -----------------------------
|
| 91 |
# CALLBACK FUNCTIONS
|
|
|
|
| 99 |
def toggle_theme():
|
| 100 |
st.session_state.theme = "dark" if st.session_state.theme == "light" else "light"
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# -----------------------------
|
| 103 |
# UI
|
| 104 |
# -----------------------------
|
|
|
|
| 144 |
if not text.strip():
|
| 145 |
st.warning("Please enter some text.")
|
| 146 |
else:
|
| 147 |
+
# Show loader
|
| 148 |
+
loader_placeholder = st.empty()
|
| 149 |
+
loader_placeholder.markdown(
|
| 150 |
+
"<div class='dots-loader'><div></div><div></div><div></div></div>",
|
| 151 |
+
unsafe_allow_html=True
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
try:
|
| 155 |
+
output = translate(src_code, tgt_code, model, text)
|
| 156 |
+
st.session_state.output_text = output
|
| 157 |
+
except Exception as e:
|
| 158 |
+
st.error(f"Translation error: {str(e)}")
|
| 159 |
+
st.session_state.output_text = ""
|
| 160 |
+
finally:
|
| 161 |
+
# Remove loader
|
| 162 |
+
loader_placeholder.empty()
|
| 163 |
|
| 164 |
# Display output if available
|
| 165 |
if st.session_state.output_text:
|
|
|
|
| 169 |
unsafe_allow_html=True
|
| 170 |
)
|
| 171 |
|
| 172 |
+
# Copy section
|
| 173 |
+
st.code(st.session_state.output_text, language=None)
|
| 174 |
+
st.caption("π Select and copy the text above")
|
|
|
|
|
|
|
|
|