File size: 4,554 Bytes
167879f e738f00 305d870 5492f08 167879f ed64977 7e534ac ad710e4 e738f00 167879f b152e75 e738f00 b152e75 e738f00 b152e75 e738f00 b152e75 e738f00 b152e75 e738f00 b152e75 e738f00 b152e75 e738f00 167879f b152e75 e738f00 b152e75 e738f00 b152e75 167879f e738f00 167879f e738f00 b152e75 167879f e738f00 b152e75 167879f e738f00 b152e75 167879f e738f00 b152e75 167879f b152e75 e738f00 167879f e738f00 ad710e4 167879f e738f00 167879f b152e75 167879f e738f00 167879f b152e75 167879f e738f00 167879f e738f00 167879f e738f00 167879f e738f00 167879f e738f00 | 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | from flask import Flask, request, session, render_template_string, jsonify
import requests
import json
app = Flask(__name__)
app.secret_key = "retro_terminal_ai_chat_2026" # required for sessions
API_URL = "https://corvo-ai-xx-claude-4-5.hf.space/chat"
HTML_PAGE = """
<html>
<head>
<title>AI Terminal Chat</title>
<style type="text/css">
body {
background-color: black;
color: #00FF00;
font-family: Courier;
margin: 0;
padding: 0;
}
#chat {
height: 400px;
overflow: auto;
border-bottom: 1px solid #00FF00;
padding: 10px;
}
textarea {
width: 98%;
height: 80px;
background: black;
color: #00FF00;
border: 1px solid #00FF00;
font-family: Courier;
font-size: 16px;
}
input[type=button] {
width: 100%;
background: black;
color: #00FF00;
border: 1px solid #00FF00;
padding: 8px;
font-family: Courier;
}
.user, .ai {
white-space: pre-wrap; /* preserves indentation and spaces */
}
.user { color: #00FFFF; }
.ai { color: #00FF00; }
</style>
<script type="text/javascript">
function addMessage(text, className) {
var chat = document.getElementById("chat");
var div = document.createElement("div");
div.className = className;
div.textContent = text; // preserve spaces and indentation
chat.appendChild(div);
chat.scrollTop = chat.scrollHeight;
}
// ENTER = new line only, SEND = send
function handleKey(e) {
var key = e.keyCode || e.which;
if (key == 13 && !e.shiftKey) {
e.preventDefault();
var textarea = document.getElementById("message");
textarea.value += "\\n";
}
}
function sendMessage() {
var textarea = document.getElementById("message");
var message = textarea.value.trim();
if (message == "") return;
addMessage("YOU: " + message, "user");
textarea.value = "";
var xhr = new XMLHttpRequest();
xhr.open("POST", "/chat", true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
addMessage("AI: " + response.reply, "ai");
}
};
xhr.send(JSON.stringify({message: message}));
}
// Load previous history from server
function loadHistory() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/history", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
for (var i=0; i<response.length; i++) {
var entry = response[i];
addMessage(entry.role.toUpperCase() + ": " + entry.content,
entry.role=="user"?"user":"ai");
}
}
};
xhr.send();
}
window.onload = function() {
loadHistory();
};
</script>
</head>
<body>
<div id="chat"></div>
<div style="padding:10px;">
<textarea id="message" onkeydown="handleKey(event)" placeholder="Type your message..."></textarea>
<br>
<input type="button" value="SEND" onclick="sendMessage()">
</div>
</body>
</html>
"""
# Serve main page
@app.route("/")
def index():
return render_template_string(HTML_PAGE)
# Get chat history for session
@app.route("/history")
def history():
chat_history = session.get("chat_history", [])
return jsonify(chat_history)
# Chat API
@app.route("/chat", methods=["POST"])
def chat():
user_message = request.json.get("message")
if not user_message:
return jsonify({"reply": "No message received."})
# Initialize session chat if not exists
if "chat_history" not in session:
session["chat_history"] = []
chat_history = session["chat_history"]
# Append user message
chat_history.append({"role": "user", "content": user_message})
payload = {
"chat_history": chat_history,
"temperature": 0.1,
"top_p": 0.95,
"max_tokens": 2000
}
try:
response = requests.post(API_URL, json=payload, timeout=120)
response.raise_for_status()
assistant_reply = response.json().get("assistant_response", "No response.")
except Exception as e:
assistant_reply = "ERROR: " + str(e)
# Append AI reply
chat_history.append({"role": "assistant", "content": assistant_reply})
# Save back to session
session["chat_history"] = chat_history
return jsonify({"reply": assistant_reply})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860) |