File size: 3,394 Bytes
7f9bcee
 
d38726f
7f9bcee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import streamlit as st
from sentence_transformers import SentenceTransformer
from qdrant_client import models, QdrantClient
import pandas as pd
from datasets import load_dataset

#************************************************************* LOAD DATA
data = load_dataset("ManuelAlv/academic_conuseling")

# Main dataset
bert_dataset = data['dataset'].to_pandas()

# Dataset used to test the chatbot
test_dataset = data['test'].to_pandas()
test_dataset.columns = ["test_question", "original_question"]

#************************************************************* Create Functions
# function to add values
def add_value(collection, key, value, id):
  encoder = SentenceTransformer(collection)

  qdrant.upsert(
      collection_name = collection,
      wait=True,
      points = [
          models.PointStruct(
              id = id,
              vector = encoder.encode(key).tolist(),
              payload = {
                  'text': value,
                  'question': key
              }
          )
      ]
  )

# Function to search for a value
def search(collection, query):
  search = qdrant.search(
      collection_name = collection,
      query_vector = encoder.encode(query).tolist(),
      limit = 1
  )
  return search

#************************************************************* Create VD
# Create a local Vector Database
qdrant = QdrantClient(":memory:")

# Load the model
model = "all-MiniLM-L6-v2"
encoder = SentenceTransformer(model)

# Create a collection for the model with its embeddings
qdrant.recreate_collection(
    collection_name = model,
    vectors_config = models.VectorParams(
        size = encoder.get_sentence_embedding_dimension(),
        distance = models.Distance.COSINE
    )
)

# Add the data to model
for index, row in bert_dataset.iterrows():
  key = row['question']
  value = row['answer']
  id = index + 1

  add_value(model, key, value, id)

# ************************************************************* QUERY
# Enter a question
question = "I'm feeling sad and lonely"

result = search(model, question)
result = result[0].payload['text']


def get_response(input):
    result = search(model, input)
    result = result[0].payload['text']
    return result

st.set_page_config(page_title="RAG", page_icon="🧊", layout="wide")
st.title("UniSA Academic Support")

# with st.sidebar:
#     st.header("Settings")
#     st.text_input("Enter a website URL")

if 'conversation_ended' not in st.session_state:
    st.session_state['conversation_ended'] = False

if not st.session_state['conversation_ended']:
  with st.chat_message("AI"):
    st.write("Hi! I'm BrainHug AI, your supportive AI friend.")
    st.write("Feel free to chat with me at any time, just enter your question. If I can't answer, try rephrasing it again.")
    st.write("If you want to finish the conversation, just say BYE")

  user_q = st.chat_input("Start typing here")

  if user_q:
    if user_q.upper() == "BYE":
      st.session_state['conversation_ended'] = True
      with st.chat_message("AI"):
        st.write("Goodbye! Feel free to come back anytime.")
      st.stop()

    elif user_q is not None or user_q is "":
      response = get_response(user_q)
      with st.chat_message("Human"):
          st.write(user_q)
      with st.chat_message("AI"):
          st.write(response)
else:
    st.write("The conversation has ended. Refresh the page to start over.")