callidus commited on
Commit
ba29aa6
Β·
verified Β·
1 Parent(s): e1b5f1d

Upload faq_system.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. faq_system.py +147 -0
faq_system.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # CodeBasics FAQ System
2
+ # Smart FAQ retrieval using TF-IDF and cosine similarity
3
+
4
+ import pandas as pd
5
+ from sklearn.feature_extraction.text import TfidfVectorizer
6
+ from sklearn.metrics.pairwise import cosine_similarity
7
+ import numpy as np
8
+
9
+ class CodeBasicsFAQ:
10
+ def __init__(self, csv_path='codebasics_faqs.csv'):
11
+ """Initialize FAQ system from CSV file"""
12
+ # Load FAQ data
13
+ encodings = ['utf-8', 'latin-1', 'iso-8859-1', 'cp1252']
14
+ df = None
15
+
16
+ for encoding in encodings:
17
+ try:
18
+ df = pd.read_csv(csv_path, encoding=encoding)
19
+ print(f"βœ… Loaded {len(df)} FAQs")
20
+ break
21
+ except:
22
+ continue
23
+
24
+ if df is None:
25
+ raise Exception("Could not load FAQ CSV")
26
+
27
+ self.questions = df['prompt'].tolist()
28
+ self.answers = df['response'].tolist()
29
+
30
+ # Create TF-IDF vectorizer
31
+ self.vectorizer = TfidfVectorizer(
32
+ lowercase=True,
33
+ stop_words='english',
34
+ ngram_range=(1, 2)
35
+ )
36
+
37
+ # Fit on all questions
38
+ self.question_vectors = self.vectorizer.fit_transform(self.questions)
39
+ print(f"βœ… FAQ System ready!")
40
+
41
+ def find_best_match(self, query, threshold=0.2):
42
+ """Find best matching FAQ"""
43
+ query_vector = self.vectorizer.transform([query])
44
+ similarities = cosine_similarity(query_vector, self.question_vectors)[0]
45
+
46
+ best_idx = np.argmax(similarities)
47
+ best_score = similarities[best_idx]
48
+
49
+ if best_score >= threshold:
50
+ return {
51
+ 'question': self.questions[best_idx],
52
+ 'answer': self.answers[best_idx],
53
+ 'confidence': best_score
54
+ }
55
+ return None
56
+
57
+ def answer(self, query):
58
+ """Get answer for a query"""
59
+ result = self.find_best_match(query)
60
+
61
+ if result:
62
+ return {
63
+ 'status': 'success',
64
+ 'confidence': f"{result['confidence']*100:.1f}%",
65
+ 'matched_question': result['question'],
66
+ 'answer': result['answer']
67
+ }
68
+ else:
69
+ return {
70
+ 'status': 'no_match',
71
+ 'message': 'No matching FAQ found. Try rephrasing your question.'
72
+ }
73
+
74
+ def search_keyword(self, keyword):
75
+ """Search FAQs by keyword"""
76
+ keyword_lower = keyword.lower()
77
+ matches = []
78
+
79
+ for i, q in enumerate(self.questions):
80
+ if keyword_lower in q.lower() or keyword_lower in self.answers[i].lower():
81
+ matches.append({
82
+ 'question': q,
83
+ 'answer': self.answers[i]
84
+ })
85
+
86
+ return matches
87
+
88
+ def list_all_questions(self):
89
+ """Return all FAQ questions"""
90
+ return self.questions
91
+
92
+
93
+ # ============================================================================
94
+ # USAGE EXAMPLE
95
+ # ============================================================================
96
+
97
+ if __name__ == "__main__":
98
+ # Initialize
99
+ faq = CodeBasicsFAQ('codebasics_faqs.csv')
100
+
101
+ # Example questions
102
+ test_questions = [
103
+ "Can I take this bootcamp without programming experience?",
104
+ "Why should I trust Codebasics?",
105
+ "What are the prerequisites?",
106
+ "Do I need a laptop?"
107
+ ]
108
+
109
+ print("\n" + "="*70)
110
+ print("TESTING FAQ SYSTEM")
111
+ print("="*70 + "\n")
112
+
113
+ for question in test_questions:
114
+ print(f"❓ {question}")
115
+ result = faq.answer(question)
116
+
117
+ if result['status'] == 'success':
118
+ print(f"βœ… Match: {result['confidence']}")
119
+ print(f"πŸ“ Q: {result['matched_question']}")
120
+ print(f"πŸ’‘ A: {result['answer'][:100]}...\n")
121
+ else:
122
+ print(f"❌ {result['message']}\n")
123
+
124
+ # Interactive mode
125
+ print("\n" + "="*70)
126
+ print("INTERACTIVE MODE")
127
+ print("="*70)
128
+ print("Type 'quit' to exit\n")
129
+
130
+ while True:
131
+ user_q = input("❓ Your question: ").strip()
132
+
133
+ if user_q.lower() in ['quit', 'exit', 'q']:
134
+ print("πŸ‘‹ Goodbye!")
135
+ break
136
+
137
+ if not user_q:
138
+ continue
139
+
140
+ result = faq.answer(user_q)
141
+
142
+ if result['status'] == 'success':
143
+ print(f"\n[Confidence: {result['confidence']}]")
144
+ print(f"\nπŸ“Œ {result['matched_question']}")
145
+ print(f"\n✨ {result['answer']}\n")
146
+ else:
147
+ print(f"\n❌ {result['message']}\n")