Azizul Hakim commited on
Commit
b2abd44
·
unverified ·
1 Parent(s): 00c0948
Files changed (1) hide show
  1. app.py +68 -0
app.py CHANGED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import PegasusForConditionalGeneration, PegasusTokenizer
4
+
5
+ # Caching the model and tokenizer loading to speed up the app
6
+ @st.cache_resource
7
+ def load_model_and_tokenizer():
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+
10
+ model = PegasusForConditionalGeneration.from_pretrained('google/pegasus-large')
11
+ model.load_state_dict(torch.load('pegasus_bengali_epoch_2.pt')) # Load the saved model
12
+ model = model.to(device)
13
+
14
+ tokenizer = PegasusTokenizer('bengali_tokenizer.model', do_lower_case=False)
15
+
16
+ return model, tokenizer, device
17
+
18
+ # Summarization function
19
+ def summarize_bengali_text(model, tokenizer, device, text):
20
+ inputs = tokenizer(text, return_tensors="pt", max_length=512, truncation=True).to(device)
21
+
22
+ # Generate the summary
23
+ summary_ids = model.generate(
24
+ inputs['input_ids'],
25
+ max_length=70,
26
+ num_beams=5,
27
+ length_penalty=2.0,
28
+ early_stopping=True
29
+ )
30
+
31
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
32
+ return summary
33
+
34
+ # Streamlit UI design
35
+ def main():
36
+ st.set_page_config(page_title="Bengali Text Summarizer", layout="centered", initial_sidebar_state="collapsed")
37
+
38
+ # Title and description
39
+ st.title("📜 Bengali Text Summarizer")
40
+ st.write("Enter a Bengali paragraph and get a concise summary instantly.")
41
+
42
+ # Sidebar info
43
+ with st.sidebar:
44
+ st.header("How to Use:")
45
+ st.write("1. Enter your Bengali text in the input box below.")
46
+ st.write("2. Click on **Summarize**.")
47
+ st.write("3. Get the summary of your input text.")
48
+
49
+ # User input
50
+ bengali_text = st.text_area("Enter Bengali Text:", height=200, placeholder="আপনার বাংলা টেক্সট এখানে লিখুন...")
51
+
52
+ # Load model and tokenizer
53
+ model, tokenizer, device = load_model_and_tokenizer()
54
+
55
+ # Summarization button
56
+ if st.button("Summarize"):
57
+ if bengali_text.strip():
58
+ # Summarize the text
59
+ with st.spinner("Summarizing..."):
60
+ summary = summarize_bengali_text(model, tokenizer, device, bengali_text)
61
+
62
+ # Display the result
63
+ st.subheader("Summary:")
64
+ st.write(summary)
65
+ else:
66
+ st.warning("Please enter some Bengali text to summarize!")
67
+ if __name__ == "__main__":
68
+ main()