Spaces:
Sleeping
Sleeping
Update lab/app.py
Browse files- lab/app.py +24 -15
lab/app.py
CHANGED
|
@@ -2,7 +2,7 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import os
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
-
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
|
| 6 |
from llama_index.readers.file import PagedCSVReader
|
| 7 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 8 |
from llama_index.llms.openai import OpenAI
|
|
@@ -19,11 +19,10 @@ import faiss
|
|
| 19 |
# Load environment variables
|
| 20 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 21 |
|
| 22 |
-
# Global
|
| 23 |
EMBED_DIMENSION = 512
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
langchain_llm = ChatOpenAI(model="gpt-3.5-turbo-0125")
|
| 27 |
|
| 28 |
# Streamlit app
|
| 29 |
st.title("Streamlit App with LangChain and LlamaIndex")
|
|
@@ -31,7 +30,13 @@ st.title("Streamlit App with LangChain and LlamaIndex")
|
|
| 31 |
# File uploader
|
| 32 |
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
| 33 |
if uploaded_file:
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
st.write("Preview of uploaded data:")
|
| 36 |
st.dataframe(data)
|
| 37 |
|
|
@@ -41,12 +46,13 @@ if uploaded_file:
|
|
| 41 |
# LangChain Tab
|
| 42 |
with tab1:
|
| 43 |
st.subheader("LangChain Query")
|
| 44 |
-
loader = CSVLoader(file_path=
|
| 45 |
docs = loader.load_and_split()
|
| 46 |
|
| 47 |
# Preview the first document
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
# LangChain FAISS VectorStore
|
| 52 |
langchain_index = faiss.IndexFlatL2(EMBED_DIMENSION)
|
|
@@ -68,7 +74,7 @@ if uploaded_file:
|
|
| 68 |
prompt = ChatPromptTemplate.from_messages(
|
| 69 |
[("system", system_prompt), ("human", "{input}")]
|
| 70 |
)
|
| 71 |
-
question_answer_chain = create_stuff_documents_chain(
|
| 72 |
langchain_rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 73 |
|
| 74 |
# Query input for LangChain
|
|
@@ -80,17 +86,17 @@ if uploaded_file:
|
|
| 80 |
# LlamaIndex Tab
|
| 81 |
with tab2:
|
| 82 |
st.subheader("LlamaIndex Query")
|
| 83 |
-
# Use PagedCSVReader for CSV loading
|
| 84 |
csv_reader = PagedCSVReader()
|
| 85 |
reader = SimpleDirectoryReader(
|
| 86 |
-
input_files=[
|
| 87 |
file_extractor={".csv": csv_reader},
|
| 88 |
)
|
| 89 |
docs = reader.load_data()
|
| 90 |
|
| 91 |
# Preview the first document
|
| 92 |
-
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
# Initialize FAISS Vector Store
|
| 96 |
llama_faiss_index = faiss.IndexFlatL2(EMBED_DIMENSION)
|
|
@@ -108,4 +114,7 @@ if uploaded_file:
|
|
| 108 |
query = st.text_input("Ask a question about your data (LlamaIndex):")
|
| 109 |
if query:
|
| 110 |
response = query_engine.query(query)
|
| 111 |
-
st.write(f"Answer: {response.response}")
|
|
|
|
|
|
|
|
|
|
|
|
| 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 import PagedCSVReader
|
| 7 |
from llama_index.embeddings.openai import OpenAIEmbedding
|
| 8 |
from llama_index.llms.openai import OpenAI
|
|
|
|
| 19 |
# Load environment variables
|
| 20 |
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
|
| 21 |
|
| 22 |
+
# Global settings for LlamaIndex
|
| 23 |
EMBED_DIMENSION = 512
|
| 24 |
+
Settings.llm = OpenAI(model="gpt-3.5-turbo")
|
| 25 |
+
Settings.embed_model = OpenAIEmbedding(model="text-embedding-3-small", dimensions=EMBED_DIMENSION)
|
|
|
|
| 26 |
|
| 27 |
# Streamlit app
|
| 28 |
st.title("Streamlit App with LangChain and LlamaIndex")
|
|
|
|
| 30 |
# File uploader
|
| 31 |
uploaded_file = st.file_uploader("Upload a CSV file", type=["csv"])
|
| 32 |
if uploaded_file:
|
| 33 |
+
# Save the uploaded file temporarily
|
| 34 |
+
temp_file_path = f"temp_{uploaded_file.name}"
|
| 35 |
+
with open(temp_file_path, "wb") as temp_file:
|
| 36 |
+
temp_file.write(uploaded_file.getbuffer())
|
| 37 |
+
|
| 38 |
+
# Read and preview CSV data
|
| 39 |
+
data = pd.read_csv(temp_file_path)
|
| 40 |
st.write("Preview of uploaded data:")
|
| 41 |
st.dataframe(data)
|
| 42 |
|
|
|
|
| 46 |
# LangChain Tab
|
| 47 |
with tab1:
|
| 48 |
st.subheader("LangChain Query")
|
| 49 |
+
loader = CSVLoader(file_path=temp_file_path)
|
| 50 |
docs = loader.load_and_split()
|
| 51 |
|
| 52 |
# Preview the first document
|
| 53 |
+
if docs:
|
| 54 |
+
st.write("Preview of a document chunk (LangChain):")
|
| 55 |
+
st.text(docs[0].page_content)
|
| 56 |
|
| 57 |
# LangChain FAISS VectorStore
|
| 58 |
langchain_index = faiss.IndexFlatL2(EMBED_DIMENSION)
|
|
|
|
| 74 |
prompt = ChatPromptTemplate.from_messages(
|
| 75 |
[("system", system_prompt), ("human", "{input}")]
|
| 76 |
)
|
| 77 |
+
question_answer_chain = create_stuff_documents_chain(ChatOpenAI(), prompt)
|
| 78 |
langchain_rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 79 |
|
| 80 |
# Query input for LangChain
|
|
|
|
| 86 |
# LlamaIndex Tab
|
| 87 |
with tab2:
|
| 88 |
st.subheader("LlamaIndex Query")
|
|
|
|
| 89 |
csv_reader = PagedCSVReader()
|
| 90 |
reader = SimpleDirectoryReader(
|
| 91 |
+
input_files=[temp_file_path],
|
| 92 |
file_extractor={".csv": csv_reader},
|
| 93 |
)
|
| 94 |
docs = reader.load_data()
|
| 95 |
|
| 96 |
# Preview the first document
|
| 97 |
+
if docs:
|
| 98 |
+
st.write("Preview of a document chunk (LlamaIndex):")
|
| 99 |
+
st.text(docs[0].text)
|
| 100 |
|
| 101 |
# Initialize FAISS Vector Store
|
| 102 |
llama_faiss_index = faiss.IndexFlatL2(EMBED_DIMENSION)
|
|
|
|
| 114 |
query = st.text_input("Ask a question about your data (LlamaIndex):")
|
| 115 |
if query:
|
| 116 |
response = query_engine.query(query)
|
| 117 |
+
st.write(f"Answer: {response.response}")
|
| 118 |
+
|
| 119 |
+
# Cleanup temporary file
|
| 120 |
+
os.remove(temp_file_path)
|