| 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" |
|
|
| 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> |
| """ |
|
|
| |
| @app.route("/") |
| def index(): |
| return render_template_string(HTML_PAGE) |
|
|
|
|
| |
| @app.route("/history") |
| def history(): |
| chat_history = session.get("chat_history", []) |
| return jsonify(chat_history) |
|
|
|
|
| |
| @app.route("/chat", methods=["POST"]) |
| def chat(): |
| user_message = request.json.get("message") |
| if not user_message: |
| return jsonify({"reply": "No message received."}) |
|
|
| |
| if "chat_history" not in session: |
| session["chat_history"] = [] |
|
|
| chat_history = session["chat_history"] |
|
|
| |
| 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) |
|
|
| |
| chat_history.append({"role": "assistant", "content": assistant_reply}) |
|
|
| |
| session["chat_history"] = chat_history |
|
|
| return jsonify({"reply": assistant_reply}) |
|
|
|
|
| if __name__ == "__main__": |
| app.run(host="0.0.0.0", port=7860) |