Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,139 @@
|
|
| 1 |
-
import
|
| 2 |
import time
|
| 3 |
-
|
| 4 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
from langchain_together import TogetherEmbeddings
|
| 6 |
-
|
| 7 |
from langchain.chat_models import ChatOpenAI
|
|
|
|
|
|
|
|
|
|
| 8 |
from langchain.chains import RetrievalQA
|
|
|
|
| 9 |
from langchain.indexes import VectorstoreIndexCreator
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@st.cache_resource
|
| 13 |
-
def
|
| 14 |
-
with st.spinner(
|
| 15 |
-
progress_bar = st.progress(0, text="در حال بارگذاری فایل PDF...")
|
| 16 |
-
|
| 17 |
pdf_loader = PyPDFLoader('test1.pdf')
|
| 18 |
-
pages = pdf_loader.load()
|
| 19 |
-
progress_bar.progress(30, text="صفحات PDF بارگذاری شد. در حال ایجاد مدل برداری...")
|
| 20 |
|
| 21 |
embeddings = TogetherEmbeddings(
|
| 22 |
api_key="0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979"
|
| 23 |
)
|
| 24 |
-
progress_bar.progress(60, text="مدل Embedding ساخته شد. در حال ایجاد ایندکس...")
|
| 25 |
|
| 26 |
-
index = VectorstoreIndexCreator(
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
progress_bar.progress(100, text="بارگذاری کامل شد! ✅")
|
| 32 |
-
return index
|
| 33 |
llm = ChatOpenAI(
|
| 34 |
base_url="https://api.together.xyz/v1",
|
| 35 |
api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
|
| 36 |
-
model="meta-llama/Llama-3-70B-Instruct-Turbo-Free"
|
| 37 |
)
|
| 38 |
|
| 39 |
-
index = load_chunks_and_embeddings()
|
| 40 |
-
|
| 41 |
chain = RetrievalQA.from_chain_type(
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
| 46 |
|
| 47 |
-
# --- 💬 چت بات
|
| 48 |
if 'messages' not in st.session_state:
|
| 49 |
st.session_state.messages = []
|
| 50 |
|
| 51 |
if 'pending_prompt' not in st.session_state:
|
| 52 |
st.session_state.pending_prompt = None
|
| 53 |
|
| 54 |
-
st.title("📄🤖 دستیار PDF شما")
|
| 55 |
-
|
| 56 |
-
# نمایش تاریخچه گفتگو
|
| 57 |
for msg in st.session_state.messages:
|
| 58 |
with st.chat_message(msg['role']):
|
| 59 |
st.markdown(f"🗨️ {msg['content']}", unsafe_allow_html=True)
|
| 60 |
|
| 61 |
-
prompt = st.chat_input("
|
| 62 |
|
| 63 |
if prompt:
|
| 64 |
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
|
@@ -70,8 +145,7 @@ if st.session_state.pending_prompt:
|
|
| 70 |
thinking = st.empty()
|
| 71 |
thinking.markdown("🤖 در حال فکر کردن...")
|
| 72 |
|
| 73 |
-
|
| 74 |
-
response = chain.run(f'فقط به زبان فارسی جواب بده. سوال: {st.session_state.pending_prompt}')
|
| 75 |
answer = response.split("Helpful Answer:")[-1].strip()
|
| 76 |
if not answer:
|
| 77 |
answer = "متأسفم، اطلاعات دقیقی در این مورد ندارم."
|
|
|
|
| 1 |
+
import os
|
| 2 |
import time
|
| 3 |
+
|
|
|
|
| 4 |
from langchain_together import TogetherEmbeddings
|
| 5 |
+
import streamlit as st
|
| 6 |
from langchain.chat_models import ChatOpenAI
|
| 7 |
+
from langchain.document_loaders import PyPDFLoader
|
| 8 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 9 |
+
from langchain.schema import Document
|
| 10 |
from langchain.chains import RetrievalQA
|
| 11 |
+
from langchain.vectorstores import FAISS
|
| 12 |
from langchain.indexes import VectorstoreIndexCreator
|
| 13 |
|
| 14 |
+
# ----------------- تنظیمات صفحه -----------------
|
| 15 |
+
st.set_page_config(page_title="چت بات توانا", page_icon="🪖", layout="wide")
|
| 16 |
+
|
| 17 |
+
st.markdown("""
|
| 18 |
+
<style>
|
| 19 |
+
@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700&display=swap');
|
| 20 |
+
html, body, [class*="css"] {
|
| 21 |
+
font-family: 'Vazirmatn', Tahoma, sans-serif;
|
| 22 |
+
direction: rtl;
|
| 23 |
+
text-align: right;
|
| 24 |
+
}
|
| 25 |
+
.stApp {
|
| 26 |
+
background: url("./military_bg.jpeg") no-repeat center center fixed;
|
| 27 |
+
background-size: cover;
|
| 28 |
+
backdrop-filter: blur(2px);
|
| 29 |
+
}
|
| 30 |
+
.stChatMessage {
|
| 31 |
+
background-color: rgba(255,255,255,0.8);
|
| 32 |
+
border: 1px solid #4e8a3e;
|
| 33 |
+
border-radius: 12px;
|
| 34 |
+
padding: 16px;
|
| 35 |
+
margin-bottom: 15px;
|
| 36 |
+
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
|
| 37 |
+
animation: fadeIn 0.4s ease-in-out;
|
| 38 |
+
}
|
| 39 |
+
.stTextInput > div > input, .stTextArea textarea {
|
| 40 |
+
background-color: rgba(255,255,255,0.9) !important;
|
| 41 |
+
border-radius: 8px !important;
|
| 42 |
+
direction: rtl;
|
| 43 |
+
text-align: right;
|
| 44 |
+
font-family: 'Vazirmatn', Tahoma;
|
| 45 |
+
}
|
| 46 |
+
.stButton>button {
|
| 47 |
+
background-color: #4e8a3e !important;
|
| 48 |
+
color: white !important;
|
| 49 |
+
font-weight: bold;
|
| 50 |
+
border-radius: 10px;
|
| 51 |
+
padding: 8px 20px;
|
| 52 |
+
transition: 0.3s;
|
| 53 |
+
}
|
| 54 |
+
.stButton>button:hover {
|
| 55 |
+
background-color: #3c6d30 !important;
|
| 56 |
+
}
|
| 57 |
+
.header-text {
|
| 58 |
+
text-align: center;
|
| 59 |
+
margin-top: 20px;
|
| 60 |
+
margin-bottom: 40px;
|
| 61 |
+
background-color: rgba(255, 255, 255, 0.75);
|
| 62 |
+
padding: 20px;
|
| 63 |
+
border-radius: 20px;
|
| 64 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
|
| 65 |
+
}
|
| 66 |
+
.header-text h1 {
|
| 67 |
+
font-size: 42px;
|
| 68 |
+
color: #2c3e50;
|
| 69 |
+
margin: 0;
|
| 70 |
+
font-weight: bold;
|
| 71 |
+
}
|
| 72 |
+
.subtitle {
|
| 73 |
+
font-size: 18px;
|
| 74 |
+
color: #34495e;
|
| 75 |
+
margin-top: 8px;
|
| 76 |
+
}
|
| 77 |
+
@keyframes fadeIn {
|
| 78 |
+
from { opacity: 0; transform: translateY(10px); }
|
| 79 |
+
to { opacity: 1; transform: translateY(0); }
|
| 80 |
+
}
|
| 81 |
+
</style>
|
| 82 |
+
""", unsafe_allow_html=True)
|
| 83 |
+
|
| 84 |
+
col1, col2, col3 = st.columns([1, 0.2, 1])
|
| 85 |
+
with col2:
|
| 86 |
+
st.image("army.png", width=240)
|
| 87 |
+
|
| 88 |
+
st.markdown("""
|
| 89 |
+
<div class="header-text">
|
| 90 |
+
<h1>چت بات توانا</h1>
|
| 91 |
+
<div class="subtitle">دستیار هوشمند</div>
|
| 92 |
+
</div>
|
| 93 |
+
""", unsafe_allow_html=True)
|
| 94 |
+
|
| 95 |
+
# ----------------- لود PDF و ساخت ایندکس -----------------
|
| 96 |
@st.cache_resource
|
| 97 |
+
def get_pdf_index():
|
| 98 |
+
with st.spinner('📄 در حال پردازش فایل PDF...'):
|
|
|
|
|
|
|
| 99 |
pdf_loader = PyPDFLoader('test1.pdf')
|
|
|
|
|
|
|
| 100 |
|
| 101 |
embeddings = TogetherEmbeddings(
|
| 102 |
api_key="0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979"
|
| 103 |
)
|
|
|
|
| 104 |
|
| 105 |
+
index = VectorstoreIndexCreator(embedding=embeddings, text_splitter=RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=0)).from_loaders([pdf_loader])
|
| 106 |
+
|
| 107 |
+
st.write(f"تعداد بخشهای پردازششده: {len(index.vectorstore)}")
|
| 108 |
+
return index
|
| 109 |
+
|
| 110 |
+
# ----------------- بارگذاری دیتا -----------------
|
| 111 |
+
index = get_pdf_index()
|
| 112 |
|
|
|
|
|
|
|
| 113 |
llm = ChatOpenAI(
|
| 114 |
base_url="https://api.together.xyz/v1",
|
| 115 |
api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979',
|
| 116 |
+
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free"
|
| 117 |
)
|
| 118 |
|
|
|
|
|
|
|
| 119 |
chain = RetrievalQA.from_chain_type(
|
| 120 |
+
llm=llm,
|
| 121 |
+
chain_type='stuff',
|
| 122 |
+
retriever=index.vectorstore.as_retriever(),
|
| 123 |
+
input_key='question'
|
| 124 |
+
)
|
| 125 |
|
|
|
|
| 126 |
if 'messages' not in st.session_state:
|
| 127 |
st.session_state.messages = []
|
| 128 |
|
| 129 |
if 'pending_prompt' not in st.session_state:
|
| 130 |
st.session_state.pending_prompt = None
|
| 131 |
|
|
|
|
|
|
|
|
|
|
| 132 |
for msg in st.session_state.messages:
|
| 133 |
with st.chat_message(msg['role']):
|
| 134 |
st.markdown(f"🗨️ {msg['content']}", unsafe_allow_html=True)
|
| 135 |
|
| 136 |
+
prompt = st.chat_input("چطور میتونم کمک کنم؟")
|
| 137 |
|
| 138 |
if prompt:
|
| 139 |
st.session_state.messages.append({'role': 'user', 'content': prompt})
|
|
|
|
| 145 |
thinking = st.empty()
|
| 146 |
thinking.markdown("🤖 در حال فکر کردن...")
|
| 147 |
|
| 148 |
+
response = chain.run(f'پاسخ را فقط به زبان فارسی جواب بده. سوال: {st.session_state.pending_prompt}')
|
|
|
|
| 149 |
answer = response.split("Helpful Answer:")[-1].strip()
|
| 150 |
if not answer:
|
| 151 |
answer = "متأسفم، اطلاعات دقیقی در این مورد ندارم."
|