FAQ_Model / app.py
krishuggingface's picture
Update app.py
a4b0377 verified
raw
history blame
5.61 kB
# Step 1: Install necessary libraries (Handled by Hugging Face via 'requirements.txt')
import streamlit as st
import spacy
from spacy.cli import download # For downloading the spaCy model
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
faqs = {
'Admissions': [
{'question': 'What is the process for admission into Saras AI Institute?',
'answer': 'The admission process at Saras AI Institute typically involves submitting the online application form along with necessary details, followed by a quick pre-enrollment assessment to evaluate your candidature based on your personal traits and basic communication skills in English.'},
{'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.'},
{'question': 'What is pre-enrollment assessment test? How do I prepare for it?',
'answer': 'It is a fully online assessment which takes less than 15 minutes. It evaluates your personal traits and basic English communication skills. No specific preparation is required.'},
{'question': 'Are there any specific requirements or prerequisites for admission into the programs?',
'answer': 'You need basic mathematical proficiency and English communication skills to join the programs. Math scores from high school or beyond can demonstrate your readiness.'},
{'question': 'When is the deadline for submitting the application?',
'answer': 'The deadline for submitting applications is 5th August 2024.'}
],
'Curriculum and Faculty': [
{'question': 'What is the curriculum like at Saras AI Institute?',
'answer': 'The curriculum imparts both technical and human skills, preparing students for roles such as AI/ML Engineer, Data Scientist, and Gen AI Engineer.'},
{'question': 'What does the program structure look like, and how is the curriculum delivered?',
'answer': 'Each year is divided into 5 semesters of 8 weeks. The program includes a mix of recorded and live sessions.'},
{'question': 'Do you also conduct LIVE sessions?',
'answer': 'Yes, live sessions provide interactive learning and Q&A opportunities with instructors.'},
{'question': 'Can I transfer credits earned at other universities to Saras AI Institute?',
'answer': 'Yes, relevant credits can be transferred after evaluation.'}
],
'Accreditation & Recognition': [
{'question': 'Is Saras AI Institute accredited?',
'answer': 'Not yet. This is our first enrollment cycle, and accreditation takes time.'},
{'question': 'Are the degree programs recognized by the government?',
'answer': 'Yes, we are a state-approved degree-granting institute based in the U.S.'}
],
'Career Services': [
{'question': 'Does Saras AI Institute offer employment support?',
'answer': 'Yes, we provide comprehensive employment support, including job placement services and interview preparation.'},
{'question': ' Does the university offer internship placement assistance?',
'answer': 'Yes, we assist students in finding internships through employer connections.'}
],
'Tuition fee and Scholarships': [
{'question': 'Does Saras AI Institute offer any scholarships for students?',
'answer': 'Yes, scholarships are available based on academic merit and financial need.'},
{'question': 'What are the tuition fees for your courses?',
'answer': "You can find detailed information on the 'Programs' page on our website."}
]
}
# 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.")