krishuggingface commited on
Commit
43305fa
·
verified ·
1 Parent(s): 93b4b34

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Step 1: Install the necessary libraries
2
+ # (Only needed locally; Hugging Face Spaces handles dependencies via 'requirements.txt')
3
+ # !pip install streamlit spacy numpy
4
+
5
+ import streamlit as st
6
+ import spacy
7
+ import numpy as np
8
+ import json
9
+ from numpy.linalg import norm
10
+
11
+ # Step 2: Load the spaCy model
12
+ nlp = spacy.load("en_core_web_md")
13
+
14
+ # Step 3: Load the FAQ data (ensure faqs.json is in the same directory)
15
+ with open('faqs.json', 'r') as f:
16
+ faqs = json.load(f)
17
+
18
+ # Step 4: Flatten the FAQ structure and precompute vectors
19
+ faq_docs = []
20
+ for category, faq_list in faqs.items():
21
+ for faq in faq_list:
22
+ question = faq['question']
23
+ answer = faq['answer']
24
+ faq_vector = nlp(question).vector # Precompute the vector
25
+ faq_docs.append((question, answer, faq_vector)) # Store question, answer, and vector
26
+
27
+ # Step 5: Define the function to find the most relevant FAQs
28
+ def find_most_relevant_faq_optimized(query, faq_docs):
29
+ """Find the top 3 most relevant FAQs based on semantic similarity."""
30
+ query_vector = nlp(query).vector
31
+
32
+ # Calculate cosine similarity between query and each FAQ
33
+ similarities = [
34
+ (question, answer, np.dot(query_vector, faq_vector) / (norm(query_vector) * norm(faq_vector)))
35
+ for question, answer, faq_vector in faq_docs
36
+ ]
37
+
38
+ # Sort by similarity score (highest first)
39
+ similarities = sorted(similarities, key=lambda x: x[2], reverse=True)
40
+
41
+ return similarities[:3] # Return top 3 FAQs
42
+
43
+ # Step 6: Create the Streamlit UI
44
+ st.title("Smart FAQ Search - SARAS AI Institute")
45
+ st.markdown("### Find Answers to Your Questions Instantly")
46
+
47
+ # Text input for the user query
48
+ query = st.text_input("Enter your question here:")
49
+
50
+ if query:
51
+ # Find the most relevant FAQs
52
+ top_faqs = find_most_relevant_faq_optimized(query, faq_docs)
53
+
54
+ # Display the results
55
+ st.markdown("### Top Relevant FAQs:")
56
+ for i, (question, answer, score) in enumerate(top_faqs, 1):
57
+ st.write(f"**{i}. {question}**")
58
+ st.write(f"*Answer:* {answer}")
59
+ st.write(f"**Similarity Score:** {score:.2f}")
60
+ else:
61
+ st.write("Please enter a query to search for relevant FAQs.")
62
+