imelia13 commited on
Commit
e9329d1
·
verified ·
1 Parent(s): 3673cc5

Create app_gradio.py

Browse files
Files changed (1) hide show
  1. app_gradio.py +91 -0
app_gradio.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import torch
3
+ import gradio as gr
4
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
5
+
6
+ # -----------------------------
7
+ # Model Loading (sama seperti inference.py)
8
+ # -----------------------------
9
+ MODEL_NAME = "imelia13/t5-indosum-summarizer"
10
+ device = torch.device("cpu") # bisa ganti "cuda" jika ada GPU
11
+
12
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
13
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
14
+ model.to(device)
15
+ model.eval()
16
+
17
+ # -----------------------------
18
+ # Fungsi Utility
19
+ # -----------------------------
20
+ def clean_text(text: str) -> str:
21
+ text = text.replace("\n", " ").replace("\t", " ")
22
+ text = re.sub(r"\s+", " ", text)
23
+ return text.strip()
24
+
25
+ def summarize_text(text: str) -> str:
26
+ text = clean_text(text)
27
+
28
+ # Tokenize input
29
+ inputs = tokenizer(
30
+ "ringkas: " + text,
31
+ return_tensors="pt",
32
+ truncation=True,
33
+ max_length=512
34
+ )
35
+ inputs = {k: v.to(device) for k, v in inputs.items()}
36
+
37
+ # Generate summary
38
+ with torch.no_grad():
39
+ outputs = model.generate(
40
+ **inputs,
41
+ max_length=128,
42
+ num_beams=4,
43
+ early_stopping=True
44
+ )
45
+
46
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
47
+
48
+ # -----------------------------
49
+ # Fungsi Wrapper untuk Gradio
50
+ # -----------------------------
51
+ def summarize_wrapper(text: str):
52
+ # Hitung jumlah kata
53
+ word_count = len(text.strip().split())
54
+ if word_count < 10:
55
+ return "⚠️ Teks terlalu pendek (minimal 10 kata)."
56
+
57
+ summary = summarize_text(text)
58
+ char_count = len(text)
59
+ return f"Ringkasan:\n{summary}\n\n(Kata: {word_count}, Karakter: {char_count})"
60
+
61
+ # -----------------------------
62
+ # Interface Gradio
63
+ # -----------------------------
64
+ with gr.Blocks(title="AI Text Summarization") as demo:
65
+ gr.Markdown("## AI Text Summarization")
66
+ gr.Markdown("Research-grade automatic summarization for Indonesian text")
67
+
68
+ with gr.Row():
69
+ with gr.Column():
70
+ text_input = gr.Textbox(
71
+ label="Input Artikel",
72
+ placeholder="Masukkan teks artikel panjang di sini...",
73
+ lines=15
74
+ )
75
+ summarize_button = gr.Button("Generate Summary")
76
+ with gr.Column():
77
+ summary_output = gr.Textbox(
78
+ label="Ringkasan",
79
+ placeholder="Hasil ringkasan akan muncul di sini...",
80
+ lines=15
81
+ )
82
+
83
+ summarize_button.click(
84
+ summarize_wrapper,
85
+ inputs=text_input,
86
+ outputs=summary_output
87
+ )
88
+
89
+ # Jalankan Gradio
90
+ if __name__ == "__main__":
91
+ demo.launch()