Spaces:
Build error
Build error
File size: 1,259 Bytes
8c99d5a | 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 | import json
import random
import spacy
from flask import Flask, request, jsonify, send_file
app = Flask(__name__)
nlp = spacy.load("en_core_web_sm")
with open("main.json", "r") as json_file:
data = json.load(json_file)
intents = data["intents"]
patterns = []
labels = []
for intent in intents:
intent_tag = intent["tag"]
for pattern in intent["patterns"]:
patterns.append(pattern.lower())
labels.append(intent_tag)
# Serving index.html from the same folder as app.py
@app.route('/')
def index():
return send_file('index.html')
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.json['user_message'].lower()
best_match_response = "I'm sorry, I don't understand."
best_match_score = 0
user_doc = nlp(user_input)
for intent in intents:
intent_tag = intent["tag"]
for pattern in intent["patterns"]:
pattern_doc = nlp(pattern.lower())
score = user_doc.similarity(pattern_doc)
if score > best_match_score:
best_match_score = score
best_match_response = random.choice(intent["responses"])
return jsonify({'bot_response': best_match_response})
if __name__ == '__main__':
app.run(debug=True)
|