-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
45 lines (38 loc) · 1.39 KB
/
app.py
File metadata and controls
45 lines (38 loc) · 1.39 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
import json
from sentence_transformers import SentenceTransformer, util
from flask import Flask, request, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# Load the model
model = SentenceTransformer('all-MiniLM-L6-v2')
# Load JSON data
with open('resume_data.json') as f:
data = json.load(f)
def get_response(user_input):
user_input = user_input.lower() # Normalize user input
user_embedding = model.encode(user_input)
responses = [
(util.cos_sim(user_embedding, model.encode(pattern.lower())).item(), item["response"]) # Normalize patterns
for item in data["questions"]
for pattern in item["patterns"]
]
best_response = max(responses, key=lambda x: x[0])
return best_response[1] if best_response[0] > 0.5 else "Sorry, I couldn't understand that."
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.json.get('input')
response = get_response(user_input)
return jsonify({'response': response})
# def main():
# print("Welcome to the CLI Chatbot. Type 'exit' to quit.")
# while True:
# user_input = input("You: ")
# if user_input.lower() == 'exit':
# break
# answer = get_response(user_input)
# if not answer.strip():
# answer = "I'm not sure how to answer that."
# print(f"Bot: {answer}")
if __name__ == "__main__":
app.run(debug=True)