ManuelAlv commited on
Commit
7f9bcee
1 Parent(s): 83dc89e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from sentence_transformers import SentenceTransformer
3
+ from qdrant_client import models, QdrantClientS
4
+ import pandas as pd
5
+ from datasets import load_dataset
6
+
7
+ #************************************************************* LOAD DATA
8
+ data = load_dataset("ManuelAlv/academic_conuseling")
9
+
10
+ # Main dataset
11
+ bert_dataset = data['dataset'].to_pandas()
12
+
13
+ # Dataset used to test the chatbot
14
+ test_dataset = data['test'].to_pandas()
15
+ test_dataset.columns = ["test_question", "original_question"]
16
+
17
+ #************************************************************* Create Functions
18
+ # function to add values
19
+ def add_value(collection, key, value, id):
20
+ encoder = SentenceTransformer(collection)
21
+
22
+ qdrant.upsert(
23
+ collection_name = collection,
24
+ wait=True,
25
+ points = [
26
+ models.PointStruct(
27
+ id = id,
28
+ vector = encoder.encode(key).tolist(),
29
+ payload = {
30
+ 'text': value,
31
+ 'question': key
32
+ }
33
+ )
34
+ ]
35
+ )
36
+
37
+ # Function to search for a value
38
+ def search(collection, query):
39
+ search = qdrant.search(
40
+ collection_name = collection,
41
+ query_vector = encoder.encode(query).tolist(),
42
+ limit = 1
43
+ )
44
+ return search
45
+
46
+ #************************************************************* Create VD
47
+ # Create a local Vector Database
48
+ qdrant = QdrantClient(":memory:")
49
+
50
+ # Load the model
51
+ model = "all-MiniLM-L6-v2"
52
+ encoder = SentenceTransformer(model)
53
+
54
+ # Create a collection for the model with its embeddings
55
+ qdrant.recreate_collection(
56
+ collection_name = model,
57
+ vectors_config = models.VectorParams(
58
+ size = encoder.get_sentence_embedding_dimension(),
59
+ distance = models.Distance.COSINE
60
+ )
61
+ )
62
+
63
+ # Add the data to model
64
+ for index, row in bert_dataset.iterrows():
65
+ key = row['question']
66
+ value = row['answer']
67
+ id = index + 1
68
+
69
+ add_value(model, key, value, id)
70
+
71
+ # ************************************************************* QUERY
72
+ # Enter a question
73
+ question = "I'm feeling sad and lonely"
74
+
75
+ result = search(model, question)
76
+ result = result[0].payload['text']
77
+
78
+
79
+ def get_response(input):
80
+ result = search(model, input)
81
+ result = result[0].payload['text']
82
+ return result
83
+
84
+ st.set_page_config(page_title="RAG", page_icon="🧊", layout="wide")
85
+ st.title("UniSA Academic Support")
86
+
87
+ # with st.sidebar:
88
+ # st.header("Settings")
89
+ # st.text_input("Enter a website URL")
90
+
91
+ if 'conversation_ended' not in st.session_state:
92
+ st.session_state['conversation_ended'] = False
93
+
94
+ if not st.session_state['conversation_ended']:
95
+ with st.chat_message("AI"):
96
+ st.write("Hi! I'm BrainHug AI, your supportive AI friend.")
97
+ st.write("Feel free to chat with me at any time, just enter your question. If I can't answer, try rephrasing it again.")
98
+ st.write("If you want to finish the conversation, just say BYE")
99
+
100
+ user_q = st.chat_input("Start typing here")
101
+
102
+ if user_q:
103
+ if user_q.upper() == "BYE":
104
+ st.session_state['conversation_ended'] = True
105
+ with st.chat_message("AI"):
106
+ st.write("Goodbye! Feel free to come back anytime.")
107
+ st.stop()
108
+
109
+ elif user_q is not None or user_q is "":
110
+ response = get_response(user_q)
111
+ with st.chat_message("Human"):
112
+ st.write(user_q)
113
+ with st.chat_message("AI"):
114
+ st.write(response)
115
+ else:
116
+ st.write("The conversation has ended. Refresh the page to start over.")