Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
from llama_index.core.indices.vector_store.base import VectorStoreIndex
|
| 4 |
from llama_index.vector_stores.qdrant import QdrantVectorStore
|
| 5 |
from llama_index.embeddings.fastembed import FastEmbedEmbedding
|
|
@@ -12,6 +14,7 @@ from llama_index.embeddings.gemini import GeminiEmbedding
|
|
| 12 |
from llama_index.core.memory import ChatMemoryBuffer
|
| 13 |
from llama_index.readers.web import FireCrawlWebReader
|
| 14 |
from llama_index.core import SummaryIndex
|
|
|
|
| 15 |
|
| 16 |
# Setup functions
|
| 17 |
def embed_setup():
|
|
@@ -61,6 +64,22 @@ def ingest_documents(url):
|
|
| 61 |
documents = firecrawl_reader.load_data(url=url)
|
| 62 |
return documents
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# Streamlit app
|
| 65 |
st.title("Talk to Software Documentation")
|
| 66 |
|
|
@@ -89,6 +108,7 @@ url = st.text_input("Enter URL to crawl and ingest documents:")
|
|
| 89 |
|
| 90 |
# Combined Ingest and Setup button
|
| 91 |
if st.button("Ingest and Setup"):
|
|
|
|
| 92 |
if url:
|
| 93 |
with st.spinner("Crawling, ingesting documents, and setting up query engine..."):
|
| 94 |
st.session_state['documents'] = ingest_documents(url)
|
|
@@ -107,6 +127,7 @@ query = st.text_input("Enter your query:")
|
|
| 107 |
|
| 108 |
# Search button
|
| 109 |
if st.button("Search"):
|
|
|
|
| 110 |
if st.session_state['chat_engine'] is None:
|
| 111 |
st.error("Please complete the setup first")
|
| 112 |
elif query:
|
|
@@ -130,5 +151,13 @@ for role, message in st.session_state['chat_history']:
|
|
| 130 |
|
| 131 |
# Clear chat history button in sidebar
|
| 132 |
if st.sidebar.button("Clear Chat History"):
|
|
|
|
| 133 |
st.session_state['chat_history'] = []
|
| 134 |
-
st.sidebar.success("Chat history cleared!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import os
|
| 3 |
+
import json
|
| 4 |
+
from datetime import datetime
|
| 5 |
from llama_index.core.indices.vector_store.base import VectorStoreIndex
|
| 6 |
from llama_index.vector_stores.qdrant import QdrantVectorStore
|
| 7 |
from llama_index.embeddings.fastembed import FastEmbedEmbedding
|
|
|
|
| 14 |
from llama_index.core.memory import ChatMemoryBuffer
|
| 15 |
from llama_index.readers.web import FireCrawlWebReader
|
| 16 |
from llama_index.core import SummaryIndex
|
| 17 |
+
import streamlit_analytics
|
| 18 |
|
| 19 |
# Setup functions
|
| 20 |
def embed_setup():
|
|
|
|
| 64 |
documents = firecrawl_reader.load_data(url=url)
|
| 65 |
return documents
|
| 66 |
|
| 67 |
+
# Function to save analytics data to JSON in a dedicated directory
|
| 68 |
+
def save_analytics_to_json(data):
|
| 69 |
+
# Create 'analytics' directory if it doesn't exist
|
| 70 |
+
analytics_dir = "./analytics"
|
| 71 |
+
os.makedirs(analytics_dir, exist_ok=True)
|
| 72 |
+
|
| 73 |
+
filename = f"analytics_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
| 74 |
+
filepath = os.path.join(analytics_dir, filename)
|
| 75 |
+
|
| 76 |
+
with open(filepath, 'w') as f:
|
| 77 |
+
json.dump(data, f, indent=4)
|
| 78 |
+
print(f"Analytics data saved to {filepath}")
|
| 79 |
+
|
| 80 |
+
# Initialize streamlit-analytics
|
| 81 |
+
streamlit_analytics.start_tracking()
|
| 82 |
+
|
| 83 |
# Streamlit app
|
| 84 |
st.title("Talk to Software Documentation")
|
| 85 |
|
|
|
|
| 108 |
|
| 109 |
# Combined Ingest and Setup button
|
| 110 |
if st.button("Ingest and Setup"):
|
| 111 |
+
streamlit_analytics.track(action="Ingest and Setup", url=url)
|
| 112 |
if url:
|
| 113 |
with st.spinner("Crawling, ingesting documents, and setting up query engine..."):
|
| 114 |
st.session_state['documents'] = ingest_documents(url)
|
|
|
|
| 127 |
|
| 128 |
# Search button
|
| 129 |
if st.button("Search"):
|
| 130 |
+
streamlit_analytics.track(action="Search", query=query)
|
| 131 |
if st.session_state['chat_engine'] is None:
|
| 132 |
st.error("Please complete the setup first")
|
| 133 |
elif query:
|
|
|
|
| 151 |
|
| 152 |
# Clear chat history button in sidebar
|
| 153 |
if st.sidebar.button("Clear Chat History"):
|
| 154 |
+
streamlit_analytics.track(action="Clear Chat History")
|
| 155 |
st.session_state['chat_history'] = []
|
| 156 |
+
st.sidebar.success("Chat history cleared!")
|
| 157 |
+
|
| 158 |
+
# Save analytics data to JSON when the app is about to close
|
| 159 |
+
analytics_data = streamlit_analytics.get_current_tracking_data()
|
| 160 |
+
save_analytics_to_json(analytics_data)
|
| 161 |
+
|
| 162 |
+
# Stop tracking before the app finishes
|
| 163 |
+
streamlit_analytics.stop_tracking()
|