DrishtiSharma commited on
Commit
1f90e38
·
verified ·
1 Parent(s): c5c1cc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -37,11 +37,6 @@ if uploaded_file:
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,6 +44,12 @@ if uploaded_file:
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()
@@ -78,7 +79,7 @@ if uploaded_file:
78
  prompt = ChatPromptTemplate.from_messages(
79
  [("system", system_prompt), ("human", "{input}")]
80
  )
81
- question_answer_chain = create_stuff_documents_chain(ChatOpenAI(), prompt)
82
  langchain_rag_chain = create_retrieval_chain(retriever, question_answer_chain)
83
 
84
  # Query input for LangChain
@@ -86,13 +87,23 @@ if uploaded_file:
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
 
92
  # LlamaIndex Tab
93
  with tab2:
94
  st.subheader("LlamaIndex Query")
95
  try:
 
 
 
 
 
96
  # Use PagedCSVReader for LlamaIndex
97
  csv_reader = PagedCSVReader()
98
  reader = SimpleDirectoryReader(
 
37
  st.write("Preview of uploaded data:")
38
  st.dataframe(data)
39
 
 
 
 
 
 
40
  # Tabs
41
  tab1, tab2 = st.tabs(["Chat w CSV using LangChain", "Chat w CSV using LlamaIndex"])
42
 
 
44
  with tab1:
45
  st.subheader("LangChain Query")
46
  try:
47
+ # Save the uploaded file to a temporary file for LangChain
48
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w") as temp_file:
49
+ # Write the DataFrame to the temp file
50
+ data.to_csv(temp_file.name, index=False)
51
+ temp_file_path = temp_file.name
52
+
53
  # Use CSVLoader with the temporary file path
54
  loader = CSVLoader(file_path=temp_file_path)
55
  docs = loader.load_and_split()
 
79
  prompt = ChatPromptTemplate.from_messages(
80
  [("system", system_prompt), ("human", "{input}")]
81
  )
82
+ question_answer_chain = create_stuff_documents_chain(ChatOpenAI(model="gpt-4o"), prompt)
83
  langchain_rag_chain = create_retrieval_chain(retriever, question_answer_chain)
84
 
85
  # Query input for LangChain
 
87
  if query:
88
  answer = langchain_rag_chain.invoke({"input": query})
89
  st.write(f"Answer: {answer['answer']}")
90
+
91
  except Exception as e:
92
  st.error(f"Error processing with LangChain: {e}")
93
+ finally:
94
+ # Clean up the temporary file
95
+ if 'temp_file_path' in locals() and os.path.exists(temp_file_path):
96
+ os.remove(temp_file_path)
97
 
98
  # LlamaIndex Tab
99
  with tab2:
100
  st.subheader("LlamaIndex Query")
101
  try:
102
+ # Save uploaded file content to a temporary CSV file for LlamaIndex
103
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w") as temp_file:
104
+ data.to_csv(temp_file.name, index=False)
105
+ temp_file_path = temp_file.name
106
+
107
  # Use PagedCSVReader for LlamaIndex
108
  csv_reader = PagedCSVReader()
109
  reader = SimpleDirectoryReader(