-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_app.py
More file actions
65 lines (49 loc) · 2.48 KB
/
web_app.py
File metadata and controls
65 lines (49 loc) · 2.48 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
# app.py
import sys
from flask import Flask, render_template, request, jsonify
from ai_core import get_ai_response
# Import a basic placeholder function for the "speak" step
from voice_io import speak
# --- FLASK SETUP ---
app = Flask(__name__)
# This list will hold the conversation history to display in the UI
conversation_history = [
{"speaker": "ATLAS", "text": "System online. Hello. How may I assist you?"}
]
@app.route("/", methods=["GET"])
def index():
"""Renders the main chat interface, passing the history."""
return render_template("index.html", history=conversation_history)
@app.route("/chat", methods=["POST"])
def chat():
"""Handles the user's command, gets the AI response, and updates history."""
global conversation_history
# 1. Get user input from the HTML form
user_query = request.form.get("user_input")
if not user_query:
return jsonify({"success": False, "message": "No input provided."})
user_query = user_query.strip()
# 2. Check for exit command
if "exit" in user_query.lower() or "shutdown" in user_query.lower():
conversation_history.append({"speaker": "User", "text": user_query})
ai_answer = "Goodbye. Shutting down system."
conversation_history.append({"speaker": "ATLAS", "text": ai_answer})
# Note: We can't immediately exit a Flask app elegantly like this,
# but the conversation logic is complete.
return jsonify({"success": True, "answer": ai_answer, "history": conversation_history})
# 3. Process the query
conversation_history.append({"speaker": "User", "text": user_query})
# Call the AI core (which is now completely decoupled from I/O)
ai_answer = get_ai_response(user_query)
conversation_history.append({"speaker": "ATLAS", "text": ai_answer})
# The 'speak' function from voice_io now only prints to the console (backend)
speak(ai_answer)
# 4. Return the response to update the frontend
return jsonify({"success": True, "answer": ai_answer, "history": conversation_history})
if __name__ == '__main__':
# You will run the app using 'flask run' in the terminal
print("\n------------------------------------------------------------------")
print("ATLAS is ready! Open your browser to http://127.0.0.1:5000")
print("Use the terminal command: flask run")
print("------------------------------------------------------------------\n")
# app.run(debug=True) # Comment out, we use 'flask run'