|
import streamlit as st |
|
import time |
|
import numpy as np |
|
from langchain.document_loaders import PyPDFLoader |
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
from langchain_together import TogetherEmbeddings |
|
from langchain.chat_models import ChatOpenAI |
|
from sklearn.metrics.pairwise import cosine_similarity |
|
|
|
|
|
|
|
@st.cache_resource |
|
def load_chunks_and_embeddings(): |
|
with st.spinner('📄 در حال پردازش PDF و ساخت امبدینگها...'): |
|
loader = PyPDFLoader('test1.pdf') |
|
pages = loader.load() |
|
|
|
splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=0) |
|
chunks = splitter.split_documents(pages) |
|
|
|
embeddings_model = TogetherEmbeddings( |
|
api_key="0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979" |
|
) |
|
|
|
chunk_texts = [chunk.page_content for chunk in chunks] |
|
chunk_embeddings = embeddings_model.embed_documents(chunk_texts) |
|
|
|
st.success(f"✅ تعداد {len(chunk_texts)} چانک ساخته شد.") |
|
return chunk_texts, chunk_embeddings, embeddings_model |
|
|
|
chunk_texts, chunk_embeddings, embeddings_model = load_chunks_and_embeddings() |
|
|
|
|
|
|
|
llm = ChatOpenAI( |
|
base_url="https://api.together.xyz/v1", |
|
api_key='0291f33aee03412a47fa5d8e562e515182dcc5d9aac5a7fb5eefdd1759005979', |
|
model="meta-llama/Llama-3.3-70B-Instruct-Turbo-Free" |
|
) |
|
|
|
|
|
|
|
def answer_from_pdf(question): |
|
|
|
question_embedding = embeddings_model.embed_query(question) |
|
|
|
|
|
similarities = cosine_similarity( |
|
[question_embedding], |
|
chunk_embeddings |
|
) |
|
|
|
|
|
best_idx = np.argmax(similarities) |
|
best_chunk = chunk_texts[best_idx] |
|
|
|
|
|
prompt = f"""بر اساس متن زیر فقط به زبان فارسی پاسخ بده: |
|
|
|
متن: |
|
{best_chunk} |
|
|
|
سوال: |
|
{question} |
|
|
|
پاسخ:""" |
|
|
|
response = llm.invoke(prompt) |
|
return response.content |
|
|
|
|
|
|
|
st.title('📚 چت با PDF') |
|
|
|
if 'messages' not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
if 'pending_prompt' not in st.session_state: |
|
st.session_state.pending_prompt = None |
|
|
|
|
|
for msg in st.session_state.messages: |
|
with st.chat_message(msg['role']): |
|
st.markdown(f"🗨️ {msg['content']}", unsafe_allow_html=True) |
|
|
|
|
|
prompt = st.chat_input("سوال خود را وارد کنید...") |
|
|
|
if prompt: |
|
st.session_state.messages.append({'role': 'user', 'content': prompt}) |
|
st.session_state.pending_prompt = prompt |
|
st.rerun() |
|
|
|
|
|
if st.session_state.pending_prompt: |
|
with st.chat_message('ai'): |
|
thinking = st.empty() |
|
thinking.markdown("🤖 در حال پردازش...") |
|
|
|
|
|
response = answer_from_pdf(st.session_state.pending_prompt) |
|
answer = response.strip() |
|
if not answer: |
|
answer = "متاسفم، اطلاعات دقیقی در این مورد ندارم." |
|
|
|
thinking.empty() |
|
full_response = "" |
|
placeholder = st.empty() |
|
|
|
|
|
for word in answer.split(): |
|
full_response += word + " " |
|
placeholder.markdown(full_response + "▌") |
|
time.sleep(0.03) |
|
|
|
placeholder.markdown(full_response) |
|
st.session_state.messages.append({'role': 'ai', 'content': full_response}) |
|
st.session_state.pending_prompt = None |
|
|