Spaces:
Running
Running
Update app.py
Browse filesI hate this thing
11 unmodified lines1213141516171819202122232425262728293031323334353624 unmodified lines6162636465666723 unmodified lines91929394959697989910081 unmodified lines18218318418518618718842 unmodified lines23123223323423523623710 unmodified lines24824925025125225325417 unmodified lines2722732742752762772781 unmodified line2802812822832842852864 unmodified lines29129229329429529629729829933 unmodified lines3333343353363373383393 unmodified lines34334434534634734834911 unmodified linesfrom docx.oxml.ns import nsdecls, qn# ---------------------------------------------------------------------------# 1. CORE API ENGINE - DYNAMIC CONTEXTUAL TRANSLATION & TRANSLITERATION# ---------------------------------------------------------------------------def process_live_ai_request(word, api_key):"""Leverages Gemini 2.5 Flash via direct REST calls to dynamically analyzethe Hebrew token, applying the exact BEI Transliteration Schema and Morphology rules."""clean_key = api_key.strip() if api_key else ""if not clean_key:return "Missing Key", "Missing Key", "Enter valid key above"url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent?key={clean_key}"headers = {"Content-Type": "application/json"}# Complete systemic prompt passing your precise organizational rulesprompt = f"""You are an expert academic philologist working for Benai Emunah International (BEI) on a critical interlinear text project for the Hebrew Book of Luke.Analyze this isolated Hebrew word token: "{word}"Generate exactly THREE lines of text in your response. Do not include any markdown, introductory remarks, bullet points, or formatting. Just output the three raw lines.24 unmodified lines---CRITICAL BEI MORPHOLOGY RULES:- STRICT NO-PERIOD CONSTRAINT: Abbreviation tags must absolutely never contain a period (use 'v', never write 'v.').- Token Assembly Sequence: [Part of Speech Abbreviation] [Binyan (Verbs Only)] [Tense/Mood/Aspect (Verbs Only)] [Concordance Block].- Part of Speech: v, n, adv, adj, conj, part, prep, pron, name, ddo, #, intj, constr- Binyan: Pa'al, Nif'al, Pi'el, Pu'al, Hif'il, Huf'al, Hitpa'el- Tense/Mood/Aspect: perf, imp, ptcpl23 unmodified linestransliteration = lines[0] if len(lines) > 0 else "error"translation = lines[1] if len(lines) > 1 else "error"morphology = lines[2] if len(lines) > 2 else "error"return transliteration, translation, morphology.replace(".", "")else:return f"API Error ({response.status_code})", "Check Key Setup", "error"except Exception as e:return "Connection Timeout", "Network Error", "error"81 unmodified linesrun.font.size = Pt(4)# ---------------------------------------------------------------------------# 3. SCREEN CANVAS VIEW COMPILER (HTML DISPLAY ENGINE)# ---------------------------------------------------------------------------def build_html_canvas(word_packages, ms_name, heading_label):html_elements = []42 unmodified linesreturn "".join(html_elements)# ---------------------------------------------------------------------------# 4. RUNTIME SYSTEM EXECUTION ENGINE# ---------------------------------------------------------------------------def process_pipeline(api_key, ms_name, book, ch, ver, raw_input):if not raw_input.strip():10 unmodified linesword_packages = []for tok in tokens:# Request transliteration, translation, and morphology concurrently via systemic instructionstl, ts, mo = process_live_ai_request(tok, api_key)word_packages.append((tok, tl, ts, mo))17 unmodified linesdoc.save(file_path)elapsed = time.time() - start_timestatus = f"Success! Processed {len(tokens)} words in {elapsed:.2f} seconds."return status, html_canvas, gr.update(value=file_path, visible=True)1 unmodified linereturn "", "Success: Canvas workspace cleared cleanly.", "", gr.update(visible=False)# ---------------------------------------------------------------------------# 5. GRADIO UI DESIGN LAYOUT# ---------------------------------------------------------------------------bei_css = """body, .gradio-container { background-color: #F4F3EF !important; }4 unmodified lines.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; }/* FIX: Forced high-visibility dark charcoal text for the instructions box title and body lines */.api-guide-box { background-color: #E2E8F0 !important; border-left: 5px solid #1B4D3E !important; padding: 12px; border-radius: 4px; margin-bottom: 10px; }.api-guide-box p, .api-guide-box b, .api-guide-box a { color: #2D3748 !important; font-family: 'Arial', sans-serif !important; font-size: 11pt !important; margin: 0; }.api-guide-box a { color: #1B4D3E !important; font-weight: bold !important; text-decoration: underline !important; }input[type="text"], input[type="password"], textarea {33 unmodified lineswith gr.Column(scale=2):gr.HTML('''<div class="api-guide-box"><p>馃攽 <b>Gemini API Key Guide:</b> To generate your credentials key, log into<a href="https://aistudio.google.com/" target="_blank">Google AI Studio</a>,click the "Get API Key" button, copy the token code block, and paste it below.</p></div>3 unmodified linesms_name = gr.Textbox(label="馃搵 Manuscript Selection Name", value="Marseilles MS-24")book = gr.Textbox(label="Book", value="Luke")with gr.Row():ch = gr.Dropdown(label="Chapter", choices=[str(i) for i in range(1, 25)], value="1")ver = gr.Dropdown(label="Verse", choices=[str(i) for i in range(1, 81)], value="5")with gr.Column(scale=2):raw_input = gr.Textbox(label="Paste Your Hebrew Here", value="讛指讬指讛 讘旨执讬诪值讬志讛讜止专职讚旨讜止住 讛址诪旨侄诇侄讱职 讻旨止讛值谉 讗侄讞指讚 讜旨砖讈职诪讜止 讝职讻址专职讬指讛讜旨 诪执讘旨值讬转 讗指讘执讬指讛 讜职讗执砖讈职转旨讜止 诪执讘旨值讬转 讗址讛植专讜止谉 讜旨砖讈职诪指讛旨 讗值诇执讬讝址讘旨值转", lines=7)11 unmodified lines121314151617181920212223242526272829303132333435363724 unmodified lines6263646566676823 unmodified lines929394959697989910081 unmodified lines18218318418518618718842 unmodified lines23123223323423523623710 unmodified lines24824925025125225325425517 unmodified lines2732742752762772782791 unmodified line2812822832842852862874 unmodified lines29229329429529629729829930030130230330430533 unmodified lines3393403413423433443453 unmodified lines34935035135235335435511 unmodified linesfrom docx.oxml.ns import nsdecls, qn# ---------------------------------------------------------------------------# 1. CORE API LINGUISTIC PIPELINE (WORD-BY-WORD SELECTION OVER MODEL 1.5 FLASH)# ---------------------------------------------------------------------------def process_live_ai_request(word, api_key):"""Queries the stable Gemini 1.5 Flash model web gateway for a single word token.Feeds your complete BEI Transliteration Schema Matrix and Morphology rulesdirectly into each call for pristine, context-aware translations."""clean_key = api_key.strip() if api_key else ""if not clean_key:return "Missing Key", "Missing Key", "Enter valid key above"# Updated API gateway url string pointing exactly to stable gemini-1.5-flashurl = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={clean_key}"headers = {"Content-Type": "application/json"}prompt = f"""You are an expert academic philologist working for Benai Emunah International (BEI) on a critical interlinear text project for the Hebrew Book of Luke.Analyze this single isolated Hebrew word token: "{word}"Generate exactly THREE lines of text in your response. Do not include any markdown, introductory remarks, bullet points, or formatting. Just output the three raw lines.24 unmodified lines---CRITICAL BEI MORPHOLOGY RULES:- STRICT NO-PERIOD CONSTRAINT: Abbreviation tags must absolutely never contain a period (use 'v', never write 'v.').- Token Assembly Sequence: [Part of Speech] [Binyan (Verbs Only)] [Tense/Mood/Aspect (Verbs Only)] [Concordance Block].- Part of Speech: v, n, adv, adj, conj, part, prep, pron, name, ddo, #, intj, constr- Binyan: Pa'al, Nif'al, Pi'el, Pu'al, Hif'il, Huf'al, Hitpa'el- Tense/Mood/Aspect: perf, imp, ptcpl23 unmodified linestransliteration = lines[0] if len(lines) > 0 else "error"translation = lines[1] if len(lines) > 1 else "error"morphology = lines[2] if len(lines) > 2 else "error"return transliteration, translation, morphology.replace(".", "")else:return f"API Error ({response.status_code})", "Rate Limit Active", "error"except Exception as e:return "Connection Timeout", "Network Error", "error"81 unmodified linesrun.font.size = Pt(4)# ---------------------------------------------------------------------------# 3. SCREEN CANVAS VIEW COMPILER# ---------------------------------------------------------------------------def build_html_canvas(word_packages, ms_name, heading_label):html_elements = []42 unmodified linesreturn "".join(html_elements)# ---------------------------------------------------------------------------# 5. PACED WORD-BY-WORD EXECUTION ROUTER (FIXES EXCEPTION 429)# ---------------------------------------------------------------------------def process_pipeline(api_key, ms_name, book, ch, ver, raw_input):if not raw_input.strip():10 unmodified linesword_packages = []for tok in tokens:# Pacing step spacing out sequential web hits by exactly 400 milliseconds to avoid 429 lockstime.sleep(0.4)tl, ts, mo = process_live_ai_request(tok, api_key)word_packages.append((tok, tl, ts, mo))17 unmodified linesdoc.save(file_path)elapsed = time.time() - start_timestatus = f"Success! Processed {len(tokens)} words in {elapsed:.2f} seconds across Gemini 1.5 Flash."return status, html_canvas, gr.update(value=file_path, visible=True)1 unmodified linereturn "", "Success: Canvas workspace cleared cleanly.", "", gr.update(visible=False)# ---------------------------------------------------------------------------# 6. GRADIO UI LAYOUT CONFIGURATIONS (WITH EXPLICIT VISIBILITY FIX)# ---------------------------------------------------------------------------bei_css = """body, .gradio-container { background-color: #F4F3EF !important; }4 unmodified lines.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; }/* FIX: Triple override rules forcing dark charcoal on text layouts inside guide box */.api-guide-box { background-color: #E2E8F0 !important; border-left: 5px solid #1B4D3E !important; padding: 12px; border-radius: 4px; margin-bottom: 10px; }.api-guide-box p, .api-guide-box span, .api-guide-box b, .api-guide-box strong, .api-guide-box a {color: #2D3748 !important;font-family: 'Arial', sans-serif !important;font-size: 11pt !important;margin: 0 !important;}.api-guide-box a { color: #1B4D3E !important; font-weight: bold !important; text-decoration: underline !important; }input[type="text"], input[type="password"], textarea {33 unmodified lineswith gr.Column(scale=2):gr.HTML('''<div class="api-guide-box"><p>馃攽 <strong>Gemini API Key Guide:</strong> To generate your credentials key, log into<a href="https://aistudio.google.com/" target="_blank">Google AI Studio</a>,click the "Get API Key" button, copy the token code block, and paste it below.</p></div>3 unmodified linesms_name = gr.Textbox(label="馃搵 Manuscript Selection Name", value="Marseilles MS-24")book = gr.Textbox(label="Book", value="Luke")with gr.Row():ch = gr.Dropdown(label="Chapter", choices=[str(i) for i in range(1, 24)], value="1")ver = gr.Dropdown(label="Verse", choices=[str(i) for i in range(1, 81)], value="5")with gr.Column(scale=2):raw_input = gr.Textbox(label="Paste Your Hebrew Here", value="讛指讬指讛 讘旨执讬诪值讬志讛讜止专职讚旨讜止住 讛址诪旨侄诇侄讱职 讻旨止讛值谉 讗侄讞指讚 讜旨砖讈职诪讜止 讝职讻址专职讬指讛讜旨 诪执讘旨值讬转 讗指讘执讬指讛 讜职讗执砖讈职转旨讜止 诪执讘旨值讬转 讗址讛植专讜止谉 讜旨砖讈职诪指讛旨 讗值诇执讬讝址讘旨值转", lines=7)
app.py+24-18