Update src/streamlit_app.py
Browse files- src/streamlit_app.py +68 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,70 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"
|
| 30 |
-
"
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from langchain.vectorstores import FAISS
|
| 3 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
| 4 |
+
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
| 5 |
+
from langchain.prompts import PromptTemplate
|
| 6 |
+
from langchain.chains import RetrievalQA
|
| 7 |
+
from langchain.llms import OpenAI
|
| 8 |
+
import os
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Create Groq LLaMA LLM
|
| 14 |
+
llm = OpenAI(
|
| 15 |
+
base_url="https://api.groq.com/openai/v1",
|
| 16 |
+
api_key="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",
|
| 17 |
+
model_name="llama3-8b-8192",
|
| 18 |
+
temperature=0.1
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
embedding_model = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-large")
|
| 22 |
+
vectordb_file_path = "faiss_index"
|
| 23 |
+
|
| 24 |
+
def create_vector_db():
|
| 25 |
+
loader = CSVLoader(file_path='codebasics_faqs.csv', source_column="prompt")
|
| 26 |
+
data = loader.load()
|
| 27 |
+
vectordb = FAISS.from_documents(documents=data, embedding=embedding_model)
|
| 28 |
+
vectordb.save_local(vectordb_file_path)
|
| 29 |
+
|
| 30 |
+
def get_qa_chain():
|
| 31 |
+
vectordb = FAISS.load_local(vectordb_file_path, embedding_model)
|
| 32 |
+
retriever = vectordb.as_retriever(score_threshold=0.7)
|
| 33 |
+
|
| 34 |
+
prompt_template = """Given the following context and a question, generate an answer based on this context only.
|
| 35 |
+
In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
|
| 36 |
+
If the answer is not found in the context, kindly state "I don't know." Don't try to make up an answer.
|
| 37 |
+
|
| 38 |
+
CONTEXT: {context}
|
| 39 |
+
|
| 40 |
+
QUESTION: {question}"""
|
| 41 |
+
|
| 42 |
+
PROMPT = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
| 43 |
+
|
| 44 |
+
chain = RetrievalQA.from_chain_type(
|
| 45 |
+
llm=llm,
|
| 46 |
+
chain_type="stuff",
|
| 47 |
+
retriever=retriever,
|
| 48 |
+
return_source_documents=True,
|
| 49 |
+
input_key="query",
|
| 50 |
+
chain_type_kwargs={"prompt": PROMPT}
|
| 51 |
+
)
|
| 52 |
+
return chain
|
| 53 |
+
|
| 54 |
+
# Streamlit UI
|
| 55 |
+
st.title("📊 Ask Questions About Your CSV")
|
| 56 |
+
if not os.path.exists(f"{vectordb_file_path}/index.faiss"):
|
| 57 |
+
with st.spinner("Creating vector DB..."):
|
| 58 |
+
create_vector_db()
|
| 59 |
+
|
| 60 |
+
user_input = st.text_input("Enter your question:")
|
| 61 |
+
if user_input:
|
| 62 |
+
qa_chain = get_qa_chain()
|
| 63 |
+
result = qa_chain({"query": user_input})
|
| 64 |
+
st.write("### Answer:")
|
| 65 |
+
st.write(result["result"])
|
| 66 |
+
|
| 67 |
+
with st.expander("Show Source Document(s)"):
|
| 68 |
+
for doc in result["source_documents"]:
|
| 69 |
+
st.markdown(f"**Source:** {doc.metadata}")
|
| 70 |
+
st.text(doc.page_content)
|