reydeuss commited on
Commit
b0fd48b
·
verified ·
1 Parent(s): 9dde301

create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py for Gradio
2
+ import gradio as gr
3
+ import torch
4
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
5
+
6
+ @gradio_cached_function
7
+ def load_model():
8
+ model = T5ForConditionalGeneration.from_pretrained(
9
+ "cahya/t5-base-indonesian-summarization",
10
+ load_in_8bit=True,
11
+ device_map="auto"
12
+ )
13
+ tokenizer = T5Tokenizer.from_pretrained("cahya/t5-base-indonesian-summarization")
14
+ return model, tokenizer
15
+
16
+ def summarize_text(text):
17
+ if not text.strip():
18
+ return "Please enter text to summarize."
19
+
20
+ model, tokenizer = load_model()
21
+
22
+ # Add T5 prefix
23
+ input_text = f"summarize: {text}"
24
+ inputs = tokenizer(input_text, return_tensors="pt", max_length=1024, truncation=True)
25
+
26
+ with torch.no_grad():
27
+ outputs = model.generate(
28
+ **inputs,
29
+ max_length=512,
30
+ num_beams=4,
31
+ length_penalty=2.0,
32
+ early_stopping=True,
33
+ no_repeat_ngram_size=2
34
+ )
35
+
36
+ summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
37
+ return summary
38
+
39
+ # Create Gradio interface
40
+ interface = gr.Interface(
41
+ fn=summarize_text,
42
+ inputs=gr.Textbox(lines=10, placeholder="Enter Indonesian text here...", label="Input Text"),
43
+ outputs=gr.Textbox(label="Generated Summary"),
44
+ title="Indonesian Text Summarization",
45
+ description="Enter Indonesian text to generate a summary using T5 model",
46
+ examples=[
47
+ ["Your example Indonesian text here..."]
48
+ ]
49
+ )
50
+
51
+ interface.launch()