Spaces:
Running
Running
Update app.py
Browse filesThe overall table width is set at 7 inches. Previous version handled the "shrink wrapped" column widths fine, but the logic made the tables too skinny.
3 unmodified lines4567789107 unmodified lines1819202121222324232425262728282930311 unmodified line3334353637383637383940414243444546474849505152535455434456575859606162636465666768697071727374757677787980818262 unmodified lines1451461471481491501511521531541551241251561571581591601281291301311321331341351361371381391401411421431611621631641651491661511521531671681691701711721731741751761771781791801811821831841851861871881561891901581911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252261 unmodified line22822923023123223323485 unmodified lines32032132225832332432532616 unmodified lines3433443452813463472833483493503513 unmodified linesimport timeimport json
# --- Advanced Friendly Styling Engine ---# --- Advanced Premium Dark/Black Theme Engine ---custom_css = """/* Completely hide technical loading bars & indicators */.subpanel.loading, .generating, div[class*="loading"], .progress-wrap {7 unmodified lines.progress-container { padding: 20px; text-align: center; border: 1px solid #e2e8f0; border: 1px solid #2d3748; border-radius: 12px; background-color: #ffffff; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05); background-color: #1a202c; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.2); margin-bottom: 20px;}
/* White Canvas Table Wrapper *//* White Canvas Table Wrapper for clear document viewing */.white-canvas { display: flex; justify-content: center;1 unmodified line width: 100%;}
/* Streamlined, approachable UI overrides */.gradio-container { background-color: #f8fafc !important;/* 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 #cbd5e1 !important; box-shadow: inset 0 1px 2px 0 rgba(0, 0, 0, 0.05) !important; border: 1px solid #374151 !important; background-color: #111827 !important; color: #ffffff !important; box-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; border-radius: 8px !important; color: #ffffff !important; transition: transform 0.1s ease !important;}button.primary:hover { transform: translateY(-1px) !important;}
/* Secondary Actions Customization */button.secondary { background-color: #1f2937 !important; border: 1px solid #374151 !important; color: #f8fafc !important;}"""
# JAVASCRIPT TO STRIP AWAY THE HUGGING FACE TOP NAVIGATION HEADER62 unmodified lines return chunks
def build_html_tables(manuscript, reference, words_data): """ Assembles right-to-left interlinear grids matching an exact 7-inch boundary template. Applies proportional cell calculations based on horizontal string weights. """ total_table_width_in = 7.0 # Static overall structural boundaries matching your publishing layout template table_style = ( "width: auto !important; border-collapse: collapse; margin-bottom: 4pt; " "direction: rtl; border: 1pt solid #b7b7b7; background-color: #ffffff; " 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; 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;" ) # Fixed to exactly 0.069in padding down below hebrew_cell_style = ( "font-family: 'Times New Roman', serif !important; font-size: 24px !important; color: #000000 !important; " "text-align: right; vertical-align: middle; border: 1pt solid #b7b7b7; " "padding: 0.069in !important; height: auto; white-space: nowrap; width: 1%;" ) english_cell_style = ( "font-family: Arial, sans-serif !important; font-size: 14.6667px !important; color: #000000 !important; " "border: 1pt solid #b7b7b7; padding: 0.069in !important; text-align: center; " "vertical-align: top; height: auto; line-height: 1.0 !important; white-space: nowrap; width: 1%;" ) heading_cell_style = ( "font-family: 'Times New Roman', serif !important; font-size: 18.6667px !important; color: #000000 !important; " "text-align: center; vertical-align: middle; border: 1pt solid #b7b7b7; " "padding: 0.069in !important; height: auto; white-space: nowrap; line-height: 1.0 !important; width: 1%;" ) html_output = ['<div id="table-payload" class="white-canvas" style="background-color: #ffffff; padding: 20px; border-radius: 8px; display: inline-block; width: 100%; box-sizing: border-box; text-align: center;">'] table_chunks = calculate_dynamic_chunks(words_data, base_limit=7) for idx, current_chunk in enumerate(table_chunks): html_output.append(f'<table style="{table_style}">') is_first = (idx == 0) target_total_cols = 7 actual_slots_used = len(current_chunk) + (1 if is_first else 0) is_last_table = (idx == len(table_chunks) - 1) total_slots = 7 # Maximum columns allowed per grid matrix # Determine actual textual length score for each available linguistic slot slot_weights = [] # If it's the first table block, column 1 is reserved for the reference metadata headers 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", "")) w_translit = len(word.get("transliteration", "")) w_trans = len(word.get("translation", "")) w_morph = len(word.get("morphology", "")) # Evaluate maximum content footprint across the stacked cell metrics 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) # Pad trailing empty cells for terminal tables to keep matrix symmetry clean is_last_table = (idx == len(table_chunks) - 1) if is_last_table: while actual_slots_used < target_total_cols: while (len(current_chunk) + (1 if is_first else 0)) < total_slots: current_chunk.append({"hebrew": " ", "transliteration": "", "translation": "", "morphology": ""}) actual_slots_used += 1 slot_weights.append(5) # Base padding allocation score for blank spaces # --- EXECUTE THREE-STEP PROPORTIONAL COLUMN BALANCING ENGINE --- total_weight_score = sum(slot_weights) calculated_widths = [] for w in slot_weights: # Distribute 7 inches proportionally using character scores allocated_width = (w / total_weight_score) * total_table_width_in # Enforce micro baseline minimum constraints to block rendering overflows if allocated_width < 0.65: allocated_width = 0.65 calculated_widths.append(round(allocated_width, 3)) # Re-verify layout sum constraints and adjust minor float round errors into the final cell column remainder_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}">') # Define structural colgroup bounds using the newly calculated balanced widths html_output.append(' <colgroup>') for c_width in calculated_widths: html_output.append(f' <col style="width: {c_width}in !important;">') html_output.append(' </colgroup>') # Core Cell Typography Styling base_cell = "border: 1pt solid #b7b7b7; padding: 0.069in !important; height: auto; overflow: hidden; word-wrap: break-word;" 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}" # --- ROW 1: Hebrew Text / Reference Metadata --- html_output.append(' <tr style="height: auto; line-height: 1.0 !important;">') if is_first: html_output.append(f' <td style="{heading_cell_style}">{reference}</td>')1 unmodified line html_output.append(f' <td style="{hebrew_cell_style}">{word.get("hebrew", " ")}</td>') html_output.append(' </tr>') # --- ROW 2: English Structural Stack --- html_output.append(' <tr style="height: auto; line-height: 1.0 !important;">') if is_first: formatted_manuscript = manuscript.replace(" ", "<br>") if " " in manuscript else manuscript85 unmodified lines yield gr.update(visible=True, value=f"<p style='color:red; text-align:center;'>❌ API Error: {str(e)}</p>"), "", gr.update(visible=False) return
yield gr.update(visible=True, value=status_html("Structuring column dimensions and applying margins")), "", gr.update(visible=False) yield gr.update(visible=True, value=status_html("Executing 3-Step Column Balance Layout Matrix")), "", gr.update(visible=False) try: final_table_html = build_html_tables(manuscript_name, reference_header, raw_json_data)16 unmodified lines
with gr.Blocks(css=custom_css, title="Hebrew Interlinear Table Generator") as demo: gr.HTML( "<div style='background: linear-gradient(135deg, #1e3a8a 0%, #2563eb 100%); padding: 24px; border-radius: 12px; margin-bottom: 24px; color: white; text-align: center; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.1);'>" "<div style='background: linear-gradient(135deg, #111827 0%, #1f2937 100%); padding: 24px; border-radius: 12px; margin-bottom: 24px; color: white; text-align: center; border: 1px solid #374151; box-shadow: 0 4px 6px -1px rgba(0,0,0,0.5);'>" "<h1 style='margin: 0 0 8px 0; font-size: 28px; font-weight: 700; color: white;'>📖 Hebrew Interlinear Table Generator</h1>" "<p style='margin: 0; font-size: 15px; opacity: 0.9;'>Convert raw Hebrew text into structured, publication-ready interlinear columns.</p>" "<p style='margin: 0; font-size: 15px; opacity: 0.8; color: #9ca3af;'>Convert raw Hebrew text into structured, balanced 7-inch columns.</p>" "</div>" ) app.py+102-37