Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import re
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 5 |
+
|
| 6 |
+
# =========================
|
| 7 |
+
# LOAD MODEL
|
| 8 |
+
# =========================
|
| 9 |
+
MODEL_ID = "imelia13/t5-indosum-summarizer"
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID)
|
| 13 |
+
|
| 14 |
+
device = torch.device("cpu")
|
| 15 |
+
model.to(device)
|
| 16 |
+
|
| 17 |
+
# =========================
|
| 18 |
+
# TEXT PROCESSING
|
| 19 |
+
# =========================
|
| 20 |
+
def clean_text(text):
|
| 21 |
+
text = re.sub(r"\s+", " ", text.replace("\n", " ").replace("\t", " "))
|
| 22 |
+
return text.strip()
|
| 23 |
+
|
| 24 |
+
def summarize_text(text):
|
| 25 |
+
if not text or len(text.split()) < 10:
|
| 26 |
+
return "⚠️ Teks terlalu pendek (minimal 10 kata)."
|
| 27 |
+
|
| 28 |
+
text = clean_text(text)
|
| 29 |
+
|
| 30 |
+
inputs = tokenizer(
|
| 31 |
+
"ringkas: " + text,
|
| 32 |
+
return_tensors="pt",
|
| 33 |
+
truncation=True,
|
| 34 |
+
max_length=512
|
| 35 |
+
).to(device)
|
| 36 |
+
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
outputs = model.generate(
|
| 39 |
+
**inputs,
|
| 40 |
+
max_length=128,
|
| 41 |
+
num_beams=2
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 45 |
+
|
| 46 |
+
# =========================
|
| 47 |
+
# CUSTOM CSS (FORCE SIDE BY SIDE)
|
| 48 |
+
# =========================
|
| 49 |
+
custom_css = """
|
| 50 |
+
body {
|
| 51 |
+
background: radial-gradient(circle at top, #141a33, #0b0f1a);
|
| 52 |
+
font-family: 'Space Grotesk', sans-serif;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
.gradio-container {
|
| 56 |
+
max-width: 1200px !important;
|
| 57 |
+
margin: auto !important;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
/* Paksa kiri-kanan di desktop */
|
| 61 |
+
.gr-row {
|
| 62 |
+
flex-wrap: nowrap !important;
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
/* Responsive: mobile tetap atas-bawah */
|
| 66 |
+
@media (max-width: 900px) {
|
| 67 |
+
.gr-row {
|
| 68 |
+
flex-wrap: wrap !important;
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
h1 {
|
| 73 |
+
font-size: 2.6rem;
|
| 74 |
+
font-weight: 700;
|
| 75 |
+
text-align: center;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
button {
|
| 79 |
+
background: linear-gradient(135deg, #8b5cf6, #10b981) !important;
|
| 80 |
+
color: white !important;
|
| 81 |
+
font-weight: 600;
|
| 82 |
+
border-radius: 14px !important;
|
| 83 |
+
}
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
# =========================
|
| 87 |
+
# UI
|
| 88 |
+
# =========================
|
| 89 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Base()) as demo:
|
| 90 |
+
|
| 91 |
+
gr.Markdown("""
|
| 92 |
+
<div style="text-align:center; margin-bottom: 2rem;">
|
| 93 |
+
<h1>AI Text Summarization</h1>
|
| 94 |
+
<p>Research-grade automatic summarization for Indonesian text</p>
|
| 95 |
+
</div>
|
| 96 |
+
""")
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
with gr.Column(scale=1, min_width=420):
|
| 100 |
+
gr.Markdown("### Input Artikel")
|
| 101 |
+
input_text = gr.Textbox(
|
| 102 |
+
lines=16,
|
| 103 |
+
placeholder="Masukkan teks artikel panjang di sini..."
|
| 104 |
+
)
|
| 105 |
+
btn = gr.Button("Generate Summary")
|
| 106 |
+
|
| 107 |
+
with gr.Column(scale=1, min_width=420):
|
| 108 |
+
gr.Markdown("### Ringkasan")
|
| 109 |
+
output_text = gr.Textbox(
|
| 110 |
+
lines=16,
|
| 111 |
+
placeholder="Hasil ringkasan akan muncul di sini..."
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
btn.click(
|
| 115 |
+
fn=summarize_text,
|
| 116 |
+
inputs=input_text,
|
| 117 |
+
outputs=output_text
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
gr.Markdown(
|
| 121 |
+
"<center><small>© 2025 — AI Summarization System</small></center>"
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
demo.launch()
|