Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from langchain.schema import SystemMessage, HumanMessage
|
|
| 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,6 +16,7 @@ nltk.download('punkt')
|
|
| 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,12 +29,14 @@ st.markdown("""
|
|
| 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,16 +44,19 @@ st.markdown("""
|
|
| 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,14 +68,17 @@ st.markdown("""
|
|
| 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,11 +92,13 @@ st.markdown("""
|
|
| 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,12 +108,14 @@ st.markdown("""
|
|
| 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,10 +133,12 @@ st.markdown("""
|
|
| 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,10 +149,12 @@ st.markdown("""
|
|
| 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,6 +162,7 @@ st.markdown("""
|
|
| 144 |
""", unsafe_allow_html=True)
|
| 145 |
|
| 146 |
|
|
|
|
| 147 |
if "authenticated" not in st.session_state:
|
| 148 |
st.session_state.authenticated = False
|
| 149 |
|
|
@@ -162,9 +181,11 @@ if not st.session_state.authenticated:
|
|
| 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,6 +193,7 @@ if not st.session_state.authenticated:
|
|
| 172 |
margin-bottom: 10px !important;
|
| 173 |
display: block;
|
| 174 |
}
|
|
|
|
| 175 |
input[type="text"],
|
| 176 |
input[type="password"],
|
| 177 |
input[type="text"]:focus,
|
|
@@ -183,6 +205,7 @@ if not st.session_state.authenticated:
|
|
| 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,6 +260,7 @@ if not st.session_state.authenticated:
|
|
| 237 |
""", unsafe_allow_html=True)
|
| 238 |
st.stop()
|
| 239 |
|
|
|
|
| 240 |
with st.sidebar:
|
| 241 |
st.image("log.png", use_container_width=True)
|
| 242 |
|
|
@@ -272,18 +296,20 @@ with st.sidebar:
|
|
| 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,17 +318,19 @@ st.markdown("""
|
|
| 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,15 +338,17 @@ st.markdown("""
|
|
| 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,8 +366,9 @@ st.markdown("""
|
|
| 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,17 +379,20 @@ st.markdown("""
|
|
| 348 |
</div>
|
| 349 |
""", unsafe_allow_html=True)
|
| 350 |
|
|
|
|
| 351 |
llm = ChatOpenAI(
|
| 352 |
base_url="https://api.together.xyz/v1",
|
| 353 |
api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
|
| 354 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 355 |
-
max_tokens=
|
| 356 |
|
| 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,37 +419,41 @@ st.markdown("""
|
|
| 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,6 +475,7 @@ if query:
|
|
| 437 |
else:
|
| 438 |
st.markdown("")
|
| 439 |
|
|
|
|
| 440 |
st.markdown("""
|
| 441 |
<style>
|
| 442 |
.thinking-message {
|
|
@@ -450,7 +489,7 @@ st.markdown("""
|
|
| 450 |
}
|
| 451 |
.spinner {
|
| 452 |
border: 4px solid #f3f3f3;
|
| 453 |
-
border-top: 4px solid #4b6d3d;
|
| 454 |
border-radius: 50%;
|
| 455 |
width: 20px;
|
| 456 |
height: 20px;
|
|
@@ -518,14 +557,13 @@ def extract_keywords_from_text(text, query_words):
|
|
| 518 |
return matched_lines
|
| 519 |
|
| 520 |
def clean_text(text):
|
| 521 |
-
return re.sub(r'[
|
| 522 |
-
|
| 523 |
|
| 524 |
|
| 525 |
from collections import Counter
|
| 526 |
import heapq
|
| 527 |
|
| 528 |
-
def summarize_text_by_frequency(text, num_sentences=
|
| 529 |
sentences = text.split('\n')
|
| 530 |
word_freq = Counter()
|
| 531 |
|
|
@@ -543,9 +581,11 @@ def summarize_text_by_frequency(text, num_sentences=1):
|
|
| 543 |
summarized_sentences = heapq.nlargest(num_sentences, sentence_scores, key=sentence_scores.get)
|
| 544 |
return "\n".join(summarized_sentences)
|
| 545 |
|
|
|
|
|
|
|
| 546 |
|
| 547 |
|
| 548 |
-
def find_closest_lines(query, doc_texts, stop_words, top_n=
|
| 549 |
cleaned_query = remove_stop_words(query, stop_words)
|
| 550 |
query_words = cleaned_query.split()
|
| 551 |
|
|
@@ -554,7 +594,7 @@ def find_closest_lines(query, doc_texts, stop_words, top_n=5):
|
|
| 554 |
for filename, text in doc_texts.items():
|
| 555 |
matched_lines = extract_keywords_from_text(text, query_words)
|
| 556 |
for line in matched_lines:
|
| 557 |
-
similarity = fuzz.partial_ratio(query, line)
|
| 558 |
all_matched_lines.append((line, similarity))
|
| 559 |
|
| 560 |
all_matched_lines.sort(key=lambda x: x[1], reverse=True)
|
|
@@ -572,23 +612,24 @@ def remove_stop_words_from_lines(lines, stop_words):
|
|
| 572 |
return cleaned_lines
|
| 573 |
|
| 574 |
if query:
|
| 575 |
-
closest_lines = find_closest_lines(query, doc_texts, stop_words, top_n=
|
| 576 |
-
|
| 577 |
-
# حذف
|
| 578 |
cleaned_closest_lines = [
|
| 579 |
clean_text(" ".join([word for word in line.split() if word not in stop_words]))
|
| 580 |
for line in closest_lines
|
| 581 |
]
|
| 582 |
-
|
| 583 |
-
# خلاصهسازی
|
| 584 |
summarized_text = summarize_text_by_frequency("\n".join(cleaned_closest_lines), num_sentences=1)
|
| 585 |
|
| 586 |
-
|
|
|
|
|
|
|
|
|
|
| 587 |
prompt = f"""
|
| 588 |
-
لطفاً با توجه به سؤال زیر و محتوای
|
| 589 |
سوال:
|
| 590 |
{query}
|
| 591 |
-
|
| 592 |
{summarized_text}
|
| 593 |
پاسخ نهایی:
|
| 594 |
"""
|
|
@@ -602,4 +643,4 @@ if query:
|
|
| 602 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
| 603 |
|
| 604 |
else:
|
| 605 |
-
st.warning("هیچ
|
|
|
|
| 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 |
|
| 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 |
""", 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 |
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 |
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 |
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 |
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 |
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 |
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 |
""", unsafe_allow_html=True)
|
| 163 |
|
| 164 |
|
| 165 |
+
# ---------- احراز هویت ----------
|
| 166 |
if "authenticated" not in st.session_state:
|
| 167 |
st.session_state.authenticated = False
|
| 168 |
|
|
|
|
| 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 |
margin-bottom: 10px !important;
|
| 194 |
display: block;
|
| 195 |
}
|
| 196 |
+
/* استایل برای ورودیها در تمام حالتها */
|
| 197 |
input[type="text"],
|
| 198 |
input[type="password"],
|
| 199 |
input[type="text"]:focus,
|
|
|
|
| 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 |
""", unsafe_allow_html=True)
|
| 261 |
st.stop()
|
| 262 |
|
| 263 |
+
# ---------- سایدبار ----------
|
| 264 |
with st.sidebar:
|
| 265 |
st.image("log.png", use_container_width=True)
|
| 266 |
|
|
|
|
| 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 |
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 |
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 |
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 |
</div>
|
| 380 |
""", unsafe_allow_html=True)
|
| 381 |
|
| 382 |
+
# ---------- مدل زبانی ----------
|
| 383 |
llm = ChatOpenAI(
|
| 384 |
base_url="https://api.together.xyz/v1",
|
| 385 |
api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
|
| 386 |
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free",
|
| 387 |
+
max_tokens=1024 # specify the max tokens as per your requirement
|
| 388 |
|
| 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 |
|
| 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 |
else:
|
| 476 |
st.markdown("")
|
| 477 |
|
| 478 |
+
# استایلها برای چرخش و پیام در حال فکر کردن
|
| 479 |
st.markdown("""
|
| 480 |
<style>
|
| 481 |
.thinking-message {
|
|
|
|
| 489 |
}
|
| 490 |
.spinner {
|
| 491 |
border: 4px solid #f3f3f3;
|
| 492 |
+
border-top: 4px solid #4b6d3d; /* رنگ سبز تیره */
|
| 493 |
border-radius: 50%;
|
| 494 |
width: 20px;
|
| 495 |
height: 20px;
|
|
|
|
| 557 |
return matched_lines
|
| 558 |
|
| 559 |
def clean_text(text):
|
| 560 |
+
return re.sub(r'[^آ-ی۰-۹0-9،.؟!؛+\-* ]+', '', text)
|
|
|
|
| 561 |
|
| 562 |
|
| 563 |
from collections import Counter
|
| 564 |
import heapq
|
| 565 |
|
| 566 |
+
def summarize_text_by_frequency(text, num_sentences=3):
|
| 567 |
sentences = text.split('\n')
|
| 568 |
word_freq = Counter()
|
| 569 |
|
|
|
|
| 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=10):
|
| 589 |
cleaned_query = remove_stop_words(query, stop_words)
|
| 590 |
query_words = cleaned_query.split()
|
| 591 |
|
|
|
|
| 594 |
for filename, text in doc_texts.items():
|
| 595 |
matched_lines = extract_keywords_from_text(text, query_words)
|
| 596 |
for line in matched_lines:
|
| 597 |
+
similarity = fuzz.partial_ratio(query, line) # محاسبه شباهت خط با سوال
|
| 598 |
all_matched_lines.append((line, similarity))
|
| 599 |
|
| 600 |
all_matched_lines.sort(key=lambda x: x[1], reverse=True)
|
|
|
|
| 612 |
return cleaned_lines
|
| 613 |
|
| 614 |
if query:
|
| 615 |
+
closest_lines = find_closest_lines(query, doc_texts, stop_words, top_n=10)
|
| 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 |
"""
|
|
|
|
| 643 |
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
| 644 |
|
| 645 |
else:
|
| 646 |
+
st.warning("هیچ خط مرتبطی پیدا نشد.")
|