File size: 2,142 Bytes
81cce87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st 
import os 
import logging 
from dotenv import load_dotenv
from langchain_community.embeddings import CohereEmbeddings
from langchain_community.vectorstores import FAISS
from langchain.document_loaders.csv_loader import CSVLoader

#setup logging
logging.basicConfig(level = logging.DEBUG)

#load Environemnt Variables
load_dotenv()

#ensure COHERE_API_KEY is set properly
if not os.getenv("COHERE_API_KEY"):
    raise ValueError("COHERE_API_KEY environment variable not set")

st.set_page_config(page_title = "Educate Kids" , page_icon=":robot:")
st.header("SIMILARITY MATCHING")

model_name = "embed-english-v3.0"

user_agent = "my-app/1.0" # Replace with your agent if you Want
embeddings = CohereEmbeddings(model = model_name , user_agent = user_agent)
# embeddings = CohereEmbeddings(model = model_name)

loader = CSVLoader(file_path = "myData.csv" , csv_args={
    'delimiter' : ',',
    'quotechar' : '"',
    'fieldnames' : ['words']
})

data = loader.load()

logging.debug(f"loaded data : {data}")
print(f"loaded data : {data}")

#extract data from data and ensure they are valid
texts = [doc.page_content for doc in data]

#validate the databeing passed to the mebeddings
for text in texts:
    logging.debug(f"Document text  : {text}")

#initialize FAISS database
try:
    db = FAISS.from_documents(data , embeddings)
    st.write("FAISS database created successfully")
except ValueError as e:
    logging.error(f"Error occured : {e}")
    for text in texts:
        try:
            embedding = embeddings.embed_documents([text])
            logging.debug(f"Embeddings : {embedding}")
        except ValueError as ve:
            logging.error(f"Failed to embed document: {text} with error :{ve}")

def get_input():
    input_text  = st.text_input("you : " , key = "input")
    return input_text

user_input = get_input()
submit = st.button("Find similar Things")

if submit:
    docs = db.similarity_search(user_input)
    print(docs)
    st.subheader("Top Matches : ")
    st.text(docs[0].page_content)
    st.text(docs[1].page_content)