Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,6 +15,7 @@ from langchain.chains.combine_documents import create_stuff_documents_chain
|
|
15 |
from langchain_core.prompts import ChatPromptTemplate
|
16 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
17 |
import faiss
|
|
|
18 |
|
19 |
# Load environment variables
|
20 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
@@ -36,6 +37,11 @@ if uploaded_file:
|
|
36 |
st.write("Preview of uploaded data:")
|
37 |
st.dataframe(data)
|
38 |
|
|
|
|
|
|
|
|
|
|
|
39 |
# Tabs
|
40 |
tab1, tab2 = st.tabs(["Chat w CSV using LangChain", "Chat w CSV using LlamaIndex"])
|
41 |
|
@@ -43,8 +49,8 @@ if uploaded_file:
|
|
43 |
with tab1:
|
44 |
st.subheader("LangChain Query")
|
45 |
try:
|
46 |
-
# Use CSVLoader with the
|
47 |
-
loader = CSVLoader(file_path=
|
48 |
docs = loader.load_and_split()
|
49 |
|
50 |
# Preview the first document
|
@@ -80,7 +86,6 @@ if uploaded_file:
|
|
80 |
if query:
|
81 |
answer = langchain_rag_chain.invoke({"input": query})
|
82 |
st.write(f"Answer: {answer['answer']}")
|
83 |
-
|
84 |
except Exception as e:
|
85 |
st.error(f"Error processing with LangChain: {e}")
|
86 |
|
@@ -91,7 +96,7 @@ if uploaded_file:
|
|
91 |
# Use PagedCSVReader for LlamaIndex
|
92 |
csv_reader = PagedCSVReader()
|
93 |
reader = SimpleDirectoryReader(
|
94 |
-
input_files=[
|
95 |
file_extractor={".csv": csv_reader},
|
96 |
)
|
97 |
docs = reader.load_data()
|
@@ -120,5 +125,9 @@ if uploaded_file:
|
|
120 |
st.write(f"Answer: {response.response}")
|
121 |
except Exception as e:
|
122 |
st.error(f"Error processing with LlamaIndex: {e}")
|
|
|
|
|
|
|
|
|
123 |
except Exception as e:
|
124 |
st.error(f"Error reading uploaded file: {e}")
|
|
|
15 |
from langchain_core.prompts import ChatPromptTemplate
|
16 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
17 |
import faiss
|
18 |
+
import tempfile
|
19 |
|
20 |
# Load environment variables
|
21 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
|
|
37 |
st.write("Preview of uploaded data:")
|
38 |
st.dataframe(data)
|
39 |
|
40 |
+
# Create a temporary file for the uploaded CSV
|
41 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w") as temp_file:
|
42 |
+
data.to_csv(temp_file.name, index=False)
|
43 |
+
temp_file_path = temp_file.name
|
44 |
+
|
45 |
# Tabs
|
46 |
tab1, tab2 = st.tabs(["Chat w CSV using LangChain", "Chat w CSV using LlamaIndex"])
|
47 |
|
|
|
49 |
with tab1:
|
50 |
st.subheader("LangChain Query")
|
51 |
try:
|
52 |
+
# Use CSVLoader with the temporary file path
|
53 |
+
loader = CSVLoader(file_path=temp_file_path)
|
54 |
docs = loader.load_and_split()
|
55 |
|
56 |
# Preview the first document
|
|
|
86 |
if query:
|
87 |
answer = langchain_rag_chain.invoke({"input": query})
|
88 |
st.write(f"Answer: {answer['answer']}")
|
|
|
89 |
except Exception as e:
|
90 |
st.error(f"Error processing with LangChain: {e}")
|
91 |
|
|
|
96 |
# Use PagedCSVReader for LlamaIndex
|
97 |
csv_reader = PagedCSVReader()
|
98 |
reader = SimpleDirectoryReader(
|
99 |
+
input_files=[temp_file_path],
|
100 |
file_extractor={".csv": csv_reader},
|
101 |
)
|
102 |
docs = reader.load_data()
|
|
|
125 |
st.write(f"Answer: {response.response}")
|
126 |
except Exception as e:
|
127 |
st.error(f"Error processing with LlamaIndex: {e}")
|
128 |
+
finally:
|
129 |
+
# Clean up the temporary file
|
130 |
+
if 'temp_file_path' in locals() and os.path.exists(temp_file_path):
|
131 |
+
os.remove(temp_file_path)
|
132 |
except Exception as e:
|
133 |
st.error(f"Error reading uploaded file: {e}")
|