Prat0 commited on
Commit
9a72b79
·
verified ·
1 Parent(s): 4ba3598

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -47
app.py CHANGED
@@ -12,6 +12,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():
@@ -84,51 +85,53 @@ if 'chat_history' not in st.query_params:
84
  if 'last_response' not in st.query_params:
85
  st.query_params['last_response'] = None
86
 
87
- # URL input for document ingestion
88
- 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.query_params['documents'] = ingest_documents(url)
95
- embed_setup()
96
- client = qdrant_setup()
97
- llm = llm_setup()
98
- vector_store = QdrantVectorStore(client=client, collection_name=os.getenv("COLLECTION_NAME"))
99
- index = VectorStoreIndex.from_documents(st.query_params['documents'], vector_store=vector_store)
100
- st.query_params['chat_engine'] = query_index(index)
101
- st.success(f"Documents ingested from {url} and query engine setup completed successfully!")
102
- else:
103
- st.error("Please enter a URL")
104
-
105
- # Query input
106
- query = st.text_input("Enter your query:")
107
-
108
- # Search button
109
- if st.button("Search"):
110
- if st.query_params['chat_engine'] is None:
111
- st.error("Please complete the setup first")
112
- elif query:
113
- with st.spinner("Searching..."):
114
- response = st.query_params['chat_engine'].chat(query)
115
-
116
- # Add the query and response to chat history
117
- st.query_params['chat_history'].append(("User", query))
118
- st.query_params['chat_history'].append(("Assistant", str(response.response)))
119
-
120
- # Display the most recent response prominently
121
- st.subheader("Assistant's Response:")
122
- st.write(response.response)
123
- else:
124
- st.error("Please enter a query")
125
-
126
- # Sidebar for chat history
127
- st.sidebar.title("Chat History")
128
- for role, message in st.query_params['chat_history']:
129
- st.sidebar.text(f"{role}: {message}")
130
-
131
- # Clear chat history button in sidebar
132
- if st.sidebar.button("Clear Chat History"):
133
- st.query_params['chat_history'] = []
134
- st.sidebar.success("Chat history cleared!")
 
 
 
 
 
12
  from llama_index.core.memory import ChatMemoryBuffer
13
  from llama_index.readers.web import FireCrawlWebReader
14
  from llama_index.core import SummaryIndex
15
+ from streamlit_analytics2 import streamlit_analytics
16
 
17
  # Setup functions
18
  def embed_setup():
 
85
  if 'last_response' not in st.query_params:
86
  st.query_params['last_response'] = None
87
 
 
 
88
 
89
+ with streamlit_analytics.track():
90
+ # URL input for document ingestion
91
+ url = st.text_input("Enter URL to crawl and ingest documents:")
92
+
93
+ # Combined Ingest and Setup button
94
+ if st.button("Ingest and Setup"):
95
+ if url:
96
+ with st.spinner("Crawling, ingesting documents, and setting up query engine..."):
97
+ st.query_params['documents'] = ingest_documents(url)
98
+ embed_setup()
99
+ client = qdrant_setup()
100
+ llm = llm_setup()
101
+ vector_store = QdrantVectorStore(client=client, collection_name=os.getenv("COLLECTION_NAME"))
102
+ index = VectorStoreIndex.from_documents(st.query_params['documents'], vector_store=vector_store)
103
+ st.query_params['chat_engine'] = query_index(index)
104
+ st.success(f"Documents ingested from {url} and query engine setup completed successfully!")
105
+ else:
106
+ st.error("Please enter a URL")
107
+
108
+ # Query input
109
+ query = st.text_input("Enter your query:")
110
+
111
+ # Search button
112
+ if st.button("Search"):
113
+ if st.query_params['chat_engine'] is None:
114
+ st.error("Please complete the setup first")
115
+ elif query:
116
+ with st.spinner("Searching..."):
117
+ response = st.query_params['chat_engine'].chat(query)
118
+
119
+ # Add the query and response to chat history
120
+ st.query_params['chat_history'].append(("User", query))
121
+ st.query_params['chat_history'].append(("Assistant", str(response.response)))
122
+
123
+ # Display the most recent response prominently
124
+ st.subheader("Assistant's Response:")
125
+ st.write(response.response)
126
+ else:
127
+ st.error("Please enter a query")
128
+
129
+ # Sidebar for chat history
130
+ st.sidebar.title("Chat History")
131
+ for role, message in st.query_params['chat_history']:
132
+ st.sidebar.text(f"{role}: {message}")
133
+
134
+ # Clear chat history button in sidebar
135
+ if st.sidebar.button("Clear Chat History"):
136
+ st.query_params['chat_history'] = []
137
+ st.sidebar.success("Chat history cleared!")