hadheedo commited on
Commit
1b3a96f
·
verified ·
1 Parent(s): ec17889

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -99
app.py CHANGED
@@ -1,151 +1,109 @@
1
  import streamlit as st
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
3
  import torch
4
- import os
5
 
6
- # Custom CSS styling for a light, elegant design
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  st.markdown("""
8
  <style>
9
  .main {
10
- background-color: #FAFAFA;
11
- background-image: linear-gradient(135deg, #ffffff 0%, #e0f7fa 100%);
 
12
  }
13
  .stTextArea textarea {
14
- border: 2px solid #81D4FA;
15
- border-radius: 15px;
16
- padding: 10px;
17
  font-family: 'Segoe UI', sans-serif;
18
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
19
  width: 100%;
20
  max-width: 800px;
21
- margin: 0 auto;
 
22
  }
23
  .stTextArea textarea:focus {
24
- border-color: #29B6F6;
25
- box-shadow: 0 0 10px #4FC3F7;
26
- }
27
- .stTitle {
28
- color: #0288D1;
29
- font-family: 'Segoe UI', sans-serif;
30
- font-size: 3em !important;
31
- text-align: center;
32
- margin-bottom: 30px !important;
33
  }
34
  .stButton>button {
35
- background-color: #29B6F6;
36
  color: white;
37
- border-radius: 20px;
38
  border: none;
39
- padding: 10px 25px;
40
- font-size: 16px;
41
  font-weight: bold;
42
- box-shadow: 0 4px 12px rgba(41, 182, 246, 0.4);
43
  transition: all 0.3s ease;
44
  }
45
  .stButton>button:hover {
46
- background-color: #0288D1;
47
- box-shadow: 0 6px 14px rgba(2, 136, 209, 0.5);
48
  transform: translateY(-2px);
49
  }
 
 
 
 
 
 
 
50
  .summary-container {
51
  background-color: #ffffff;
52
- border-radius: 15px;
53
  padding: 20px;
54
- box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
55
- border-left: 5px solid #29B6F6;
56
  margin-top: 20px;
57
- width: 100%;
58
  max-width: 800px;
59
- margin-left: auto;
60
- margin-right: auto;
61
  }
62
  .summary-title {
63
- color: #0288D1;
64
  font-weight: bold;
65
  font-size: 1.5em;
66
  margin-bottom: 10px;
67
- font-family: 'Segoe UI', sans-serif;
68
  }
69
  .footer {
70
  text-align: center;
71
  margin-top: 50px;
72
  padding: 20px;
73
- color: #0288D1;
74
  font-style: italic;
75
  }
76
  </style>
77
  """, unsafe_allow_html=True)
78
 
79
- # Load model and tokenizer
80
- model_path = "./saved_model"
81
- tokenizer_path = "./saved_tokenizer" # Define this path for saved tokenizer
82
-
83
- try:
84
- # Check if the tokenizer is saved, if not, add custom tokens
85
- if not os.path.exists(tokenizer_path):
86
- tokenizer = T5Tokenizer.from_pretrained("t5-small")
87
-
88
- # Add the custom token and ensure the tokenizer is saved properly
89
- added_tokens = tokenizer.add_tokens(['<extra_id_99>']) # Add custom token if required
90
-
91
- # Ensure the tokenization process handles the vocabulary correctly
92
- tokenizer.save_pretrained(tokenizer_path)
93
- else:
94
- tokenizer = T5Tokenizer.from_pretrained(tokenizer_path, local_files_only=True)
95
-
96
- model = T5ForConditionalGeneration.from_pretrained(model_path, local_files_only=True, ignore_mismatched_sizes=True)
97
- model.resize_token_embeddings(len(tokenizer)) # Resize the embeddings to match tokenizer size
98
- device = torch.device("cpu")
99
- model.to(device)
100
- model_loaded = True
101
- except Exception as e:
102
- st.error(f"Error loading model: {e}")
103
- model_loaded = False
104
-
105
- def generate_summary(text):
106
- try:
107
- inputs = ["summarize: " + text]
108
- inputs = tokenizer(inputs, max_length=1024, truncation=True, return_tensors="pt").to(device)
109
- outputs = model.generate(
110
- inputs.input_ids,
111
- max_length=150,
112
- length_penalty=2.0,
113
- num_beams=4,
114
- early_stopping=True
115
- )
116
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
117
- except Exception as e:
118
- st.error(f"Error generating summary: {e}")
119
- return None
120
-
121
- st.title("🧠 Smart Text Summarizer")
122
-
123
- st.markdown("""
124
- <div style="text-align: center; margin-bottom: 30px;">
125
- <img src="https://api.placeholder.com/300x150?text=Smart+Summary" width="300" class="header-image">
126
- </div>
127
- """, unsafe_allow_html=True)
128
 
129
  text = st.text_area("Enter the text you want to summarize...", height=200)
130
 
131
- col1, col2, col3 = st.columns([1, 2, 1])
132
- with col2:
133
- if st.button("🔍 Generate Summary"):
134
- if text and model_loaded:
135
- with st.spinner("Generating summary..."):
136
- summary = generate_summary(text)
137
- if summary:
138
- st.markdown('<div class="summary-container"><div class="summary-title">📋 Summary</div>' +
139
- summary + '</div>', unsafe_allow_html=True)
140
- else:
141
- st.error("❌ Failed to generate summary. Please check your input.")
142
- elif not model_loaded:
143
- st.error("❌ Failed to load model. Please check the application logs.")
144
- else:
145
- st.warning("⚠️ Please enter text to summarize.")
146
 
 
147
  st.markdown("""
148
  <div class="footer">
149
- Smart Text Summarizer - Crafted with hadheedo
150
  </div>
151
  """, unsafe_allow_html=True)
 
1
  import streamlit as st
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
3
  import torch
 
4
 
5
+ # Load model and tokenizer
6
+ model_path = "./saved_model"
7
+ tokenizer_path = "./saved_tokenizer"
8
+
9
+ tokenizer = T5Tokenizer.from_pretrained(tokenizer_path)
10
+ model = T5ForConditionalGeneration.from_pretrained(model_path)
11
+
12
+ # Function to generate summary
13
+ def generate_summary(text):
14
+ inputs = ["summarize: " + text]
15
+ inputs = tokenizer(inputs, max_length=1024, truncation=True, return_tensors="pt")
16
+ outputs = model.generate(inputs.input_ids.to(model.device), max_length=150, length_penalty=2.0, num_beams=4, early_stopping=True)
17
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
18
+
19
+ # Custom CSS styling for a clean, modern interface
20
  st.markdown("""
21
  <style>
22
  .main {
23
+ background-color: #f0f2f6;
24
+ background-image: linear-gradient(135deg, #e6f7ff 0%, #b3e0ff 100%);
25
+ font-family: 'Arial', sans-serif;
26
  }
27
  .stTextArea textarea {
28
+ border: 2px solid #0078d4;
29
+ border-radius: 12px;
30
+ padding: 15px;
31
  font-family: 'Segoe UI', sans-serif;
32
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
33
  width: 100%;
34
  max-width: 800px;
35
+ margin: 20px auto;
36
+ font-size: 16px;
37
  }
38
  .stTextArea textarea:focus {
39
+ border-color: #0058a3;
40
+ box-shadow: 0 0 10px #4f8bf9;
 
 
 
 
 
 
 
41
  }
42
  .stButton>button {
43
+ background-color: #29b6f6;
44
  color: white;
45
+ border-radius: 25px;
46
  border: none;
47
+ padding: 12px 30px;
48
+ font-size: 18px;
49
  font-weight: bold;
50
+ box-shadow: 0 4px 12px rgba(41, 182, 246, 0.3);
51
  transition: all 0.3s ease;
52
  }
53
  .stButton>button:hover {
54
+ background-color: #0288d1;
55
+ box-shadow: 0 6px 14px rgba(2, 136, 209, 0.4);
56
  transform: translateY(-2px);
57
  }
58
+ .stTitle {
59
+ color: #0078d4;
60
+ font-size: 2.5em;
61
+ text-align: center;
62
+ margin-bottom: 20px;
63
+ font-family: 'Segoe UI', sans-serif;
64
+ }
65
  .summary-container {
66
  background-color: #ffffff;
67
+ border-radius: 12px;
68
  padding: 20px;
69
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
 
70
  margin-top: 20px;
 
71
  max-width: 800px;
72
+ margin: 20px auto;
 
73
  }
74
  .summary-title {
75
+ color: #0288d1;
76
  font-weight: bold;
77
  font-size: 1.5em;
78
  margin-bottom: 10px;
 
79
  }
80
  .footer {
81
  text-align: center;
82
  margin-top: 50px;
83
  padding: 20px;
84
+ color: #0078d4;
85
  font-style: italic;
86
  }
87
  </style>
88
  """, unsafe_allow_html=True)
89
 
90
+ # Application UI
91
+ st.title("📝 Text Summarizer App")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
 
93
  text = st.text_area("Enter the text you want to summarize...", height=200)
94
 
95
+ if st.button("Generate Summary"):
96
+ if text:
97
+ with st.spinner("Generating summary..."):
98
+ summary = generate_summary(text)
99
+ st.markdown('<div class="summary-container"><div class="summary-title">📋 Summary</div>' +
100
+ summary + '</div>', unsafe_allow_html=True)
101
+ else:
102
+ st.warning("⚠️ Please enter text to summarize.")
 
 
 
 
 
 
 
103
 
104
+ # Footer
105
  st.markdown("""
106
  <div class="footer">
107
+ Created with hadheedo
108
  </div>
109
  """, unsafe_allow_html=True)