Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sentence_transformers import SentenceTransformer
|
| 2 |
+
import faiss
|
| 3 |
+
import numpy as np
|
| 4 |
+
import streamlit as st
|
| 5 |
+
|
| 6 |
+
# Title
|
| 7 |
+
st.title("Custom Data Chatbot")
|
| 8 |
+
|
| 9 |
+
def chatbot(query):
|
| 10 |
+
query_embedding = model.encode(query).reshape(1, -1)
|
| 11 |
+
D, I = index.search(query_embedding, 1)
|
| 12 |
+
similarity_score = D[0][0]
|
| 13 |
+
st.write(f'similarity_score: {similarity_score}')
|
| 14 |
+
if similarity_score < 0:
|
| 15 |
+
return "I'm not sure I understand. Please try again with different wording."
|
| 16 |
+
else:
|
| 17 |
+
retrieved_answer = answers[I[0][0]]
|
| 18 |
+
return retrieved_answer
|
| 19 |
+
|
| 20 |
+
# Title
|
| 21 |
+
st.write("### File Upload")
|
| 22 |
+
|
| 23 |
+
uploaded_file = st.file_uploader("Choose a file", type=["csv"])
|
| 24 |
+
|
| 25 |
+
if uploaded_file is not None:
|
| 26 |
+
if uploaded_file.type == 'text/csv':
|
| 27 |
+
st.write("### File Details:")
|
| 28 |
+
st.write(f"Filename: {uploaded_file.name}")
|
| 29 |
+
if uploaded_file.type in ["text/plain", "text/csv"]:
|
| 30 |
+
file_content = uploaded_file.read().decode("utf-8")
|
| 31 |
+
|
| 32 |
+
answers = file_content.splitlines()
|
| 33 |
+
st.write('Total Content:' , len(answers))
|
| 34 |
+
|
| 35 |
+
user_input = st.text_input("Enter text base on content you upload:")
|
| 36 |
+
|
| 37 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 38 |
+
answers_embeddings = model.encode(answers)
|
| 39 |
+
d = answers_embeddings.shape[1]
|
| 40 |
+
index = faiss.IndexFlatL2(d)
|
| 41 |
+
index.add(np.array(answers_embeddings))
|
| 42 |
+
|
| 43 |
+
if user_input.strip():
|
| 44 |
+
st.write("You:", user_input)
|
| 45 |
+
st.write("Bot:",chatbot(user_input))
|
| 46 |
+
else:
|
| 47 |
+
st.write("Please enter some text.")
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
|