CRITICAL FIX: Remove broken locals() checks that prevented translation
Browse filesProblem: Translation always required Apertus-8B even for Coptic→English
Root cause: Streamlit's execution model means variables defined at module level
aren't in locals() when checked inside with/try blocks
Solution:
1. Initialize analysis_type, target_lang, target_language_name to None at top
2. Replace all 'variable in locals()' checks with direct None checks
3. Use 'analysis_type is not None' instead of 'analysis_type' in locals()'
4. Use 'target_lang == "en"' instead of 'target_lang in locals() and target_lang == "en"'
Changed lines:
- Line 293-295: Added variable initialization
- Line 524: Removed locals() check for dependency_parse
- Line 571: Removed locals() check for parse_and_translate
- Line 644: Changed to 'is not None' check
- Line 673: Removed locals() check for translation
- Line 675: Removed locals() check for target_lang
Now Coptic→English translation works WITHOUT requiring Apertus-8B API token!
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- apertus_ui.py +11 -6
|
@@ -289,8 +289,13 @@ LANGUAGES = {
|
|
| 289 |
|
| 290 |
st.set_page_config(page_title="Apertus Chat", layout="wide")
|
| 291 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
# Language selector
|
| 293 |
-
selected_lang = st.selectbox("Language / Langue / Idioma",
|
| 294 |
options=list(LANGUAGES.keys()),
|
| 295 |
format_func=lambda x: LANGUAGES[x])
|
| 296 |
|
|
@@ -516,7 +521,7 @@ for message in st.session_state.messages:
|
|
| 516 |
# User input
|
| 517 |
if prompt := st.chat_input("Type your message..."):
|
| 518 |
# Handle dependency parsing (doesn't need API token)
|
| 519 |
-
if selected_lang in ['cop', 'cop-sa', 'cop-bo'] and
|
| 520 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 521 |
|
| 522 |
with st.chat_message("user"):
|
|
@@ -563,7 +568,7 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 563 |
inference_client = get_inference_client(hf_token_input)
|
| 564 |
|
| 565 |
# Handle parse_and_translate mode
|
| 566 |
-
if selected_lang in ['cop', 'cop-sa', 'cop-bo'] and
|
| 567 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 568 |
|
| 569 |
with st.chat_message("user"):
|
|
@@ -636,7 +641,7 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 636 |
st.stop() # Special handling complete
|
| 637 |
|
| 638 |
# Standard translation/analysis handling
|
| 639 |
-
if selected_lang in ['cop', 'cop-sa', 'cop-bo'] and
|
| 640 |
# For translation, use raw text without prompt template
|
| 641 |
if analysis_type == 'translation':
|
| 642 |
full_prompt = prompt
|
|
@@ -665,9 +670,9 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 665 |
with st.chat_message("assistant"):
|
| 666 |
try:
|
| 667 |
# Check if this is a Coptic→English translation task
|
| 668 |
-
if selected_lang in ['cop', 'cop-sa', 'cop-bo'] and
|
| 669 |
# Use local Coptic translator (Norelad/coptic-megalaa-finetuned)
|
| 670 |
-
if
|
| 671 |
with st.spinner("🤖 Translating with local Coptic translator..."):
|
| 672 |
translation = translate_coptic_to_english(prompt, dialect=selected_lang)
|
| 673 |
st.markdown(translation)
|
|
|
|
| 289 |
|
| 290 |
st.set_page_config(page_title="Apertus Chat", layout="wide")
|
| 291 |
|
| 292 |
+
# Initialize variables (so they're accessible throughout the script)
|
| 293 |
+
analysis_type = None
|
| 294 |
+
target_lang = None
|
| 295 |
+
target_language_name = "English"
|
| 296 |
+
|
| 297 |
# Language selector
|
| 298 |
+
selected_lang = st.selectbox("Language / Langue / Idioma",
|
| 299 |
options=list(LANGUAGES.keys()),
|
| 300 |
format_func=lambda x: LANGUAGES[x])
|
| 301 |
|
|
|
|
| 521 |
# User input
|
| 522 |
if prompt := st.chat_input("Type your message..."):
|
| 523 |
# Handle dependency parsing (doesn't need API token)
|
| 524 |
+
if selected_lang in ['cop', 'cop-sa', 'cop-bo'] and analysis_type == 'dependency_parse':
|
| 525 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 526 |
|
| 527 |
with st.chat_message("user"):
|
|
|
|
| 568 |
inference_client = get_inference_client(hf_token_input)
|
| 569 |
|
| 570 |
# Handle parse_and_translate mode
|
| 571 |
+
if selected_lang in ['cop', 'cop-sa', 'cop-bo'] and analysis_type == 'parse_and_translate':
|
| 572 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 573 |
|
| 574 |
with st.chat_message("user"):
|
|
|
|
| 641 |
st.stop() # Special handling complete
|
| 642 |
|
| 643 |
# Standard translation/analysis handling
|
| 644 |
+
if selected_lang in ['cop', 'cop-sa', 'cop-bo'] and analysis_type is not None:
|
| 645 |
# For translation, use raw text without prompt template
|
| 646 |
if analysis_type == 'translation':
|
| 647 |
full_prompt = prompt
|
|
|
|
| 670 |
with st.chat_message("assistant"):
|
| 671 |
try:
|
| 672 |
# Check if this is a Coptic→English translation task
|
| 673 |
+
if selected_lang in ['cop', 'cop-sa', 'cop-bo'] and analysis_type == 'translation':
|
| 674 |
# Use local Coptic translator (Norelad/coptic-megalaa-finetuned)
|
| 675 |
+
if target_lang == 'en':
|
| 676 |
with st.spinner("🤖 Translating with local Coptic translator..."):
|
| 677 |
translation = translate_coptic_to_english(prompt, dialect=selected_lang)
|
| 678 |
st.markdown(translation)
|