File size: 3,147 Bytes
195b6e9
43305fa
 
195b6e9
43305fa
 
 
195b6e9
 
 
 
 
 
 
43305fa
195b6e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43305fa
195b6e9
43305fa
 
 
 
 
195b6e9
 
43305fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Step 1: Install necessary libraries (Handled by Hugging Face via 'requirements.txt')
import streamlit as st
import spacy
from spacy.cli import download  # To download the model programmatically
import numpy as np
from numpy.linalg import norm

# Step 2: Download the spaCy model if not already installed
try:
    nlp = spacy.load("en_core_web_md")
except OSError:
    st.warning("Downloading spaCy model 'en_core_web_md'. This may take a few minutes...")
    download("en_core_web_md")
    nlp = spacy.load("en_core_web_md")

# Step 3: Hardcode the FAQ data within the code
faqs = {
    'Admissions': [
        {
            'question': 'What is the process for admission into Saras AI Institute?',
            'answer': 'The admission process involves submitting the online application form, followed by a pre-enrollment assessment.'
        },
        {
            'question': 'Is there an application fee for applying to Saras AI Institute?',
            'answer': 'There is no application fee for applying to any program at Saras.'
        }
    ],
    'Curriculum and Faculty': [
        {
            'question': 'What is the curriculum like at Saras AI Institute?',
            'answer': 'The curriculum prepares students for roles like AI/ML Engineer, Data Scientist, and Gen AI Engineer.'
        },
        {
            'question': 'Do you also conduct LIVE sessions?',
            'answer': 'Yes, live sessions are conducted regularly to provide interactive learning and Q&A.'
        }
    ]
}

# Step 4: Precompute vectors for FAQ questions
faq_docs = []
for category, faq_list in faqs.items():
    for faq in faq_list:
        question = faq['question']
        answer = faq['answer']
        faq_vector = nlp(question).vector  # Precompute vector
        faq_docs.append((question, answer, faq_vector))

# Step 5: Define the function to find the most relevant FAQs
def find_most_relevant_faq_optimized(query, faq_docs):
    """Find the top 3 most relevant FAQs based on semantic similarity."""
    query_vector = nlp(query).vector

    # Calculate cosine similarity between query and each FAQ
    similarities = [
        (question, answer, np.dot(query_vector, faq_vector) / (norm(query_vector) * norm(faq_vector)))
        for question, answer, faq_vector in faq_docs
    ]

    # Sort by similarity score (highest first)
    similarities = sorted(similarities, key=lambda x: x[2], reverse=True)

    return similarities[:3]  # Return top 3 FAQs

# Step 6: Create the Streamlit UI
st.title("Smart FAQ Search - SARAS AI Institute")
st.markdown("### Find Answers to Your Questions Instantly")

# Text input for the user query
query = st.text_input("Enter your question here:")

if query:
    # Find the most relevant FAQs
    top_faqs = find_most_relevant_faq_optimized(query, faq_docs)

    # Display the results
    st.markdown("### Top Relevant FAQs:")
    for i, (question, answer, score) in enumerate(top_faqs, 1):
        st.write(f"**{i}. {question}**")
        st.write(f"*Answer:* {answer}")
        st.write(f"**Similarity Score:** {score:.2f}")
else:
    st.write("Please enter a query to search for relevant FAQs.")