Spaces:
Running
Running
Upload app (1).py
Browse files- app (1).py +58 -0
app (1).py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Local imports
|
| 7 |
+
from embedding import load_embeddings
|
| 8 |
+
from vectorstore import load_or_build_vectorstore
|
| 9 |
+
from chain_setup import build_conversational_chain
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
st.title("💬 المحادثة التفاعلية - ادارة البياناتات و حماية البيانات الشخصية")
|
| 13 |
+
|
| 14 |
+
# Paths and constants
|
| 15 |
+
local_file = "Policies001.pdf"
|
| 16 |
+
index_folder = "faiss_index"
|
| 17 |
+
|
| 18 |
+
# Step 1: Load Embeddings
|
| 19 |
+
embeddings = load_embeddings()
|
| 20 |
+
|
| 21 |
+
# Step 2: Build or load VectorStore
|
| 22 |
+
vectorstore = load_or_build_vectorstore(local_file, index_folder, embeddings)
|
| 23 |
+
|
| 24 |
+
# Step 3: Build the Conversational Retrieval Chain
|
| 25 |
+
qa_chain = build_conversational_chain(vectorstore)
|
| 26 |
+
|
| 27 |
+
# Step 4: Session State for UI Chat
|
| 28 |
+
if "messages" not in st.session_state:
|
| 29 |
+
st.session_state["messages"] = [
|
| 30 |
+
{"role": "assistant", "content": "👋 مرحبًا! اسألني أي شيء عن إدارة البيانات وحماية البيانات الشخصية"}
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# Display existing messages
|
| 34 |
+
for msg in st.session_state["messages"]:
|
| 35 |
+
with st.chat_message(msg["role"]):
|
| 36 |
+
st.markdown(msg["content"])
|
| 37 |
+
|
| 38 |
+
# Step 5: Chat Input
|
| 39 |
+
user_input = st.chat_input("اكتب سؤالك")
|
| 40 |
+
|
| 41 |
+
# Step 6: Process user input
|
| 42 |
+
if user_input:
|
| 43 |
+
# a) Display user message
|
| 44 |
+
st.session_state["messages"].append({"role": "user", "content": user_input})
|
| 45 |
+
with st.chat_message("user"):
|
| 46 |
+
st.markdown(user_input)
|
| 47 |
+
|
| 48 |
+
# b) Run chain
|
| 49 |
+
response_dict = qa_chain({"question": user_input})
|
| 50 |
+
answer = response_dict["answer"]
|
| 51 |
+
|
| 52 |
+
# c) Display assistant response
|
| 53 |
+
st.session_state["messages"].append({"role": "assistant", "content": answer})
|
| 54 |
+
with st.chat_message("assistant"):
|
| 55 |
+
st.markdown(answer)
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
main()
|