imelia13 commited on
Commit
5cda286
·
verified ·
1 Parent(s): 380414d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+ import gradio as gr
5
+
6
+ # ===========================
7
+ # Load Model
8
+ # ===========================
9
+ MODEL_NAME = "imelia13/t5-indosum-summarizer"
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
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
+ # Functions
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
+ inputs = tokenizer(
28
+ "ringkas: " + text,
29
+ return_tensors="pt",
30
+ truncation=True,
31
+ max_length=512
32
+ )
33
+ inputs = {k: v.to(device) for k, v in inputs.items()}
34
+
35
+ with torch.no_grad():
36
+ outputs = model.generate(
37
+ **inputs,
38
+ max_length=128,
39
+ num_beams=4,
40
+ early_stopping=True
41
+ )
42
+
43
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
44
+
45
+ # ===========================
46
+ # Gradio Interface
47
+ # ===========================
48
+ def gradio_summarize(text):
49
+ word_count = len(text.split())
50
+ if word_count < 10:
51
+ return "⚠️ Teks terlalu pendek (minimal 10 kata)."
52
+ return summarize_text(text)
53
+
54
+ # UI layout
55
+ iface = gr.Interface(
56
+ fn=gradio_summarize,
57
+ inputs=gr.Textbox(
58
+ lines=15,
59
+ placeholder="Masukkan teks artikel panjang di sini...",
60
+ label="Input Artikel"
61
+ ),
62
+ outputs=gr.Textbox(
63
+ label="Ringkasan"
64
+ ),
65
+ title="AI Text Summarization",
66
+ description="Research-grade automatic summarization for Indonesian text",
67
+ theme="default"
68
+ )
69
+
70
+ # Run app
71
+ if __name__ == "__main__":
72
+ iface.launch()