Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -145,7 +145,7 @@ def query_vector_db(query, vector_db):
|
|
| 145 |
return chat_completion.choices[0].message.content
|
| 146 |
|
| 147 |
# Streamlit app
|
| 148 |
-
st.title("Interactive
|
| 149 |
|
| 150 |
# Upload PDF
|
| 151 |
uploaded_file = st.file_uploader("Upload a PDF document", type=["pdf"])
|
|
@@ -155,34 +155,40 @@ if uploaded_file:
|
|
| 155 |
temp_file.write(uploaded_file.read())
|
| 156 |
pdf_path = temp_file.name
|
| 157 |
|
| 158 |
-
# Extract text
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
chunks = chunk_text(text)
|
| 162 |
-
st.session_state.vector_db = create_embeddings_and_store(chunks)
|
| 163 |
|
| 164 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
if "chat_history" not in st.session_state:
|
| 166 |
st.session_state.chat_history = []
|
| 167 |
|
| 168 |
-
#
|
| 169 |
-
|
| 170 |
-
st.write(f"**Query {i+1}:** {chat['query']}")
|
| 171 |
-
st.write(f"**Response:** {chat['response']}")
|
| 172 |
-
st.write("---")
|
| 173 |
-
|
| 174 |
-
# Add new query input dynamically
|
| 175 |
-
query_key = f"query_{len(st.session_state.chat_history) + 1}"
|
| 176 |
-
user_query = st.text_input("Enter your query:", key=query_key)
|
| 177 |
|
| 178 |
-
if
|
| 179 |
-
|
| 180 |
-
|
|
|
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
|
|
|
|
| 145 |
return chat_completion.choices[0].message.content
|
| 146 |
|
| 147 |
# Streamlit app
|
| 148 |
+
st.title("Interactive RAG-Based Application")
|
| 149 |
|
| 150 |
# Upload PDF
|
| 151 |
uploaded_file = st.file_uploader("Upload a PDF document", type=["pdf"])
|
|
|
|
| 155 |
temp_file.write(uploaded_file.read())
|
| 156 |
pdf_path = temp_file.name
|
| 157 |
|
| 158 |
+
# Extract text
|
| 159 |
+
text = extract_text_from_pdf(pdf_path)
|
| 160 |
+
st.write("PDF Text Extracted Successfully!")
|
|
|
|
|
|
|
| 161 |
|
| 162 |
+
# Chunk text
|
| 163 |
+
chunks = chunk_text(text)
|
| 164 |
+
st.write("Text Chunked Successfully!")
|
| 165 |
+
|
| 166 |
+
# Generate embeddings and store in FAISS
|
| 167 |
+
vector_db = create_embeddings_and_store(chunks)
|
| 168 |
+
st.write("Embeddings Generated and Stored Successfully!")
|
| 169 |
+
|
| 170 |
+
# Interactive chat section
|
| 171 |
+
st.write("### Interactive Chat Section")
|
| 172 |
+
|
| 173 |
+
# State management for chat history
|
| 174 |
if "chat_history" not in st.session_state:
|
| 175 |
st.session_state.chat_history = []
|
| 176 |
|
| 177 |
+
# User query input
|
| 178 |
+
user_query = st.text_input("Enter your query:", key="user_query")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
+
if st.button("Submit Query"):
|
| 181 |
+
if user_query:
|
| 182 |
+
# Get response from the model
|
| 183 |
+
response = query_vector_db(user_query, vector_db)
|
| 184 |
|
| 185 |
+
# Append the query and response to the chat history
|
| 186 |
+
st.session_state.chat_history.append({"query": user_query, "response": response})
|
| 187 |
|
| 188 |
+
# Display chat history
|
| 189 |
+
for chat in st.session_state.chat_history:
|
| 190 |
+
st.write(f"**User Query:** {chat['query']}")
|
| 191 |
+
st.write(f"**Response:** {chat['response']}")
|
| 192 |
+
st.write("---")
|
| 193 |
|
| 194 |
|