File size: 2,404 Bytes
3a93500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st 
from sentence_transformers import SentenceTransformer
import faiss
import pandas as pd
import numpy as np
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
df = pd.read_csv('course_data.csv',index_col=0)
courses = df.to_dict('records')
descriptions = [course['Content'] for course in courses]
embeddings = model.encode(descriptions)
index = faiss.IndexFlatL2(embeddings.shape[1])  # L2 distance index
index.add(np.array(embeddings))
# Function to simulate chatbot response (replace with your AI model logic)
def generate_response(query,k=5):
    # print(query)
    # Placeholder response logic (you can replace this with your model/API call)
    query_embedding = model.encode([query[-1]])  # Encode the user query
    # Search in FAISS index for the closest matches
    D, I = index.search(np.array(query_embedding), k=k)  # k is the number of top results
    # Retrieve course titles based on the search results
    results = []
    desc = []
    for idx in I[0]:
        course_title = courses[idx]['Course_Name']  # Get the course title
        desc.append(courses[idx]['Content'])
        results.append(course_title)
    # output=''
    # for i,j in enumerate(list(set(results))):
    #     output+=str(i+1)+j+'\n'
    return list(set(results))


# Define session state variables
if 'messages' not in st.session_state:
    st.session_state.messages = []
if 'mess' not in st.session_state:
    st.session_state.mess=[]


if st.sidebar.button("RESET"):
    st.session_state.messages=[]
    st.session_state.mess=[]
    
# User input
st.title('Analytics Vidhya Course Finder')
user_input = st.chat_input('Write your message here...')

if user_input:
    # Append user input to messages
    st.session_state.messages.append({"role": "user", "content": user_input})
    st.session_state.mess+=[user_input]
    # Generate chatbot response
    bot_response = generate_response(st.session_state.mess)
    st.session_state.messages.append({"role": "bot", "content": bot_response})

# Display chat messages in correct order
for message in st.session_state.messages:
    if message["role"] == "user":
        with st.chat_message("human"):
            st.write(message['content'])
    else:
        with st.chat_message("ai"):
            for i in message['content']:
                st.write('* '+i)