# app.py ─ AI-Aware Writing Assistant (final)
import warnings
warnings.filterwarnings("ignore")
import gradio as gr
from nlp_pipeline import initialize_pipeline, process_full
import config
print(f"[App] Gradio {gr.__version__}")
########################################
# 1. Pipeline init
########################################
print("[App] Initializing pipeline…")
_p = initialize_pipeline()
ngram_models = _p["ngram_models"]
vocab = _p["vocab"]
word_probs = _p["word_probs"]
grammar_tokenizer = _p["grammar_tokenizer"]
grammar_model = _p["grammar_model"]
para_tokenizer = _p["para_tokenizer"]
para_model = _p["para_model"]
device = _p["device"]
GRAMMAR_OK = grammar_model is not None
PARAPHRASE_OK = para_model is not None
VOCAB_SIZE = len(vocab)
MAX_WORDS = config.MAX_WORDS
########################################
# 2. CSS
# All theme colours live on :root as
# CSS custom properties. The JS toggle
# overwrites them directly on body via
# element.style.setProperty() — inline
# styles always beat Gradio's Svelte CSS.
########################################
PAGE_STYLE = """
"""
########################################
# 3. JS — dark mode + copy
#
# Dark mode: directly sets CSS custom
# properties on body via setProperty().
# Inline styles always beat Svelte's
# scoped CSS — no specificity fight.
#
# Copy button: finds the accepted textarea
# by placeholder text (reliable across
# Gradio versions) then copies its value.
########################################
SCRIPTS = """
"""
########################################
# 4. Header
########################################
def _header_html():
b_g = 'GEC ✓'
b_ag = 'GEC: rules only'
b_p = 'Paraphrase ✓'
b_ap = 'Paraphrase: fallback'
b_v = f'Vocab {VOCAB_SIZE:,}'
b_lim = f'Max {MAX_WORDS} words'
return f"""
Writing Assistant
{b_g if GRAMMAR_OK else b_ag}
{b_p if PARAPHRASE_OK else b_ap}
{b_v} {b_lim}
"""
########################################
# 5. Diff wrapper
########################################
_PH = 'Suggestions will appear here as you type…'
def _wrap(inner): return f'