-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (44 loc) · 1.45 KB
/
Copy pathapp.py
File metadata and controls
70 lines (44 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from flask import Flask, render_template, request, jsonify
from faq_data import faqs
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
app = Flask(__name__)
# FAQ questions ko alag karna
questions = [faq["question"] for faq in faqs]
# TF-IDF vectorizer banana
vectorizer = TfidfVectorizer(stop_words="english")
faq_vectors = vectorizer.fit_transform(questions)
# User ke question ka answer find karna
def get_answer(user_question):
# User question ko vector mein convert karna
user_vector = vectorizer.transform([user_question])
# Similarity calculate karna
similarities = cosine_similarity(
user_vector,
faq_vectors
)
# Sabse similar question ka index
best_match_index = similarities.argmax()
# Similarity score
best_score = similarities[0][best_match_index]
# Agar koi suitable answer nahi mila
if best_score < 0.35:
return "Sorry, I don't understand your question."
# Best matching FAQ ka answer
return faqs[best_match_index]["answer"]
# Home page
@app.route("/")
def home():
return render_template("index.html")
# Chatbot API
@app.route("/ask", methods=["POST"])
def ask():
data = request.get_json()
user_question = data.get("question", "")
answer = get_answer(user_question)
return jsonify({
"answer": answer
})
# Application start
if __name__ == "__main__":
app.run(debug=True)