Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ import io
|
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
6 |
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
|
7 |
-
from llama_index.core.readers.base import BaseReader
|
8 |
from llama_index.readers.file.paged_csv.base import PagedCSVReader
|
9 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
10 |
from llama_index.llms.openai import OpenAI
|
@@ -17,6 +16,7 @@ from langchain.chains.combine_documents import create_stuff_documents_chain
|
|
17 |
from langchain_core.prompts import ChatPromptTemplate
|
18 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
19 |
import faiss
|
|
|
20 |
|
21 |
# Load environment variables
|
22 |
load_dotenv()
|
@@ -34,7 +34,7 @@ st.title("Chat with CSV Files - LangChain vs LlamaIndex")
|
|
34 |
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
35 |
if uploaded_file:
|
36 |
try:
|
37 |
-
#
|
38 |
data = pd.read_csv(uploaded_file)
|
39 |
st.write("Preview of uploaded data:")
|
40 |
st.dataframe(data)
|
@@ -46,8 +46,13 @@ if uploaded_file:
|
|
46 |
with tab1:
|
47 |
st.subheader("LangChain Query")
|
48 |
try:
|
49 |
-
#
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
51 |
docs = loader.load_and_split()
|
52 |
|
53 |
# Preview the first document
|
@@ -83,8 +88,13 @@ if uploaded_file:
|
|
83 |
if query:
|
84 |
answer = langchain_rag_chain.invoke({"input": query})
|
85 |
st.write(f"Answer: {answer['answer']}")
|
|
|
86 |
except Exception as e:
|
87 |
st.error(f"Error processing with LangChain: {e}")
|
|
|
|
|
|
|
|
|
88 |
|
89 |
# LlamaIndex Tab
|
90 |
with tab2:
|
|
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
6 |
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
|
|
|
7 |
from llama_index.readers.file.paged_csv.base import PagedCSVReader
|
8 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
9 |
from llama_index.llms.openai import OpenAI
|
|
|
16 |
from langchain_core.prompts import ChatPromptTemplate
|
17 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
18 |
import faiss
|
19 |
+
import tempfile
|
20 |
|
21 |
# Load environment variables
|
22 |
load_dotenv()
|
|
|
34 |
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
35 |
if uploaded_file:
|
36 |
try:
|
37 |
+
# Read and preview CSV data using pandas
|
38 |
data = pd.read_csv(uploaded_file)
|
39 |
st.write("Preview of uploaded data:")
|
40 |
st.dataframe(data)
|
|
|
46 |
with tab1:
|
47 |
st.subheader("LangChain Query")
|
48 |
try:
|
49 |
+
# Save the uploaded file to a temporary file for LangChain
|
50 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv") as temp_file:
|
51 |
+
temp_file.write(uploaded_file.getvalue())
|
52 |
+
temp_file_path = temp_file.name
|
53 |
+
|
54 |
+
# Use CSVLoader with the temporary file path
|
55 |
+
loader = CSVLoader(file_path=temp_file_path)
|
56 |
docs = loader.load_and_split()
|
57 |
|
58 |
# Preview the first document
|
|
|
88 |
if query:
|
89 |
answer = langchain_rag_chain.invoke({"input": query})
|
90 |
st.write(f"Answer: {answer['answer']}")
|
91 |
+
|
92 |
except Exception as e:
|
93 |
st.error(f"Error processing with LangChain: {e}")
|
94 |
+
finally:
|
95 |
+
# Clean up the temporary file
|
96 |
+
if 'temp_file_path' in locals() and os.path.exists(temp_file_path):
|
97 |
+
os.remove(temp_file_path)
|
98 |
|
99 |
# LlamaIndex Tab
|
100 |
with tab2:
|