Update app.py
Browse files
app.py
CHANGED
@@ -3,15 +3,191 @@ from hazm import Normalizer, SentenceTokenizer
|
|
3 |
import os
|
4 |
import docx
|
5 |
from langchain.chat_models import ChatOpenAI
|
|
|
|
|
6 |
|
7 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
llm = ChatOpenAI(
|
9 |
base_url="https://api.together.xyz/v1",
|
10 |
api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
|
11 |
-
model="
|
12 |
)
|
13 |
|
14 |
-
|
|
|
15 |
texts = []
|
16 |
|
17 |
for filename in os.listdir(folder_path):
|
@@ -31,34 +207,37 @@ for text in texts:
|
|
31 |
sentences = sentence_tokenizer.tokenize(normalized)
|
32 |
all_sentences.extend(sentences)
|
33 |
|
|
|
34 |
query = st.text_input("🔎 کلمه یا عبارت موردنظر خود را وارد کنید:")
|
35 |
|
36 |
-
# ✅ نمایش جمله و ۵ جمله بعدی + بازنویسی با LLM
|
37 |
if query:
|
38 |
found = False
|
39 |
-
|
40 |
-
if query in sentence:
|
41 |
|
|
|
|
|
|
|
42 |
next_sentences = []
|
43 |
for i in range(1, 6):
|
44 |
if idx + i < len(all_sentences):
|
45 |
next_sentences.append(all_sentences[idx + i])
|
46 |
|
47 |
-
# ↪️ آمادهسازی پرامپت برای ارسال به مدل
|
48 |
total_text = sentence + " " + " ".join(next_sentences)
|
49 |
prompt = f"پاسخی که باید بازنویسی شود:\n{total_text}\n\nلطفاً این پاسخ را با در نظر گرفتن محتوای سوال زیر و لحن آن بازنویسی کن:\n\nسوال: {query}"
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
rewritten =
|
56 |
-
st.markdown("🎨 **بازنویسی شده با LLM:**")
|
57 |
-
st.write(rewritten)
|
58 |
-
st.warning("پاسخ دریافتی معتبر نیست.")
|
59 |
-
|
60 |
found = True
|
61 |
break
|
62 |
|
63 |
if not found:
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
import docx
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
+
from langchain.schema import SystemMessage, HumanMessage
|
7 |
+
from rapidfuzz import fuzz
|
8 |
|
9 |
+
# ---------- استایل ----------
|
10 |
+
st.markdown("""
|
11 |
+
<style>
|
12 |
+
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700&display=swap');
|
13 |
+
|
14 |
+
html, body, [class*="css"] {
|
15 |
+
font-family: 'Vazirmatn', Tahoma, sans-serif;
|
16 |
+
direction: rtl;
|
17 |
+
text-align: right;
|
18 |
+
}
|
19 |
+
|
20 |
+
.stApp {
|
21 |
+
background: linear-gradient(to left, #4b5e40, #2e3b2e);
|
22 |
+
color: #ffffff;
|
23 |
+
}
|
24 |
+
|
25 |
+
[data-testid="stSidebar"] {
|
26 |
+
width: 260px !important;
|
27 |
+
background-color: #1a2b1e;
|
28 |
+
border: none !important;
|
29 |
+
padding-top: 20px;
|
30 |
+
}
|
31 |
+
|
32 |
+
.menu-item {
|
33 |
+
display: flex;
|
34 |
+
align-items: center;
|
35 |
+
gap: 12px;
|
36 |
+
padding: 12px 20px;
|
37 |
+
font-size: 16px;
|
38 |
+
color: #d4d4d4;
|
39 |
+
cursor: pointer;
|
40 |
+
transition: background-color 0.3s ease;
|
41 |
+
}
|
42 |
+
|
43 |
+
.menu-item:hover {
|
44 |
+
background-color: #2e3b2e;
|
45 |
+
color: #b8860b;
|
46 |
+
}
|
47 |
+
|
48 |
+
.menu-item img {
|
49 |
+
width: 24px;
|
50 |
+
height: 24px;
|
51 |
+
}
|
52 |
+
|
53 |
+
.stButton>button {
|
54 |
+
background-color: #b8860b !important;
|
55 |
+
color: #1a2b1e !important;
|
56 |
+
font-family: 'Vazirmatn', Tahoma;
|
57 |
+
font-weight: 700;
|
58 |
+
border-radius: 10px;
|
59 |
+
padding: 12px 24px;
|
60 |
+
border: none;
|
61 |
+
transition: all 0.3s ease;
|
62 |
+
font-size: 16px;
|
63 |
+
width: 100%;
|
64 |
+
margin: 10px 0;
|
65 |
+
}
|
66 |
+
|
67 |
+
.stButton>button:hover {
|
68 |
+
background-color: #8b6508 !important;
|
69 |
+
transform: translateY(-2px);
|
70 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
|
71 |
+
}
|
72 |
+
|
73 |
+
.header-text {
|
74 |
+
text-align: center;
|
75 |
+
margin: 20px 0;
|
76 |
+
background-color: rgba(26, 43, 30, 0.9);
|
77 |
+
padding: 25px;
|
78 |
+
border-radius: 15px;
|
79 |
+
box-shadow: 0 6px 12px rgba(0,0,0,0.4);
|
80 |
+
}
|
81 |
+
|
82 |
+
.header-text h1 {
|
83 |
+
font-size: 42px;
|
84 |
+
color: #b8860b;
|
85 |
+
margin: 0;
|
86 |
+
font-weight: 700;
|
87 |
+
}
|
88 |
+
|
89 |
+
.subtitle {
|
90 |
+
font-size: 18px;
|
91 |
+
color: #d4d4d4;
|
92 |
+
margin-top: 10px;
|
93 |
+
}
|
94 |
+
|
95 |
+
.chat-message {
|
96 |
+
background-color: rgba(26, 43, 30, 0.95);
|
97 |
+
border: 2px solid #b8860b;
|
98 |
+
border-radius: 15px;
|
99 |
+
padding: 20px;
|
100 |
+
margin: 15px 0;
|
101 |
+
box-shadow: 0 6px 12px rgba(0,0,0,0.3);
|
102 |
+
animation: fadeIn 0.6s ease;
|
103 |
+
font-size: 18px;
|
104 |
+
color: #d4d4d4;
|
105 |
+
display: flex;
|
106 |
+
align-items: center;
|
107 |
+
gap: 15px;
|
108 |
+
}
|
109 |
+
|
110 |
+
@keyframes fadeIn {
|
111 |
+
from { opacity: 0; transform: translateY(10px); }
|
112 |
+
to { opacity: 1; transform: translateY(0); }
|
113 |
+
}
|
114 |
+
|
115 |
+
.stTextInput>div>input, .stTextArea textarea {
|
116 |
+
background-color: rgba(26, 43, 30, 0.95) !important;
|
117 |
+
border-radius: 10px !important;
|
118 |
+
border: 1px solid #b8860b !important;
|
119 |
+
padding: 12px !important;
|
120 |
+
font-family: 'Vazirmatn', Tahoma;
|
121 |
+
font-size: 16px;
|
122 |
+
color: #d4d4d4 !important;
|
123 |
+
}
|
124 |
+
|
125 |
+
hr {
|
126 |
+
border: 1px solid #b8860b;
|
127 |
+
margin: 15px 0;
|
128 |
+
}
|
129 |
+
|
130 |
+
[data-testid="stSidebar"] > div {
|
131 |
+
border: none !important;
|
132 |
+
}
|
133 |
+
</style>
|
134 |
+
""", unsafe_allow_html=True)
|
135 |
+
|
136 |
+
# ---------- احراز هویت ----------
|
137 |
+
if "authenticated" not in st.session_state:
|
138 |
+
st.session_state.authenticated = False
|
139 |
+
|
140 |
+
if not st.session_state.authenticated:
|
141 |
+
st.markdown("<h3 style='text-align: center; color: #b8860b;'>ورود به رزمیار ارتش</h3>", unsafe_allow_html=True)
|
142 |
+
username = st.text_input("نام کاربری:", placeholder="شناسه نظامی خود را وارد کنید")
|
143 |
+
password = st.text_input("رمز عبور:", type="password", placeholder="رمز عبور نظامی")
|
144 |
+
if st.button("ورود"):
|
145 |
+
if username == "admin" and password == "123":
|
146 |
+
st.session_state.authenticated = True
|
147 |
+
st.rerun()
|
148 |
+
else:
|
149 |
+
st.error("نام کاربری یا رمز عبور اشتباه است.")
|
150 |
+
st.stop()
|
151 |
+
|
152 |
+
# ---------- سایدبار ----------
|
153 |
+
with st.sidebar:
|
154 |
+
st.image("log.png", use_container_width=True)
|
155 |
+
menu_items = [
|
156 |
+
("گزارش عملیاتی", "https://cdn-icons-png.flaticon.com/512/3596/3596165.png"),
|
157 |
+
("تاریخچه ماموریتها", "https://cdn-icons-png.flaticon.com/512/709/709496.png"),
|
158 |
+
("تحلیل دادههای نظامی", "https://cdn-icons-png.flaticon.com/512/1828/1828932.png"),
|
159 |
+
("مدیریت منابع", "https://cdn-icons-png.flaticon.com/512/681/681494.png"),
|
160 |
+
("دستیار فرماندهی", "https://cdn-icons-png.flaticon.com/512/3601/3601646.png"),
|
161 |
+
("تنظیمات امنیتی", "https://cdn-icons-png.flaticon.com/512/2099/2099058.png"),
|
162 |
+
("پشتیبانی فنی", "https://cdn-icons-png.flaticon.com/512/597/597177.png"),
|
163 |
+
]
|
164 |
+
for idx, (text, icon) in enumerate(menu_items):
|
165 |
+
st.markdown(f"""
|
166 |
+
<div class="menu-item">
|
167 |
+
<img src="{icon}" />
|
168 |
+
{text}
|
169 |
+
</div>
|
170 |
+
""", unsafe_allow_html=True)
|
171 |
+
if idx in [1, 3, 5]:
|
172 |
+
st.markdown("<hr/>", unsafe_allow_html=True)
|
173 |
+
|
174 |
+
# ---------- سربرگ ----------
|
175 |
+
st.markdown("""
|
176 |
+
<div class="header-text">
|
177 |
+
<h1>رزمیار ارتش</h1>
|
178 |
+
<div class="subtitle">دستیار هوشمند ارتش جمهوری اسلامی ایران</div>
|
179 |
+
</div>
|
180 |
+
""", unsafe_allow_html=True)
|
181 |
+
|
182 |
+
# ---------- مدل زبانی ----------
|
183 |
llm = ChatOpenAI(
|
184 |
base_url="https://api.together.xyz/v1",
|
185 |
api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
|
186 |
+
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
|
187 |
)
|
188 |
|
189 |
+
# ---------- پردازش فایلها ----------
|
190 |
+
folder_path = 'C:/Users/ici/Downloads/Telegram Desktop/45/46'
|
191 |
texts = []
|
192 |
|
193 |
for filename in os.listdir(folder_path):
|
|
|
207 |
sentences = sentence_tokenizer.tokenize(normalized)
|
208 |
all_sentences.extend(sentences)
|
209 |
|
210 |
+
# ---------- ورودی جستجو ----------
|
211 |
query = st.text_input("🔎 کلمه یا عبارت موردنظر خود را وارد کنید:")
|
212 |
|
|
|
213 |
if query:
|
214 |
found = False
|
215 |
+
threshold = 80
|
|
|
216 |
|
217 |
+
for idx, sentence in enumerate(all_sentences):
|
218 |
+
similarity = fuzz.partial_ratio(query, sentence)
|
219 |
+
if similarity >= threshold:
|
220 |
next_sentences = []
|
221 |
for i in range(1, 6):
|
222 |
if idx + i < len(all_sentences):
|
223 |
next_sentences.append(all_sentences[idx + i])
|
224 |
|
|
|
225 |
total_text = sentence + " " + " ".join(next_sentences)
|
226 |
prompt = f"پاسخی که باید بازنویسی شود:\n{total_text}\n\nلطفاً این پاسخ را با در نظر گرفتن محتوای سوال زیر و لحن آن بازنویسی کن:\n\nسوال: {query}"
|
227 |
+
response = llm([
|
228 |
+
SystemMessage(content="You are a helpful assistant."),
|
229 |
+
HumanMessage(content=prompt)
|
230 |
+
])
|
231 |
+
rewritten = response.content.strip()
|
232 |
+
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
233 |
found = True
|
234 |
break
|
235 |
|
236 |
if not found:
|
237 |
+
prompt = f"لطفاً بر اساس سوال زیر یک متن مرتبط و معنادار تولید کن:\n\nسوال: {query}"
|
238 |
+
response = llm([
|
239 |
+
SystemMessage(content="You are a helpful assistant."),
|
240 |
+
HumanMessage(content=prompt)
|
241 |
+
])
|
242 |
+
rewritten = response.content.strip()
|
243 |
+
st.markdown(f'<div class="chat-message">{rewritten}</div>', unsafe_allow_html=True)
|