File size: 3,201 Bytes
5cda286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb405b2
5cda286
fb405b2
5cda286
fb405b2
5cda286
fb405b2
 
 
5cda286
fb405b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5cda286
fb405b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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()