Upload 3 files
Browse files- app.py +50 -29
- myData.csv +12 -0
- requirements.txt +4 -3
app.py
CHANGED
|
@@ -1,49 +1,70 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from langchain.chat_models import ChatOpenAI
|
| 3 |
-
from langchain.schema import AIMessage, HumanMessage, SystemMessage
|
| 4 |
|
| 5 |
-
# From here down is all the StreamLit UI.
|
| 6 |
-
st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
|
| 7 |
-
st.header("Hey, I'm your Chat GPT")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
st.session_state.sessionMessages.append(HumanMessage(content=question))
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
# st.session_state.sessionMessages.append(
|
| 23 |
-
# AIMessage(content=assistant_answer.content)
|
| 24 |
-
# )
|
| 25 |
|
| 26 |
-
|
| 27 |
-
assistant_answer
|
| 28 |
|
| 29 |
-
return assistant_answer
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return input_text
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
|
|
|
| 38 |
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
if submit:
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
st.write(response, key=1)
|
|
|
|
| 1 |
+
#Allows you to use Streamlit, a framework for building interactive web applications.
|
| 2 |
+
#It provides functions for creating UIs, displaying data, and handling user inputs.
|
| 3 |
import streamlit as st
|
|
|
|
|
|
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
#This module provides a way to interact with the operating system, such as accessing environment variables, working with files
|
| 7 |
+
#and directories, executing shell commands, etc
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
+
#Helps us generate embeddings
|
| 11 |
+
#An embedding is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness.
|
| 12 |
+
#Small distances suggest high relatedness and large distances suggest low relatedness.
|
| 13 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 14 |
|
| 15 |
|
| 16 |
+
#FAISS is an open-source library developed by Facebook AI Research for efficient similarity search and clustering of large-scale datasets, particularly with high-dimensional vectors.
|
| 17 |
+
#It provides optimized indexing structures and algorithms for tasks like nearest neighbor search and recommendation systems.
|
| 18 |
+
from langchain.vectorstores import FAISS
|
| 19 |
|
|
|
|
| 20 |
|
| 21 |
+
#load_dotenv() is a function that loads variables from a .env file into environment variables in a Python script.
|
| 22 |
+
#It allows you to store sensitive information or configuration settings separate from your code
|
| 23 |
+
#and access them within your application.
|
| 24 |
+
from dotenv import load_dotenv
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
load_dotenv()
|
|
|
|
| 28 |
|
|
|
|
| 29 |
|
| 30 |
+
#By using st.set_page_config(), you can customize the appearance of your Streamlit application's web page
|
| 31 |
+
st.set_page_config(page_title="Educate Kids", page_icon=":robot:")
|
| 32 |
+
st.header("Hey, Ask me something & I will give out similar things")
|
| 33 |
|
| 34 |
+
#Initialize the OpenAIEmbeddings object
|
| 35 |
+
embeddings = OpenAIEmbeddings()
|
|
|
|
| 36 |
|
| 37 |
+
#The below snippet helps us to import CSV file data for our tasks
|
| 38 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
| 39 |
+
loader = CSVLoader(file_path='myData.csv', csv_args={
|
| 40 |
+
'delimiter': ',',
|
| 41 |
+
'quotechar': '"',
|
| 42 |
+
'fieldnames': ['Words']
|
| 43 |
+
})
|
| 44 |
|
| 45 |
+
#Assigning the data inside the csv to our variable here
|
| 46 |
+
data = loader.load()
|
| 47 |
|
| 48 |
+
#Display the data
|
| 49 |
+
print(data)
|
| 50 |
|
| 51 |
+
db = FAISS.from_documents(data, embeddings)
|
| 52 |
+
|
| 53 |
+
#Function to receive input from user and store it in a variable
|
| 54 |
+
def get_text():
|
| 55 |
+
input_text = st.text_input("You: ", key= input)
|
| 56 |
+
return input_text
|
| 57 |
|
|
|
|
| 58 |
|
| 59 |
+
user_input=get_text()
|
| 60 |
+
submit = st.button('Find similar Things')
|
| 61 |
+
|
| 62 |
+
if submit:
|
| 63 |
+
|
| 64 |
+
#If the button is clicked, the below snippet will fetch us the similar text
|
| 65 |
+
docs = db.similarity_search(user_input)
|
| 66 |
+
print(docs)
|
| 67 |
+
st.subheader("Top Matches:")
|
| 68 |
+
st.text(docs[0])
|
| 69 |
+
st.text(docs[1].page_content)
|
| 70 |
|
|
|
myData.csv
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Words
|
| 2 |
+
Elephant
|
| 3 |
+
Lion
|
| 4 |
+
Tiger
|
| 5 |
+
Dog
|
| 6 |
+
Cricket
|
| 7 |
+
Footbal
|
| 8 |
+
Tennis
|
| 9 |
+
Basketball
|
| 10 |
+
Apple
|
| 11 |
+
Orange
|
| 12 |
+
Banana
|
requirements.txt
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
-
streamlit
|
| 2 |
langchain
|
|
|
|
| 3 |
openai
|
| 4 |
-
|
| 5 |
-
python-dotenv
|
|
|
|
|
|
|
|
|
| 1 |
langchain
|
| 2 |
+
streamlit
|
| 3 |
openai
|
| 4 |
+
tiktoken
|
| 5 |
+
python-dotenv
|
| 6 |
+
faiss-cpu
|