Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,14 @@ from langchain.chat_models import ChatOpenAI
|
|
| 6 |
from langchain.schema import SystemMessage, HumanMessage
|
| 7 |
from rapidfuzz import fuzz
|
| 8 |
import concurrent.futures
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
st.markdown("""
|
| 10 |
<style>
|
| 11 |
/* استایل برای هدر */
|
|
@@ -102,6 +110,7 @@ st.markdown("""
|
|
| 102 |
margin-top: 10px;
|
| 103 |
}
|
| 104 |
.chat-message {
|
|
|
|
| 105 |
background-color: rgba(26, 43, 30, 0.95);
|
| 106 |
border: 2px solid #b8860b;
|
| 107 |
border-radius: 15px;
|
|
@@ -113,6 +122,7 @@ st.markdown("""
|
|
| 113 |
color: #d4d4d4;
|
| 114 |
font-weight: 600;
|
| 115 |
display: flex;
|
|
|
|
| 116 |
align-items: center;
|
| 117 |
gap: 15px;
|
| 118 |
}
|
|
@@ -207,7 +217,6 @@ st.markdown("""
|
|
| 207 |
font-weight: 400;
|
| 208 |
font-style: normal;
|
| 209 |
}
|
| 210 |
-
|
| 211 |
.header-text h1 {
|
| 212 |
font-family: 'IRANSans', Tahoma, sans-serif !important;
|
| 213 |
font-weight: 900 !important;
|
|
@@ -215,7 +224,6 @@ st.markdown("""
|
|
| 215 |
color: #b8860b !important;
|
| 216 |
margin: 0 !important;
|
| 217 |
}
|
| 218 |
-
|
| 219 |
.header-text .subtitle {
|
| 220 |
font-family: 'IRANSans', Tahoma, sans-serif !important;
|
| 221 |
font-weight: 600 !important;
|
|
@@ -237,11 +245,12 @@ st.markdown("""
|
|
| 237 |
llm = ChatOpenAI(
|
| 238 |
base_url="https://api.together.xyz/v1",
|
| 239 |
api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
|
| 240 |
-
model="
|
|
|
|
| 241 |
)
|
| 242 |
|
| 243 |
# ---------- پردازش فایلها با کش و موازی ----------
|
| 244 |
-
folder_path = '46'
|
| 245 |
normalizer = Normalizer()
|
| 246 |
sentence_tokenizer = SentenceTokenizer()
|
| 247 |
|
|
@@ -251,24 +260,27 @@ def load_and_process_documents(path):
|
|
| 251 |
try:
|
| 252 |
full_path = os.path.join(path, filename)
|
| 253 |
doc = docx.Document(full_path)
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
except:
|
|
|
|
| 259 |
return []
|
| 260 |
-
return []
|
| 261 |
-
|
| 262 |
-
all_sentences = []
|
| 263 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 264 |
results = executor.map(process_docx, [f for f in os.listdir(path) if f.endswith(".docx")])
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
return
|
|
|
|
| 269 |
|
| 270 |
all_sentences = load_and_process_documents(folder_path)
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
# ---------- ورودی جستجو ----------
|
| 273 |
st.markdown("""
|
| 274 |
<style>
|
|
@@ -283,19 +295,20 @@ st.markdown("""
|
|
| 283 |
}
|
| 284 |
</style>
|
| 285 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 286 |
st.markdown("""
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
}
|
| 297 |
-
</style>
|
| 298 |
""", unsafe_allow_html=True)
|
|
|
|
| 299 |
st.markdown("""
|
| 300 |
<style>
|
| 301 |
/* استایل برای کلاس st-emotion-cache-yd4u6l e1togvvn1 */
|
|
@@ -322,28 +335,106 @@ st.markdown("""
|
|
| 322 |
}
|
| 323 |
</style>
|
| 324 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
query = st.chat_input("چطور میتونم کمک کنم؟")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
if query:
|
| 327 |
found = False
|
| 328 |
-
threshold =
|
| 329 |
|
| 330 |
for idx, sentence in enumerate(all_sentences):
|
| 331 |
similarity = fuzz.partial_ratio(query, sentence)
|
| 332 |
if similarity >= threshold:
|
| 333 |
-
|
| 334 |
-
for i in range(1, 10):
|
| 335 |
-
if idx + i < len(all_sentences):
|
| 336 |
-
next_sentences.append(all_sentences[idx + i])
|
| 337 |
|
| 338 |
-
total_text = sentence + " " + " ".join(next_sentences)
|
| 339 |
prompt = f"""
|
| 340 |
-
تعدادی پاسخ برای سوال زیر تولید شده است. لطفاً ابتدا این پاسخها را بررسی کن، سپس با در نظر گرفتن محتوای سوال و لحن آن، یک پاسخ نهایی حرفهای، دقیق و روان ارائه کن که هم به سوال پاسخ دهد و هم از نظر نگارشی و ساختاری در سطح بالایی باشد. از تکرار اضافی پرهیز کن و محتوای چند پاسخ را در صورت نیاز با هم ترکیب کن تا بهترین نتیجه حاصل شود.
|
| 341 |
|
| 342 |
سوال:
|
| 343 |
{query}
|
| 344 |
|
| 345 |
پاسخها:
|
| 346 |
-
{
|
| 347 |
|
| 348 |
پاسخ نهایی حرفهای بازنویسیشده:
|
| 349 |
"""
|
|
@@ -358,7 +449,7 @@ if query:
|
|
| 358 |
break
|
| 359 |
|
| 360 |
if not found:
|
| 361 |
-
prompt = f"لطفاً بر اساس سوال زیر یک متن مرتبط و معنادار تولید
|
| 362 |
response = llm([
|
| 363 |
SystemMessage(content="You are a helpful assistant."),
|
| 364 |
HumanMessage(content=prompt)
|
|
|
|
| 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
|
| 14 |
+
import nltk
|
| 15 |
+
nltk.download('punkt')
|
| 16 |
+
|
| 17 |
st.markdown("""
|
| 18 |
<style>
|
| 19 |
/* استایل برای هدر */
|
|
|
|
| 110 |
margin-top: 10px;
|
| 111 |
}
|
| 112 |
.chat-message {
|
| 113 |
+
flex-wrap: wrap;
|
| 114 |
background-color: rgba(26, 43, 30, 0.95);
|
| 115 |
border: 2px solid #b8860b;
|
| 116 |
border-radius: 15px;
|
|
|
|
| 122 |
color: #d4d4d4;
|
| 123 |
font-weight: 600;
|
| 124 |
display: flex;
|
| 125 |
+
flex-wrap: wrap;
|
| 126 |
align-items: center;
|
| 127 |
gap: 15px;
|
| 128 |
}
|
|
|
|
| 217 |
font-weight: 400;
|
| 218 |
font-style: normal;
|
| 219 |
}
|
|
|
|
| 220 |
.header-text h1 {
|
| 221 |
font-family: 'IRANSans', Tahoma, sans-serif !important;
|
| 222 |
font-weight: 900 !important;
|
|
|
|
| 224 |
color: #b8860b !important;
|
| 225 |
margin: 0 !important;
|
| 226 |
}
|
|
|
|
| 227 |
.header-text .subtitle {
|
| 228 |
font-family: 'IRANSans', Tahoma, sans-serif !important;
|
| 229 |
font-weight: 600 !important;
|
|
|
|
| 245 |
llm = ChatOpenAI(
|
| 246 |
base_url="https://api.together.xyz/v1",
|
| 247 |
api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
|
| 248 |
+
model="deepseek-ai/DeepSeek-R1-Distill-Llama-70B-free",
|
| 249 |
+
max_tokens=1024
|
| 250 |
)
|
| 251 |
|
| 252 |
# ---------- پردازش فایلها با کش و موازی ----------
|
| 253 |
+
folder_path = 'C:/Users/ici/Downloads/Telegram Desktop/45/46'
|
| 254 |
normalizer = Normalizer()
|
| 255 |
sentence_tokenizer = SentenceTokenizer()
|
| 256 |
|
|
|
|
| 260 |
try:
|
| 261 |
full_path = os.path.join(path, filename)
|
| 262 |
doc = docx.Document(full_path)
|
| 263 |
+
text = "\n".join([para.text for para in doc.paragraphs]) # استخراج متن
|
| 264 |
+
normalized = normalizer.normalize(text)
|
| 265 |
+
sentences = normalized
|
| 266 |
+
return sentences
|
| 267 |
+
except Exception as e:
|
| 268 |
+
print(f"Error processing {filename}: {e}")
|
| 269 |
return []
|
|
|
|
|
|
|
|
|
|
| 270 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
| 271 |
results = executor.map(process_docx, [f for f in os.listdir(path) if f.endswith(".docx")])
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
return list(results)
|
| 276 |
+
|
| 277 |
|
| 278 |
all_sentences = load_and_process_documents(folder_path)
|
| 279 |
|
| 280 |
+
|
| 281 |
+
# all_sentences = load_and_process_documents(folder_path)
|
| 282 |
+
# st.markdown(all_sentences[2])
|
| 283 |
+
|
| 284 |
# ---------- ورودی جستجو ----------
|
| 285 |
st.markdown("""
|
| 286 |
<style>
|
|
|
|
| 295 |
}
|
| 296 |
</style>
|
| 297 |
""", unsafe_allow_html=True)
|
| 298 |
+
|
| 299 |
+
|
| 300 |
st.markdown("""
|
| 301 |
+
<style>
|
| 302 |
+
.st-af.st-ah.st-bb.st-ar.st-as.st-ax.st-ay.st-az.st-b0.st-b1.st-b2.st-bc.st-b7 {
|
| 303 |
+
background-color: #3a5338 !important;
|
| 304 |
+
color: #d4d4d4 !important;
|
| 305 |
+
border: 1px solid #c8a200 !important;
|
| 306 |
+
border-radius: 10px;
|
| 307 |
+
padding: 15px;
|
| 308 |
+
}
|
| 309 |
+
</style>
|
|
|
|
|
|
|
| 310 |
""", unsafe_allow_html=True)
|
| 311 |
+
|
| 312 |
st.markdown("""
|
| 313 |
<style>
|
| 314 |
/* استایل برای کلاس st-emotion-cache-yd4u6l e1togvvn1 */
|
|
|
|
| 335 |
}
|
| 336 |
</style>
|
| 337 |
""", unsafe_allow_html=True)
|
| 338 |
+
st.markdown("""
|
| 339 |
+
<style>
|
| 340 |
+
.stBottom.st-emotion-cache-1p2n2i4.eht7o1d2 {
|
| 341 |
+
background-color: #556B2F !important; /* زیتونی تیره */
|
| 342 |
+
color: white !important; /* رنگ متن سفید برای تضاد بهتر */
|
| 343 |
+
}
|
| 344 |
+
</style>
|
| 345 |
+
""", unsafe_allow_html=True)
|
| 346 |
+
st.markdown("""
|
| 347 |
+
<style>
|
| 348 |
+
/* تغییر رنگ متن placeholder به خاکستری */
|
| 349 |
+
textarea::placeholder {
|
| 350 |
+
color: #808080 !important; /* خاکستری */
|
| 351 |
+
opacity: 1 !important; /* برای اینکه مرورگرها بهش بیتوجه نباشن */
|
| 352 |
+
}
|
| 353 |
+
/* تغییر رنگ متن داخل چت اینپوت به خاکستری */
|
| 354 |
+
textarea {
|
| 355 |
+
color: #808080 !important; /* خاکستری */
|
| 356 |
+
border-radius: 10px !important;
|
| 357 |
+
padding: 10px !important;
|
| 358 |
+
}
|
| 359 |
+
</style>
|
| 360 |
+
""", unsafe_allow_html=True)
|
| 361 |
query = st.chat_input("چطور میتونم کمک کنم؟")
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
if query:
|
| 365 |
+
st.markdown(f'<div class="chat-message">{query}</div>', unsafe_allow_html=True)
|
| 366 |
+
|
| 367 |
+
think = st.markdown("""
|
| 368 |
+
<div class="thinking-message">
|
| 369 |
+
<p>در حال فکر کردن...</p>
|
| 370 |
+
<div class="spinner"></div>
|
| 371 |
+
</div>
|
| 372 |
+
""", unsafe_allow_html=True)
|
| 373 |
+
time.sleep(4)
|
| 374 |
+
think.empty()
|
| 375 |
+
else:
|
| 376 |
+
st.markdown("")
|
| 377 |
+
|
| 378 |
+
# استایلها برای چرخش و پیام در حال فکر کردن
|
| 379 |
+
st.markdown("""
|
| 380 |
+
<style>
|
| 381 |
+
.thinking-message {
|
| 382 |
+
display: flex;
|
| 383 |
+
align-items: center;
|
| 384 |
+
font-size: 18px;
|
| 385 |
+
color: #333;
|
| 386 |
+
}
|
| 387 |
+
|
| 388 |
+
.thinking-message p {
|
| 389 |
+
margin-right: 10px;
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
.spinner {
|
| 393 |
+
border: 4px solid #f3f3f3;
|
| 394 |
+
border-top: 4px solid #4b6d3d; /* رنگ سبز تیره */
|
| 395 |
+
border-radius: 50%;
|
| 396 |
+
width: 20px;
|
| 397 |
+
height: 20px;
|
| 398 |
+
animation: spin 2s linear infinite;
|
| 399 |
+
}
|
| 400 |
+
|
| 401 |
+
@keyframes spin {
|
| 402 |
+
0% { transform: rotate(0deg); }
|
| 403 |
+
100% { transform: rotate(360deg); }
|
| 404 |
+
}
|
| 405 |
+
</style>
|
| 406 |
+
""", unsafe_allow_html=True)
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
sentEmbedding = SentEmbedding()
|
| 411 |
+
sentEmbedding.load_model("C:/Users/ici/Downloads/sent2vec/sent2vec-naab.model")
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
top_k = 5
|
| 416 |
+
threshold = 0.8
|
| 417 |
+
|
| 418 |
+
original_sentences = all_sentences.copy()
|
| 419 |
+
all_vectors = [sentEmbedding.get_sentence_vector(sentence) for sentence in original_sentences]
|
| 420 |
+
|
| 421 |
if query:
|
| 422 |
found = False
|
| 423 |
+
threshold = 85
|
| 424 |
|
| 425 |
for idx, sentence in enumerate(all_sentences):
|
| 426 |
similarity = fuzz.partial_ratio(query, sentence)
|
| 427 |
if similarity >= threshold:
|
| 428 |
+
st.markdown(sentence)
|
|
|
|
|
|
|
|
|
|
| 429 |
|
|
|
|
| 430 |
prompt = f"""
|
| 431 |
+
تعدادی پاسخ برای سوال زیر تولید شده است. لطفاً ابتدا این پاسخها را بررسی کن، سپس با در نظر گرفتن محتوای سوال و لحن آن، یک پاسخ نهایی حرفهای، دقیق و روان ارائه کن که هم به سوال پاسخ دهد و هم از نظر نگارشی و ساختاری در سطح بالایی باشد. از تکرار اضافی پرهیز کن و محتوای چند پاسخ را در صورت نیاز با هم ترکیب کن و به هیچ عنوان از جملاتی جز فارسی استفاده نکن تا بهترین نتیجه حاصل شود.
|
| 432 |
|
| 433 |
سوال:
|
| 434 |
{query}
|
| 435 |
|
| 436 |
پاسخها:
|
| 437 |
+
{sentence}
|
| 438 |
|
| 439 |
پاسخ نهایی حرفهای بازنویسیشده:
|
| 440 |
"""
|
|
|
|
| 449 |
break
|
| 450 |
|
| 451 |
if not found:
|
| 452 |
+
prompt = f" لطفاً بر اساس سوال زیر یک متن مرتبط و معنادار تولید کن و از خودت ننویس و جملات معتبر باشن و از زبانی جز فارسی استفاده نکن:\n\nسوال: {query}"
|
| 453 |
response = llm([
|
| 454 |
SystemMessage(content="You are a helpful assistant."),
|
| 455 |
HumanMessage(content=prompt)
|