CORVO-AI commited on
Commit
e738f00
·
verified ·
1 Parent(s): a7b1d1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +157 -34
app.py CHANGED
@@ -1,39 +1,162 @@
1
- from flask import Flask, request, redirect, url_for
 
 
2
 
3
  app = Flask(__name__)
4
 
5
- # In-memory storage for chat messages
6
- messages = []
7
 
8
- # --- Home page ---
9
- @app.route('/', methods=['GET', 'POST'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def index():
11
- global messages
12
- html = "<html><body>"
13
- html += "<h2>Simple Chat Share</h2>"
14
-
15
- if request.method == 'POST':
16
- text = request.form.get('text')
17
- if text:
18
- messages.append(text)
19
- return redirect(url_for('index'))
20
-
21
- # Chat input form
22
- html += "<form method='POST'>"
23
- html += "Message:<br>"
24
- html += "<input type='text' name='text' style='width:300px'>"
25
- html += "<input type='submit' value='Send'>"
26
- html += "</form><hr>"
27
-
28
- # Display messages with copy button
29
- html += "<h3>Messages:</h3>"
30
- for i, msg in enumerate(messages[::-1], 1): # newest first
31
- # Using simple input box to allow easy copy/paste
32
- html += f"<p>{i}: <input type='text' value='{msg}' readonly style='width:300px'> <button onclick='this.previousElementSibling.select();document.execCommand(\"copy\");'>Copy</button></p>"
33
-
34
- html += "</body></html>"
35
- return html
36
-
37
- # --- Run the app ---
38
- if __name__ == '__main__':
39
- app.run(host='0.0.0.0', port=7860)
 
 
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)