CoolFace
Apppublic

ashunooji/Text_embedding

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
app.py79 linesDownload Raw Back to root
1import streamlit as st2 3 4#This module provides a way to interact with the operating system, such as accessing environment variables, working with files5#and directories, executing shell commands, etc6import os7 8#Helps us generate embeddings9#An embedding is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness.10#Small distances suggest high relatedness and large distances suggest low relatedness.11 12#As Langchain team has been working aggresively on improving the tool, we can see a lot of changes happening every weeek,13#As a part of it, the below import has been depreciated14#from langchain.embeddings import OpenAIEmbeddings15 16#New import from langchain, which replaces the above17from langchain_openai import OpenAIEmbeddings18 19 20#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.21#It provides optimized indexing structures and algorithms for tasks like nearest neighbor search and recommendation systems.22 23#As Langchain team has been working aggresively on improving the tool, we can see a lot of changes happening every weeek,24#As a part of it, the below import has been depreciated25#from langchain.vectorstores import FAISS26 27#New import from langchain, which replaces the above28from langchain_community.vectorstores import FAISS29 30 31#load_dotenv() is a function that loads variables from a .env file into environment variables in a Python script.32#It allows you to store sensitive information or configuration settings separate from your code33#and access them within your application.34from dotenv import load_dotenv35 36 37load_dotenv()38 39 40#By using st.set_page_config(), you can customize the appearance of your Streamlit application's web page41st.set_page_config(page_title="Educate Kids", page_icon=":robot:")42st.header("Hey, Ask me something & I will give out similar things")43 44#Initialize the OpenAIEmbeddings object45embeddings = OpenAIEmbeddings()46 47#The below snippet helps us to import CSV file data for our tasks48from langchain.document_loaders.csv_loader import CSVLoader49loader = CSVLoader(file_path='my_data.csv', csv_args={50    'delimiter': ',',51    'quotechar': '"',52    'fieldnames': ['Words']53})54 55#Assigning the data inside the csv to our variable here...56data = loader.load()57 58#Display the data59print(data)60 61db = FAISS.from_documents(data, embeddings)62 63#Function to receive input from user and store it in a variable64def get_text():65    input_text = st.text_input("You: ", key= input)66    return input_text67 68 69user_input=get_text()70submit = st.button('Find similar Things')  71 72if submit:73    74    #If the button is clicked, the below snippet will fetch us the similar text75    docs = db.similarity_search(user_input)76    print(docs)77    st.subheader("Top Matches:")78    st.text(docs[0])79    st.text(docs[1].page_content)