DrishtiSharma commited on
Commit
e020ff3
Β·
verified Β·
1 Parent(s): cfb9d35

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -67
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import os
 
4
  from dotenv import load_dotenv
5
  from llama_index.readers.file.paged_csv.base import PagedCSVReader
6
  from llama_index.core import Settings, VectorStoreIndex
@@ -14,14 +15,18 @@ from langchain.chains import create_retrieval_chain
14
  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
- from langchain_core.documents import Document # βœ… FIX: Import LangChain Document
18
  import faiss
19
  import tempfile
20
 
21
  # Load environment variables
22
- load_dotenv()
23
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
24
 
 
 
 
 
25
  # Global settings for LlamaIndex
26
  EMBED_DIMENSION = 512
27
  Settings.llm = OpenAI(model="gpt-4o")
@@ -45,7 +50,7 @@ if uploaded_file:
45
  data.to_csv(temp_file.name, index=False, encoding="utf-8")
46
  temp_file.flush()
47
 
48
- # Verify the temporary file (Display partial content)
49
  st.write("Temporary file path:", temp_file_path)
50
  with open(temp_file_path, "r") as f:
51
  content = f.read()
@@ -55,23 +60,28 @@ if uploaded_file:
55
  # Tabs for LangChain and LlamaIndex
56
  tab1, tab2 = st.tabs(["LangChain", "LlamaIndex"])
57
 
58
- # βœ… LangChain Processing with Proper Document Format
59
  with tab1:
60
  st.subheader("LangChain Query")
61
 
62
  try:
63
- # βœ… Convert CSV rows into LangChain Document objects (Fix for `dict` error)
64
  st.write("Processing CSV with a custom loader...")
65
  documents = []
66
  for _, row in data.iterrows():
67
  content = "\n".join([f"{col}: {row[col]}" for col in data.columns])
68
- doc = Document(page_content=content) # Convert to Document object
69
- documents.append(doc) # Append to list
 
 
 
 
 
70
 
71
- # βœ… Create FAISS VectorStore with proper arguments
72
  langchain_index = faiss.IndexFlatL2(EMBED_DIMENSION)
73
- docstore = InMemoryDocstore() # In-memory storage for documents
74
- index_to_docstore_id = {} # Mapping of index to document ID
75
 
76
  langchain_vector_store = LangChainFAISS(
77
  embedding_function=OpenAIEmbeddings(),
@@ -82,23 +92,9 @@ if uploaded_file:
82
 
83
  # βœ… Add properly formatted documents to FAISS
84
  langchain_vector_store.add_documents(documents)
 
85
 
86
- # βœ… LangChain Retrieval Chain
87
- retriever = langchain_vector_store.as_retriever()
88
- system_prompt = (
89
- "You are an assistant for question-answering tasks. "
90
- "Use the following pieces of retrieved context to answer "
91
- "the question. If you don't know the answer, say that you "
92
- "don't know. Use three sentences maximum and keep the "
93
- "answer concise.\n\n{context}"
94
- )
95
- prompt = ChatPromptTemplate.from_messages(
96
- [("system", system_prompt), ("human", "{input}")]
97
- )
98
- question_answer_chain = create_stuff_documents_chain(ChatOpenAI(model="gpt-4o"), prompt)
99
- langchain_rag_chain = create_retrieval_chain(retriever, question_answer_chain)
100
-
101
- # βœ… Query Input Field for LangChain
102
  query = st.text_input("Ask a question about your data (LangChain):")
103
 
104
  if query:
@@ -107,51 +103,16 @@ if uploaded_file:
107
  answer = langchain_rag_chain.invoke({"input": query})
108
  st.write(f"**Answer:** {answer['answer']}")
109
  except Exception as e:
 
110
  st.error(f"Error processing query: {e}")
 
111
 
112
  except Exception as e:
 
113
  st.error(f"Error processing with LangChain: {e}")
114
-
115
- # βœ… LlamaIndex Processing
116
- with tab2:
117
- st.subheader("LlamaIndex Query")
118
-
119
- try:
120
- # Use PagedCSVReader to load CSV
121
- st.write("Loading file with LlamaIndex PagedCSVReader...")
122
- csv_reader = PagedCSVReader()
123
- docs = csv_reader.load_from_file(temp_file_path)
124
-
125
- # βœ… Create FAISS Vector Store
126
- llama_faiss_index = faiss.IndexFlatL2(EMBED_DIMENSION)
127
- llama_vector_store = FaissVectorStore(faiss_index=llama_faiss_index)
128
-
129
- # βœ… Create ingestion pipeline and process data
130
- pipeline = IngestionPipeline(vector_store=llama_vector_store, documents=docs)
131
- nodes = pipeline.run()
132
-
133
- # βœ… Create a query engine
134
- llama_index = VectorStoreIndex(nodes)
135
- query_engine = llama_index.as_query_engine(similarity_top_k=3)
136
-
137
- # βœ… Query Input Field for LlamaIndex
138
- query_llama = st.text_input("Ask a question about your data (LlamaIndex):")
139
-
140
- if query_llama:
141
- try:
142
- st.write("Processing your question...")
143
- response = query_engine.query(query_llama)
144
- st.write(f"**Answer:** {response.response}")
145
- except Exception as e:
146
- st.error(f"Error processing query: {e}")
147
-
148
- except Exception as e:
149
- st.error(f"Error processing with LlamaIndex: {e}")
150
-
151
- finally:
152
- # Clean up the temporary file
153
- if 'temp_file_path' in locals() and os.path.exists(temp_file_path):
154
- os.remove(temp_file_path)
155
 
156
  except Exception as e:
 
157
  st.error(f"Error reading uploaded file: {e}")
 
 
1
  import streamlit as st
2
  import pandas as pd
3
  import os
4
+ import traceback
5
  from dotenv import load_dotenv
6
  from llama_index.readers.file.paged_csv.base import PagedCSVReader
7
  from llama_index.core import Settings, VectorStoreIndex
 
15
  from langchain.chains.combine_documents import create_stuff_documents_chain
16
  from langchain_core.prompts import ChatPromptTemplate
17
  from langchain_openai import OpenAIEmbeddings, ChatOpenAI
18
+ from langchain_core.documents import Document
19
  import faiss
20
  import tempfile
21
 
22
  # Load environment variables
23
+
24
  os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
25
 
26
+ # Check OpenAI API Key
27
+ if not os.getenv("OPENAI_API_KEY"):
28
+ st.error("⚠️ OpenAI API Key is missing! Please check your .env file or environment variables.")
29
+
30
  # Global settings for LlamaIndex
31
  EMBED_DIMENSION = 512
32
  Settings.llm = OpenAI(model="gpt-4o")
 
50
  data.to_csv(temp_file.name, index=False, encoding="utf-8")
51
  temp_file.flush()
52
 
53
+ # Debugging: Verify the temporary file (Display partial content)
54
  st.write("Temporary file path:", temp_file_path)
55
  with open(temp_file_path, "r") as f:
56
  content = f.read()
 
60
  # Tabs for LangChain and LlamaIndex
61
  tab1, tab2 = st.tabs(["LangChain", "LlamaIndex"])
62
 
63
+ # βœ… LangChain Processing
64
  with tab1:
65
  st.subheader("LangChain Query")
66
 
67
  try:
68
+ # βœ… Convert CSV rows into LangChain Document objects
69
  st.write("Processing CSV with a custom loader...")
70
  documents = []
71
  for _, row in data.iterrows():
72
  content = "\n".join([f"{col}: {row[col]}" for col in data.columns])
73
+ doc = Document(page_content=content)
74
+ documents.append(doc)
75
+
76
+ # Print a sample document
77
+ if documents:
78
+ st.write("Sample processed document (LangChain):")
79
+ st.text(documents[0].page_content)
80
 
81
+ # βœ… Create FAISS VectorStore
82
  langchain_index = faiss.IndexFlatL2(EMBED_DIMENSION)
83
+ docstore = InMemoryDocstore()
84
+ index_to_docstore_id = {}
85
 
86
  langchain_vector_store = LangChainFAISS(
87
  embedding_function=OpenAIEmbeddings(),
 
92
 
93
  # βœ… Add properly formatted documents to FAISS
94
  langchain_vector_store.add_documents(documents)
95
+ st.write("Documents successfully added to FAISS VectorStore.")
96
 
97
+ # βœ… Query Processing
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  query = st.text_input("Ask a question about your data (LangChain):")
99
 
100
  if query:
 
103
  answer = langchain_rag_chain.invoke({"input": query})
104
  st.write(f"**Answer:** {answer['answer']}")
105
  except Exception as e:
106
+ error_message = traceback.format_exc()
107
  st.error(f"Error processing query: {e}")
108
+ st.text(error_message)
109
 
110
  except Exception as e:
111
+ error_message = traceback.format_exc()
112
  st.error(f"Error processing with LangChain: {e}")
113
+ st.text(error_message)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  except Exception as e:
116
+ error_message = traceback.format_exc()
117
  st.error(f"Error reading uploaded file: {e}")
118
+ st.text(error_message)