Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +57 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from txtai.pipeline import Summary, Textractor
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
|
5 |
+
st.set_page_config(layout="wide")
|
6 |
+
|
7 |
+
@st.cache_resource
|
8 |
+
def text_summary(text, maxlength=None):
|
9 |
+
#create summary instance
|
10 |
+
summary = Summary()
|
11 |
+
text = (text)
|
12 |
+
result = summary(text)
|
13 |
+
return result
|
14 |
+
|
15 |
+
def extract_text_from_pdf(file_path):
|
16 |
+
# Open the PDF file using PyPDF2
|
17 |
+
with open(file_path, "rb") as f:
|
18 |
+
reader = PdfReader(f)
|
19 |
+
page = reader.pages[0]
|
20 |
+
text = page.extract_text()
|
21 |
+
return text
|
22 |
+
|
23 |
+
choice = st.sidebar.selectbox("Select your choice", ["Summarize Text", "Summarize Document"])
|
24 |
+
|
25 |
+
if choice == "Summarize Text":
|
26 |
+
st.subheader("Summarize Text using txtai")
|
27 |
+
input_text = st.text_area("Enter your text here")
|
28 |
+
if input_text is not None:
|
29 |
+
if st.button("Summarize Text"):
|
30 |
+
col1, col2 = st.columns([1,1])
|
31 |
+
with col1:
|
32 |
+
st.markdown("**Your Input Text**")
|
33 |
+
st.info(input_text)
|
34 |
+
with col2:
|
35 |
+
st.markdown("**Summary Result**")
|
36 |
+
result = text_summary(input_text)
|
37 |
+
st.success(result)
|
38 |
+
|
39 |
+
elif choice == "Summarize Document":
|
40 |
+
st.subheader("Summarize Document using txtai")
|
41 |
+
input_file = st.file_uploader("Upload your document here", type=['pdf'])
|
42 |
+
if input_file is not None:
|
43 |
+
if st.button("Summarize Document"):
|
44 |
+
with open("doc_file.pdf", "wb") as f:
|
45 |
+
f.write(input_file.getbuffer())
|
46 |
+
col1, col2 = st.columns([1,1])
|
47 |
+
with col1:
|
48 |
+
st.info("File uploaded successfully")
|
49 |
+
extracted_text = extract_text_from_pdf("doc_file.pdf")
|
50 |
+
st.markdown("**Extracted Text is Below:**")
|
51 |
+
st.info(extracted_text)
|
52 |
+
with col2:
|
53 |
+
st.markdown("**Summary Result**")
|
54 |
+
text = extract_text_from_pdf("doc_file.pdf")
|
55 |
+
doc_summary = text_summary(text)
|
56 |
+
st.success(doc_summary)
|
57 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
txtai[all]
|
2 |
+
streamlit
|
3 |
+
PyPDF2
|