hadheedo commited on
Commit
c170413
·
verified ·
1 Parent(s): 9860181

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -18
app.py CHANGED
@@ -2,16 +2,129 @@ import streamlit as st
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
3
  import torch
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  # تحميل النموذج والـ Tokenizer
6
  model_path = "./saved_model"
7
  tokenizer_path = "./saved_tokenizer"
8
 
9
  try:
10
- tokenizer = T5Tokenizer.from_pretrained(tokenizer_path)
11
- model = T5ForConditionalGeneration.from_pretrained(model_path)
 
 
 
 
 
 
 
 
 
12
  device = torch.device("cpu") # تحديد الجهاز على CPU
13
  model.to(device) # نقل النموذج إلى CPU
14
  model_loaded = True
 
15
  except Exception as e:
16
  st.error(f"Error loading model: {e}")
17
  model_loaded = False
@@ -20,28 +133,53 @@ except Exception as e:
20
  def generate_summary(text):
21
  try:
22
  inputs = ["summarize: " + text]
23
- inputs = tokenizer(inputs, max_length=1024, truncation=True, return_tensors="pt").to(device) # نقل ال inputs الي ال CPU
24
- outputs = model.generate(inputs.input_ids, max_length=150, length_penalty=2.0, num_beams=4, early_stopping=True)
 
 
 
 
 
 
25
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
26
  except Exception as e:
27
  st.error(f"Error generating summary: {e}")
28
  return None
29
 
30
  # واجهة المستخدم باستخدام Streamlit
31
- st.title("application summarize")
 
 
32
 
33
- text = st.text_area("enter text you want to summarize.")
 
 
 
 
 
34
 
35
- if st.button("summary:"):
36
- if text and model_loaded:
37
- with st.spinner("Generating summary..."):
38
- summary = generate_summary(text)
39
- if summary:
40
- st.write("summary")
41
- st.write(summary)
 
 
 
 
 
 
 
 
42
  else:
43
- st.warning("Summary generation failed. Please check the input text.")
44
- elif not model_loaded:
45
- st.error("Model failed to load. Please check the application logs.")
46
- else:
47
- st.warning("Please enter text.")
 
 
 
 
 
2
  from transformers import T5Tokenizer, T5ForConditionalGeneration
3
  import torch
4
 
5
+ # تعيين سمات CSS مخصصة لتصميم أنثوي متجاوب
6
+ st.markdown("""
7
+ <style>
8
+ .main {
9
+ background-color: #FDF2F9;
10
+ background-image: linear-gradient(120deg, #FDF2F9 0%, #FFEBEE 100%);
11
+ }
12
+ .stTextArea textarea {
13
+ border: 2px solid #FF80AB;
14
+ border-radius: 15px;
15
+ padding: 10px;
16
+ font-family: 'Arial', sans-serif;
17
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
18
+ width: 100%;
19
+ max-width: 800px;
20
+ margin: 0 auto;
21
+ }
22
+ .stTextArea textarea:focus {
23
+ border-color: #F06292;
24
+ box-shadow: 0 0 10px #F48FB1;
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: #D81B60;
47
+ font-family: 'Brush Script MT', cursive;
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: white;
55
+ border-radius: 15px;
56
+ padding: 20px;
57
+ box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
58
+ border-left: 5px solid #F06292;
59
+ margin-top: 20px;
60
+ width: 100%;
61
+ max-width: 800px;
62
+ margin-left: auto;
63
+ margin-right: auto;
64
+ }
65
+ .summary-title {
66
+ color: #D81B60;
67
+ font-weight: bold;
68
+ font-size: 1.5em;
69
+ margin-bottom: 10px;
70
+ font-family: 'Arial', sans-serif;
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: #E91E63;
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
  # تحميل النموذج والـ Tokenizer
109
  model_path = "./saved_model"
110
  tokenizer_path = "./saved_tokenizer"
111
 
112
  try:
113
+ tokenizer = T5Tokenizer.from_pretrained(
114
+ tokenizer_path,
115
+ local_files_only=True
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
 
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)
145
  except Exception as e:
146
  st.error(f"Error generating summary: {e}")
147
  return None
148
 
149
  # واجهة المستخدم باستخدام Streamlit
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=✨Summary Magic✨" width="300" class="header-image">
158
+ </div>
159
+ """, unsafe_allow_html=True)
160
 
161
+ text = st.text_area("أدخل النص الذي تريد تلخيصه...", height=200)
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">💕 الملخص 💕</div>' +
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)