import gradio as gr import torch import re from transformers import AutoTokenizer, AutoModelForSeq2SeqLM # ========================= # LOAD MODEL # ========================= MODEL_ID = "imelia13/t5-indosum-summarizer" tokenizer = AutoTokenizer.from_pretrained(MODEL_ID) model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID) device = torch.device("cpu") model.to(device) # ========================= # TEXT PROCESSING # ========================= def clean_text(text): text = re.sub(r"\s+", " ", text.replace("\n", " ").replace("\t", " ")) return text.strip() def summarize_text(text): if not text or len(text.split()) < 10: return "Teks terlalu pendek (minimal 10 kata)." text = clean_text(text) inputs = tokenizer( "ringkas: " + text, return_tensors="pt", truncation=True, max_length=512 ).to(device) with torch.no_grad(): outputs = model.generate( **inputs, max_length=128, num_beams=4, early_stopping=True ) return tokenizer.decode(outputs[0], skip_special_tokens=True) # ========================= # CUSTOM CSS # ========================= custom_css = """ body { background: radial-gradient(circle at top, #141a33, #0b0f1a); font-family: Inter, system-ui, sans-serif; } .gradio-container { max-width: 1100px !important; margin: auto !important; } /* Cards */ .card { background: rgba(255,255,255,0.04); border-radius: 18px; padding: 1.5rem; box-shadow: 0 12px 32px rgba(0,0,0,0.3); margin-bottom: 1.5rem; } /* Hero */ .hero { text-align: center; margin-bottom: 2.5rem; } .hero h1 { font-size: 2.6rem; margin-bottom: 0.3rem; } .hero p { opacity: 0.8; font-size: 1rem; } /* Info grid */ .info-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; } .info-item { font-size: 0.9rem; opacity: 0.85; } /* Main app grid */ .app-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; } /* Responsive */ @media (max-width: 900px) { .info-grid { grid-template-columns: 1fr; } .app-grid { grid-template-columns: 1fr; } } /* Button */ button { background: linear-gradient(135deg, #8b5cf6, #10b981) !important; color: white !important; font-weight: 600; border-radius: 14px !important; height: 48px; } /* Helper text */ .helper { font-size: 0.8rem; opacity: 0.65; margin-top: 0.5rem; } """ # ========================= # UI # ========================= with gr.Blocks() as demo: # HERO gr.Markdown("""
Research-grade automatic summarization for Indonesian text