Spaces:
Running
Running
Update app.py
Browse files137 unmodified lines1381391401411411421431441441451461 unmodified line148149150152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331151137 unmodified lines run.font.size = Pt(4)
# ---------------------------------------------------------------------------# 3. SCREEN CANVAS VIEW COMPILER (HTML DISPLAY WITH EXPLICIT ID FOR CLIPBOARD)# 3. SCREEN CANVAS VIEW COMPILER (HTML DISPLAY ENGINE)# ---------------------------------------------------------------------------def build_html_canvas(word_packages, ms_name, heading_label): # Added wrapper ID 'bei-target-tables' so the system knows exactly what to capture for copy actions html = '<div id="bei-target-tables" style="overflow-x:auto; background-color:white; padding:15px; border-radius:4px;">' t1_data = word_packages[:6]1 unmodified line html += '<table dir="rtl" style="width:7in; border-collapse:collapse; margin:0 auto 4pt auto; table-layout:fixed; border:1px solid #b7b7b7;">' html += '<tr style="height:40px;">' html += f'<td style="width:1in; border:1px solid #b7b7b7; font-family:\'Times New Roman\'; font-size:14pt; text-align:center; vertical-align:top; color:#000; font-weight:bold;">{heading_label}</td>' for pkg in t1_data: text = pkg[0] if pkg else "" html += f'<td style="width:1in; border:1px solid #b7b7b7; font-family:\'Times New Roman\'; font-size:18pt; text-align:right; padding:0.069in; vertical-align:middle; color:#000;">{text}</td>' html += '</tr><tr>' html += f'<td style="border:1px solid #b7b7b7; font-family:\'Arial\'; font-size:11pt; text-align:center; vertical-align:top; color:#000;">{ms_name}</td>' for pkg in t1_data: content = f'<div style="line-height:1.2;">{pkg[1]}<br>{pkg[2]}<br>{clean_morphology(pkg[3])}</div>' if pkg else "" html += f'<td style="border:1px solid #b7b7b7; font-family:\'Arial\'; font-size:11pt; text-align:center; padding:0.069in; vertical-align:top; color:#000;">{content}</td>' html += '</tr></table>' rem = word_packages[6:] while len(rem) > 0: chunk = rem[:7] while len(chunk) < 7: chunk.append(None) html += '<table dir="rtl" style="width:7in; border-collapse:collapse; margin:0 auto 4pt auto; table-layout:fixed; border:1px solid #b7b7b7;"><tr>' for pkg in chunk: text = pkg[0] if pkg else "" html += f'<td style="width:1in; border:1px solid #b7b7b7; font-family:\'Times New Roman\'; font-size:18pt; text-align:right; padding:0.069in; vertical-align:middle; color:#000;">{text}</td>' html += '</tr><tr>' for pkg in chunk: content = f'<div style="line-height:1.2;">{pkg[1]}<br>{pkg[2]}<br>{clean_morphology(pkg[3])}</div>' if pkg else "" html += f'<td style="border:1px solid #b7b7b7; font-family:\'Arial\'; font-size:11pt; text-align:center; padding:0.069in; vertical-align:top; color:#000;">{content}</td>' html += '</tr></table>' rem = rem[7:] html += '</div>' return html
# ---------------------------------------------------------------------------# 4. RUNTIME PIPELINE CONTROLLER# ---------------------------------------------------------------------------def process_pipeline(api_key, ms_name, book, ch, ver, raw_input): if not raw_input.strip(): return "System Warning: Please paste Hebrew strings to build.", "", gr.update(visible=False) start_time = time.time() time.sleep(1.2) heading_label = f"{book[:3]}. {ch}:{ver}" tokens = raw_input.strip().split() word_packages = [((tok,) + call_mock_engine(tok)) for tok in tokens] html_canvas = build_html_canvas(word_packages, ms_name, heading_label) doc = Document() t1_data = word_packages[:6] while len(t1_data) < 6: t1_data.append(None) build_docx_table(doc, t1_data, ms_name, heading_label, is_first=True) rem = word_packages[6:] while len(rem) > 0: chunk = rem[:7] while len(chunk) < 7: chunk.append(None) build_docx_table(doc, chunk, ms_name, heading_label, is_first=False) rem = rem[7:] file_path = "BEI_Interlinear_Output.docx" doc.save(file_path) elapsed = time.time() - start_time status = f"Success! Processed {len(tokens)} words in {elapsed:.2f} seconds." return status, html_canvas, gr.update(value=file_path, visible=True)
def clear_workspace(): """Wipes out the input fields and drops generated canvas tables instantly.""" return "", "", "", gr.update(visible=False)
# ---------------------------------------------------------------------------# 5. GRADIO UI DEFINITIONS WITH INTEGRATED CLIPBOARD JS INJECTIONS# ---------------------------------------------------------------------------bei_css = """body, .gradio-container { background-color: #F4F3EF !important; }.bei-banner { background-color: #1B4D3E; padding: 22px; text-align: center; border-radius: 8px; border-bottom: 4px solid #D4AF37; }.bei-banner h1 { color: #F4F3EF !important; font-size: 28px; margin: 0; font-weight: bold; }.bei-banner p { color: #D4AF37 !important; margin: 4px 0 0 0; font-size: 14px; font-weight: 500; }.action-btn { background-color: #1B4D3E !important; color: white !important; font-weight: bold !important; border-radius: 4px !important; margin: 5px 0; }.clear-btn { background-color: #718096 !important; color: white !important; font-weight: bold !important; border-radius: 4px !important; margin: 5px 0; }.copy-btn { background-color: #D4AF37 !important; color: #1B4D3E !important; font-weight: bold !important; border-radius: 4px !important; margin: 5px 0; border: 1px solid #1B4D3E !important; }.api-guide { background-color: #E2E8F0; padding: 12px; border-radius: 6px; border-left: 4px solid #1B4D3E; font-size: 11.5pt; color: #2D3748; margin-bottom: 10px; }.hourglass-box { text-align: center; font-size: 24px; padding: 10px; color: #1B4D3E; }"""
# JavaScript module function that copies formatted HTML tables directly onto your system clipboardcopy_tables_js = """function() { var container = document.getElementById('bei-target-tables'); if (!container) { alert('No tables found to copy! Generate an interlinear layout canvas first.'); return; } window.getSelection().removeAllRanges(); var range = document.createRange(); range.selectNode(container); window.getSelection().addRange(range); try { document.execCommand('copy'); alert('Success! Tables copied to clipboard. You can now paste directly into Google Docs.'); } catch (err) { alert('Clipboard copy encountered an error.'); } window.getSelection().removeAllRanges();}"""
with gr.Blocks(css=bei_css, title="Shorashim Pro Workspace") as demo: with gr.Row(elem_classes="bei-banner"): # Adjustments 1: Replaced subheading line text parameters gr.Markdown("<h1>📖 Shorashim Pro Workspace</h1><p>BEI Interlinear Table Generator</p>") with gr.Row(): with gr.Column(scale=2): # Adjustment 5: Integrated simple guide instructions block for key acquisition gr.HTML(''' <div class="api-guide"> 🔑 <b>Gemini API Key Guide:</b> To generate your credentials key, log into <a href="https://aistudio.google.com/" target="_blank" style="color:#1B4D3E; font-weight:bold; text-decoration:underline;">Google AI Studio</a>, click the "Get API Key" button, copy the token code block, and paste it below. </div> ''') api_key = gr.Textbox(label="🔑 Gemini API Access Key", type="password", placeholder="AI key token platform credentials...") with gr.Row(): ms_name = gr.Textbox(label="📋 Manuscript Selection Name", value="Marseilles MS-24") # Adjustments 6, 7, 8: Unified text field inputs targeting the Book selection properties book = gr.Textbox(label="Book", value="Luke") with gr.Row(): ch = gr.Dropdown(label="Chapter Target", choices=[str(i) for i in range(1, 25)], value="1") ver = gr.Dropdown(label="Verse Target", choices=[str(i) for i in range(1, 81)], value="5") with gr.Column(scale=2): # Adjustment 2: Text parameters changed to 'Paste Your Hebrew Here' raw_input = gr.Textbox(label="Paste Your Hebrew Here", value="הָיָה בִּימֵי־הוֹרְדּוֹס הַמֶּלֶךְ כֹּהֵן אֶחָד", lines=7) with gr.Row(): gen_btn = gr.Button("🚀 Generate Interlinear Canvas", elem_classes="action-btn") # Adjustment 3: Clean reset trigger button action clear_btn = gr.Button("Clear Text", elem_classes="clear-btn") # Adjustment 4: Live Clipboard copy button component added copy_btn = gr.Button("Copy Tables for Google Docs", elem_classes="copy-btn") with gr.Row(): with gr.Column(scale=1, visible=False) as loading_area: gr.Markdown("<div class='hourglass-box'>⏳ <b>Backend Operational Threads Active... Please Wait.</b></div>") with gr.Row(): with gr.Column(): status_field = gr.Textbox(label="System Status Pipeline Logs", interactive=False) doc_file = gr.File(label="📥 Download Structured Document Asset (.docx)", visible=False) with gr.Row(): canvas_area = gr.HTML(label="Interlinear Render Canvas Layer Output")
# Connect UI Action Listeners gen_btn.click( fn=lambda: gr.update(visible=True), inputs=None, outputs=loading_area, queue=False ).then( fn=process_pipeline, inputs=[api_key, ms_name, book, ch, ver, raw_input], outputs=[status_field, canvas_area, doc_file] ).then( fn=lambda: gr.update(visible=False), inputs=None, outputs=loading_area, queue=False )
# Clean reset action pipeline callback linkage clear_btn.click( fn=clear_workspace, inputs=None, outputs=[raw_input, status_field, canvas_area, doc_file] )
# Clipboard browser execution routing linkage copy_btn.click(fn=None, js=copy_tables_js)
if __name__ == "__main__": demo.launch() html += f'<td style="width:1in; border:1px solid #b7b7b7; font-family:\'Times New Roman\'; font-size:14pt; text-align:center; vertical-align:top; color:#000;app.py+2-182