from sentence_transformers import SentenceTransformer import faiss import numpy as np import streamlit as st # Title st.title("Custom Data Chatbot") def chatbot(query): query_embedding = model.encode(query).reshape(1, -1) D, I = index.search(query_embedding, 1) similarity_score = D[0][0] st.write(f'similarity_score: {similarity_score}') if similarity_score < 0: return "I'm not sure I understand. Please try again with different wording." else: retrieved_answer = answers[I[0][0]] return retrieved_answer # Title st.write("### File Upload") uploaded_file = st.file_uploader("Choose a file", type=["csv"]) if uploaded_file is not None: if uploaded_file.type == 'text/csv': st.write("### File Details:") st.write(f"Filename: {uploaded_file.name}") if uploaded_file.type in ["text/plain", "text/csv"]: file_content = uploaded_file.read().decode("utf-8") answers = file_content.splitlines() st.write('Total Content:' , len(answers)) user_input = st.text_input("Enter text base on content you upload:") model = SentenceTransformer('all-MiniLM-L6-v2') answers_embeddings = model.encode(answers) d = answers_embeddings.shape[1] index = faiss.IndexFlatL2(d) index.add(np.array(answers_embeddings)) if user_input.strip(): st.write("You:", user_input) st.write("Bot:",chatbot(user_input)) else: st.write("Please enter some text.")