junaid17 commited on
Commit
08ca12e
Β·
verified Β·
1 Parent(s): 31cecd9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +76 -46
main.py CHANGED
@@ -4,24 +4,32 @@ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
4
  from peft import PeftModel
5
 
6
  # -----------------------------
7
- # PAGE CONFIG + STYLE
8
  # -----------------------------
9
  st.set_page_config(page_title="Kurdish Translator", layout="wide")
10
 
11
- THEME = st.session_state.get("theme", "light")
12
-
 
 
 
 
 
 
 
 
 
 
13
  bg = "#111111" if THEME == "dark" else "#ffffff"
14
  fg = "#ffffff" if THEME == "dark" else "#000000"
15
  card_bg = "rgba(255,255,255,0.07)" if THEME == "dark" else "rgba(0,0,0,0.05)"
16
 
17
  st.markdown(
18
- f"""
19
- <style>
20
  body {{
21
  background-color: {bg};
22
  color: {fg};
23
  }}
24
-
25
  .dots-loader {{
26
  display: flex;
27
  justify-content: center;
@@ -37,31 +45,28 @@ body {{
37
  }}
38
  .dots-loader div:nth-child(2) {{ animation-delay: 0.2s; }}
39
  .dots-loader div:nth-child(3) {{ animation-delay: 0.4s; }}
40
-
41
  @keyframes bounce {{
42
  from {{ transform: translateY(0); }}
43
  to {{ transform: translateY(-12px); }}
44
  }}
45
-
46
  .output-box {{
47
  padding: 20px;
48
  border-radius: 12px;
49
  background: {card_bg};
50
  font-size: 18px;
51
  min-height: 120px;
 
52
  }}
53
- </style>
54
- """,
55
  unsafe_allow_html=True
56
  )
57
 
58
  # -----------------------------
59
- # LOAD MODEL (NO LOADER HERE)
60
  # -----------------------------
61
  BASE_MODEL = "facebook/nllb-200-distilled-600M"
62
  LORA_REPO = "junaid17/nllb-kurdish-lora"
63
 
64
-
65
  @st.cache_resource
66
  def load_model():
67
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
@@ -69,10 +74,8 @@ def load_model():
69
  model = PeftModel.from_pretrained(base, LORA_REPO)
70
  return model, tokenizer
71
 
72
-
73
  model, tokenizer = load_model()
74
 
75
-
76
  # -----------------------------
77
  # TRANSLATION FUNCTION
78
  # -----------------------------
@@ -87,6 +90,21 @@ def translate(text, src, tgt):
87
  )
88
  return translator(text)[0]["translation_text"]
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  # -----------------------------
92
  # UI
@@ -96,55 +114,67 @@ st.title("Kurdish ↔ English Translator (NLLB + LoRA)")
96
  col1, colSwap, colTheme, col2 = st.columns([1, 0.6, 0.8, 1])
97
 
98
  with col1:
99
- src_lang = st.selectbox("From:", ["English", "Kurdish"])
 
 
 
 
100
 
101
  with colSwap:
102
- swap = st.button("⇆ Swap", use_container_width=True)
103
 
104
  with colTheme:
105
- toggle = st.button("πŸŒ™" if THEME == "light" else "β˜€οΈ", use_container_width=True)
 
106
 
107
  with col2:
108
- tgt_lang = st.selectbox("To:", ["Kurdish", "English"])
109
-
110
- # Swap
111
- if swap:
112
- src_lang, tgt_lang = tgt_lang, src_lang
113
- st.rerun()
114
-
115
- # Toggle theme
116
- if toggle:
117
- st.session_state["theme"] = "dark" if THEME == "light" else "light"
118
- st.rerun()
119
 
120
  lang_codes = {
121
  "English": "eng_Latn",
122
  "Kurdish": "ckb_Arab"
123
  }
124
 
125
- src_code = lang_codes[src_lang]
126
- tgt_code = lang_codes[tgt_lang]
127
 
128
  text = st.text_area("Enter text:", height=180)
129
 
130
-
131
  # -----------------------------
132
- # TRANSLATE BUTTON (with loader)
133
  # -----------------------------
134
- if st.button("Translate"):
135
  if not text.strip():
136
  st.warning("Please enter some text.")
137
  else:
138
- loader = st.markdown(
139
- "<div class='dots-loader'><div></div><div></div><div></div></div>",
140
- unsafe_allow_html=True
141
- )
142
-
143
- output = translate(text, src_code, tgt_code)
144
-
145
- loader.empty()
146
-
147
- st.subheader("Output")
148
- st.markdown(f"<div class='output-box'>{output}</div>", unsafe_allow_html=True)
149
-
150
- st.button("Copy Output", on_click=lambda: st.session_state.update({"copied": output}))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from peft import PeftModel
5
 
6
  # -----------------------------
7
+ # PAGE CONFIG
8
  # -----------------------------
9
  st.set_page_config(page_title="Kurdish Translator", layout="wide")
10
 
11
+ # Initialize session state
12
+ if "theme" not in st.session_state:
13
+ st.session_state.theme = "light"
14
+ if "src_lang" not in st.session_state:
15
+ st.session_state.src_lang = "English"
16
+ if "tgt_lang" not in st.session_state:
17
+ st.session_state.tgt_lang = "Kurdish"
18
+ if "output_text" not in st.session_state:
19
+ st.session_state.output_text = ""
20
+
21
+ # Theme colors
22
+ 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
  body {{
30
  background-color: {bg};
31
  color: {fg};
32
  }}
 
33
  .dots-loader {{
34
  display: flex;
35
  justify-content: center;
 
45
  }}
46
  .dots-loader div:nth-child(2) {{ animation-delay: 0.2s; }}
47
  .dots-loader div:nth-child(3) {{ animation-delay: 0.4s; }}
 
48
  @keyframes bounce {{
49
  from {{ transform: translateY(0); }}
50
  to {{ transform: translateY(-12px); }}
51
  }}
 
52
  .output-box {{
53
  padding: 20px;
54
  border-radius: 12px;
55
  background: {card_bg};
56
  font-size: 18px;
57
  min-height: 120px;
58
+ word-wrap: break-word;
59
  }}
60
+ </style>""",
 
61
  unsafe_allow_html=True
62
  )
63
 
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 load_model():
72
  tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
 
74
  model = PeftModel.from_pretrained(base, LORA_REPO)
75
  return model, tokenizer
76
 
 
77
  model, tokenizer = load_model()
78
 
 
79
  # -----------------------------
80
  # TRANSLATION FUNCTION
81
  # -----------------------------
 
90
  )
91
  return translator(text)[0]["translation_text"]
92
 
93
+ # -----------------------------
94
+ # CALLBACK FUNCTIONS
95
+ # -----------------------------
96
+ def swap_languages():
97
+ st.session_state.src_lang, st.session_state.tgt_lang = (
98
+ st.session_state.tgt_lang,
99
+ st.session_state.src_lang
100
+ )
101
+
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
 
114
  col1, colSwap, colTheme, col2 = st.columns([1, 0.6, 0.8, 1])
115
 
116
  with col1:
117
+ src_lang = st.selectbox(
118
+ "From:",
119
+ ["English", "Kurdish"],
120
+ key="src_lang"
121
+ )
122
 
123
  with colSwap:
124
+ st.button("⇆ Swap", use_container_width=True, on_click=swap_languages)
125
 
126
  with colTheme:
127
+ theme_icon = "πŸŒ™" if THEME == "light" else "β˜€οΈ"
128
+ st.button(theme_icon, use_container_width=True, on_click=toggle_theme)
129
 
130
  with col2:
131
+ tgt_lang = st.selectbox(
132
+ "To:",
133
+ ["Kurdish", "English"],
134
+ key="tgt_lang"
135
+ )
 
 
 
 
 
 
136
 
137
  lang_codes = {
138
  "English": "eng_Latn",
139
  "Kurdish": "ckb_Arab"
140
  }
141
 
142
+ src_code = lang_codes[st.session_state.src_lang]
143
+ tgt_code = lang_codes[st.session_state.tgt_lang]
144
 
145
  text = st.text_area("Enter text:", height=180)
146
 
 
147
  # -----------------------------
148
+ # TRANSLATE BUTTON
149
  # -----------------------------
150
+ 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
+ with st.spinner("Translating..."):
155
+ try:
156
+ output = translate(text, src_code, tgt_code)
157
+ st.session_state.output_text = output
158
+ except Exception as e:
159
+ st.error(f"Translation error: {str(e)}")
160
+ st.session_state.output_text = ""
161
+
162
+ # Display output if available
163
+ if st.session_state.output_text:
164
+ st.subheader("Output")
165
+ st.markdown(
166
+ f"<div class='output-box'>{st.session_state.output_text}</div>",
167
+ unsafe_allow_html=True
168
+ )
169
+
170
+ # Copy button with actual clipboard functionality
171
+ col_copy, col_empty = st.columns([1, 3])
172
+ with col_copy:
173
+ if st.button("πŸ“‹ Copy Output", use_container_width=True):
174
+ # Use Streamlit's native clipboard through components
175
+ st.write(f"""
176
+ <script>
177
+ navigator.clipboard.writeText(`{st.session_state.output_text}`);
178
+ </script>
179
+ """, unsafe_allow_html=True)
180
+ st.toast("βœ“ Copied to clipboard!", icon="βœ…")