Update app.py
Browse files
app.py
CHANGED
@@ -2,60 +2,56 @@ import streamlit as st
|
|
2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
3 |
import torch
|
4 |
|
5 |
-
#
|
6 |
st.markdown("""
|
7 |
<style>
|
8 |
.main {
|
9 |
-
background-color: #
|
10 |
-
background-image: linear-gradient(
|
11 |
}
|
12 |
.stTextArea textarea {
|
13 |
-
border: 2px solid #
|
14 |
border-radius: 15px;
|
15 |
padding: 10px;
|
16 |
-
font-family: '
|
17 |
-
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.
|
18 |
width: 100%;
|
19 |
max-width: 800px;
|
20 |
margin: 0 auto;
|
21 |
}
|
22 |
.stTextArea textarea:focus {
|
23 |
-
border-color: #
|
24 |
-
box-shadow: 0 0 10px #
|
25 |
-
}
|
26 |
-
.css-4yfn50, div[data-baseweb="base-button"] {
|
27 |
-
background-color: #F06292 !important;
|
28 |
-
color: white !important;
|
29 |
-
border-radius: 20px !important;
|
30 |
-
border: none !important;
|
31 |
-
padding: 10px 25px !important;
|
32 |
-
font-size: 16px !important;
|
33 |
-
font-weight: bold !important;
|
34 |
-
box-shadow: 0 4px 12px rgba(240, 98, 146, 0.4) !important;
|
35 |
-
transition: all 0.3s ease !important;
|
36 |
-
width: 100%;
|
37 |
-
max-width: 300px;
|
38 |
-
margin: 0 auto;
|
39 |
-
}
|
40 |
-
.css-4yfn50:hover, div[data-baseweb="base-button"]:hover {
|
41 |
-
background-color: #E91E63 !important;
|
42 |
-
box-shadow: 0 6px 14px rgba(233, 30, 99, 0.5) !important;
|
43 |
-
transform: translateY(-2px) !important;
|
44 |
}
|
45 |
.stTitle {
|
46 |
-
color: #
|
47 |
-
font-family: '
|
48 |
font-size: 3em !important;
|
49 |
text-align: center;
|
50 |
-
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
|
51 |
margin-bottom: 30px !important;
|
52 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
.summary-container {
|
54 |
-
background-color:
|
55 |
border-radius: 15px;
|
56 |
padding: 20px;
|
57 |
-
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.
|
58 |
-
border-left: 5px solid #
|
59 |
margin-top: 20px;
|
60 |
width: 100%;
|
61 |
max-width: 800px;
|
@@ -63,82 +59,45 @@ st.markdown("""
|
|
63 |
margin-right: auto;
|
64 |
}
|
65 |
.summary-title {
|
66 |
-
color: #
|
67 |
font-weight: bold;
|
68 |
font-size: 1.5em;
|
69 |
margin-bottom: 10px;
|
70 |
-
font-family: '
|
71 |
-
}
|
72 |
-
.app-container {
|
73 |
-
max-width: 1000px;
|
74 |
-
margin: 0 auto;
|
75 |
-
padding: 20px;
|
76 |
}
|
77 |
.footer {
|
78 |
text-align: center;
|
79 |
margin-top: 50px;
|
80 |
padding: 20px;
|
81 |
-
color: #
|
82 |
font-style: italic;
|
83 |
}
|
84 |
-
.header-image {
|
85 |
-
display: block;
|
86 |
-
margin: 0 auto 30px auto;
|
87 |
-
max-width: 100%;
|
88 |
-
height: auto;
|
89 |
-
}
|
90 |
-
/* تحسينات الاستجابة للشاشات الصغيرة */
|
91 |
-
@media screen and (max-width: 768px) {
|
92 |
-
.stTitle {
|
93 |
-
font-size: 2em !important;
|
94 |
-
}
|
95 |
-
.stTextArea textarea {
|
96 |
-
font-size: 14px;
|
97 |
-
}
|
98 |
-
.summary-container {
|
99 |
-
padding: 15px;
|
100 |
-
}
|
101 |
-
.summary-title {
|
102 |
-
font-size: 1.2em;
|
103 |
-
}
|
104 |
-
}
|
105 |
</style>
|
106 |
""", unsafe_allow_html=True)
|
107 |
|
108 |
-
#
|
109 |
model_path = "./saved_model"
|
110 |
tokenizer_path = "./saved_tokenizer"
|
111 |
|
112 |
try:
|
113 |
-
tokenizer = T5Tokenizer.from_pretrained(
|
114 |
-
|
115 |
-
|
116 |
-
)
|
117 |
-
|
118 |
-
model = T5ForConditionalGeneration.from_pretrained(
|
119 |
-
model_path,
|
120 |
-
local_files_only=True,
|
121 |
-
ignore_mismatched_sizes=True
|
122 |
-
)
|
123 |
-
|
124 |
-
device = torch.device("cpu") # تحديد الجهاز على CPU
|
125 |
-
model.to(device) # نقل النموذج إلى CPU
|
126 |
model_loaded = True
|
127 |
-
|
128 |
except Exception as e:
|
129 |
st.error(f"Error loading model: {e}")
|
130 |
model_loaded = False
|
131 |
|
132 |
-
# دالة توليد الملخص
|
133 |
def generate_summary(text):
|
134 |
try:
|
135 |
inputs = ["summarize: " + text]
|
136 |
inputs = tokenizer(inputs, max_length=1024, truncation=True, return_tensors="pt").to(device)
|
137 |
outputs = model.generate(
|
138 |
-
inputs.input_ids,
|
139 |
-
max_length=150,
|
140 |
-
length_penalty=2.0,
|
141 |
-
num_beams=4,
|
142 |
early_stopping=True
|
143 |
)
|
144 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
@@ -146,40 +105,34 @@ def generate_summary(text):
|
|
146 |
st.error(f"Error generating summary: {e}")
|
147 |
return None
|
148 |
|
149 |
-
|
150 |
-
st.markdown('<div class="app-container">', unsafe_allow_html=True)
|
151 |
|
152 |
-
st.title("✨ تطبيق التلخيص الذكي ✨")
|
153 |
-
|
154 |
-
# إضافة صورة زخرفية (اختياري)
|
155 |
st.markdown("""
|
156 |
<div style="text-align: center; margin-bottom: 30px;">
|
157 |
-
<img src="https://api.placeholder.com/300x150?text
|
158 |
</div>
|
159 |
""", unsafe_allow_html=True)
|
160 |
|
161 |
-
text = st.text_area("
|
162 |
|
163 |
col1, col2, col3 = st.columns([1, 2, 1])
|
164 |
with col2:
|
165 |
-
if st.button("
|
166 |
if text and model_loaded:
|
167 |
-
with st.spinner("
|
168 |
summary = generate_summary(text)
|
169 |
if summary:
|
170 |
-
st.markdown('<div class="summary-container"><div class="summary-title"
|
171 |
summary + '</div>', unsafe_allow_html=True)
|
172 |
else:
|
173 |
-
st.error("❌
|
174 |
elif not model_loaded:
|
175 |
-
st.error("❌
|
176 |
else:
|
177 |
-
st.warning("⚠️
|
178 |
|
179 |
-
# إضافة فوتر جميل
|
180 |
st.markdown("""
|
181 |
<div class="footer">
|
182 |
-
|
183 |
-
</div>
|
184 |
</div>
|
185 |
-
""", unsafe_allow_html=True)
|
|
|
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 {
|
9 |
+
background-color: #FAFAFA;
|
10 |
+
background-image: linear-gradient(135deg, #ffffff 0%, #e0f7fa 100%);
|
11 |
}
|
12 |
.stTextArea textarea {
|
13 |
+
border: 2px solid #81D4FA;
|
14 |
border-radius: 15px;
|
15 |
padding: 10px;
|
16 |
+
font-family: 'Segoe UI', sans-serif;
|
17 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
|
18 |
width: 100%;
|
19 |
max-width: 800px;
|
20 |
margin: 0 auto;
|
21 |
}
|
22 |
.stTextArea textarea:focus {
|
23 |
+
border-color: #29B6F6;
|
24 |
+
box-shadow: 0 0 10px #4FC3F7;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
}
|
26 |
.stTitle {
|
27 |
+
color: #0288D1;
|
28 |
+
font-family: 'Segoe UI', sans-serif;
|
29 |
font-size: 3em !important;
|
30 |
text-align: center;
|
|
|
31 |
margin-bottom: 30px !important;
|
32 |
}
|
33 |
+
.stButton>button {
|
34 |
+
background-color: #29B6F6;
|
35 |
+
color: white;
|
36 |
+
border-radius: 20px;
|
37 |
+
border: none;
|
38 |
+
padding: 10px 25px;
|
39 |
+
font-size: 16px;
|
40 |
+
font-weight: bold;
|
41 |
+
box-shadow: 0 4px 12px rgba(41, 182, 246, 0.4);
|
42 |
+
transition: all 0.3s ease;
|
43 |
+
}
|
44 |
+
.stButton>button:hover {
|
45 |
+
background-color: #0288D1;
|
46 |
+
box-shadow: 0 6px 14px rgba(2, 136, 209, 0.5);
|
47 |
+
transform: translateY(-2px);
|
48 |
+
}
|
49 |
.summary-container {
|
50 |
+
background-color: #ffffff;
|
51 |
border-radius: 15px;
|
52 |
padding: 20px;
|
53 |
+
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);
|
54 |
+
border-left: 5px solid #29B6F6;
|
55 |
margin-top: 20px;
|
56 |
width: 100%;
|
57 |
max-width: 800px;
|
|
|
59 |
margin-right: auto;
|
60 |
}
|
61 |
.summary-title {
|
62 |
+
color: #0288D1;
|
63 |
font-weight: bold;
|
64 |
font-size: 1.5em;
|
65 |
margin-bottom: 10px;
|
66 |
+
font-family: 'Segoe UI', sans-serif;
|
|
|
|
|
|
|
|
|
|
|
67 |
}
|
68 |
.footer {
|
69 |
text-align: center;
|
70 |
margin-top: 50px;
|
71 |
padding: 20px;
|
72 |
+
color: #0288D1;
|
73 |
font-style: italic;
|
74 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
</style>
|
76 |
""", unsafe_allow_html=True)
|
77 |
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
model_loaded = True
|
|
|
88 |
except Exception as e:
|
89 |
st.error(f"Error loading model: {e}")
|
90 |
model_loaded = False
|
91 |
|
|
|
92 |
def generate_summary(text):
|
93 |
try:
|
94 |
inputs = ["summarize: " + text]
|
95 |
inputs = tokenizer(inputs, max_length=1024, truncation=True, return_tensors="pt").to(device)
|
96 |
outputs = model.generate(
|
97 |
+
inputs.input_ids,
|
98 |
+
max_length=150,
|
99 |
+
length_penalty=2.0,
|
100 |
+
num_beams=4,
|
101 |
early_stopping=True
|
102 |
)
|
103 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
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)
|