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23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 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 |
+
<!DOCTYPE html>
|
| 13 |
+
<html>
|
| 14 |
+
<head>
|
| 15 |
+
<title>AI Terminal Chat</title>
|
| 16 |
+
<style>
|
| 17 |
+
body {
|
| 18 |
+
background-color: black;
|
| 19 |
+
color: #00ff00;
|
| 20 |
+
font-family: "Courier New", monospace;
|
| 21 |
+
margin: 0;
|
| 22 |
+
padding: 0;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
#chat {
|
| 26 |
+
height: 80vh;
|
| 27 |
+
overflow-y: auto;
|
| 28 |
+
padding: 10px;
|
| 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: 100%;
|
| 46 |
+
height: 80px;
|
| 47 |
+
background: black;
|
| 48 |
+
color: #00ff00;
|
| 49 |
+
border: 1px solid #00ff00;
|
| 50 |
+
font-family: "Courier New", monospace;
|
| 51 |
+
font-size: 16px;
|
| 52 |
+
resize: none;
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
button {
|
| 56 |
+
margin-top: 5px;
|
| 57 |
+
width: 100%;
|
| 58 |
+
background: black;
|
| 59 |
+
color: #00ff00;
|
| 60 |
+
border: 1px solid #00ff00;
|
| 61 |
+
padding: 10px;
|
| 62 |
+
font-family: "Courier New", monospace;
|
| 63 |
+
cursor: pointer;
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
button:hover {
|
| 67 |
+
background: #003300;
|
| 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 |
+
// iPad friendly
|
| 93 |
+
textarea.addEventListener("focus", function() {
|
| 94 |
+
setTimeout(() => {
|
| 95 |
+
window.scrollTo(0, document.body.scrollHeight);
|
| 96 |
+
}, 300);
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
function addMessage(text, className) {
|
| 100 |
+
const chat = document.getElementById("chat");
|
| 101 |
+
const div = document.createElement("div");
|
| 102 |
+
div.className = "message " + className;
|
| 103 |
+
div.textContent = text;
|
| 104 |
+
chat.appendChild(div);
|
| 105 |
+
chat.scrollTop = chat.scrollHeight;
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
function sendMessage() {
|
| 109 |
+
const message = textarea.value.trim();
|
| 110 |
+
if (!message) return;
|
| 111 |
+
|
| 112 |
+
addMessage("YOU: " + message, "user");
|
| 113 |
+
textarea.value = "";
|
| 114 |
+
|
| 115 |
+
fetch("/chat", {
|
| 116 |
+
method: "POST",
|
| 117 |
+
headers: {"Content-Type": "application/json"},
|
| 118 |
+
body: JSON.stringify({message: message})
|
| 119 |
+
})
|
| 120 |
+
.then(res => res.json())
|
| 121 |
+
.then(data => {
|
| 122 |
+
addMessage("AI: " + data.reply, "ai");
|
| 123 |
+
});
|
| 124 |
+
}
|
| 125 |
+
</script>
|
| 126 |
+
|
| 127 |
+
</body>
|
| 128 |
+
</html>
|
| 129 |
+
"""
|
| 130 |
+
|
| 131 |
+
@app.route("/")
|
| 132 |
def index():
|
| 133 |
+
return HTML_PAGE
|
| 134 |
+
|
| 135 |
+
@app.route("/chat", methods=["POST"])
|
| 136 |
+
def chat():
|
| 137 |
+
global chat_history
|
| 138 |
+
user_message = request.json.get("message")
|
| 139 |
+
|
| 140 |
+
chat_history.append({"role": "user", "content": user_message})
|
| 141 |
+
|
| 142 |
+
payload = {
|
| 143 |
+
"chat_history": chat_history,
|
| 144 |
+
"temperature": 0.1,
|
| 145 |
+
"top_p": 0.95,
|
| 146 |
+
"max_tokens": 2000
|
| 147 |
+
}
|
| 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 jsonify({"reply": assistant_reply})
|
| 159 |
+
|
| 160 |
+
|
| 161 |
+
if __name__ == "__main__":
|
| 162 |
+
app.run(host="0.0.0.0", port=7860)
|