M17idd commited on
Commit
4116887
·
1 Parent(s): 29d9865

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -75
app.py CHANGED
@@ -7,7 +7,6 @@ from langchain.schema import SystemMessage, HumanMessage
7
  from rapidfuzz import fuzz
8
  import concurrent.futures
9
  import time
10
- # from sentence_transformers import SentenceTransformer
11
  import numpy as np
12
  from hazm import *
13
  import re
@@ -16,7 +15,6 @@ nltk.download('punkt')
16
 
17
  st.markdown("""
18
  <style>
19
- /* استایل برای هدر */
20
  .stAppHeader.st-emotion-cache-12fmjuu.e4hpqof0 {
21
  background-color: rgba(46,59,46, 0.8) !important; /* سبز متمایل به خاکی */
22
  color: #2e3b2e !important; /* رنگ متن روشن */
@@ -29,14 +27,12 @@ st.markdown("""
29
  """, unsafe_allow_html=True)
30
  st.markdown("""
31
  <style>
32
- /* بارگذاری فونت Roboto */
33
  @font-face {
34
  font-family: 'Roboto';
35
  src: url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap') format('woff2');
36
  font-weight: 400;
37
  font-style: normal;
38
  }
39
- /* تنظیم فونت برای کل سایت */
40
  html, body, [class*="css"] {
41
  font-family: 'Roboto', Tahoma, sans-serif !important;
42
  font-weight: 400 !important;
@@ -44,19 +40,16 @@ st.markdown("""
44
  text-align: right;
45
  ظ color: #ffffff;
46
  }
47
- /* طراحی برای بخش استایل اپ */
48
  .stApp {
49
  background: linear-gradient(to left, #4b5e40, #2e3b2e);
50
  color: #ffffff;
51
  }
52
- /* استایل برای سایدبار */
53
  [data-testid="stSidebar"] {
54
  width: 260px !important;
55
  background-color: #1a2b1e;
56
  border: none !important;
57
  padding-top: 20px;
58
  }
59
- /* استایل برای آیتم‌های منو */
60
  .menu-item {
61
  display: flex;
62
  align-items: center;
@@ -68,17 +61,14 @@ st.markdown("""
68
  cursor: pointer;
69
  transition: background-color 0.3s ease;
70
  }
71
- /* استایل برای آیتم‌های منو هنگام هاور */
72
  .menu-item:hover {
73
  background-color: #2e3b2e;
74
  color: #b8860b;
75
  }
76
- /* استایل برای آیکون‌ها در منو */
77
  .menu-item img {
78
  width: 25px;
79
  height: 25px;
80
  }
81
- /* استایل برای دکمه‌ها */
82
  .stButton>button {
83
  background-color: #b8860b !important;
84
  color: #1a2b1e !important;
@@ -92,13 +82,11 @@ st.markdown("""
92
  width: 100%;
93
  margin: 10px 0;
94
  }
95
- /* استایل برای دکمه‌ها هنگام هاور */
96
  .stButton>button:hover {
97
  background-color: #8b6508 !important;
98
  transform: translateY(-2px);
99
  box-shadow: 0 4px 8px rgba(0,0,0,0.3);
100
  }
101
- /* استایل برای متن هدر */
102
  .header-text {
103
  text-align: center;
104
  margin: 20px 0;
@@ -108,14 +96,12 @@ st.markdown("""
108
  box-shadow: 0 6px 12px rgba(0,0,0,0.4);
109
  font-family: 'Roboto', Tahoma, sans-serif; /* اضافه شد */
110
  }
111
- /* استایل برای زیرنویس */
112
  .subtitle {
113
  font-size: 18px;
114
  color: #d4d4d4;
115
  font-weight: 600;
116
  margin-top: 10px;
117
  }
118
- /* استایل برای پیام‌های چت */
119
  .chat-message {
120
  flex-wrap: wrap;
121
  background-color: rgba(26, 43, 30, 0.95);
@@ -133,12 +119,10 @@ st.markdown("""
133
  align-items: center;
134
  gap: 15px;
135
  }
136
- /* انیمیشن برای ورود پیام چت */
137
  @keyframes fadeIn {
138
  from { opacity: 0; transform: translateY(10px); }
139
  to { opacity: 1; transform: translateY(0); }
140
  }
141
- /* استایل برای ورودی متن */
142
  .stTextInput>div>input, .stTextArea textarea {
143
  background-color: rgba(26, 43, 30, 0.95) !important;
144
  border-radius: 10px !important;
@@ -149,12 +133,10 @@ st.markdown("""
149
  font-size: 16px;
150
  color: #d4d4d4 !important;
151
  }
152
- /* استایل برای خط افقی */
153
  hr {
154
  border: 1px solid #b8860b;
155
  margin: 15px 0;
156
  }
157
- /* حذف مرز از قسمت سایدبار */
158
  [data-testid="stSidebar"] > div {
159
  border: none !important;
160
  }
@@ -162,7 +144,6 @@ st.markdown("""
162
  """, unsafe_allow_html=True)
163
 
164
 
165
- # ---------- احراز هویت ----------
166
  if "authenticated" not in st.session_state:
167
  st.session_state.authenticated = False
168
 
@@ -181,11 +162,9 @@ if not st.session_state.authenticated:
181
  """, unsafe_allow_html=True)
182
  st.markdown("""
183
  <style>
184
- /* فونت عمومی */
185
  html, body, [class*="css"] {
186
  font-family: 'Vazir', sans-serif;
187
  }
188
- /* استایل برای برچسب فیلدهای ورودی */
189
  label {
190
  font-size: 20px !important;
191
  color: #ffffff !important;
@@ -193,7 +172,6 @@ if not st.session_state.authenticated:
193
  margin-bottom: 10px !important;
194
  display: block;
195
  }
196
- /* استایل برای ورودی‌ها در تمام حالت‌ها */
197
  input[type="text"],
198
  input[type="password"],
199
  input[type="text"]:focus,
@@ -205,7 +183,6 @@ if not st.session_state.authenticated:
205
  font-size: 18px !important;
206
  font-family: 'Vazir', sans-serif !important;
207
  }
208
- /* Placeholder style */
209
  ::placeholder {
210
  color: #bbbbbb !important;
211
  opacity: 0.8 !important;
@@ -260,7 +237,6 @@ if not st.session_state.authenticated:
260
  """, unsafe_allow_html=True)
261
  st.stop()
262
 
263
- # ---------- سایدبار ----------
264
  with st.sidebar:
265
  st.image("log.png", use_container_width=True)
266
 
@@ -296,20 +272,18 @@ with st.sidebar:
296
 
297
  st.markdown("""
298
  <style>
299
- /* استایل برای متن هدر */
300
  .header-text {
301
  text-align: center;
302
  margin: 50px 0;
303
- background: #2e3b2e; /* سبز زیتونی تیره جدید */
304
  padding: 60px 30px;
305
  border-radius: 25px;
306
- box-shadow: 0 12px 24px rgba(0, 0, 0, 0.8); /* سایه بیشتر */
307
  animation: slideIn 2s ease-in-out, fadeIn 3s ease-in-out;
308
  background-size: cover;
309
  background-position: center;
310
  position: relative;
311
  }
312
- /* انیمیشن ورودی */
313
  @keyframes fadeIn {
314
  0% { opacity: 0; transform: translateY(30px); }
315
  100% { opacity: 1; transform: translateY(0); }
@@ -318,19 +292,17 @@ st.markdown("""
318
  0% { transform: translateX(-50%); opacity: 0; }
319
  100% { transform: translateX(0); opacity: 1; }
320
  }
321
- /* تغییر فونت برای h1 */
322
  .header-text h1 {
323
  font-family: 'Vazir', sans-serif;
324
  font-size: 62px;
325
- color: #d89b00; /* طلایی تیره‌تر */
326
  margin: 0;
327
  font-weight: 900;
328
  letter-spacing: 4px;
329
- text-shadow: 4px 4px 15px rgba(0, 0, 0, 0.9); /* سایه سیاه بیشتر */
330
- transform: scale(1.08); /* کمی بزرگتر شدن */
331
  animation: glow 2s ease-in-out infinite alternate;
332
  }
333
- /* تغییر استایل زیرعنوان */
334
  .subtitle {
335
  font-family: 'Vazir', sans-serif;
336
  font-size: 24px;
@@ -338,17 +310,15 @@ st.markdown("""
338
  font-weight: 700;
339
  margin-top: 15px;
340
  letter-spacing: 2px;
341
- text-shadow: 3px 3px 10px rgba(0,0,0,0.8); /* سایه بیشتر برای زیرعنوان */
342
  animation: fadeInSubtitle 2s ease-in-out;
343
  }
344
- /* انیمیشن زیرعنوان */
345
  @keyframes fadeInSubtitle {
346
  0% { opacity: 0; transform: translateY(20px); }
347
  100% { opacity: 1; transform: translateY(0); }
348
  }
349
- /* استایل برای دکمه‌ها */
350
  .stButton>button {
351
- background-color: #e67e22 !important; /* رنگ دکمه تغییر کرده */
352
  color: #4b5320 !important;
353
  font-family: 'Vazir', sans-serif;
354
  font-weight: 700 !important;
@@ -366,9 +336,8 @@ st.markdown("""
366
  transform: translateY(-4px);
367
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.6);
368
  }
369
- /* استایل برای استایل کلی صفحه */
370
  .stApp {
371
- background: #2e3b2e; /* سبز زیتونی تیره جدید */
372
  color: white;
373
  font-family: 'Vazir', sans-serif;
374
  }
@@ -379,7 +348,6 @@ st.markdown("""
379
  </div>
380
  """, unsafe_allow_html=True)
381
 
382
- # ---------- مدل زبانی ----------
383
  llm = ChatOpenAI(
384
  base_url="https://api.together.xyz/v1",
385
  api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
@@ -389,10 +357,8 @@ llm = ChatOpenAI(
389
  )
390
 
391
 
392
- # ---------- ورودی جستجو ----------
393
  st.markdown("""
394
  <style>
395
- /* استایل برای کلاس خاص st-emotion-cache-128upt6 eht7o1d3 */
396
  .st-emotion-cache-128upt6.eht7o1d3 {
397
  background-color: rgba(46,59,46, 0.8) !important; /* سبز تیره (44533f) */
398
  border-radius: 10px !important; /* گوشه‌های گرد */
@@ -419,41 +385,37 @@ st.markdown("""
419
 
420
  st.markdown("""
421
  <style>
422
- /* استایل برای کلاس st-emotion-cache-yd4u6l e1togvvn1 */
423
  .st-emotion-cache-yd4u6l.e1togvvn1 {
424
- background-color: rgba(106, 127, 83, 0.8) !important; /* سبز خاکی مایل به زرد (#6a7f53) */
425
- border-radius: 10px !important; /* گوشه‌های گرد */
426
- color: #d4d4d4 !important; /* رنگ متن روشن */
427
  font-family: 'Vazirmatn', Tahoma, sans-serif !important;
428
- padding: 15px !important; /* فضای داخلی بیشتر */
429
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3) !important; /* سایه برای برجسته شدن */
430
  }
431
  </style>
432
  """, unsafe_allow_html=True)
433
  st.markdown("""
434
  <style>
435
- /* استایل برای هدر */
436
  .stAppHeader.st-emotion-cache-12fmjuu.e4hpqof0 {
437
- background-color: rgba(42, 55, 39, 0.9) !important; /* سبز تیره‌تر */
438
- color: #d4d4d4 !important; /* رنگ متن روشن */
439
  font-family: 'Vazirmatn', Tahoma, sans-serif !important;
440
- padding: 20px !important; /* فضای داخلی بیشتر */
441
- border-radius: 10px !important; /* گوشه‌های گرد */
442
- box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3) !important; /* سایه برای برجسته شدن */
443
  }
444
  </style>
445
  """, unsafe_allow_html=True)
446
 
447
  st.markdown("""
448
  <style>
449
- /* تغییر رنگ متن placeholder به خاکستری */
450
  textarea::placeholder {
451
- color: #ffffff !important; /* خاکستری */
452
- opacity: 1 !important; /* برای اینکه مرورگرها بهش بی‌توجه نباشن */
453
  }
454
- /* تغییر رنگ متن داخل چت اینپوت به خاکستری */
455
  textarea {
456
- color: #ffffff !important; /* خاکستری */
457
  border-radius: 10px !important;
458
  padding: 10px !important;
459
  }
@@ -475,7 +437,6 @@ if query:
475
  else:
476
  st.markdown("")
477
 
478
- # استایل‌ها برای چرخش و پیام در حال فکر کردن
479
  st.markdown("""
480
  <style>
481
  .thinking-message {
@@ -489,7 +450,7 @@ st.markdown("""
489
  }
490
  .spinner {
491
  border: 4px solid #f3f3f3;
492
- border-top: 4px solid #4b6d3d; /* رنگ سبز تیره */
493
  border-radius: 50%;
494
  width: 20px;
495
  height: 20px;
@@ -581,8 +542,6 @@ def summarize_text_by_frequency(text, num_sentences=1):
581
  summarized_sentences = heapq.nlargest(num_sentences, sentence_scores, key=sentence_scores.get)
582
  return "\n".join(summarized_sentences)
583
 
584
-
585
-
586
 
587
 
588
  def find_closest_lines(query, doc_texts, stop_words, top_n=5):
@@ -613,34 +572,56 @@ def remove_stop_words_from_lines(lines, stop_words):
613
 
614
  if query:
615
  closest_lines = find_closest_lines(query, doc_texts, stop_words, top_n=5)
616
-
617
- # حذف استپ‌وردها از خطوط و سپس پاکسازی نهایی متن
618
  cleaned_closest_lines = [
619
  clean_text(" ".join([word for word in line.split() if word not in stop_words]))
620
  for line in closest_lines
621
  ]
622
- summarized_text = summarize_text_by_frequency("\n".join(cleaned_closest_lines), num_sentences=1)
623
 
 
624
 
625
  if summarized_text:
626
- # cleaned_text = "\n".join(cleaned_closest_lines[:1])
627
-
628
  prompt = f"""
629
- لطفاً با توجه به سؤال زیر و محتوای خطوط مرتبط، یک پاسخ نهایی حرفه‌ای، دقیق و روان تولید کن. فقط از متن خطوط مرتبط استفاده کن. اگر اطلاعات کافی در متن وجود ندارد، صادقانه اعلام کن.
630
  سوال:
631
  {query}
632
- خطوط مرتبط:
633
  {summarized_text}
634
- پاسخ نهایی:
635
  """
636
-
637
  response = llm([
638
  SystemMessage(content="You are a helpful assistant."),
639
  HumanMessage(content=prompt)
640
  ])
641
- rewritten = clean_text(response.content.strip())
642
 
643
- st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
644
 
 
 
 
 
645
  else:
646
- st.warning("هیچ خط مرتبطی پیدا نشد.")
 
 
 
 
 
 
 
 
 
 
 
 
7
  from rapidfuzz import fuzz
8
  import concurrent.futures
9
  import time
 
10
  import numpy as np
11
  from hazm import *
12
  import re
 
15
 
16
  st.markdown("""
17
  <style>
 
18
  .stAppHeader.st-emotion-cache-12fmjuu.e4hpqof0 {
19
  background-color: rgba(46,59,46, 0.8) !important; /* سبز متمایل به خاکی */
20
  color: #2e3b2e !important; /* رنگ متن روشن */
 
27
  """, unsafe_allow_html=True)
28
  st.markdown("""
29
  <style>
 
30
  @font-face {
31
  font-family: 'Roboto';
32
  src: url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap') format('woff2');
33
  font-weight: 400;
34
  font-style: normal;
35
  }
 
36
  html, body, [class*="css"] {
37
  font-family: 'Roboto', Tahoma, sans-serif !important;
38
  font-weight: 400 !important;
 
40
  text-align: right;
41
  ظ color: #ffffff;
42
  }
 
43
  .stApp {
44
  background: linear-gradient(to left, #4b5e40, #2e3b2e);
45
  color: #ffffff;
46
  }
 
47
  [data-testid="stSidebar"] {
48
  width: 260px !important;
49
  background-color: #1a2b1e;
50
  border: none !important;
51
  padding-top: 20px;
52
  }
 
53
  .menu-item {
54
  display: flex;
55
  align-items: center;
 
61
  cursor: pointer;
62
  transition: background-color 0.3s ease;
63
  }
 
64
  .menu-item:hover {
65
  background-color: #2e3b2e;
66
  color: #b8860b;
67
  }
 
68
  .menu-item img {
69
  width: 25px;
70
  height: 25px;
71
  }
 
72
  .stButton>button {
73
  background-color: #b8860b !important;
74
  color: #1a2b1e !important;
 
82
  width: 100%;
83
  margin: 10px 0;
84
  }
 
85
  .stButton>button:hover {
86
  background-color: #8b6508 !important;
87
  transform: translateY(-2px);
88
  box-shadow: 0 4px 8px rgba(0,0,0,0.3);
89
  }
 
90
  .header-text {
91
  text-align: center;
92
  margin: 20px 0;
 
96
  box-shadow: 0 6px 12px rgba(0,0,0,0.4);
97
  font-family: 'Roboto', Tahoma, sans-serif; /* اضافه شد */
98
  }
 
99
  .subtitle {
100
  font-size: 18px;
101
  color: #d4d4d4;
102
  font-weight: 600;
103
  margin-top: 10px;
104
  }
 
105
  .chat-message {
106
  flex-wrap: wrap;
107
  background-color: rgba(26, 43, 30, 0.95);
 
119
  align-items: center;
120
  gap: 15px;
121
  }
 
122
  @keyframes fadeIn {
123
  from { opacity: 0; transform: translateY(10px); }
124
  to { opacity: 1; transform: translateY(0); }
125
  }
 
126
  .stTextInput>div>input, .stTextArea textarea {
127
  background-color: rgba(26, 43, 30, 0.95) !important;
128
  border-radius: 10px !important;
 
133
  font-size: 16px;
134
  color: #d4d4d4 !important;
135
  }
 
136
  hr {
137
  border: 1px solid #b8860b;
138
  margin: 15px 0;
139
  }
 
140
  [data-testid="stSidebar"] > div {
141
  border: none !important;
142
  }
 
144
  """, unsafe_allow_html=True)
145
 
146
 
 
147
  if "authenticated" not in st.session_state:
148
  st.session_state.authenticated = False
149
 
 
162
  """, unsafe_allow_html=True)
163
  st.markdown("""
164
  <style>
 
165
  html, body, [class*="css"] {
166
  font-family: 'Vazir', sans-serif;
167
  }
 
168
  label {
169
  font-size: 20px !important;
170
  color: #ffffff !important;
 
172
  margin-bottom: 10px !important;
173
  display: block;
174
  }
 
175
  input[type="text"],
176
  input[type="password"],
177
  input[type="text"]:focus,
 
183
  font-size: 18px !important;
184
  font-family: 'Vazir', sans-serif !important;
185
  }
 
186
  ::placeholder {
187
  color: #bbbbbb !important;
188
  opacity: 0.8 !important;
 
237
  """, unsafe_allow_html=True)
238
  st.stop()
239
 
 
240
  with st.sidebar:
241
  st.image("log.png", use_container_width=True)
242
 
 
272
 
273
  st.markdown("""
274
  <style>
 
275
  .header-text {
276
  text-align: center;
277
  margin: 50px 0;
278
+ background: #2e3b2e;
279
  padding: 60px 30px;
280
  border-radius: 25px;
281
+ box-shadow: 0 12px 24px rgba(0, 0, 0, 0.8);
282
  animation: slideIn 2s ease-in-out, fadeIn 3s ease-in-out;
283
  background-size: cover;
284
  background-position: center;
285
  position: relative;
286
  }
 
287
  @keyframes fadeIn {
288
  0% { opacity: 0; transform: translateY(30px); }
289
  100% { opacity: 1; transform: translateY(0); }
 
292
  0% { transform: translateX(-50%); opacity: 0; }
293
  100% { transform: translateX(0); opacity: 1; }
294
  }
 
295
  .header-text h1 {
296
  font-family: 'Vazir', sans-serif;
297
  font-size: 62px;
298
+ color: #d89b00;
299
  margin: 0;
300
  font-weight: 900;
301
  letter-spacing: 4px;
302
+ text-shadow: 4px 4px 15px rgba(0, 0, 0, 0.9);
303
+ transform: scale(1.08);
304
  animation: glow 2s ease-in-out infinite alternate;
305
  }
 
306
  .subtitle {
307
  font-family: 'Vazir', sans-serif;
308
  font-size: 24px;
 
310
  font-weight: 700;
311
  margin-top: 15px;
312
  letter-spacing: 2px;
313
+ text-shadow: 3px 3px 10px rgba(0,0,0,0.8);
314
  animation: fadeInSubtitle 2s ease-in-out;
315
  }
 
316
  @keyframes fadeInSubtitle {
317
  0% { opacity: 0; transform: translateY(20px); }
318
  100% { opacity: 1; transform: translateY(0); }
319
  }
 
320
  .stButton>button {
321
+ background-color: #e67e22 !important;
322
  color: #4b5320 !important;
323
  font-family: 'Vazir', sans-serif;
324
  font-weight: 700 !important;
 
336
  transform: translateY(-4px);
337
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.6);
338
  }
 
339
  .stApp {
340
+ background: #2e3b2e;
341
  color: white;
342
  font-family: 'Vazir', sans-serif;
343
  }
 
348
  </div>
349
  """, unsafe_allow_html=True)
350
 
 
351
  llm = ChatOpenAI(
352
  base_url="https://api.together.xyz/v1",
353
  api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
 
357
  )
358
 
359
 
 
360
  st.markdown("""
361
  <style>
 
362
  .st-emotion-cache-128upt6.eht7o1d3 {
363
  background-color: rgba(46,59,46, 0.8) !important; /* سبز تیره (44533f) */
364
  border-radius: 10px !important; /* گوشه‌های گرد */
 
385
 
386
  st.markdown("""
387
  <style>
 
388
  .st-emotion-cache-yd4u6l.e1togvvn1 {
389
+ background-color: rgba(106, 127, 83, 0.8) !important;
390
+ border-radius: 10px !important;
391
+ color: #d4d4d4 !important;
392
  font-family: 'Vazirmatn', Tahoma, sans-serif !important;
393
+ padding: 15px !important;
394
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3) !important;
395
  }
396
  </style>
397
  """, unsafe_allow_html=True)
398
  st.markdown("""
399
  <style>
 
400
  .stAppHeader.st-emotion-cache-12fmjuu.e4hpqof0 {
401
+ background-color: rgba(42, 55, 39, 0.9) !important;
402
+ color: #d4d4d4 !important;
403
  font-family: 'Vazirmatn', Tahoma, sans-serif !important;
404
+ padding: 20px !important;
405
+ border-radius: 10px !important;
406
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3) !important;
407
  }
408
  </style>
409
  """, unsafe_allow_html=True)
410
 
411
  st.markdown("""
412
  <style>
 
413
  textarea::placeholder {
414
+ color: #ffffff !important;
415
+ opacity: 1 !important;
416
  }
 
417
  textarea {
418
+ color: #ffffff !important;
419
  border-radius: 10px !important;
420
  padding: 10px !important;
421
  }
 
437
  else:
438
  st.markdown("")
439
 
 
440
  st.markdown("""
441
  <style>
442
  .thinking-message {
 
450
  }
451
  .spinner {
452
  border: 4px solid #f3f3f3;
453
+ border-top: 4px solid #4b6d3d;
454
  border-radius: 50%;
455
  width: 20px;
456
  height: 20px;
 
542
  summarized_sentences = heapq.nlargest(num_sentences, sentence_scores, key=sentence_scores.get)
543
  return "\n".join(summarized_sentences)
544
 
 
 
545
 
546
 
547
  def find_closest_lines(query, doc_texts, stop_words, top_n=5):
 
572
 
573
  if query:
574
  closest_lines = find_closest_lines(query, doc_texts, stop_words, top_n=5)
575
+
 
576
  cleaned_closest_lines = [
577
  clean_text(" ".join([word for word in line.split() if word not in stop_words]))
578
  for line in closest_lines
579
  ]
 
580
 
581
+ summarized_text = summarize_text_by_frequency("\n".join(cleaned_closest_lines), num_sentences=1)
582
 
583
  if summarized_text:
 
 
584
  prompt = f"""
585
+ تعدادی جمله مرتبط با سوال زیر استخراج شده است. لطفاً ابتدا این جملات را بررسی کن، سپس با در نظر گرفتن محتوای سوال و لحن آن، یک پاسخ نهایی حرفه‌ای، دقیق و روان ارائه کن که هم به سوال پاسخ دهد و هم از نظر نگارشی و ساختاری در سطح بالایی باشد. پاسخ نهایی باید حداکثر 1024 کاراکتر و حداقل 512 کاراکتر باشد، خلاصه و واضح نوشته شود و فقط به زبان فارسی باشد. از تکرار اضافی پرهیز کن و در صورت نیاز، محتوای جملات را ترکیب کن.
586
  سوال:
587
  {query}
588
+ جملات مرتبط:
589
  {summarized_text}
590
+ پاسخ نهایی حرفه‌ای بازنویسی‌شده:
591
  """
 
592
  response = llm([
593
  SystemMessage(content="You are a helpful assistant."),
594
  HumanMessage(content=prompt)
595
  ])
596
+ rewritten = response.content.strip()
597
 
598
+ review_prompt = f"""
599
+ لطفاً بررسی کن که آیا پاسخ زیر به سوال داده‌شده مرتبط، صحیح، معتبر و قابل قبول است یا نه. اگر پاسخ مرتبط و صحیح است، فقط بنویس: 'تأیید شد'. اگر پاسخ اشتباه است یا ربطی به سوال ندارد یا معتبر نیست، فقط بنویس: 'نیاز به اصلاح دارد'.
600
+ سوال:
601
+ {query}
602
+ پاسخ:
603
+ {rewritten}
604
+ """
605
+ review_response = llm([
606
+ SystemMessage(content="You are a helpful assistant."),
607
+ HumanMessage(content=review_prompt)
608
+ ])
609
+ review_result = review_response.content.strip()
610
 
611
+ if "تأیید شد" in review_result:
612
+ rewritten = clean_text(rewritten)
613
+ st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
614
+ think.empty()
615
  else:
616
+ prompt = f"""
617
+ هیچ جمله مرتبط مستقیمی با سوال یافت نشد. لطفاً با توجه به سوال زیر، یک پاسخ حرفه‌ای، مرتبط، معنادار و معتبر تولید کن. فقط از جملات موجود در اسناد معتبر استفاده کن و از ساخت جملات ساختگی یا استفاده از زبان غیر فارسی خودداری کن.
618
+ سوال:
619
+ {query}
620
+ """
621
+ response = llm([
622
+ SystemMessage(content="You are a helpful assistant."),
623
+ HumanMessage(content=prompt)
624
+ ])
625
+ rewritten = clean_text(response.content.strip())
626
+ st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
627
+ think.empty()