ntnmmm commited on
Commit
1ffe3f3
·
verified ·
1 Parent(s): 8a7ba4d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -0
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ st.set_page_config(page_title="VNU Summarizer", layout="wide")
4
+
5
+ # Chèn JavaScript để thay đổi tiêu đề ngay lập tức
6
+ st.markdown(
7
+ """
8
+ <script>
9
+ document.title = "VNU Summarizer";
10
+ </script>
11
+ """,
12
+ unsafe_allow_html=True
13
+ )
14
+ from summarization import MultiDocSummarizationAPI
15
+ import fitz
16
+ from docx import Document
17
+
18
+ # Cấu hình tiêu đề trang ngay từ đầu
19
+
20
+
21
+ # Ẩn footer "Made with Streamlit"
22
+ hide_streamlit_style = """
23
+ <style>
24
+ #MainMenu {visibility: hidden;}
25
+ footer {visibility: hidden;}
26
+ </style>
27
+ """
28
+ st.markdown(hide_streamlit_style, unsafe_allow_html=True)
29
+
30
+ st.image("./Logo_UET.png", width=150)
31
+
32
+ def extract_text_from_pdf(uploaded_file):
33
+ pdf_text = ""
34
+ try:
35
+ with fitz.open(stream=uploaded_file.read(), filetype="pdf") as doc:
36
+ for page in doc:
37
+ pdf_text += page.get_text("text") + "\n"
38
+ except Exception as e:
39
+ st.error(f"Lỗi khi xử lý PDF: {e}")
40
+ return pdf_text
41
+
42
+ def extract_text_from_docx(uploaded_file):
43
+ try:
44
+ doc = Document(uploaded_file)
45
+ return "\n".join([para.text for para in doc.paragraphs])
46
+ except Exception as e:
47
+ st.error(f"Lỗi khi xử lý DOCX: {e}")
48
+ return ""
49
+
50
+ def add_text_area():
51
+ st.session_state.additional_texts.append("")
52
+
53
+ def remove_text_area(index):
54
+ st.session_state.additional_texts.pop(index)
55
+
56
+ if "show_summary" not in st.session_state:
57
+ st.session_state.show_summary = False
58
+
59
+ if "additional_texts" not in st.session_state:
60
+ st.session_state.additional_texts = []
61
+
62
+ st.markdown("<h1>Hệ thống tóm tắt đa văn bản tiếng Việt</h1>", unsafe_allow_html=True)
63
+
64
+ col1, col2 = st.columns([3, 1])
65
+
66
+ with col1:
67
+ st.markdown("### ✍️ Nhập văn bản")
68
+ input_method = st.radio("Phương thức nhập liệu:", ["Nhập văn bản", "Kéo thả tệp"], horizontal=True)
69
+ texts = []
70
+
71
+ if input_method == "Nhập văn bản":
72
+ if st.button("➕ Thêm vùng nhập văn bản"):
73
+ add_text_area()
74
+ for i, text in enumerate(st.session_state.additional_texts):
75
+ with st.expander(f"📌 Văn bản {i + 1}", expanded=True):
76
+ col_expander = st.columns([13, 0.5])
77
+ with col_expander[0]:
78
+ updated_text = st.text_area("", text, height=200, key=f"text_{i}")
79
+ st.session_state.additional_texts[i] = updated_text
80
+ with col_expander[1]:
81
+ if st.button("🗑", key=f"delete_{i}", help="Xóa văn bản"):
82
+ remove_text_area(i)
83
+ st.experimental_rerun()
84
+ texts.append(st.session_state.additional_texts[i])
85
+
86
+ else:
87
+ uploaded_files = st.file_uploader(
88
+ "📂 Kéo thả tệp văn bản:",
89
+ type=["txt", "pdf", "docx"],
90
+ accept_multiple_files=True
91
+ )
92
+
93
+ if uploaded_files:
94
+ for uploaded_file in uploaded_files:
95
+ all_texts = ""
96
+ if uploaded_file.type == "text/plain":
97
+ all_texts = uploaded_file.getvalue().decode("utf-8")
98
+ elif uploaded_file.type == "application/pdf":
99
+ all_texts = extract_text_from_pdf(uploaded_file)
100
+ elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
101
+ all_texts = extract_text_from_docx(uploaded_file)
102
+
103
+ texts.append(all_texts)
104
+
105
+ # st.markdown("### 🎯 Nhập tóm tắt mẫu")
106
+ # golden_ext = st.text_area("📑 Tóm tắt tóm lược", height=100)
107
+ # golden_abs = st.text_area("📝 Tóm tắt trích rút", height=100)
108
+
109
+
110
+ with col2:
111
+ st.markdown("### ⚙️ Tuỳ chọn tóm tắt")
112
+ summary_method = st.selectbox("Chọn phương thức rút gọn:", ["Số câu", "Tỷ lệ"])
113
+
114
+ if summary_method == "Tỷ lệ":
115
+ compress_ratio = st.slider("🔽 Chọn tỷ lệ rút gọn:", 0, 50, 15, step=1, format="%d%%") / 100
116
+ else:
117
+ compress_ratio = st.number_input("🔢 Số câu đầu ra:", min_value=1, max_value=20, value=5, step=1)
118
+
119
+ if st.button("🚀 Tóm tắt") and any(texts):
120
+ summary_results = MultiDocSummarizationAPI(
121
+ texts, compress_ratio#, golden_ext=golden_ext or None, golden_abs=golden_abs or None
122
+ )
123
+ st.session_state.extractive_summary = summary_results.get("extractive_summ", "Không có kết quả")
124
+ st.session_state.abstractive_summary = summary_results.get("abstractive_summ", "Không có kết quả")
125
+ st.session_state.rouge_ext = summary_results.get("score_ext", ("None", "None", "None"))
126
+ st.session_state.rouge_abs = summary_results.get("score_abs", ("None", "None", "None"))
127
+ st.session_state.show_summary = True
128
+ st.experimental_rerun()
129
+
130
+ if st.session_state.get("show_summary", False):
131
+ col_summary = st.columns(2)
132
+ rouge_ext = st.session_state.rouge_ext if st.session_state.rouge_ext is not None else ("None", "None", "None")
133
+ rouge_abs = st.session_state.rouge_abs if st.session_state.rouge_abs is not None else ("None", "None", "None")
134
+
135
+ with col_summary[0]:
136
+ st.markdown("### 📑 Tóm tắt tóm lược")
137
+ # st.markdown(f"**🔹 ROUGE 1:** {rouge_ext[0]}")
138
+ # st.markdown(f"**🔹 ROUGE 2:** {rouge_ext[1]}")
139
+ # st.markdown(f"**🔹 ROUGE L:** {rouge_ext[2]}")
140
+ st.text_area("📑 Tóm tắt trích lược:", st.session_state.extractive_summary, height=250)
141
+
142
+ # with col_summary[1]:
143
+ # st.markdown("### 📝 Tóm tắt trích rút")
144
+ # st.markdown(f"**🔹 ROUGE 1:** {rouge_abs[0]}")
145
+ # st.markdown(f"**🔹 ROUGE 2:** {rouge_abs[1]}")
146
+ # st.markdown(f"**🔹 ROUGE L:** {rouge_abs[2]}")
147
+ # st.text_area("Văn bản tóm tắt trích rút:", st.session_state.abstractive_summary, height=250)