Update pdf-rag-bot/app.py
Browse files- pdf-rag-bot/app.py +72 -38
pdf-rag-bot/app.py
CHANGED
|
@@ -11,57 +11,91 @@ from langchain_community.embeddings import OpenAIEmbeddings
|
|
| 11 |
from langchain.chains import ConversationalRetrievalChain
|
| 12 |
from langchain.memory import ConversationBufferMemory
|
| 13 |
|
|
|
|
| 14 |
load_dotenv()
|
| 15 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 16 |
|
| 17 |
-
st.set_page_config(page_title="PDF
|
| 18 |
-
st.
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 27 |
-
tmp.write(uploaded_file.read())
|
| 28 |
-
tmp_path = tmp.name
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
llm=
|
| 44 |
-
retriever=vectordb.as_retriever(search_kwargs={"k": 3}),
|
| 45 |
-
memory=memory
|
| 46 |
-
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
st.success("✅ PDF başarıyla işlendi!")
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
with st.
|
| 59 |
-
|
| 60 |
-
st.
|
| 61 |
-
st.session_state.chat_history.append(("🤖 Bot", response))
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
| 11 |
from langchain.chains import ConversationalRetrievalChain
|
| 12 |
from langchain.memory import ConversationBufferMemory
|
| 13 |
|
| 14 |
+
# Ortam değişkenlerini yükle
|
| 15 |
load_dotenv()
|
| 16 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 17 |
|
| 18 |
+
st.set_page_config(page_title="ChatGPT PDF Botu", page_icon="💬", layout="wide")
|
| 19 |
+
st.markdown(
|
| 20 |
+
"""
|
| 21 |
+
<h2 style='text-align: center;'>📄 Customer Service Bot</h2>
|
| 22 |
+
<p style='text-align: center; color: #888;'>PDF içeriğinden ChatGPT tarzı sohbet ile bilgi alın.</p>
|
| 23 |
+
""",
|
| 24 |
+
unsafe_allow_html=True
|
| 25 |
+
)
|
| 26 |
|
| 27 |
+
# Sayfa ikiye böl: Solda PDF yükleme, sağda sohbet
|
| 28 |
+
col1, col2 = st.columns([1, 2])
|
| 29 |
|
| 30 |
+
with col1:
|
| 31 |
+
st.subheader("📄 PDF Yükle")
|
| 32 |
+
uploaded_file = st.file_uploader("PDF dosyanızı yükleyin", type="pdf", key="pdf_uploader")
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
if uploaded_file is not None:
|
| 35 |
+
if "last_uploaded_name" not in st.session_state or uploaded_file.name != st.session_state.last_uploaded_name:
|
| 36 |
+
with st.spinner("PDF işleniyor..."):
|
| 37 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
|
| 38 |
+
tmp.write(uploaded_file.read())
|
| 39 |
+
tmp_path = tmp.name
|
| 40 |
|
| 41 |
+
loader = PyPDFLoader(tmp_path)
|
| 42 |
+
documents = loader.load()
|
| 43 |
|
| 44 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 45 |
+
docs = splitter.split_documents(documents)
|
| 46 |
|
| 47 |
+
embedding = OpenAIEmbeddings(model="text-embedding-3-large")
|
| 48 |
+
vectordb = FAISS.from_documents(docs, embedding)
|
| 49 |
|
| 50 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 51 |
+
llm = ChatOpenAI(model_name="gpt-4", temperature=0)
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
qa_chain = ConversationalRetrievalChain.from_llm(
|
| 54 |
+
llm=llm,
|
| 55 |
+
retriever=vectordb.as_retriever(search_kwargs={"k": 3}),
|
| 56 |
+
memory=memory
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
st.session_state.qa_chain = qa_chain
|
| 60 |
+
st.session_state.chat_history = []
|
| 61 |
+
st.session_state.last_uploaded_name = uploaded_file.name
|
| 62 |
|
| 63 |
st.success("✅ PDF başarıyla işlendi!")
|
| 64 |
|
| 65 |
+
with col2:
|
| 66 |
+
st.subheader("💬 Sohbet Alanı")
|
| 67 |
+
|
| 68 |
+
if "qa_chain" not in st.session_state:
|
| 69 |
+
st.info("Lütfen sol taraftan bir PDF yükleyin.")
|
| 70 |
+
else:
|
| 71 |
+
if "chat_history" not in st.session_state:
|
| 72 |
+
st.session_state.chat_history = []
|
| 73 |
+
|
| 74 |
+
# Sohbeti chat balonları olarak göster
|
| 75 |
+
chat_placeholder = st.container()
|
| 76 |
+
with chat_placeholder:
|
| 77 |
+
for sender, message in st.session_state.chat_history:
|
| 78 |
+
align = "flex-end" if sender == "🧑💼 Siz" else "flex-start"
|
| 79 |
+
color = "#DCF8C6" if sender == "🧑💼 Siz" else "#F1F0F0"
|
| 80 |
+
st.markdown(
|
| 81 |
+
f"""
|
| 82 |
+
<div style='display: flex; justify-content: {align};'>
|
| 83 |
+
<div style='background: {color}; border-radius: 12px; padding: 8px 14px; margin: 6px 0; max-width: 70%;'>
|
| 84 |
+
<b>{sender}:</b> {message}
|
| 85 |
+
</div>
|
| 86 |
+
</div>
|
| 87 |
+
""",
|
| 88 |
+
unsafe_allow_html=True
|
| 89 |
+
)
|
| 90 |
|
| 91 |
+
# Sohbet giriş kutusu aşağıda sabit
|
| 92 |
+
with st.form(key="user_input_form", clear_on_submit=True):
|
| 93 |
+
user_input = st.text_input("Soru sorun veya yazışmaya devam edin...", key="input_field")
|
| 94 |
+
submit = st.form_submit_button("Gönder")
|
|
|
|
| 95 |
|
| 96 |
+
if submit and user_input:
|
| 97 |
+
with st.spinner("Yanıtlanıyor..."):
|
| 98 |
+
response = st.session_state.qa_chain.run(user_input)
|
| 99 |
+
st.session_state.chat_history.append(("🧑💼 Siz", user_input))
|
| 100 |
+
st.session_state.chat_history.append(("🤖 Bot", response))
|
| 101 |
+
st.rerun()
|