-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
117 lines (92 loc) · 4.03 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
Flask Application for the Chatbot
==================================
This application serves as the backend for the AI chatbot. It handles user
queries, manages session history, and communicates with the query processing module.
Routes:
- `/`: Serves the home page.
- `/api/query`: Processes user queries and returns responses.
- `/new_conversation`: Resets the conversation history.
Dependencies:
- Flask
- Flask-Session
- Cachelib
"""
import os
from flask import Flask, request, jsonify, render_template, session
from flask_session import Session
from cachelib.file import FileSystemCache
from backend.query_data import main
# ─────────────────────────────────────────────────────────────────────────────
# Flask Application Setup
# ─────────────────────────────────────────────────────────────────────────────
app = Flask(
__name__,
template_folder="frontend/templates",
static_folder="frontend/static"
)
# Session configuration using cachelib
session_cache = FileSystemCache(
cache_dir="./.flask_session/",
threshold=100,
mode=0o600
)
app.config.update(
SESSION_TYPE='cachelib',
SESSION_CACHELIB=session_cache,
SESSION_PERMANENT=False,
SECRET_KEY=os.urandom(24)
)
Session(app)
# ─────────────────────────────────────────────────────────────────────────────
# Routes
# ─────────────────────────────────────────────────────────────────────────────
@app.route('/')
def home():
"""
Home Page Route
Serves the main HTML template.
"""
return render_template('index.html')
@app.route('/api/query', methods=['POST'])
def handle_query():
"""
API Route: Handle User Query
Receives a query from the user, processes it using the backend logic,
and returns the AI-generated response.
Request Body:
- `query` (str): The user's query text.
Response:
- JSON containing the bot's response and sources.
"""
data = request.get_json()
query_text = data.get('query', '').strip()
if not query_text:
return jsonify({'error': 'Invalid input'}), 400
# Retrieve or initialize conversation history from session
history = session.get('history', [])
# Append user query to history
history.append({"role": "user", "content": query_text})
# Process the query and get the bot's response
response_data = main(query_text, history)
# Append the bot's response to history
history.append({"role": "assistant", "content": response_data["response"]})
# Save updated history back to the session
session['history'] = history
# Return the bot's response
return jsonify(response_data)
@app.route('/new_conversation', methods=['POST'])
def new_conversation():
"""
API Route: Start a New Conversation
Clears the conversation history stored in the session.
Response:
- JSON confirmation message.
"""
session.pop('history', None)
return jsonify({"message": "New conversation started"})
# ─────────────────────────────────────────────────────────────────────────────
# Main Entry Point
# ─────────────────────────────────────────────────────────────────────────────
if __name__ == '__main__':
app.run(debug=True)