hadheedo commited on
Commit
6df877a
Β·
verified Β·
1 Parent(s): e84051a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -25
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import streamlit as st
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
3
  import torch
 
 
4
 
5
- # Custom CSS styling for a light, elegant design
6
  st.markdown("""
7
  <style>
8
  .main {
@@ -78,9 +80,15 @@ st.markdown("""
78
  # Load model and tokenizer
79
  model_path = "./saved_model"
80
  tokenizer_path = "./saved_tokenizer"
 
81
 
82
  try:
83
- tokenizer = T5Tokenizer.from_pretrained(tokenizer_path, local_files_only=True)
 
 
 
 
 
84
  model = T5ForConditionalGeneration.from_pretrained(model_path, local_files_only=True, ignore_mismatched_sizes=True)
85
  device = torch.device("cpu")
86
  model.to(device)
@@ -105,34 +113,55 @@ def generate_summary(text):
105
  st.error(f"Error generating summary: {e}")
106
  return None
107
 
108
- st.title("🧠 Smart Text Summarizer")
 
 
 
 
109
 
110
- st.markdown("""
111
- <div style="text-align: center; margin-bottom: 30px;">
112
- <img src="https://api.placeholder.com/300x150?text=Smart+Summary" width="300" class="header-image">
113
- </div>
114
- """, unsafe_allow_html=True)
 
 
 
115
 
116
- text = st.text_area("Enter the text you want to summarize...", height=200)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
- col1, col2, col3 = st.columns([1, 2, 1])
119
- with col2:
120
- if st.button("πŸ” Generate Summary"):
121
- if text and model_loaded:
122
- with st.spinner("Generating summary..."):
123
- summary = generate_summary(text)
124
- if summary:
125
- st.markdown('<div class="summary-container"><div class="summary-title">πŸ“‹ Summary</div>' +
126
- summary + '</div>', unsafe_allow_html=True)
127
- else:
128
- st.error("❌ Failed to generate summary. Please check your input.")
129
- elif not model_loaded:
130
- st.error("❌ Failed to load model. Please check the application logs.")
131
- else:
132
- st.warning("⚠️ Please enter text to summarize.")
133
 
134
  st.markdown("""
135
  <div class="footer">
136
- Smart Text Summarizer.
137
  </div>
138
  """, unsafe_allow_html=True)
 
 
1
  import streamlit as st
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
3
  import torch
4
+ import os
5
+ import json
6
 
7
+ # Custom CSS styling
8
  st.markdown("""
9
  <style>
10
  .main {
 
80
  # Load model and tokenizer
81
  model_path = "./saved_model"
82
  tokenizer_path = "./saved_tokenizer"
83
+ summary_file = "summaries.json"
84
 
85
  try:
86
+ if not os.path.exists(tokenizer_path):
87
+ tokenizer = T5Tokenizer.from_pretrained("t5-small")
88
+ tokenizer.save_pretrained(tokenizer_path)
89
+ else:
90
+ tokenizer = T5Tokenizer.from_pretrained(tokenizer_path, local_files_only=True)
91
+
92
  model = T5ForConditionalGeneration.from_pretrained(model_path, local_files_only=True, ignore_mismatched_sizes=True)
93
  device = torch.device("cpu")
94
  model.to(device)
 
113
  st.error(f"Error generating summary: {e}")
114
  return None
115
 
116
+ def load_summaries():
117
+ if os.path.exists(summary_file):
118
+ with open(summary_file, "r", encoding="utf-8") as f:
119
+ return json.load(f)
120
+ return []
121
 
122
+ def save_summary(original, summary):
123
+ summaries = load_summaries()
124
+ summaries.append({"text": original, "summary": summary})
125
+ with open(summary_file, "w", encoding="utf-8") as f:
126
+ json.dump(summaries, f, indent=4, ensure_ascii=False)
127
+
128
+ # Sidebar Navigation
129
+ page = st.sidebar.selectbox("Choose Page", ["Generate Summary", "Saved Summaries"])
130
 
131
+ if page == "Generate Summary":
132
+ st.title("🧠 Smart Text Summarizer")
133
+ text = st.text_area("Enter the text you want to summarize...", height=200)
134
+ col1, col2, col3 = st.columns([1, 2, 1])
135
+ with col2:
136
+ if st.button("πŸ” Generate Summary"):
137
+ if text and model_loaded:
138
+ with st.spinner("Generating summary..."):
139
+ summary = generate_summary(text)
140
+ if summary:
141
+ st.markdown('<div class="summary-container"><div class="summary-title">πŸ“‹ Summary</div>' +
142
+ summary + '</div>', unsafe_allow_html=True)
143
+ save_summary(text, summary)
144
+ else:
145
+ st.error("❌ Failed to generate summary. Please check your input.")
146
+ elif not model_loaded:
147
+ st.error("❌ Failed to load model. Please check the application logs.")
148
+ else:
149
+ st.warning("⚠️ Please enter text to summarize.")
150
 
151
+ elif page == "Saved Summaries":
152
+ st.title("πŸ“š Saved Summaries")
153
+ summaries = load_summaries()
154
+ if summaries:
155
+ for i, item in enumerate(summaries[::-1], 1):
156
+ st.markdown(f"<div class='summary-container'><div class='summary-title'>πŸ“ Summary #{i}</div>"
157
+ f"<strong>Original:</strong> {item['text']}<br><br>"
158
+ f"<strong>Summary:</strong> {item['summary']}</div>", unsafe_allow_html=True)
159
+ else:
160
+ st.info("No saved summaries yet. Generate some first!")
 
 
 
 
 
161
 
162
  st.markdown("""
163
  <div class="footer">
164
+ Smart Text Summarizer - Crafted with hadheedo
165
  </div>
166
  """, unsafe_allow_html=True)
167
+