File size: 1,662 Bytes
7c0a1a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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.")