Spaces:
Sleeping
Sleeping
import streamlit as st | |
import spacy | |
from spacy.cli import download | |
import numpy as np | |
from numpy.linalg import norm | |
# Download spaCy model if not already installed | |
try: | |
nlp = spacy.load("en_core_web_md") | |
except OSError: | |
st.warning("Downloading the spaCy model. Please wait...") | |
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."} | |
] | |
} | |
# 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 | |
faq_docs.append((question, answer, faq_vector)) | |
def find_most_relevant_faq(query, faq_docs): | |
"""Find the most relevant FAQs based on cosine similarity.""" | |
query_vector = nlp(query).vector | |
similarities = [ | |
(question, answer, np.dot(query_vector, faq_vector) / (norm(query_vector) * norm(faq_vector))) | |
for question, answer, faq_vector in faq_docs | |
] | |
similarities = sorted(similarities, key=lambda x: x[2], reverse=True) | |
return similarities[:3] | |
# Enhanced Streamlit UI | |
st.set_page_config( | |
page_title="Smart FAQ Search - SARAS AI Institute", | |
page_icon="π", | |
layout="wide" | |
) | |
# Sidebar for Navigation | |
with st.sidebar: | |
st.image("https://via.placeholder.com/150", caption="Saras AI Institute") | |
st.title("FAQ Search") | |
st.markdown("### Navigate:") | |
st.markdown("1. **Ask a Question**") | |
st.markdown("2. **Explore FAQs by Category**") | |
st.markdown("---") | |
st.write("π§ Contact us: support@sarasai.edu") | |
# Main Header Section | |
st.title("π Smart FAQ Search") | |
st.markdown( | |
"<h4 style='color: #4CAF50;'>Find answers to your questions instantly!</h4>", | |
unsafe_allow_html=True | |
) | |
# Input section with a placeholder | |
query = st.text_input("π Ask a question:", placeholder="E.g., What is the admission process?") | |
# Display FAQs based on user query | |
if query: | |
st.markdown("---") | |
st.markdown("### π Top Relevant FAQs:") | |
top_faqs = find_most_relevant_faq(query, faq_docs) | |
for i, (question, answer, score) in enumerate(top_faqs, 1): | |
with st.expander(f"**{i}. {question}**"): | |
st.write(answer) | |
st.caption(f"Similarity Score: {score:.2f}") | |
else: | |
st.info("Enter a question above to find the most relevant FAQs.") | |
# Add an Explore Section with FAQ Categories | |
st.markdown("---") | |
st.markdown("### π Explore FAQs by Category") | |
for category, faq_list in faqs.items(): | |
with st.expander(f"**{category}**"): | |
for faq in faq_list: | |
st.write(f"**Q:** {faq['question']}") | |
st.write(f"**A:** {faq['answer']}") | |
# Footer Section | |
st.markdown("---") | |
st.markdown( | |
"<div style='text-align: center;'>" | |
"π¬ Need more help? Contact us at <a href='mailto:support@sarasai.edu'>support@sarasai.edu</a>." | |
"</div>", | |
unsafe_allow_html=True | |
) |