Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,16 @@
|
|
| 1 |
-
from flask import Flask, request
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
|
|
|
| 6 |
|
| 7 |
API_URL = "https://dooratre-xx-claude-4-5.hf.space/chat"
|
| 8 |
|
| 9 |
-
chat_history = []
|
| 10 |
-
|
| 11 |
HTML_PAGE = """
|
| 12 |
<html>
|
| 13 |
<head>
|
| 14 |
-
<title>AI
|
| 15 |
-
|
| 16 |
<style type="text/css">
|
| 17 |
body {
|
| 18 |
background-color: black;
|
|
@@ -48,6 +46,10 @@ input[type=button] {
|
|
| 48 |
font-family: Courier;
|
| 49 |
}
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
.user { color: #00FFFF; }
|
| 52 |
.ai { color: #00FF00; }
|
| 53 |
</style>
|
|
@@ -58,18 +60,25 @@ function addMessage(text, className) {
|
|
| 58 |
var chat = document.getElementById("chat");
|
| 59 |
var div = document.createElement("div");
|
| 60 |
div.className = className;
|
| 61 |
-
div.
|
| 62 |
chat.appendChild(div);
|
| 63 |
chat.scrollTop = chat.scrollHeight;
|
| 64 |
}
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
function sendMessage() {
|
| 67 |
var textarea = document.getElementById("message");
|
| 68 |
-
var message = textarea.value;
|
| 69 |
-
|
| 70 |
-
if (message == "") {
|
| 71 |
-
return;
|
| 72 |
-
}
|
| 73 |
|
| 74 |
addMessage("YOU: " + message, "user");
|
| 75 |
textarea.value = "";
|
|
@@ -88,25 +97,36 @@ function sendMessage() {
|
|
| 88 |
xhr.send(JSON.stringify({message: message}));
|
| 89 |
}
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
}
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
</script>
|
| 101 |
|
| 102 |
</head>
|
| 103 |
-
|
| 104 |
<body>
|
| 105 |
|
| 106 |
<div id="chat"></div>
|
| 107 |
|
| 108 |
<div style="padding:10px;">
|
| 109 |
-
<textarea id="message" onkeydown="handleKey(event)"></textarea>
|
| 110 |
<br>
|
| 111 |
<input type="button" value="SEND" onclick="sendMessage()">
|
| 112 |
</div>
|
|
@@ -115,19 +135,33 @@ function handleKey(e) {
|
|
| 115 |
</html>
|
| 116 |
"""
|
| 117 |
|
|
|
|
| 118 |
@app.route("/")
|
| 119 |
def index():
|
| 120 |
-
return HTML_PAGE
|
|
|
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
|
|
|
|
|
|
| 123 |
@app.route("/chat", methods=["POST"])
|
| 124 |
def chat():
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
|
| 128 |
-
parsed = json.loads(data)
|
| 129 |
-
user_message = parsed.get("message")
|
| 130 |
|
|
|
|
| 131 |
chat_history.append({"role": "user", "content": user_message})
|
| 132 |
|
| 133 |
payload = {
|
|
@@ -139,13 +173,18 @@ def chat():
|
|
| 139 |
|
| 140 |
try:
|
| 141 |
response = requests.post(API_URL, json=payload, timeout=120)
|
|
|
|
| 142 |
assistant_reply = response.json().get("assistant_response", "No response.")
|
| 143 |
except Exception as e:
|
| 144 |
assistant_reply = "ERROR: " + str(e)
|
| 145 |
|
|
|
|
| 146 |
chat_history.append({"role": "assistant", "content": assistant_reply})
|
| 147 |
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
from flask import Flask, request, session, render_template_string, jsonify
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
+
app.secret_key = "retro_terminal_ai_chat_2026" # required for sessions
|
| 7 |
|
| 8 |
API_URL = "https://dooratre-xx-claude-4-5.hf.space/chat"
|
| 9 |
|
|
|
|
|
|
|
| 10 |
HTML_PAGE = """
|
| 11 |
<html>
|
| 12 |
<head>
|
| 13 |
+
<title>AI Terminal Chat</title>
|
|
|
|
| 14 |
<style type="text/css">
|
| 15 |
body {
|
| 16 |
background-color: black;
|
|
|
|
| 46 |
font-family: Courier;
|
| 47 |
}
|
| 48 |
|
| 49 |
+
.user, .ai {
|
| 50 |
+
white-space: pre-wrap; /* preserves indentation and spaces */
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
.user { color: #00FFFF; }
|
| 54 |
.ai { color: #00FF00; }
|
| 55 |
</style>
|
|
|
|
| 60 |
var chat = document.getElementById("chat");
|
| 61 |
var div = document.createElement("div");
|
| 62 |
div.className = className;
|
| 63 |
+
div.textContent = text; // preserve spaces and indentation
|
| 64 |
chat.appendChild(div);
|
| 65 |
chat.scrollTop = chat.scrollHeight;
|
| 66 |
}
|
| 67 |
|
| 68 |
+
// ENTER = new line only, SEND = send
|
| 69 |
+
function handleKey(e) {
|
| 70 |
+
var key = e.keyCode || e.which;
|
| 71 |
+
if (key == 13 && !e.shiftKey) {
|
| 72 |
+
e.preventDefault();
|
| 73 |
+
var textarea = document.getElementById("message");
|
| 74 |
+
textarea.value += "\\n";
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
function sendMessage() {
|
| 79 |
var textarea = document.getElementById("message");
|
| 80 |
+
var message = textarea.value.trim();
|
| 81 |
+
if (message == "") return;
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
addMessage("YOU: " + message, "user");
|
| 84 |
textarea.value = "";
|
|
|
|
| 97 |
xhr.send(JSON.stringify({message: message}));
|
| 98 |
}
|
| 99 |
|
| 100 |
+
// Load previous history from server
|
| 101 |
+
function loadHistory() {
|
| 102 |
+
var xhr = new XMLHttpRequest();
|
| 103 |
+
xhr.open("GET", "/history", true);
|
| 104 |
+
xhr.onreadystatechange = function() {
|
| 105 |
+
if (xhr.readyState == 4 && xhr.status == 200) {
|
| 106 |
+
var response = JSON.parse(xhr.responseText);
|
| 107 |
+
for (var i=0; i<response.length; i++) {
|
| 108 |
+
var entry = response[i];
|
| 109 |
+
addMessage(entry.role.toUpperCase() + ": " + entry.content,
|
| 110 |
+
entry.role=="user"?"user":"ai");
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
};
|
| 114 |
+
xhr.send();
|
| 115 |
}
|
| 116 |
|
| 117 |
+
window.onload = function() {
|
| 118 |
+
loadHistory();
|
| 119 |
+
};
|
| 120 |
+
|
| 121 |
</script>
|
| 122 |
|
| 123 |
</head>
|
|
|
|
| 124 |
<body>
|
| 125 |
|
| 126 |
<div id="chat"></div>
|
| 127 |
|
| 128 |
<div style="padding:10px;">
|
| 129 |
+
<textarea id="message" onkeydown="handleKey(event)" placeholder="Type your message..."></textarea>
|
| 130 |
<br>
|
| 131 |
<input type="button" value="SEND" onclick="sendMessage()">
|
| 132 |
</div>
|
|
|
|
| 135 |
</html>
|
| 136 |
"""
|
| 137 |
|
| 138 |
+
# Serve main page
|
| 139 |
@app.route("/")
|
| 140 |
def index():
|
| 141 |
+
return render_template_string(HTML_PAGE)
|
| 142 |
+
|
| 143 |
|
| 144 |
+
# Get chat history for session
|
| 145 |
+
@app.route("/history")
|
| 146 |
+
def history():
|
| 147 |
+
chat_history = session.get("chat_history", [])
|
| 148 |
+
return jsonify(chat_history)
|
| 149 |
|
| 150 |
+
|
| 151 |
+
# Chat API
|
| 152 |
@app.route("/chat", methods=["POST"])
|
| 153 |
def chat():
|
| 154 |
+
user_message = request.json.get("message")
|
| 155 |
+
if not user_message:
|
| 156 |
+
return jsonify({"reply": "No message received."})
|
| 157 |
+
|
| 158 |
+
# Initialize session chat if not exists
|
| 159 |
+
if "chat_history" not in session:
|
| 160 |
+
session["chat_history"] = []
|
| 161 |
|
| 162 |
+
chat_history = session["chat_history"]
|
|
|
|
|
|
|
| 163 |
|
| 164 |
+
# Append user message
|
| 165 |
chat_history.append({"role": "user", "content": user_message})
|
| 166 |
|
| 167 |
payload = {
|
|
|
|
| 173 |
|
| 174 |
try:
|
| 175 |
response = requests.post(API_URL, json=payload, timeout=120)
|
| 176 |
+
response.raise_for_status()
|
| 177 |
assistant_reply = response.json().get("assistant_response", "No response.")
|
| 178 |
except Exception as e:
|
| 179 |
assistant_reply = "ERROR: " + str(e)
|
| 180 |
|
| 181 |
+
# Append AI reply
|
| 182 |
chat_history.append({"role": "assistant", "content": assistant_reply})
|
| 183 |
|
| 184 |
+
# Save back to session
|
| 185 |
+
session["chat_history"] = chat_history
|
| 186 |
+
|
| 187 |
+
return jsonify({"reply": assistant_reply})
|
| 188 |
|
| 189 |
|
| 190 |
if __name__ == "__main__":
|