-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
68 lines (54 loc) · 1.86 KB
/
Copy pathapp.py
File metadata and controls
68 lines (54 loc) · 1.86 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
"""
Flask Web Interface for ITER Bhubaneswar Chatbot
Simple web interface for testing the intelligent chatbot
"""
from flask import Flask, render_template, request, jsonify
from iter_chatbot import ITERChatbot
import os
app = Flask(__name__)
# Initialize chatbot
chatbot = None
def initialize_chatbot():
"""Initialize the chatbot globally"""
global chatbot
json_path = '/home/gaurav/Downloads/iter_bhubaneswar_knowledge.json'
if not os.path.exists(json_path):
print(f"Error: Knowledge base file not found at {json_path}")
return False
chatbot = ITERChatbot(json_path)
return chatbot.initialize()
@app.route('/')
def home():
"""Main page"""
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
"""Handle chat requests"""
if not chatbot:
return jsonify({'error': 'Chatbot not initialized'}), 500
data = request.get_json()
user_message = data.get('message', '').strip()
if not user_message:
return jsonify({'error': 'Empty message'}), 400
try:
response = chatbot.chat(user_message)
return jsonify({'response': response})
except Exception as e:
return jsonify({'error': f'Chat error: {str(e)}'}), 500
@app.route('/status')
def status():
"""Check chatbot status"""
return jsonify({
'status': 'ready' if chatbot else 'not initialized',
'message': 'Chatbot is ready!' if chatbot else 'Chatbot needs initialization'
})
if __name__ == '__main__':
# Create templates directory
os.makedirs('templates', exist_ok=True)
print("Initializing ITER Chatbot...")
if initialize_chatbot():
print("✅ Chatbot ready!")
print("🌐 Starting web interface...")
app.run(debug=True, host='0.0.0.0', port=5000)
else:
print("❌ Failed to initialize chatbot")