Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
|
|
@@ -9,121 +9,108 @@ API_URL = "https://dooratre-xx-claude-4-5.hf.space/chat"
|
|
| 9 |
chat_history = []
|
| 10 |
|
| 11 |
HTML_PAGE = """
|
| 12 |
-
<!DOCTYPE html>
|
| 13 |
<html>
|
| 14 |
<head>
|
| 15 |
-
<title>AI
|
| 16 |
-
|
|
|
|
| 17 |
body {
|
| 18 |
background-color: black;
|
| 19 |
-
color: #
|
| 20 |
-
font-family:
|
| 21 |
margin: 0;
|
| 22 |
padding: 0;
|
| 23 |
}
|
| 24 |
|
| 25 |
#chat {
|
| 26 |
-
height:
|
| 27 |
-
overflow
|
| 28 |
-
|
| 29 |
-
border-bottom: 1px solid #00ff00;
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
.message {
|
| 33 |
-
margin-bottom: 10px;
|
| 34 |
-
white-space: pre-wrap;
|
| 35 |
-
}
|
| 36 |
-
|
| 37 |
-
.user { color: #00ffff; }
|
| 38 |
-
.ai { color: #00ff00; }
|
| 39 |
-
|
| 40 |
-
#input-area {
|
| 41 |
padding: 10px;
|
| 42 |
}
|
| 43 |
|
| 44 |
textarea {
|
| 45 |
-
width:
|
| 46 |
height: 80px;
|
| 47 |
background: black;
|
| 48 |
-
color: #
|
| 49 |
-
border: 1px solid #
|
| 50 |
-
font-family:
|
| 51 |
font-size: 16px;
|
| 52 |
-
resize: none;
|
| 53 |
}
|
| 54 |
|
| 55 |
-
button {
|
| 56 |
-
margin-top: 5px;
|
| 57 |
width: 100%;
|
| 58 |
background: black;
|
| 59 |
-
color: #
|
| 60 |
-
border: 1px solid #
|
| 61 |
-
padding:
|
| 62 |
-
font-family:
|
| 63 |
-
cursor: pointer;
|
| 64 |
}
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
}
|
| 69 |
</style>
|
| 70 |
-
</head>
|
| 71 |
-
|
| 72 |
-
<body>
|
| 73 |
-
|
| 74 |
-
<div id="chat"></div>
|
| 75 |
-
|
| 76 |
-
<div id="input-area">
|
| 77 |
-
<textarea id="message" placeholder="Type your message..."></textarea>
|
| 78 |
-
<button onclick="sendMessage()">SEND</button>
|
| 79 |
-
</div>
|
| 80 |
-
|
| 81 |
-
<script>
|
| 82 |
-
const textarea = document.getElementById("message");
|
| 83 |
-
|
| 84 |
-
// ENTER = new line only (do NOT send)
|
| 85 |
-
textarea.addEventListener("keydown", function(e) {
|
| 86 |
-
if (e.key === "Enter" && !e.shiftKey) {
|
| 87 |
-
e.preventDefault();
|
| 88 |
-
textarea.value += "\\n";
|
| 89 |
-
}
|
| 90 |
-
});
|
| 91 |
|
| 92 |
-
|
| 93 |
-
textarea.addEventListener("focus", function() {
|
| 94 |
-
setTimeout(() => {
|
| 95 |
-
window.scrollTo(0, document.body.scrollHeight);
|
| 96 |
-
}, 300);
|
| 97 |
-
});
|
| 98 |
|
| 99 |
function addMessage(text, className) {
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
div.className =
|
| 103 |
-
div.
|
| 104 |
chat.appendChild(div);
|
| 105 |
chat.scrollTop = chat.scrollHeight;
|
| 106 |
}
|
| 107 |
|
| 108 |
function sendMessage() {
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
addMessage("YOU: " + message, "user");
|
| 113 |
textarea.value = "";
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
}
|
|
|
|
| 125 |
</script>
|
| 126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
</body>
|
| 128 |
</html>
|
| 129 |
"""
|
|
@@ -132,10 +119,14 @@ function sendMessage() {
|
|
| 132 |
def index():
|
| 133 |
return HTML_PAGE
|
| 134 |
|
|
|
|
| 135 |
@app.route("/chat", methods=["POST"])
|
| 136 |
def chat():
|
| 137 |
global chat_history
|
| 138 |
-
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
chat_history.append({"role": "user", "content": user_message})
|
| 141 |
|
|
@@ -148,14 +139,13 @@ def chat():
|
|
| 148 |
|
| 149 |
try:
|
| 150 |
response = requests.post(API_URL, json=payload, timeout=120)
|
| 151 |
-
response.raise_for_status()
|
| 152 |
assistant_reply = response.json().get("assistant_response", "No response.")
|
| 153 |
except Exception as e:
|
| 154 |
assistant_reply = "ERROR: " + str(e)
|
| 155 |
|
| 156 |
chat_history.append({"role": "assistant", "content": assistant_reply})
|
| 157 |
|
| 158 |
-
return
|
| 159 |
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
|
|
|
| 1 |
+
from flask import Flask, request
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
|
|
|
|
| 9 |
chat_history = []
|
| 10 |
|
| 11 |
HTML_PAGE = """
|
|
|
|
| 12 |
<html>
|
| 13 |
<head>
|
| 14 |
+
<title>AI TERMINAL</title>
|
| 15 |
+
|
| 16 |
+
<style type="text/css">
|
| 17 |
body {
|
| 18 |
background-color: black;
|
| 19 |
+
color: #00FF00;
|
| 20 |
+
font-family: Courier;
|
| 21 |
margin: 0;
|
| 22 |
padding: 0;
|
| 23 |
}
|
| 24 |
|
| 25 |
#chat {
|
| 26 |
+
height: 400px;
|
| 27 |
+
overflow: auto;
|
| 28 |
+
border-bottom: 1px solid #00FF00;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
padding: 10px;
|
| 30 |
}
|
| 31 |
|
| 32 |
textarea {
|
| 33 |
+
width: 98%;
|
| 34 |
height: 80px;
|
| 35 |
background: black;
|
| 36 |
+
color: #00FF00;
|
| 37 |
+
border: 1px solid #00FF00;
|
| 38 |
+
font-family: Courier;
|
| 39 |
font-size: 16px;
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
+
input[type=button] {
|
|
|
|
| 43 |
width: 100%;
|
| 44 |
background: black;
|
| 45 |
+
color: #00FF00;
|
| 46 |
+
border: 1px solid #00FF00;
|
| 47 |
+
padding: 8px;
|
| 48 |
+
font-family: Courier;
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
+
.user { color: #00FFFF; }
|
| 52 |
+
.ai { color: #00FF00; }
|
|
|
|
| 53 |
</style>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
<script type="text/javascript">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
function addMessage(text, className) {
|
| 58 |
+
var chat = document.getElementById("chat");
|
| 59 |
+
var div = document.createElement("div");
|
| 60 |
+
div.className = className;
|
| 61 |
+
div.innerHTML = text.replace(/\\n/g, "<br>");
|
| 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 = "";
|
| 76 |
|
| 77 |
+
var xhr = new XMLHttpRequest();
|
| 78 |
+
xhr.open("POST", "/chat", true);
|
| 79 |
+
xhr.setRequestHeader("Content-type", "application/json");
|
| 80 |
+
|
| 81 |
+
xhr.onreadystatechange = function() {
|
| 82 |
+
if (xhr.readyState == 4 && xhr.status == 200) {
|
| 83 |
+
var response = JSON.parse(xhr.responseText);
|
| 84 |
+
addMessage("AI: " + response.reply, "ai");
|
| 85 |
+
}
|
| 86 |
+
};
|
| 87 |
+
|
| 88 |
+
xhr.send(JSON.stringify({message: message}));
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
function handleKey(e) {
|
| 92 |
+
var key = e.keyCode || e.which;
|
| 93 |
+
|
| 94 |
+
if (key == 13) {
|
| 95 |
+
// ENTER pressed -> new line only
|
| 96 |
+
return true;
|
| 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>
|
| 113 |
+
|
| 114 |
</body>
|
| 115 |
</html>
|
| 116 |
"""
|
|
|
|
| 119 |
def index():
|
| 120 |
return HTML_PAGE
|
| 121 |
|
| 122 |
+
|
| 123 |
@app.route("/chat", methods=["POST"])
|
| 124 |
def chat():
|
| 125 |
global chat_history
|
| 126 |
+
|
| 127 |
+
data = request.data
|
| 128 |
+
parsed = json.loads(data)
|
| 129 |
+
user_message = parsed.get("message")
|
| 130 |
|
| 131 |
chat_history.append({"role": "user", "content": user_message})
|
| 132 |
|
|
|
|
| 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 |
+
return json.dumps({"reply": assistant_reply})
|
| 149 |
|
| 150 |
|
| 151 |
if __name__ == "__main__":
|