solewarrior commited on
Commit
bfba113
·
verified ·
1 Parent(s): f1450f3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -0
app.py ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import PyPDF2
4
+ import docx
5
+ import textwrap
6
+
7
+ # Streamlit Page Config
8
+ st.set_page_config(
9
+ page_title="TextSphere",
10
+ page_icon="🤖",
11
+ layout="wide",
12
+ initial_sidebar_state="expanded"
13
+ )
14
+
15
+ # Footer
16
+ st.markdown("""
17
+ <style>
18
+ .footer {
19
+ position: fixed;
20
+ bottom: 0;
21
+ right: 0;
22
+ padding: 10px;
23
+ font-size: 16px;
24
+ color: #333;
25
+ background-color: #f1f1f1;
26
+ }
27
+ </style>
28
+ <div class="footer">
29
+ Made with ❤️ by Baibhav Malviya
30
+ </div>
31
+ """, unsafe_allow_html=True)
32
+
33
+ # Load Model
34
+ @st.cache_resource
35
+ def load_models():
36
+ try:
37
+ summarization_model = pipeline("summarization", model="facebook/bart-large-cnn")
38
+ except Exception as e:
39
+ raise RuntimeError(f"Failed to load model: {str(e)}")
40
+ return summarization_model
41
+
42
+ summarization_model = load_models()
43
+
44
+ # Function to Extract Text from PDF
45
+ def extract_text_from_pdf(uploaded_pdf):
46
+ try:
47
+ pdf_reader = PyPDF2.PdfReader(uploaded_pdf)
48
+ pdf_text = ""
49
+ for page in pdf_reader.pages:
50
+ text = page.extract_text()
51
+ if text:
52
+ pdf_text += text + "\n"
53
+ if not pdf_text.strip():
54
+ st.error("No text found in the PDF.")
55
+ return None
56
+ return pdf_text
57
+ except Exception as e:
58
+ st.error(f"Error reading the PDF: {e}")
59
+ return None
60
+
61
+ # Function to Extract Text from TXT
62
+ def extract_text_from_txt(uploaded_txt):
63
+ try:
64
+ return uploaded_txt.read().decode("utf-8").strip()
65
+ except Exception as e:
66
+ st.error(f"Error reading the TXT file: {e}")
67
+ return None
68
+
69
+ # Function to Extract Text from DOCX
70
+ def extract_text_from_docx(uploaded_docx):
71
+ try:
72
+ doc = docx.Document(uploaded_docx)
73
+ return "\n".join([para.text for para in doc.paragraphs]).strip()
74
+ except Exception as e:
75
+ st.error(f"Error reading the DOCX file: {e}")
76
+ return None
77
+
78
+ # Function to Split Text into 1024-Token Chunks
79
+ def chunk_text(text, max_tokens=1024):
80
+ return textwrap.wrap(text, width=max_tokens)
81
+
82
+ # Sidebar for Task Selection (Default: Text Summarization)
83
+ st.sidebar.title("AI Solutions")
84
+ option = st.sidebar.selectbox(
85
+ "Choose a task",
86
+ ["Text Summarization", "Question Answering", "Text Classification", "Language Translation"],
87
+ index=0 # Default to "Text Summarization"
88
+ )
89
+
90
+ # Text Summarization Task
91
+ if option == "Text Summarization":
92
+ st.title("📄 Text Summarization")
93
+ st.markdown("<h4 style='font-size: 20px;'>- because who needs to read the whole document? 🥵</h4>", unsafe_allow_html=True)
94
+
95
+ uploaded_file = st.file_uploader(
96
+ "Upload a document (PDF, TXT, DOCX) - *Note: Processes only 1024 tokens per chunk*",
97
+ type=["pdf", "txt", "docx"]
98
+ )
99
+
100
+ text_to_summarize = ""
101
+
102
+ if uploaded_file:
103
+ file_type = uploaded_file.name.split(".")[-1].lower()
104
+
105
+ if file_type == "pdf":
106
+ text_to_summarize = extract_text_from_pdf(uploaded_file)
107
+ elif file_type == "txt":
108
+ text_to_summarize = extract_text_from_txt(uploaded_file)
109
+ elif file_type == "docx":
110
+ text_to_summarize = extract_text_from_docx(uploaded_file)
111
+ else:
112
+ st.error("Unsupported file format.")
113
+
114
+ if st.button("Summarize"):
115
+ with st.spinner('Summarizing...'):
116
+ try:
117
+ if text_to_summarize:
118
+ chunks = chunk_text(text_to_summarize, max_tokens=1024)
119
+ summaries = []
120
+
121
+ for chunk in chunks:
122
+ input_length = len(chunk.split()) # Count words in the chunk
123
+ max_summary_length = max(50, input_length // 2) # Dynamically adjust max_length
124
+
125
+ summary = summarization_model(chunk, max_length=max_summary_length, min_length=50, do_sample=False)
126
+ summaries.append(summary[0]['summary_text'])
127
+
128
+ final_summary = " ".join(summaries) # Combine all chunk summaries
129
+
130
+ st.write("### Summary:")
131
+ st.write(final_summary)
132
+ else:
133
+ st.error("Please upload a document first.")
134
+ except Exception as e:
135
+ st.error(f"Error: {e}")