Spaces:
Running
Running
Update app.py
Browse filesyet again with the wrapping logic
5 unmodified lines678910111 unmodified line1314151617184 unmodified lines2324252627281 unmodified line30313233343536373839404142434445464748492 unmodified lines5253545556575 unmodified lines6364656667681 unmodified line70717273747525 unmodified lines1011021031041051061071081091101111121131141151161171181191201211221231241251261271286 unmodified lines1351361371381391401411421431441451461474 unmodified lines15215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021198 unmodified lines3103113123133143153165 unmodified lines# --- Advanced Premium Dark/Black Theme Engine ---custom_css = """.subpanel.loading, .generating, div[class*="loading"], .progress-wrap {display: none !important;visibility: hidden !important;1 unmodified lineoverflow: hidden !important;}.progress-container {padding: 20px;text-align: center;4 unmodified linesmargin-bottom: 20px;}.white-canvas {display: flex;justify-content: center;1 unmodified linewidth: 100%;}.gradio-container, html, body {background-color: #000000 !important;font-family: 'Segoe UI', system-ui, -apple-system, sans-serif !important;}.gradio-container h1, .gradio-container h2, .gradio-container h3, .gradio-container p, .gradio-container span {color: #f8fafc !important;}.label-wrap, .accordion-trigger {background-color: #111827 !important;border: 1px solid #1f2937 !important;}input, textarea, select {border-radius: 8px !important;border: 1px solid #374151 !important;2 unmodified linesbox-shadow: inset 0 1px 2px 0 rgba(0, 0, 0, 0.5) !important;}button.primary {background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%) !important;border: none !important;5 unmodified linestransform: translateY(-1px) !important;}button.secondary {background-color: #1f2937 !important;border: 1px solid #374151 !important;1 unmodified line}"""force_clean_layout_js = """function() {const removeHeaderBlock = () => {25 unmodified linesreturn str(text_string).replace("-", "‑")def calculate_dynamic_chunks(words_data, base_limit=7):chunks = []current_chunk = []word_index = 0while word_index < len(words_data):current_limit = (base_limit - 1) if len(chunks) == 0 else base_limitword = words_data[word_index]current_chunk.append(word)total_row_footprint = 0for w in current_chunk:lit_len = len(w.get("transliteration", ""))lat_len = len(w.get("translation", ""))morph_len = len(w.get("morphology", ""))total_row_footprint += max(lit_len, lat_len, morph_len)if total_row_footprint > 68 and len(current_chunk) > 1:current_chunk.pop()chunks.append(current_chunk)current_chunk = []continueif len(current_chunk) == current_limit:chunks.append(current_chunk)current_chunk = []6 unmodified linesreturn chunksdef build_html_tables(manuscript, reference, words_data):total_table_width_in = 7.0total_slots = 7 # Exactly 7 columns per template gridtable_style = (f"width: {total_table_width_in}in !important; min-width: {total_table_width_in}in !important; "f"max-width: {total_table_width_in}in !important; table-layout: fixed !important; border-collapse: collapse; ""margin-bottom: 4pt; direction: rtl; border: 1pt solid #b7b7b7; background-color: #ffffff; ""line-height: 1.0 !important; height: auto; margin-left: auto; margin-right: auto;")4 unmodified linesis_first = (idx == 0)is_last_table = (idx == len(table_chunks) - 1)# Count actual data positions vs blank fill requirementsactual_data_count = len(current_chunk)if is_first:actual_data_count += 1# If it's the short trailing table, pad it out with clean space objectsif is_last_table:while (len(current_chunk) + (1 if is_first else 0)) < total_slots:current_chunk.append({"hebrew": " ", "transliteration": " ", "translation": " ", "morphology": " "})# --- STEP BALANCING MATRIX ---calculated_widths = []# Scenario A: Short trailing table - distribute 7 inches perfectly evenly across columnsif is_last_table and not is_first:fixed_slice = round(total_table_width_in / total_slots, 4)calculated_widths = [fixed_slice] * total_slots# Scenario B: Full table - run character density scoring calculationselse:slot_weights = []if is_first:ref_len = max(len(reference), len(manuscript))slot_weights.append(ref_len)for word in current_chunk:w_hebrew = len(word.get("hebrew", "")) * 1.8w_translit = len(word.get("transliteration", ""))w_trans = len(word.get("translation", ""))w_morph = len(word.get("morphology", ""))max_text_footprint = max(w_hebrew, w_translit, w_trans, w_morph)slot_weights.append(max_text_footprint if max_text_footprint > 0 else 5)total_weight_score = sum(slot_weights)for w in slot_weights:allocated_width = (w / total_weight_score) * total_table_width_inif allocated_width < 0.75:allocated_width = 0.75calculated_widths.append(round(allocated_width, 3))# Proportional Float Correctionremainder_adjustment = round(total_table_width_in - sum(calculated_widths), 3)if remainder_adjustment != 0 and len(calculated_widths) > 0:calculated_widths[-1] = round(calculated_widths[-1] + remainder_adjustment, 3)html_output.append(f'<table style="{table_style}">')html_output.append(' <colgroup>')for c_width in calculated_widths:html_output.append(f' <col style="width: {c_width}in !important; min-width: {c_width}in !important; max-width: {c_width}in !important;">')html_output.append(' </colgroup>')# Strictly locked 0.069in box model propertiesbase_cell = "border: 1pt solid #b7b7b7; padding: 0.069in !important; height: auto; overflow: hidden; word-wrap: break-word; white-space: normal !important; box-sizing: border-box !important;"hebrew_cell_style = f"font-family: 'Times New Roman', serif !important; font-size: 24px !important; color: #000000 !important; text-align: right; vertical-align: middle; {base_cell}"english_cell_style = f"font-family: Arial, sans-serif !important; font-size: 14.6667px !important; color: #000000 !important; text-align: center; vertical-align: top; line-height: 1.0 !important; {base_cell}"heading_cell_style = f"font-family: 'Times New Roman', serif !important; font-size: 18.6667px !important; color: #000000 !important; text-align: center; vertical-align: middle; line-height: 1.0 !important; {base_cell}"98 unmodified linesyield gr.update(visible=True, value=f"<p style='color:red; text-align:center;'>❌ API Error: {str(e)}</p>"), "", gr.update(visible=False)returnyield gr.update(visible=True, value=status_html("Executing Symmetric Grid Alignment Blueprint")), "", gr.update(visible=False)try:final_table_html = build_html_tables(manuscript_name, reference_header, raw_json_data)5 unmodified lines67891011121 unmodified line141516171819204 unmodified lines252627282930311 unmodified line3334353637383940414243444546474849505152535455562 unmodified lines596061626364655 unmodified lines717273747576771 unmodified line7980818283848525 unmodified lines1111121131141151161171181191201211221231241251261271281291301311326 unmodified lines1391401411421431441451461471481494 unmodified lines15415515615715815916016116216316416516616716816917017117217317417517617717898 unmodified lines2772782792802812822835 unmodified lines# --- Advanced Premium Dark/Black Theme Engine ---custom_css = """/* Completely hide technical loading bars & indicators */.subpanel.loading, .generating, div[class*="loading"], .progress-wrap {display: none !important;visibility: hidden !important;1 unmodified lineoverflow: hidden !important;}/* Polished Minimalist Progress Window */.progress-container {padding: 20px;text-align: center;4 unmodified linesmargin-bottom: 20px;}/* White Canvas Table Wrapper for clear document viewing */.white-canvas {display: flex;justify-content: center;1 unmodified linewidth: 100%;}/* Forcing Total UI Background to Black */.gradio-container, html, body {background-color: #000000 !important;font-family: 'Segoe UI', system-ui, -apple-system, sans-serif !important;}/* Text elements color correction for readability against black background */.gradio-container h1, .gradio-container h2, .gradio-container h3, .gradio-container p, .gradio-container span {color: #f8fafc !important;}/* Style Accordion Boxes for Dark Mode */.label-wrap, .accordion-trigger {background-color: #111827 !important;border: 1px solid #1f2937 !important;}/* Form Input Field Polishing */input, textarea, select {border-radius: 8px !important;border: 1px solid #374151 !important;2 unmodified linesbox-shadow: inset 0 1px 2px 0 rgba(0, 0, 0, 0.5) !important;}/* Primary Action Button Customization */button.primary {background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%) !important;border: none !important;5 unmodified linestransform: translateY(-1px) !important;}/* Secondary Actions Customization */button.secondary {background-color: #1f2937 !important;border: 1px solid #374151 !important;1 unmodified line}"""# JAVASCRIPT TO STRIP AWAY THE HUGGING FACE TOP NAVIGATION HEADERforce_clean_layout_js = """function() {const removeHeaderBlock = () => {25 unmodified linesreturn str(text_string).replace("-", "‑")def calculate_dynamic_chunks(words_data, base_limit=7):"""Splits words into groups of 7.The first group accommodates the reference block, leaving 6 slots for words.Subsequent groups take exactly 7 words."""chunks = []current_chunk = []word_index = 0while word_index < len(words_data):# First table row needs 1 slot for reference block + 6 slots for words = 7 total columnscurrent_limit = 6 if len(chunks) == 0 else 7word = words_data[word_index]current_chunk.append(word)if len(current_chunk) == current_limit:chunks.append(current_chunk)current_chunk = []6 unmodified linesreturn chunksdef build_html_tables(manuscript, reference, words_data):# ESSENTIAL RULE: Master constraint locked to exactly 7.0 inches widetable_style = ("width: 7.0in !important; min-width: 7.0in !important; max-width: 7.0in !important; ""table-layout: fixed !important; border-collapse: collapse; margin-bottom: 4pt; ""direction: rtl; border: 1pt solid #b7b7b7; background-color: #ffffff; ""line-height: 1.0 !important; height: auto; margin-left: auto; margin-right: auto;")4 unmodified linesis_first = (idx == 0)is_last_table = (idx == len(table_chunks) - 1)# Determine how many open slots remain to build a perfectly symmetrical 7-column matrixallocated_slots = len(current_chunk) + (1 if is_first else 0)# If the trailing table is shorter than 7 slots, fill the empty spaces with clean blank objectsif is_last_table:while allocated_slots < 7:current_chunk.append({"hebrew": " ", "transliteration": " ", "translation": " ", "morphology": " "})allocated_slots += 1html_output.append(f'<table style="{table_style}">')# ESSENTIAL RULE: Force every single column to be EXACTLY 1.0 inch wide to match your templatehtml_output.append(' <colgroup>')for _ in range(7):html_output.append(' <col style="width: 1.0in !important; min-width: 1.0in !important; max-width: 1.0in !important;">')html_output.append(' </colgroup>')# ESSENTIAL RULE: Exact 0.069in padding on all four sides + strict word wrap suppression (nowrap)base_cell = "border: 1pt solid #b7b7b7; padding: 0.069in !important; height: auto; overflow: hidden; white-space: nowrap !important; box-sizing: border-box !important;"hebrew_cell_style = f"font-family: 'Times New Roman', serif !important; font-size: 24px !important; color: #000000 !important; text-align: right; vertical-align: middle; {base_cell}"english_cell_style = f"font-family: Arial, sans-serif !important; font-size: 14.6667px !important; color: #000000 !important; text-align: center; vertical-align: top; line-height: 1.0 !important; {base_cell}"heading_cell_style = f"font-family: 'Times New Roman', serif !important; font-size: 18.6667px !important; color: #000000 !important; text-align: center; vertical-align: middle; line-height: 1.0 !important; {base_cell}"98 unmodified linesyield gr.update(visible=True, value=f"<p style='color:red; text-align:center;'>❌ API Error: {str(e)}</p>"), "", gr.update(visible=False)returnyield gr.update(visible=True, value=status_html("Assembling Fixed 7-Inch Symmetrical Grid Blueprint")), "", gr.update(visible=False)try:final_table_html = build_html_tables(manuscript_name, reference_header, raw_json_data)
app.py+35-68