Spaces:
Sleeping
Sleeping
Download app.py from imelia13/summarization_app: direct link, hf CLI and curl.
- Browser
- Download file 3.2 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
3.2 kB
| import re | |
| import torch | |
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| import gradio as gr | |
| # =========================== | |
| # Load Model | |
| # =========================== | |
| MODEL_NAME = "imelia13/t5-indosum-summarizer" | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME) | |
| model.to(device) | |
| model.eval() | |
| # =========================== | |
| # Functions | |
| # =========================== | |
| def clean_text(text: str) -> str: | |
| text = text.replace("\n", " ").replace("\t", " ") | |
| text = re.sub(r"\s+", " ", text) | |
| return text.strip() | |
| def summarize_text(text: str) -> str: | |
| text = clean_text(text) | |
| inputs = tokenizer( | |
| "ringkas: " + text, | |
| return_tensors="pt", | |
| truncation=True, | |
| max_length=512 | |
| ) | |
| inputs = {k: v.to(device) for k, v in inputs.items()} | |
| 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) | |
| # =========================== | |
| # Gradio Functions | |
| # =========================== | |
| def summarize_with_counter(text): | |
| word_count = len(text.split()) | |
| char_count = len(text) | |
| if word_count < 10: | |
| return "⚠️ Teks terlalu pendek (minimal 10 kata).", word_count, char_count | |
| summary = summarize_text(text) | |
| return summary, word_count, char_count | |
| # =========================== | |
| # Gradio Layout | |
| # =========================== | |
| with gr.Blocks(css=""" | |
| body { background: radial-gradient(circle at top, #141a33, #0b0f1a); color: #e5e7eb; font-family: 'Space Grotesk', sans-serif; } | |
| .gradio-container { max-width: 1200px; margin: auto; padding: 32px; } | |
| h1, h2 { color: #e5e7eb; } | |
| textarea { background-color: rgba(0,0,0,0.35); color: #e5e7eb; } | |
| .output-textbox { background-color: rgba(0,0,0,0.35); color: #e5e7eb; } | |
| button { background: linear-gradient(135deg, #8b5cf6, #10b981); color: white; } | |
| """) as demo: | |
| gr.Markdown("# AI Text Summarization") | |
| gr.Markdown("Research-grade automatic summarization for Indonesian text") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_text = gr.Textbox( | |
| label="Input Artikel", | |
| placeholder="Masukkan teks artikel panjang di sini...", | |
| lines=15 | |
| ) | |
| word_count_display = gr.Label(label="Jumlah Kata") | |
| char_count_display = gr.Label(label="Jumlah Karakter") | |
| summarize_btn = gr.Button("Generate Summary") | |
| with gr.Column(): | |
| output_summary = gr.Textbox( | |
| label="Ringkasan", | |
| interactive=False | |
| ) | |
| def update_counter(text): | |
| word_count = len(text.split()) | |
| char_count = len(text) | |
| return word_count, char_count | |
| input_text.change(fn=update_counter, inputs=input_text, outputs=[word_count_display, char_count_display]) | |
| summarize_btn.click(fn=summarize_with_counter, inputs=input_text, outputs=[output_summary, word_count_display, char_count_display]) | |
| demo.launch() | |