Spaces:
Sleeping
Sleeping
Download app.py from imelia13/summarization-app: direct link, hf CLI and curl.
- Browser
- Download file 4.38 kB
-
https://huggingface.co/spaces/imelia13/summarization-app/resolve/main/app.py
- Command line
-
hf download hf://spaces/imelia13/summarization-app/app.py
-
curl -L -o app.py https://huggingface.co/spaces/imelia13/summarization-app/resolve/main/app.py
4.38 kB
| 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(""" | |
| <div class="hero"> | |
| <h1>AI Text Summarization</h1> | |
| <p>Research-grade automatic summarization for Indonesian text</p> | |
| </div> | |
| """) | |
| # INFO SECTION | |
| gr.Markdown(""" | |
| <div class="card"> | |
| <div class="info-grid"> | |
| <div class="info-item"> | |
| <b>Fungsi</b><br/> | |
| Meringkas artikel, berita, dan dokumen panjang secara otomatis. | |
| </div> | |
| <div class="info-item"> | |
| <b>Model</b><br/> | |
| T5 IndoSum fine-tuned untuk Bahasa Indonesia. | |
| </div> | |
| <div class="info-item"> | |
| <b>Cara Pakai</b><br/> | |
| Tempel teks → klik Generate → salin ringkasan. | |
| </div> | |
| </div> | |
| </div> | |
| """) | |
| # MAIN APP | |
| gr.Markdown('<div class="app-grid">') | |
| # INPUT | |
| gr.Markdown('<div class="card">') | |
| gr.Markdown("### Input Artikel") | |
| input_text = gr.Textbox( | |
| lines=14, | |
| placeholder="Masukkan teks artikel panjang di sini..." | |
| ) | |
| gr.Markdown('<div class="helper">Minimal 10 kata • Maks ±500 kata</div>') | |
| btn = gr.Button("Generate Summary") | |
| gr.Markdown('</div>') | |
| # OUTPUT | |
| gr.Markdown('<div class="card">') | |
| gr.Markdown("### Ringkasan") | |
| output_text = gr.Textbox( | |
| lines=14, | |
| placeholder="Ringkasan akan muncul di sini..." | |
| ) | |
| gr.Markdown('</div>') | |
| gr.Markdown('</div>') | |
| # ACTION | |
| btn.click( | |
| fn=summarize_text, | |
| inputs=input_text, | |
| outputs=output_text | |
| ) | |
| # FOOTER | |
| gr.Markdown(""" | |
| <center style="opacity:0.6; font-size:0.85rem; margin-top:2rem;"> | |
| © 2025 — AI Summarization System • Built with Gradio & Transformers | |
| </center> | |
| """) | |
| demo.launch( | |
| css=custom_css, | |
| theme=gr.themes.Base() | |
| ) | |