DrishtiSharma commited on
Commit
11dd106
·
verified ·
1 Parent(s): b56da48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -4
app.py CHANGED
@@ -44,8 +44,14 @@ if uploaded_file:
44
  with tab1:
45
  st.subheader("LangChain Query")
46
  try:
47
- # Use CSVLoader with the uploaded file (as a stream)
48
- loader = CSVLoader(file_path=uploaded_file)
 
 
 
 
 
 
49
  docs = loader.load_and_split()
50
 
51
  # Preview the first document
@@ -84,15 +90,24 @@ if uploaded_file:
84
 
85
  except Exception as e:
86
  st.error(f"Error processing with LangChain: {e}")
 
 
 
 
87
 
88
  # LlamaIndex Tab
89
  with tab2:
90
  st.subheader("LlamaIndex Query")
91
  try:
 
 
 
 
 
92
  # Use PagedCSVReader for LlamaIndex
93
  csv_reader = PagedCSVReader()
94
  reader = SimpleDirectoryReader(
95
- input_files=[uploaded_file.name],
96
  file_extractor={".csv": csv_reader},
97
  )
98
  docs = reader.load_data()
@@ -121,5 +136,9 @@ if uploaded_file:
121
  st.write(f"Answer: {response.response}")
122
  except Exception as e:
123
  st.error(f"Error processing with LlamaIndex: {e}")
 
 
 
 
124
  except Exception as e:
125
- st.error(f"Error reading uploaded file: {e}")
 
44
  with tab1:
45
  st.subheader("LangChain Query")
46
  try:
47
+ # Write uploaded file to a temporary file for LangChain
48
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w") as temp_file:
49
+ # Save the DataFrame content to the temporary 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()
56
 
57
  # Preview the first document
 
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
+ # Write uploaded file to a temporary 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(
110
+ input_files=[temp_file_path],
111
  file_extractor={".csv": csv_reader},
112
  )
113
  docs = reader.load_data()
 
136
  st.write(f"Answer: {response.response}")
137
  except Exception as e:
138
  st.error(f"Error processing with LlamaIndex: {e}")
139
+ finally:
140
+ # Clean up the temporary file
141
+ if 'temp_file_path' in locals() and os.path.exists(temp_file_path):
142
+ os.remove(temp_file_path)
143
  except Exception as e:
144
+ st.error(f"Error reading uploaded file: {e}")