maaz21 commited on
Commit
75574d6
·
verified ·
1 Parent(s): bd41f85

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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)