songhieng commited on
Commit
02a4337
·
verified ·
1 Parent(s): 686f671

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py CHANGED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+
4
+ # Load tokenizer and model
5
+ model_identifier = "songhieng/khmer-mt5-summarization"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_identifier)
7
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_identifier)
8
+
9
+ # Set page configuration
10
+ st.set_page_config(page_title="Khmer Text Summarization", layout="wide")
11
+
12
+ # App title and description
13
+ st.title("Khmer Text Summarization")
14
+ st.write("Enter Khmer text below to generate a concise summary.")
15
+
16
+ # Text input
17
+ user_input = st.text_area("Input Text:", height=300)
18
+
19
+ # Summarization parameters
20
+ st.sidebar.header("Summarization Settings")
21
+ max_length = st.sidebar.slider("Maximum Summary Length", min_value=50, max_value=300, value=150, step=10)
22
+ min_length = st.sidebar.slider("Minimum Summary Length", min_value=10, max_value=100, value=30, step=5)
23
+ num_beams = st.sidebar.slider("Number of Beams", min_value=1, max_value=10, value=4, step=1)
24
+
25
+ # Summarize button
26
+ if st.button("Summarize"):
27
+ if user_input.strip():
28
+ try:
29
+ # Tokenize input
30
+ inputs = tokenizer.encode(user_input, return_tensors="pt", truncation=True)
31
+
32
+ # Generate summary
33
+ summary_ids = model.generate(
34
+ inputs,
35
+ max_length=max_length,
36
+ min_length=min_length,
37
+ num_beams=num_beams,
38
+ length_penalty=2.0,
39
+ early_stopping=True
40
+ )
41
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
42
+
43
+ # Display summary
44
+ st.subheader("Summary:")
45
+ st.write(summary)
46
+ except Exception as e:
47
+ st.error(f"An error occurred during summarization: {e}")
48
+ else:
49
+ st.warning("Please enter some text to summarize.")