Update app.py
Browse files
app.py
CHANGED
|
@@ -24,6 +24,61 @@ from Virtualization import visualize_data
|
|
| 24 |
|
| 25 |
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Helper Functions
|
| 28 |
def create_documents(df):
|
| 29 |
"""Converts a DataFrame into a list of Document objects."""
|
|
@@ -106,33 +161,73 @@ def preview_data():
|
|
| 106 |
|
| 107 |
|
| 108 |
api="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21"
|
|
|
|
| 109 |
def rag_chatbot():
|
| 110 |
st.title("RAG Chatbot")
|
|
|
|
| 111 |
|
| 112 |
# Check if data is uploaded
|
| 113 |
if "data" in st.session_state and isinstance(st.session_state["data"], pd.DataFrame):
|
| 114 |
df = st.session_state["data"]
|
| 115 |
|
| 116 |
# Convert data to documents
|
| 117 |
-
st.write("Processing the dataset...")
|
| 118 |
documents = create_documents(df)
|
| 119 |
|
| 120 |
# Load models
|
| 121 |
-
st.write("Loading models...")
|
| 122 |
embedding_model = load_embedding_model()
|
| 123 |
llm_model = load_llm(api_key=api[:-2])
|
| 124 |
-
|
| 125 |
-
# Create retriever using Chroma
|
| 126 |
-
# FAISS.from_documents(documents, embedding)
|
| 127 |
retriever = FAISS.from_documents(documents, embedding=embedding_model).as_retriever()
|
| 128 |
|
| 129 |
-
#
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
else:
|
| 135 |
st.warning("Please upload a dataset to proceed.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
def main():
|
| 138 |
st.sidebar.title("Navigation")
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
+
bot_template = '''
|
| 28 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px; background-color: #B22222; padding: 10px; border-radius: 10px; border: 1px solid #7A0000;">
|
| 29 |
+
<div style="flex-shrink: 0; margin-right: 10px;">
|
| 30 |
+
<img src="https://raw.githubusercontent.com/AalaaAyman24/Test/main/chatbot.png"
|
| 31 |
+
style="max-height: 50px; max-width: 50px; object-fit: cover;">
|
| 32 |
+
</div>
|
| 33 |
+
<div style="background-color: #B22222; color: white; padding: 10px; border-radius: 10px; max-width: 75%; word-wrap: break-word; overflow-wrap: break-word;">
|
| 34 |
+
{msg}
|
| 35 |
+
</div>
|
| 36 |
+
</div>
|
| 37 |
+
'''
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
user_template = '''
|
| 41 |
+
<div style="display: flex; align-items: center; margin-bottom: 10px; justify-content: flex-end;">
|
| 42 |
+
<div style="flex-shrink: 0; margin-left: 10px;">
|
| 43 |
+
<img src="https://raw.githubusercontent.com/AalaaAyman24/Test/main/question.png"
|
| 44 |
+
style="max-height: 50px; max-width: 50px; border-radius: 50%; object-fit: cover;">
|
| 45 |
+
</div>
|
| 46 |
+
<div style="background-color: #757882; color: white; padding: 10px; border-radius: 10px; max-width: 75%; word-wrap: break-word; overflow-wrap: break-word;">
|
| 47 |
+
{msg}
|
| 48 |
+
</div>
|
| 49 |
+
</div>
|
| 50 |
+
'''
|
| 51 |
+
|
| 52 |
+
button_style = """
|
| 53 |
+
<style>
|
| 54 |
+
.small-button {
|
| 55 |
+
display: inline-block;
|
| 56 |
+
padding: 5px 10px;
|
| 57 |
+
font-size: 12px;
|
| 58 |
+
color: white;
|
| 59 |
+
background-color: #007bff;
|
| 60 |
+
border: none;
|
| 61 |
+
border-radius: 5px;
|
| 62 |
+
cursor: pointer;
|
| 63 |
+
margin-right: 5px;
|
| 64 |
+
}
|
| 65 |
+
.small-button:hover {
|
| 66 |
+
background-color: #0056b3;
|
| 67 |
+
}
|
| 68 |
+
.chat-box {
|
| 69 |
+
position: fixed;
|
| 70 |
+
bottom: 20px;
|
| 71 |
+
width: 100%;
|
| 72 |
+
left: 0;
|
| 73 |
+
padding: 20px;
|
| 74 |
+
background-color: #f1f1f1;
|
| 75 |
+
border-radius: 10px;
|
| 76 |
+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
|
| 77 |
+
}
|
| 78 |
+
</style>
|
| 79 |
+
"""
|
| 80 |
+
|
| 81 |
+
|
| 82 |
# Helper Functions
|
| 83 |
def create_documents(df):
|
| 84 |
"""Converts a DataFrame into a list of Document objects."""
|
|
|
|
| 161 |
|
| 162 |
|
| 163 |
api="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21"
|
| 164 |
+
|
| 165 |
def rag_chatbot():
|
| 166 |
st.title("RAG Chatbot")
|
| 167 |
+
st.markdown(button_style, unsafe_allow_html=True)
|
| 168 |
|
| 169 |
# Check if data is uploaded
|
| 170 |
if "data" in st.session_state and isinstance(st.session_state["data"], pd.DataFrame):
|
| 171 |
df = st.session_state["data"]
|
| 172 |
|
| 173 |
# Convert data to documents
|
|
|
|
| 174 |
documents = create_documents(df)
|
| 175 |
|
| 176 |
# Load models
|
|
|
|
| 177 |
embedding_model = load_embedding_model()
|
| 178 |
llm_model = load_llm(api_key=api[:-2])
|
|
|
|
|
|
|
|
|
|
| 179 |
retriever = FAISS.from_documents(documents, embedding=embedding_model).as_retriever()
|
| 180 |
|
| 181 |
+
# Chat Interface
|
| 182 |
+
if "chat_history" not in st.session_state:
|
| 183 |
+
st.session_state["chat_history"] = []
|
| 184 |
+
|
| 185 |
+
question = st.text_area("Ask a question about your dataset:", key="question_input")
|
| 186 |
+
if st.button("Send", key="send_button"):
|
| 187 |
+
if question.strip():
|
| 188 |
+
# Append user message
|
| 189 |
+
st.session_state["chat_history"].append({"role": "user", "content": question})
|
| 190 |
+
|
| 191 |
+
# Generate response
|
| 192 |
+
response = ask_question(question, retriever, llm_model)
|
| 193 |
+
st.session_state["chat_history"].append({"role": "bot", "content": response})
|
| 194 |
+
|
| 195 |
+
# Render chat history
|
| 196 |
+
for message in st.session_state["chat_history"]:
|
| 197 |
+
if message["role"] == "user":
|
| 198 |
+
st.markdown(user_template.format(msg=message["content"]), unsafe_allow_html=True)
|
| 199 |
+
else:
|
| 200 |
+
st.markdown(bot_template.format(msg=message["content"]), unsafe_allow_html=True)
|
| 201 |
else:
|
| 202 |
st.warning("Please upload a dataset to proceed.")
|
| 203 |
+
|
| 204 |
+
# def rag_chatbot():
|
| 205 |
+
# st.title("RAG Chatbot")
|
| 206 |
+
|
| 207 |
+
# # Check if data is uploaded
|
| 208 |
+
# if "data" in st.session_state and isinstance(st.session_state["data"], pd.DataFrame):
|
| 209 |
+
# df = st.session_state["data"]
|
| 210 |
+
|
| 211 |
+
# # Convert data to documents
|
| 212 |
+
# st.write("Processing the dataset...")
|
| 213 |
+
# documents = create_documents(df)
|
| 214 |
+
|
| 215 |
+
# # Load models
|
| 216 |
+
# st.write("Loading models...")
|
| 217 |
+
# embedding_model = load_embedding_model()
|
| 218 |
+
# llm_model = load_llm(api_key=api[:-2])
|
| 219 |
+
|
| 220 |
+
# # Create retriever using Chroma
|
| 221 |
+
# # FAISS.from_documents(documents, embedding)
|
| 222 |
+
# retriever = FAISS.from_documents(documents, embedding=embedding_model).as_retriever()
|
| 223 |
+
|
| 224 |
+
# # Ask a question
|
| 225 |
+
# question = st.text_input("Ask a question about your dataset:")
|
| 226 |
+
# if question:
|
| 227 |
+
# response = ask_question(question, retriever, llm_model)
|
| 228 |
+
# st.write(f"Answer: {response}")
|
| 229 |
+
# else:
|
| 230 |
+
# st.warning("Please upload a dataset to proceed.")
|
| 231 |
|
| 232 |
def main():
|
| 233 |
st.sidebar.title("Navigation")
|