Spaces:
Sleeping
Sleeping
edithram23
commited on
initial push
Browse files- course_data.csv +0 -0
- main.py +64 -0
- requirements.txt +3 -0
course_data.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|
main.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from sentence_transformers import SentenceTransformer
|
3 |
+
import faiss
|
4 |
+
import pandas as pd
|
5 |
+
import numpy as np
|
6 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
7 |
+
df = pd.read_csv('course_data.csv',index_col=0)
|
8 |
+
courses = df.to_dict('records')
|
9 |
+
descriptions = [course['Content'] for course in courses]
|
10 |
+
embeddings = model.encode(descriptions)
|
11 |
+
index = faiss.IndexFlatL2(embeddings.shape[1]) # L2 distance index
|
12 |
+
index.add(np.array(embeddings))
|
13 |
+
# Function to simulate chatbot response (replace with your AI model logic)
|
14 |
+
def generate_response(query,k=5):
|
15 |
+
# print(query)
|
16 |
+
# Placeholder response logic (you can replace this with your model/API call)
|
17 |
+
query_embedding = model.encode([query[-1]]) # Encode the user query
|
18 |
+
# Search in FAISS index for the closest matches
|
19 |
+
D, I = index.search(np.array(query_embedding), k=k) # k is the number of top results
|
20 |
+
# Retrieve course titles based on the search results
|
21 |
+
results = []
|
22 |
+
desc = []
|
23 |
+
for idx in I[0]:
|
24 |
+
course_title = courses[idx]['Course_Name'] # Get the course title
|
25 |
+
desc.append(courses[idx]['Content'])
|
26 |
+
results.append(course_title)
|
27 |
+
# output=''
|
28 |
+
# for i,j in enumerate(list(set(results))):
|
29 |
+
# output+=str(i+1)+j+'\n'
|
30 |
+
return list(set(results))
|
31 |
+
|
32 |
+
|
33 |
+
# Define session state variables
|
34 |
+
if 'messages' not in st.session_state:
|
35 |
+
st.session_state.messages = []
|
36 |
+
if 'mess' not in st.session_state:
|
37 |
+
st.session_state.mess=[]
|
38 |
+
|
39 |
+
|
40 |
+
if st.sidebar.button("RESET"):
|
41 |
+
st.session_state.messages=[]
|
42 |
+
st.session_state.mess=[]
|
43 |
+
|
44 |
+
# User input
|
45 |
+
st.title('Analytics Vidhya Course Finder')
|
46 |
+
user_input = st.chat_input('Write your message here...')
|
47 |
+
|
48 |
+
if user_input:
|
49 |
+
# Append user input to messages
|
50 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
51 |
+
st.session_state.mess+=[user_input]
|
52 |
+
# Generate chatbot response
|
53 |
+
bot_response = generate_response(st.session_state.mess)
|
54 |
+
st.session_state.messages.append({"role": "bot", "content": bot_response})
|
55 |
+
|
56 |
+
# Display chat messages in correct order
|
57 |
+
for message in st.session_state.messages:
|
58 |
+
if message["role"] == "user":
|
59 |
+
with st.chat_message("human"):
|
60 |
+
st.write(message['content'])
|
61 |
+
else:
|
62 |
+
with st.chat_message("ai"):
|
63 |
+
for i in message['content']:
|
64 |
+
st.write('* '+i)
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
faiss-cpu
|
2 |
+
streamlit
|
3 |
+
sentence_transformers
|