ayush2917 commited on
Commit
896a3b9
·
verified ·
1 Parent(s): 1ac26fd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import yaml
3
+ import json
4
+ try:
5
+ from src.retrieval import RetrievalSystem
6
+ from src.generation import ResponseGenerator
7
+ from src.history import HistoryManager
8
+ except ImportError as e:
9
+ print(f"Dependency error: {e}. Please ensure all dependencies are installed (pip install -r requirements.txt).")
10
+ raise
11
+
12
+ app = Flask(__name__)
13
+
14
+ # Load configuration
15
+ try:
16
+ with open("config/config.yaml", "r") as f:
17
+ config = yaml.safe_load(f)
18
+ except FileNotFoundError:
19
+ print("Configuration file config.yaml not found in config/ directory.")
20
+ raise
21
+
22
+ # Initialize components
23
+ try:
24
+ retrieval_system = RetrievalSystem(config["data"]["document_path"], config["retrieval"]["model"])
25
+ response_generator = ResponseGenerator(config["generation"]["model_name"])
26
+ history_manager = HistoryManager(config["data"]["history_path"])
27
+ except Exception as e:
28
+ print(f"Initialization error: {e}")
29
+ raise
30
+
31
+ # Quick replies for Rupeia categories
32
+ QUICK_REPLIES = [
33
+ {"title": "Investments", "value": "Tell me about investments"},
34
+ {"title": "Goals", "value": "How can I set financial goals?"},
35
+ {"title": "Benefits", "value": "What are the benefits of Rupeia?"},
36
+ {"title": "Gadgets", "value": "Help me save for a gadget"},
37
+ {"title": "News & Blogs", "value": "Show me financial news"}
38
+ ]
39
+
40
+ @app.route('/')
41
+ def index():
42
+ return render_template('index.html')
43
+
44
+ @app.route('/api/chat', methods=['POST'])
45
+ def chat():
46
+ try:
47
+ data = request.get_json()
48
+ user_input = data.get('message', '').strip()
49
+ if not user_input:
50
+ return jsonify({"text": "Please provide a message.", "quickReplies": []}), 400
51
+
52
+ # Load history
53
+ history = history_manager.load_history()
54
+
55
+ # Handle welcome message
56
+ if user_input.lower() == "hello":
57
+ response = "Hi there! How can I help you today?"
58
+ history.append(("hello", response))
59
+ history_manager.save_history(history)
60
+ return jsonify({"text": response, "quickReplies": QUICK_REPLIES})
61
+
62
+ # Retrieve context using RAG
63
+ context = retrieval_system.get_context(user_input)
64
+
65
+ # Generate response
66
+ response = response_generator.generate_response(user_input, context, history)
67
+
68
+ # Update history
69
+ history.append((user_input, response))
70
+ history_manager.save_history(history)
71
+
72
+ # Return response with quick replies
73
+ return jsonify({"text": response, "quickReplies": QUICK_REPLIES})
74
+
75
+ except Exception as e:
76
+ return jsonify({"text": f"Sorry, we're experiencing technical difficulties: {str(e)}", "quickReplies": []}), 500
77
+
78
+ if __name__ == "__main__":
79
+ app.run(host="0.0.0.0", port=8000, debug=True)