forked from gauravjakhar/mlbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
76 lines (53 loc) · 2.04 KB
/
app.py
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
71
72
73
74
75
76
import os
import numpy as np
from flask import Flask, render_template, request, jsonify, send_from_directory
from pymongo import MongoClient
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
app = Flask(__name__)
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017")
db = client["bot_db"]
collection = db["bot_data"]
vectorizer = TfidfVectorizer()
# Function to retrieve content from MongoDB
def get_content_from_mongodb():
documents = collection.find()
content_list = [doc["content"] for doc in documents]
return content_list
# TF-IDF Vectorization
def vectorize_text(text_list):
vectors = vectorizer.fit_transform(text_list)
return vectors
# Function to get the most similar document based on user query
def get_most_similar_document(query, vectors, text_list):
query_vector = vectorizer.transform([query])
similarity_scores = cosine_similarity(query_vector, vectors)
most_similar_index = similarity_scores.argmax()
# Additional logging
predicted_answer = text_list[most_similar_index]
confidence_score = np.max(similarity_scores)
print(f"User Query: {query}")
print(f"Predicted Answer: {predicted_answer}")
print(f"Confidence Score: {confidence_score}")
return predicted_answer
# Serve static files (images in this case)
@app.route('/static/<path:filename>')
def static_files(filename):
root_dir = os.path.dirname(os.getcwd())
return send_from_directory(os.path.join(root_dir, 'static'), filename)
# Endpoint for the main chat page
@app.route('/')
def chat():
return render_template('chat.html')
# Endpoint for question answering
@app.route('/ask', methods=['POST'])
def ask_question():
data = request.get_json()
user_query = data['question']
content_list = get_content_from_mongodb()
vectors = vectorize_text(content_list)
answer = get_most_similar_document(user_query, vectors, content_list)
return jsonify({'answer': answer})
if __name__ == '__main__':
app.run(debug=True)