Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,10 @@ import streamlit as st
|
|
2 |
import pandas as pd
|
3 |
import os
|
4 |
from dotenv import load_dotenv
|
5 |
-
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
|
6 |
from llama_index.readers.file.paged_csv.base import PagedCSVReader
|
7 |
-
from llama_index.
|
8 |
from llama_index.llms.openai import OpenAI
|
|
|
9 |
from llama_index.vector_stores.faiss import FaissVectorStore
|
10 |
from llama_index.core.ingestion import IngestionPipeline
|
11 |
from langchain_community.document_loaders.csv_loader import CSVLoader
|
@@ -15,9 +15,9 @@ 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 |
-
import tempfile
|
19 |
|
20 |
# Load environment variables
|
|
|
21 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
22 |
|
23 |
# Global settings for LlamaIndex
|
@@ -37,29 +37,23 @@ if uploaded_file:
|
|
37 |
st.write("Preview of uploaded data:")
|
38 |
st.dataframe(data)
|
39 |
|
40 |
-
# Tabs
|
41 |
-
tab1, tab2 = st.tabs(["
|
42 |
|
43 |
# LangChain Tab
|
44 |
with tab1:
|
45 |
st.subheader("LangChain Query")
|
46 |
try:
|
47 |
-
#
|
48 |
-
|
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
|
58 |
if docs:
|
59 |
st.write("Preview of a document chunk (LangChain):")
|
60 |
st.text(docs[0].page_content)
|
61 |
|
62 |
-
#
|
63 |
langchain_index = faiss.IndexFlatL2(EMBED_DIMENSION)
|
64 |
langchain_vector_store = LangChainFAISS(
|
65 |
embedding_function=OpenAIEmbeddings(),
|
@@ -82,37 +76,23 @@ if uploaded_file:
|
|
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
|
86 |
query = st.text_input("Ask a question about your data (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 |
-
#
|
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 |
-
|
110 |
-
input_files=[temp_file_path],
|
111 |
-
file_extractor={".csv": csv_reader},
|
112 |
-
)
|
113 |
-
docs = reader.load_data()
|
114 |
|
115 |
-
# Preview the first document
|
116 |
if docs:
|
117 |
st.write("Preview of a document chunk (LlamaIndex):")
|
118 |
st.text(docs[0].text)
|
@@ -129,16 +109,13 @@ if uploaded_file:
|
|
129 |
llama_index = VectorStoreIndex(nodes)
|
130 |
query_engine = llama_index.as_query_engine(similarity_top_k=3)
|
131 |
|
132 |
-
# Query input
|
133 |
query = st.text_input("Ask a question about your data (LlamaIndex):")
|
134 |
if query:
|
135 |
response = query_engine.query(query)
|
136 |
st.write(f"Answer: {response.response}")
|
137 |
except Exception as e:
|
138 |
st.error(f"Error processing with LlamaIndex: {e}")
|
139 |
-
|
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}")
|
|
|
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
|
7 |
from llama_index.llms.openai import OpenAI
|
8 |
+
from llama_index.embeddings.openai import OpenAIEmbedding
|
9 |
from llama_index.vector_stores.faiss import FaissVectorStore
|
10 |
from llama_index.core.ingestion import IngestionPipeline
|
11 |
from langchain_community.document_loaders.csv_loader import CSVLoader
|
|
|
15 |
from langchain_core.prompts import ChatPromptTemplate
|
16 |
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
|
17 |
import faiss
|
|
|
18 |
|
19 |
# Load environment variables
|
20 |
+
load_dotenv()
|
21 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
22 |
|
23 |
# Global settings for LlamaIndex
|
|
|
37 |
st.write("Preview of uploaded data:")
|
38 |
st.dataframe(data)
|
39 |
|
40 |
+
# Tabs for LangChain and LlamaIndex
|
41 |
+
tab1, tab2 = st.tabs(["LangChain", "LlamaIndex"])
|
42 |
|
43 |
# LangChain Tab
|
44 |
with tab1:
|
45 |
st.subheader("LangChain Query")
|
46 |
try:
|
47 |
+
# Use CSVLoader with the uploaded DataFrame
|
48 |
+
loader = CSVLoader(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
docs = loader.load_and_split()
|
50 |
|
51 |
+
# Preview the first document chunk
|
52 |
if docs:
|
53 |
st.write("Preview of a document chunk (LangChain):")
|
54 |
st.text(docs[0].page_content)
|
55 |
|
56 |
+
# Create FAISS VectorStore
|
57 |
langchain_index = faiss.IndexFlatL2(EMBED_DIMENSION)
|
58 |
langchain_vector_store = LangChainFAISS(
|
59 |
embedding_function=OpenAIEmbeddings(),
|
|
|
76 |
question_answer_chain = create_stuff_documents_chain(ChatOpenAI(model="gpt-4o"), prompt)
|
77 |
langchain_rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
78 |
|
79 |
+
# Query input
|
80 |
query = st.text_input("Ask a question about your data (LangChain):")
|
81 |
if query:
|
82 |
answer = langchain_rag_chain.invoke({"input": query})
|
83 |
st.write(f"Answer: {answer['answer']}")
|
|
|
84 |
except Exception as e:
|
85 |
st.error(f"Error processing with LangChain: {e}")
|
|
|
|
|
|
|
|
|
86 |
|
87 |
# LlamaIndex Tab
|
88 |
with tab2:
|
89 |
st.subheader("LlamaIndex Query")
|
90 |
try:
|
91 |
+
# Use PagedCSVReader directly on the uploaded DataFrame
|
|
|
|
|
|
|
|
|
|
|
92 |
csv_reader = PagedCSVReader()
|
93 |
+
docs = csv_reader.load_from_dataframe(data)
|
|
|
|
|
|
|
|
|
94 |
|
95 |
+
# Preview the first document chunk
|
96 |
if docs:
|
97 |
st.write("Preview of a document chunk (LlamaIndex):")
|
98 |
st.text(docs[0].text)
|
|
|
109 |
llama_index = VectorStoreIndex(nodes)
|
110 |
query_engine = llama_index.as_query_engine(similarity_top_k=3)
|
111 |
|
112 |
+
# Query input
|
113 |
query = st.text_input("Ask a question about your data (LlamaIndex):")
|
114 |
if query:
|
115 |
response = query_engine.query(query)
|
116 |
st.write(f"Answer: {response.response}")
|
117 |
except Exception as e:
|
118 |
st.error(f"Error processing with LlamaIndex: {e}")
|
119 |
+
|
|
|
|
|
|
|
120 |
except Exception as e:
|
121 |
st.error(f"Error reading uploaded file: {e}")
|