Rogaton
Claude
commited on
Commit
·
c0bf168
1
Parent(s):
7e208b2
fix: Add missing coptic_keyboard module and update UI with LFS
Browse files- Add coptic_keyboard.py module (resolves ModuleNotFoundError)
- Replace apertus_ui.py with working standalone version
- Update Comprehensive_Coptic_Lexicon with full 12MB file via Git LFS
This fixes the deployment error and makes the Space functional.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- apertus_ui.py +108 -223
- coptic_keyboard.py +36 -0
apertus_ui.py
CHANGED
|
@@ -4,14 +4,10 @@ import torch
|
|
| 4 |
import os
|
| 5 |
import xml.etree.ElementTree as ET
|
| 6 |
import re
|
| 7 |
-
from coptic_keyboard import coptic_keyboard
|
| 8 |
-
from coptic_morphology import analyze_coptic_morphology, CopticMorphologyTokenizer
|
| 9 |
-
from morphology_informed_translation import get_morphology_enhanced_translation
|
| 10 |
|
| 11 |
-
|
| 12 |
-
#Coptic alphabet helper
|
| 13 |
COPTIC_ALPHABET = {
|
| 14 |
-
|
| 15 |
'Ⲏ': 'Eta', 'Ⲑ': 'Theta', 'Ⲓ': 'Iota', 'Ⲕ': 'Kappa', 'Ⲗ': 'Lambda', 'Ⲙ': 'Mu',
|
| 16 |
'Ⲛ': 'Nu', 'Ⲝ': 'Xi', 'Ⲟ': 'Omicron', 'Ⲡ': 'Pi', 'Ⲣ': 'Rho', 'Ⲥ': 'Sigma',
|
| 17 |
'Ⲧ': 'Tau', 'Ⲩ': 'Upsilon', 'Ⲫ': 'Phi', 'Ⲭ': 'Chi', 'Ⲯ': 'Psi', 'Ⲱ': 'Omega',
|
|
@@ -48,37 +44,34 @@ def load_coptic_lexicon(file_path=None):
|
|
| 48 |
# Find entries in TEI format
|
| 49 |
entries = root.findall('.//tei:entry', ns)
|
| 50 |
|
| 51 |
-
for entry in entries: #
|
| 52 |
coptic_word = ""
|
| 53 |
definition = ""
|
| 54 |
|
| 55 |
# Extract Coptic headword from TEI structure
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
# Extract definition - try multiple approaches
|
| 62 |
-
definition = ""
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
cit_elems = entry.findall('.//tei:cit', ns)
|
| 73 |
-
if cit_elems:
|
| 74 |
-
definitions = [c.text.strip() for c in cit_elems if c.text]
|
| 75 |
-
definition = "; ".join(definitions[:2])
|
| 76 |
|
| 77 |
-
#
|
| 78 |
if coptic_word and definition:
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
|
| 83 |
# Handle text formats
|
| 84 |
else:
|
|
@@ -107,17 +100,19 @@ def load_coptic_lexicon(file_path=None):
|
|
| 107 |
|
| 108 |
return lexicon
|
| 109 |
|
| 110 |
-
#
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
-
#
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
with col2:
|
| 118 |
-
target_lang = st.selectbox("**Target Language:**",
|
| 119 |
-
["English", "Français", "Deutsch", "Español"],
|
| 120 |
-
key="target_language")
|
| 121 |
|
| 122 |
# Sidebar for Coptic tools
|
| 123 |
with st.sidebar:
|
|
@@ -152,100 +147,60 @@ with st.sidebar:
|
|
| 152 |
for letter, name in COPTIC_ALPHABET.items():
|
| 153 |
st.text(f"{letter} - {name}")
|
| 154 |
|
| 155 |
-
# Lexicon search
|
| 156 |
if coptic_lexicon:
|
| 157 |
st.subheader("Lexicon Search")
|
| 158 |
|
| 159 |
-
#
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
key="search_method")
|
| 163 |
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
search_term += transliteration_map.get(char, char)
|
| 182 |
-
st.write(f"**Searching for:** {search_term}")
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
pasted_text = st.text_input("Paste Coptic text:", key="lexicon_coptic")
|
| 187 |
-
|
| 188 |
-
if pasted_text:
|
| 189 |
-
# Check if it contains Coptic Unicode
|
| 190 |
-
is_coptic = any(0x2C80 <= ord(char) <= 0x2CFF for char in pasted_text)
|
| 191 |
-
|
| 192 |
-
if is_coptic:
|
| 193 |
-
st.success("✅ Coptic Unicode detected")
|
| 194 |
-
search_term = pasted_text
|
| 195 |
-
else:
|
| 196 |
-
st.warning("⚠️ Converting PDF text to Coptic Unicode")
|
| 197 |
-
|
| 198 |
-
# Convert common PDF/Greek characters to Coptic
|
| 199 |
-
pdf_to_coptic = {
|
| 200 |
-
'α': 'ⲁ', 'β': 'ⲃ', 'γ': 'ⲅ', 'δ': 'ⲇ', 'ε': 'ⲉ', 'ζ': 'ⲍ',
|
| 201 |
-
'η': 'ⲏ', 'θ': 'ⲑ', 'ι': 'ⲓ', 'κ': 'ⲕ', 'λ': 'ⲗ', 'μ': 'ⲙ',
|
| 202 |
-
'ν': 'ⲛ', 'ξ': 'ⲝ', 'ο': 'ⲟ', 'π': 'ⲡ', 'ρ': 'ⲣ', 'σ': 'ⲥ',
|
| 203 |
-
'τ': 'ⲧ', 'υ': 'ⲩ', 'φ': 'ⲫ', 'χ': 'ⲭ', 'ψ': 'ⲯ', 'ω': 'ⲱ',
|
| 204 |
-
'ς': 'ⲥ', 'ϣ': 'ϣ', 'ϥ': 'ϥ', 'ϧ': 'ϧ', 'ϩ': 'ϩ', 'ϫ': 'ϫ', 'ϭ': 'ϭ', 'ϯ': 'ϯ',
|
| 205 |
-
# Latin fallbacks
|
| 206 |
-
'a': 'ⲁ', 'b': 'ⲃ', 'g': 'ⲅ', 'd': 'ⲇ', 'e': 'ⲉ', 'z': 'ⲍ',
|
| 207 |
-
'h': 'ⲏ', 'q': 'ⲑ', 'i': 'ⲓ', 'k': 'ⲕ', 'l': 'ⲗ', 'm': 'ⲙ',
|
| 208 |
-
'n': 'ⲛ', 'x': 'ⲝ', 'o': 'ⲟ', 'p': 'ⲡ', 'r': 'ⲣ', 's': 'ⲥ',
|
| 209 |
-
't': 'ⲧ', 'u': 'ⲩ', 'f': 'ⲫ', 'c': 'ⲭ', 'y': 'ⲯ', 'w': 'ⲱ'
|
| 210 |
-
}
|
| 211 |
-
|
| 212 |
-
converted = ""
|
| 213 |
-
for char in pasted_text:
|
| 214 |
-
converted += pdf_to_coptic.get(char, char)
|
| 215 |
-
|
| 216 |
-
search_term = converted
|
| 217 |
-
st.write(f"**Converted to:** {converted}")
|
| 218 |
|
| 219 |
-
# Perform search
|
| 220 |
if search_term:
|
| 221 |
-
# Exact match first
|
| 222 |
if search_term in coptic_lexicon:
|
| 223 |
-
st.
|
| 224 |
-
st.
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
st.divider()
|
| 235 |
-
|
| 236 |
-
# Contains matches
|
| 237 |
-
contains = [k for k in coptic_lexicon.keys() if search_term in k and not k.startswith(search_term)]
|
| 238 |
-
if contains:
|
| 239 |
-
st.write("**Words containing your search:**")
|
| 240 |
-
for match in contains[:5]:
|
| 241 |
-
with st.expander(f"📖 {match}"):
|
| 242 |
-
st.write(coptic_lexicon[match])
|
| 243 |
-
|
| 244 |
-
# If no matches at all
|
| 245 |
-
if not (search_term in coptic_lexicon or starts_with or contains):
|
| 246 |
-
st.error("❌ No matches found in lexicon")
|
| 247 |
-
st.info(f"Searched for: **{search_term}** | Available entries: {len(coptic_lexicon)}")
|
| 248 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
# Load model (cached)
|
| 250 |
@st.cache_resource
|
| 251 |
def load_model():
|
|
@@ -260,73 +215,6 @@ def load_model():
|
|
| 260 |
|
| 261 |
tokenizer, model = load_model()
|
| 262 |
|
| 263 |
-
# Check if model loaded successfully
|
| 264 |
-
if tokenizer is None or model is None:
|
| 265 |
-
st.error("❌ Model failed to load. Translation unavailable.")
|
| 266 |
-
st.stop()
|
| 267 |
-
|
| 268 |
-
# Morphological Analysis Section
|
| 269 |
-
st.subheader("🔍 Morphological Analysis")
|
| 270 |
-
|
| 271 |
-
morph_text = st.text_area(
|
| 272 |
-
"Enter Coptic text for morphological analysis:",
|
| 273 |
-
height=100,
|
| 274 |
-
placeholder="ⲡⲉϫⲉⲡⲛⲟⲩⲧⲉⲛⲛⲁϩⲣⲛⲡⲓⲥⲣⲁⲏⲗ..."
|
| 275 |
-
)
|
| 276 |
-
|
| 277 |
-
if st.button("Analyze Morphology"):
|
| 278 |
-
if morph_text.strip():
|
| 279 |
-
with st.spinner("Analyzing morphology..."):
|
| 280 |
-
analysis = analyze_coptic_morphology(morph_text)
|
| 281 |
-
|
| 282 |
-
st.subheader("Morphological Breakdown:")
|
| 283 |
-
st.text(analysis)
|
| 284 |
-
|
| 285 |
-
with st.expander("Detailed Analysis"):
|
| 286 |
-
tokenizer_morph = CopticMorphologyTokenizer()
|
| 287 |
-
analyses = tokenizer_morph.tokenize_text(morph_text)
|
| 288 |
-
|
| 289 |
-
for i, word_analysis in enumerate(analyses):
|
| 290 |
-
if word_analysis['morphemes']:
|
| 291 |
-
st.write(f"**Word {i+1}: {word_analysis['word']}**")
|
| 292 |
-
for morpheme in word_analysis['morphemes']:
|
| 293 |
-
st.write(f" - {morpheme['form']} ({morpheme['type']}: {morpheme['function']})")
|
| 294 |
-
st.write("---")
|
| 295 |
-
else:
|
| 296 |
-
st.warning("Please enter some Coptic text to analyze.")
|
| 297 |
-
|
| 298 |
-
# Enhanced translation with morphological context
|
| 299 |
-
col1, col2 = st.columns(2)
|
| 300 |
-
with col1:
|
| 301 |
-
if st.button("🧠 Enhanced Translation (with morphology)"):
|
| 302 |
-
if morph_text.strip():
|
| 303 |
-
with st.spinner("Generating morphology-enhanced translation..."):
|
| 304 |
-
enhanced_translation = get_morphology_enhanced_translation(
|
| 305 |
-
morph_text, tokenizer, model, "English"
|
| 306 |
-
)
|
| 307 |
-
st.subheader("Enhanced Translation:")
|
| 308 |
-
st.write(enhanced_translation)
|
| 309 |
-
else:
|
| 310 |
-
st.warning("Please enter Coptic text first.")
|
| 311 |
-
|
| 312 |
-
with col2:
|
| 313 |
-
if st.button("📝 Standard Translation"):
|
| 314 |
-
if morph_text.strip():
|
| 315 |
-
with st.spinner("Generating standard translation..."):
|
| 316 |
-
standard_prompt = f"Translate this Coptic text to English: {morph_text}"
|
| 317 |
-
messages = [{"role": "user", "content": standard_prompt}]
|
| 318 |
-
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 319 |
-
inputs = tokenizer([text], return_tensors="pt")
|
| 320 |
-
|
| 321 |
-
with torch.no_grad():
|
| 322 |
-
outputs = model.generate(**inputs, max_new_tokens=300, temperature=0.6, top_p=0.9, do_sample=True)
|
| 323 |
-
|
| 324 |
-
response = tokenizer.decode(outputs[0][len(inputs.input_ids[0]):], skip_special_tokens=True)
|
| 325 |
-
st.subheader("Standard Translation:")
|
| 326 |
-
st.write(response)
|
| 327 |
-
else:
|
| 328 |
-
st.warning("Please enter Coptic text first.")
|
| 329 |
-
|
| 330 |
# Chat interface
|
| 331 |
if "messages" not in st.session_state:
|
| 332 |
st.session_state.messages = []
|
|
@@ -337,41 +225,38 @@ for message in st.session_state.messages:
|
|
| 337 |
st.markdown(message["content"])
|
| 338 |
|
| 339 |
# User input
|
| 340 |
-
if prompt := st.chat_input("
|
| 341 |
-
#
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
|
| 354 |
-
|
| 355 |
|
| 356 |
-
# Display user input
|
| 357 |
-
st.session_state.messages.append({"role": "user", "content": coptic_text})
|
| 358 |
with st.chat_message("user"):
|
| 359 |
-
st.markdown(
|
| 360 |
-
|
| 361 |
-
# Generate translation
|
| 362 |
-
translation_prompt = f"You are a Coptic language expert. Translate this Coptic text to {target_lang} and provide the meaning: {coptic_text}"
|
| 363 |
|
|
|
|
| 364 |
with st.chat_message("assistant"):
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 376 |
-
except Exception as e:
|
| 377 |
-
st.error(f"Translation error: {str(e)}")
|
|
|
|
| 4 |
import os
|
| 5 |
import xml.etree.ElementTree as ET
|
| 6 |
import re
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Coptic alphabet helper
|
|
|
|
| 9 |
COPTIC_ALPHABET = {
|
| 10 |
+
'Ⲁ': 'Alpha', 'Ⲃ': 'Beta', 'Ⲅ': 'Gamma', 'Ⲇ': 'Delta', 'Ⲉ': 'Epsilon', 'Ⲋ': 'Zeta',
|
| 11 |
'Ⲏ': 'Eta', 'Ⲑ': 'Theta', 'Ⲓ': 'Iota', 'Ⲕ': 'Kappa', 'Ⲗ': 'Lambda', 'Ⲙ': 'Mu',
|
| 12 |
'Ⲛ': 'Nu', 'Ⲝ': 'Xi', 'Ⲟ': 'Omicron', 'Ⲡ': 'Pi', 'Ⲣ': 'Rho', 'Ⲥ': 'Sigma',
|
| 13 |
'Ⲧ': 'Tau', 'Ⲩ': 'Upsilon', 'Ⲫ': 'Phi', 'Ⲭ': 'Chi', 'Ⲯ': 'Psi', 'Ⲱ': 'Omega',
|
|
|
|
| 44 |
# Find entries in TEI format
|
| 45 |
entries = root.findall('.//tei:entry', ns)
|
| 46 |
|
| 47 |
+
for entry in entries[:100]: # Limit to first 100 entries for performance
|
| 48 |
coptic_word = ""
|
| 49 |
definition = ""
|
| 50 |
|
| 51 |
# Extract Coptic headword from TEI structure
|
| 52 |
+
form = entry.find('.//tei:form[@type="lemma"]', ns) or entry.find('.//tei:form', ns)
|
| 53 |
+
if form is not None:
|
| 54 |
+
orth = form.find('.//tei:orth', ns)
|
| 55 |
+
if orth is not None and orth.text:
|
| 56 |
+
coptic_word = orth.text.strip()
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Extract definition from sense elements
|
| 59 |
+
senses = entry.findall('.//tei:sense', ns)
|
| 60 |
+
definitions = []
|
| 61 |
+
for sense in senses[:2]: # Limit to first 2 senses
|
| 62 |
+
def_elem = sense.find('.//tei:def', ns)
|
| 63 |
+
if def_elem is not None and def_elem.text:
|
| 64 |
+
definitions.append(def_elem.text.strip())
|
| 65 |
|
| 66 |
+
if definitions:
|
| 67 |
+
definition = "; ".join(definitions)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
# Clean and store
|
| 70 |
if coptic_word and definition:
|
| 71 |
+
# Clean Coptic word (preserve Coptic and Greek Unicode)
|
| 72 |
+
coptic_word = re.sub(r'[^\u2C80-\u2CFF\u03B0-\u03FF\u1F00-\u1FFF\w\s\-]', '', coptic_word).strip()
|
| 73 |
+
if coptic_word:
|
| 74 |
+
lexicon[coptic_word] = definition[:200] # Limit definition length
|
| 75 |
|
| 76 |
# Handle text formats
|
| 77 |
else:
|
|
|
|
| 100 |
|
| 101 |
return lexicon
|
| 102 |
|
| 103 |
+
# Language detection and UI
|
| 104 |
+
LANGUAGES = {
|
| 105 |
+
'en': 'English', 'es': 'Español', 'fr': 'Français', 'de': 'Deutsch',
|
| 106 |
+
'zh': '中文', 'ja': '日本語', 'ar': 'العربية', 'hi': 'हिन्दी',
|
| 107 |
+
'cop': 'Coptic (ⲘⲉⲧⲢⲉⲙ̀ⲛⲭⲏⲙⲓ)', 'cop-sa': 'Sahidic Coptic', 'cop-bo': 'Bohairic Coptic'
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
st.set_page_config(page_title="Apertus Chat", layout="wide")
|
| 111 |
|
| 112 |
+
# Language selector
|
| 113 |
+
selected_lang = st.selectbox("Language / Langue / Idioma",
|
| 114 |
+
options=list(LANGUAGES.keys()),
|
| 115 |
+
format_func=lambda x: LANGUAGES[x])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
# Sidebar for Coptic tools
|
| 118 |
with st.sidebar:
|
|
|
|
| 147 |
for letter, name in COPTIC_ALPHABET.items():
|
| 148 |
st.text(f"{letter} - {name}")
|
| 149 |
|
| 150 |
+
# Lexicon search
|
| 151 |
if coptic_lexicon:
|
| 152 |
st.subheader("Lexicon Search")
|
| 153 |
|
| 154 |
+
# Virtual Coptic keyboard
|
| 155 |
+
st.write("**Virtual Keyboard:**")
|
| 156 |
+
coptic_letters = ['ⲁ', 'ⲃ', 'ⲅ', 'ⲇ', 'ⲉ', 'ⲍ', 'ⲏ', 'ⲑ', 'ⲓ', 'ⲕ', 'ⲗ', 'ⲙ', 'ⲛ', 'ⲝ', 'ⲟ', 'ⲡ', 'ⲣ', 'ⲥ', 'ⲧ', 'ⲩ', 'ⲫ', 'ⲭ', 'ⲯ', 'ⲱ', 'ϣ', 'ϥ', 'ϧ', 'ϩ', 'ϫ', 'ϭ', 'ϯ']
|
|
|
|
| 157 |
|
| 158 |
+
# Create keyboard layout in rows
|
| 159 |
+
cols1 = st.columns(8)
|
| 160 |
+
cols2 = st.columns(8)
|
| 161 |
+
cols3 = st.columns(8)
|
| 162 |
+
cols4 = st.columns(8)
|
| 163 |
|
| 164 |
+
keyboard_input = ""
|
| 165 |
+
for i, letter in enumerate(coptic_letters):
|
| 166 |
+
col_idx = i % 8
|
| 167 |
+
if i < 8:
|
| 168 |
+
if cols1[col_idx].button(letter, key=f"key_{letter}"):
|
| 169 |
+
keyboard_input = letter
|
| 170 |
+
elif i < 16:
|
| 171 |
+
if cols2[col_idx].button(letter, key=f"key_{letter}"):
|
| 172 |
+
keyboard_input = letter
|
| 173 |
+
elif i < 24:
|
| 174 |
+
if cols3[col_idx].button(letter, key=f"key_{letter}"):
|
| 175 |
+
keyboard_input = letter
|
| 176 |
+
else:
|
| 177 |
+
if cols4[col_idx].button(letter, key=f"key_{letter}"):
|
| 178 |
+
keyboard_input = letter
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
# Search input
|
| 181 |
+
search_term = st.text_input("Search Coptic word:", value=keyboard_input if keyboard_input else "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
|
|
|
| 183 |
if search_term:
|
|
|
|
| 184 |
if search_term in coptic_lexicon:
|
| 185 |
+
st.write(f"**{search_term}**")
|
| 186 |
+
st.write(coptic_lexicon[search_term])
|
| 187 |
+
else:
|
| 188 |
+
# Partial matches
|
| 189 |
+
matches = [k for k in coptic_lexicon.keys() if search_term in k]
|
| 190 |
+
if matches:
|
| 191 |
+
st.write("Partial matches:")
|
| 192 |
+
for match in matches[:5]: # Show first 5 matches
|
| 193 |
+
st.write(f"**{match}** → {coptic_lexicon[match][:100]}...")
|
| 194 |
+
else:
|
| 195 |
+
st.write("No matches found")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
# Linguistic analysis options
|
| 198 |
+
if selected_lang in ['cop', 'cop-sa', 'cop-bo']:
|
| 199 |
+
st.subheader("Analysis Type")
|
| 200 |
+
analysis_type = st.selectbox("Choose analysis:",
|
| 201 |
+
options=list(COPTIC_PROMPTS.keys()),
|
| 202 |
+
format_func=lambda x: x.replace('_', ' ').title())
|
| 203 |
+
|
| 204 |
# Load model (cached)
|
| 205 |
@st.cache_resource
|
| 206 |
def load_model():
|
|
|
|
| 215 |
|
| 216 |
tokenizer, model = load_model()
|
| 217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
# Chat interface
|
| 219 |
if "messages" not in st.session_state:
|
| 220 |
st.session_state.messages = []
|
|
|
|
| 225 |
st.markdown(message["content"])
|
| 226 |
|
| 227 |
# User input
|
| 228 |
+
if prompt := st.chat_input("Type your message..."):
|
| 229 |
+
# Add Coptic-specific prompt prefix if applicable
|
| 230 |
+
if selected_lang in ['cop', 'cop-sa', 'cop-bo'] and 'analysis_type' in locals():
|
| 231 |
+
full_prompt = f"{COPTIC_PROMPTS[analysis_type]} {prompt}"
|
| 232 |
+
|
| 233 |
+
# Add lexicon context for lexicon lookup
|
| 234 |
+
if analysis_type == 'lexicon_lookup' and coptic_lexicon:
|
| 235 |
+
words_in_prompt = prompt.split()
|
| 236 |
+
lexicon_matches = []
|
| 237 |
+
for word in words_in_prompt:
|
| 238 |
+
if word in coptic_lexicon:
|
| 239 |
+
lexicon_matches.append(f"{word} = {coptic_lexicon[word]}")
|
| 240 |
+
|
| 241 |
+
if lexicon_matches:
|
| 242 |
+
full_prompt += f"\n\nLexicon entries found: {'; '.join(lexicon_matches)}"
|
| 243 |
+
else:
|
| 244 |
+
full_prompt = prompt
|
| 245 |
|
| 246 |
+
st.session_state.messages.append({"role": "user", "content": full_prompt})
|
| 247 |
|
|
|
|
|
|
|
| 248 |
with st.chat_message("user"):
|
| 249 |
+
st.markdown(full_prompt)
|
|
|
|
|
|
|
|
|
|
| 250 |
|
| 251 |
+
# Generate response
|
| 252 |
with st.chat_message("assistant"):
|
| 253 |
+
messages = [{"role": "user", "content": full_prompt}]
|
| 254 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 255 |
+
inputs = tokenizer([text], return_tensors="pt")
|
| 256 |
+
|
| 257 |
+
with torch.no_grad():
|
| 258 |
+
outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.8, top_p=0.9)
|
| 259 |
+
|
| 260 |
+
response = tokenizer.decode(outputs[0][len(inputs.input_ids[0]):], skip_special_tokens=True)
|
| 261 |
+
st.markdown(response)
|
| 262 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
coptic_keyboard.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
COPTIC_LETTERS = [
|
| 4 |
+
['Ⲁ', 'Ⲃ', 'Ⲅ', 'Ⲇ', 'Ⲉ', 'Ⲋ', 'Ⲏ', 'Ⲑ', 'Ⲓ', 'Ⲕ'],
|
| 5 |
+
['Ⲗ', 'Ⲙ', 'Ⲛ', 'Ⲝ', 'Ⲟ', 'Ⲡ', 'Ⲣ', 'Ⲥ', 'Ⲧ', 'Ⲩ'],
|
| 6 |
+
['Ⲫ', 'Ⲭ', 'Ⲯ', 'Ⲱ', 'Ϣ', 'Ϥ', 'Ϧ', 'Ϩ', 'Ϫ', 'Ϭ', 'Ϯ'],
|
| 7 |
+
['ⲁ', 'ⲃ', 'ⲅ', 'ⲇ', 'ⲉ', 'ⲋ', 'ⲏ', 'ⲑ', 'ⲓ', 'ⲕ'],
|
| 8 |
+
['ⲗ', 'ⲙ', 'ⲛ', 'ⲝ', 'ⲟ', 'ⲡ', 'ⲣ', 'ⲥ', 'ⲧ', 'ⲩ'],
|
| 9 |
+
['ⲫ', 'ⲭ', 'ⲯ', 'ⲱ', 'ϣ', 'ϥ', 'ϧ', 'ϩ', 'ϫ', 'ϭ', 'ϯ']
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
def coptic_keyboard(target_key):
|
| 13 |
+
if target_key not in st.session_state:
|
| 14 |
+
st.session_state[target_key] = ""
|
| 15 |
+
|
| 16 |
+
for i, row in enumerate(COPTIC_LETTERS):
|
| 17 |
+
cols = st.columns(len(row))
|
| 18 |
+
for j, letter in enumerate(row):
|
| 19 |
+
with cols[j]:
|
| 20 |
+
if st.button(letter, key=f"kb_{i}_{j}"):
|
| 21 |
+
st.session_state[target_key] += letter
|
| 22 |
+
st.rerun()
|
| 23 |
+
|
| 24 |
+
col1, col2, col3 = st.columns(3)
|
| 25 |
+
with col1:
|
| 26 |
+
if st.button("Space"):
|
| 27 |
+
st.session_state[target_key] += " "
|
| 28 |
+
st.rerun()
|
| 29 |
+
with col2:
|
| 30 |
+
if st.button("⌫"):
|
| 31 |
+
st.session_state[target_key] = st.session_state[target_key][:-1]
|
| 32 |
+
st.rerun()
|
| 33 |
+
with col3:
|
| 34 |
+
if st.button("Clear"):
|
| 35 |
+
st.session_state[target_key] = ""
|
| 36 |
+
st.rerun()
|