Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,14 @@
|
|
| 1 |
-
# Step 1: Install necessary libraries (Handled by Hugging Face via 'requirements.txt')
|
| 2 |
import streamlit as st
|
| 3 |
import spacy
|
| 4 |
-
from spacy.cli import download
|
| 5 |
import numpy as np
|
| 6 |
from numpy.linalg import norm
|
| 7 |
|
| 8 |
-
#
|
| 9 |
try:
|
| 10 |
nlp = spacy.load("en_core_web_md")
|
| 11 |
except OSError:
|
| 12 |
-
st.warning("Downloading spaCy model
|
| 13 |
download("en_core_web_md")
|
| 14 |
nlp = spacy.load("en_core_web_md")
|
| 15 |
|
|
@@ -57,47 +56,80 @@ faqs = {
|
|
| 57 |
]
|
| 58 |
}
|
| 59 |
|
| 60 |
-
#
|
| 61 |
faq_docs = []
|
| 62 |
for category, faq_list in faqs.items():
|
| 63 |
for faq in faq_list:
|
| 64 |
question = faq['question']
|
| 65 |
answer = faq['answer']
|
| 66 |
-
faq_vector = nlp(question).vector
|
| 67 |
faq_docs.append((question, answer, faq_vector))
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
"""Find the top 3 most relevant FAQs based on semantic similarity."""
|
| 72 |
query_vector = nlp(query).vector
|
| 73 |
-
|
| 74 |
-
# Calculate cosine similarity between query and each FAQ
|
| 75 |
similarities = [
|
| 76 |
(question, answer, np.dot(query_vector, faq_vector) / (norm(query_vector) * norm(faq_vector)))
|
| 77 |
for question, answer, faq_vector in faq_docs
|
| 78 |
]
|
| 79 |
-
|
| 80 |
-
# Sort by similarity score (highest first)
|
| 81 |
similarities = sorted(similarities, key=lambda x: x[2], reverse=True)
|
|
|
|
| 82 |
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
st.
|
| 87 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
if query:
|
| 93 |
-
|
| 94 |
-
|
|
|
|
| 95 |
|
| 96 |
-
# Display the results
|
| 97 |
-
st.markdown("### Top Relevant FAQs:")
|
| 98 |
for i, (question, answer, score) in enumerate(top_faqs, 1):
|
| 99 |
-
st.
|
| 100 |
-
|
| 101 |
-
|
| 102 |
else:
|
| 103 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import spacy
|
| 3 |
+
from spacy.cli import download
|
| 4 |
import numpy as np
|
| 5 |
from numpy.linalg import norm
|
| 6 |
|
| 7 |
+
# Download spaCy model if not already installed
|
| 8 |
try:
|
| 9 |
nlp = spacy.load("en_core_web_md")
|
| 10 |
except OSError:
|
| 11 |
+
st.warning("Downloading the spaCy model. Please wait...")
|
| 12 |
download("en_core_web_md")
|
| 13 |
nlp = spacy.load("en_core_web_md")
|
| 14 |
|
|
|
|
| 56 |
]
|
| 57 |
}
|
| 58 |
|
| 59 |
+
# Precompute vectors for FAQ questions
|
| 60 |
faq_docs = []
|
| 61 |
for category, faq_list in faqs.items():
|
| 62 |
for faq in faq_list:
|
| 63 |
question = faq['question']
|
| 64 |
answer = faq['answer']
|
| 65 |
+
faq_vector = nlp(question).vector
|
| 66 |
faq_docs.append((question, answer, faq_vector))
|
| 67 |
|
| 68 |
+
def find_most_relevant_faq(query, faq_docs):
|
| 69 |
+
"""Find the most relevant FAQs based on cosine similarity."""
|
|
|
|
| 70 |
query_vector = nlp(query).vector
|
|
|
|
|
|
|
| 71 |
similarities = [
|
| 72 |
(question, answer, np.dot(query_vector, faq_vector) / (norm(query_vector) * norm(faq_vector)))
|
| 73 |
for question, answer, faq_vector in faq_docs
|
| 74 |
]
|
|
|
|
|
|
|
| 75 |
similarities = sorted(similarities, key=lambda x: x[2], reverse=True)
|
| 76 |
+
return similarities[:3]
|
| 77 |
|
| 78 |
+
# Enhanced Streamlit UI
|
| 79 |
+
st.set_page_config(
|
| 80 |
+
page_title="Smart FAQ Search - SARAS AI Institute",
|
| 81 |
+
page_icon="π",
|
| 82 |
+
layout="wide"
|
| 83 |
+
)
|
| 84 |
|
| 85 |
+
# Sidebar for Navigation
|
| 86 |
+
with st.sidebar:
|
| 87 |
+
st.image("https://via.placeholder.com/150", caption="Saras AI Institute")
|
| 88 |
+
st.title("FAQ Search")
|
| 89 |
+
st.markdown("### Navigate:")
|
| 90 |
+
st.markdown("1. **Ask a Question**")
|
| 91 |
+
st.markdown("2. **Explore FAQs by Category**")
|
| 92 |
+
st.markdown("---")
|
| 93 |
+
st.write("π§ Contact us: support@sarasai.edu")
|
| 94 |
|
| 95 |
+
# Main Header Section
|
| 96 |
+
st.title("π Smart FAQ Search")
|
| 97 |
+
st.markdown(
|
| 98 |
+
"<h4 style='color: #4CAF50;'>Find answers to your questions instantly!</h4>",
|
| 99 |
+
unsafe_allow_html=True
|
| 100 |
+
)
|
| 101 |
|
| 102 |
+
# Input section with a placeholder
|
| 103 |
+
query = st.text_input("π Ask a question:", placeholder="E.g., What is the admission process?")
|
| 104 |
+
|
| 105 |
+
# Display FAQs based on user query
|
| 106 |
if query:
|
| 107 |
+
st.markdown("---")
|
| 108 |
+
st.markdown("### π Top Relevant FAQs:")
|
| 109 |
+
top_faqs = find_most_relevant_faq(query, faq_docs)
|
| 110 |
|
|
|
|
|
|
|
| 111 |
for i, (question, answer, score) in enumerate(top_faqs, 1):
|
| 112 |
+
with st.expander(f"**{i}. {question}**"):
|
| 113 |
+
st.write(answer)
|
| 114 |
+
st.caption(f"Similarity Score: {score:.2f}")
|
| 115 |
else:
|
| 116 |
+
st.info("Enter a question above to find the most relevant FAQs.")
|
| 117 |
+
|
| 118 |
+
# Add an Explore Section with FAQ Categories
|
| 119 |
+
st.markdown("---")
|
| 120 |
+
st.markdown("### π Explore FAQs by Category")
|
| 121 |
+
|
| 122 |
+
for category, faq_list in faqs.items():
|
| 123 |
+
with st.expander(f"**{category}**"):
|
| 124 |
+
for faq in faq_list:
|
| 125 |
+
st.write(f"**Q:** {faq['question']}")
|
| 126 |
+
st.write(f"**A:** {faq['answer']}")
|
| 127 |
+
|
| 128 |
+
# Footer Section
|
| 129 |
+
st.markdown("---")
|
| 130 |
+
st.markdown(
|
| 131 |
+
"<div style='text-align: center;'>"
|
| 132 |
+
"π¬ Need more help? Contact us at <a href='mailto:support@sarasai.edu'>support@sarasai.edu</a>."
|
| 133 |
+
"</div>",
|
| 134 |
+
unsafe_allow_html=True
|
| 135 |
+
)
|