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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
-
if __name__ ==
|
64 |
-
|
|
|
1 |
+
from flask import Flask, request, Response, render_template
|
2 |
+
from huggingface_hub import hf_hub_download
|
3 |
+
from llama_cpp import Llama
|
4 |
+
|
5 |
+
app = Flask(__name__)
|
6 |
+
|
7 |
+
# HTML content as a string
|
8 |
+
HTML_CONTENT = '''
|
9 |
+
<!DOCTYPE html>
|
10 |
+
<html lang="en">
|
11 |
+
<head>
|
12 |
+
<meta charset="UTF-8">
|
13 |
+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
14 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
15 |
+
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Rounded:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
|
16 |
+
<link rel="preconnect" href="https://fonts.googleapis.com">
|
17 |
+
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
18 |
+
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300&display=swap" rel="stylesheet">
|
19 |
+
<title>AI Chat Interface</title>
|
20 |
+
<style>
|
21 |
+
*{
|
22 |
+
padding: 0;
|
23 |
+
margin: 0;
|
24 |
+
font-family: 'Poppins', sans-serif;
|
25 |
+
box-sizing: border-box;
|
26 |
+
}
|
27 |
+
body {
|
28 |
+
overflow: hidden;
|
29 |
+
}
|
30 |
+
|
31 |
+
/* Hide scrollbar only for Webkit browsers (Chrome, Safari, Opera) */
|
32 |
+
::-webkit-scrollbar {
|
33 |
+
display: none;
|
34 |
+
}
|
35 |
+
|
36 |
+
body{
|
37 |
+
width: 100%;
|
38 |
+
height: 100vh;
|
39 |
+
background-color: #212121;
|
40 |
+
}
|
41 |
+
|
42 |
+
.chat{
|
43 |
+
display: flex;
|
44 |
+
gap: 20px;
|
45 |
+
padding: 25px;
|
46 |
+
color: #fff;
|
47 |
+
font-size: 15px;
|
48 |
+
font-weight: 300;
|
49 |
+
}
|
50 |
+
|
51 |
+
.chat img{
|
52 |
+
width: 35px;
|
53 |
+
height: 35px;
|
54 |
+
border-radius: 50px;
|
55 |
+
}
|
56 |
+
|
57 |
+
.response{
|
58 |
+
background-color: #212121;
|
59 |
+
}
|
60 |
+
|
61 |
+
.messagebar{
|
62 |
+
position: fixed;
|
63 |
+
bottom: 0;
|
64 |
+
height: 5rem;
|
65 |
+
width: 100%;
|
66 |
+
display: flex;
|
67 |
+
align-items: center;
|
68 |
+
justify-content: center;
|
69 |
+
background-color: #212121;
|
70 |
+
}
|
71 |
+
|
72 |
+
.messagebar .bar-wrapper{
|
73 |
+
background-color: #2f2f2f;
|
74 |
+
border-radius: 20px;
|
75 |
+
width: 70vw;
|
76 |
+
padding: 10px;
|
77 |
+
display: flex;
|
78 |
+
align-items: center;
|
79 |
+
justify-content: space-between;
|
80 |
+
}
|
81 |
+
|
82 |
+
.bar-wrapper input{
|
83 |
+
width: 100%;
|
84 |
+
padding: 5px;
|
85 |
+
border: none;
|
86 |
+
outline: none;
|
87 |
+
font-size: 14px;
|
88 |
+
background: none;
|
89 |
+
color: #ccc;
|
90 |
+
}
|
91 |
+
|
92 |
+
.bar-wrapper input::placeholder{
|
93 |
+
color: #ccc;
|
94 |
+
}
|
95 |
+
|
96 |
+
.messagebar button{
|
97 |
+
display: flex;
|
98 |
+
align-items: center;
|
99 |
+
justify-content: center;
|
100 |
+
background: none;
|
101 |
+
border: none;
|
102 |
+
color: #fff;
|
103 |
+
cursor: pointer;
|
104 |
+
}
|
105 |
+
|
106 |
+
.message-box{
|
107 |
+
height: calc(100vh - 5rem);
|
108 |
+
overflow-y: auto;
|
109 |
+
}
|
110 |
+
</style>
|
111 |
+
</head>
|
112 |
+
<body>
|
113 |
+
<div class="chatbox-wrapper">
|
114 |
+
<div class="message-box" id="chat-container">
|
115 |
+
<div class="chat response">
|
116 |
+
<img src="https://freelogopng.com/images/all_img/1681038800chatgpt-logo-black.png" alt="AI">
|
117 |
+
<span>Hello there! <br>
|
118 |
+
How can I help you today.
|
119 |
+
</span>
|
120 |
+
</div>
|
121 |
+
</div>
|
122 |
+
<div class="messagebar">
|
123 |
+
<div class="bar-wrapper">
|
124 |
+
<input type="text" id="user-input" placeholder="Enter your message...">
|
125 |
+
<button onclick="sendMessage()">
|
126 |
+
<span class="material-symbols-rounded">
|
127 |
+
send
|
128 |
+
</span>
|
129 |
+
</button>
|
130 |
+
</div>
|
131 |
+
</div>
|
132 |
+
</div>
|
133 |
+
|
134 |
+
<script>
|
135 |
+
const messageBar = document.querySelector("#user-input");
|
136 |
+
const sendBtn = document.querySelector(".bar-wrapper button");
|
137 |
+
const messageBox = document.querySelector("#chat-container");
|
138 |
+
|
139 |
+
function addMessage(message, isUser) {
|
140 |
+
const messageElement = document.createElement('div');
|
141 |
+
messageElement.classList.add('chat');
|
142 |
+
if (!isUser) messageElement.classList.add('response');
|
143 |
+
|
144 |
+
const imgElement = document.createElement('img');
|
145 |
+
imgElement.src = isUser ? "https://wallpaperaccess.com/full/1595920.jpg" : "https://freelogopng.com/images/all_img/1681038800chatgpt-logo-black.png";
|
146 |
+
imgElement.alt = isUser ? "User" : "AI";
|
147 |
+
|
148 |
+
const spanElement = document.createElement('span');
|
149 |
+
spanElement.textContent = message;
|
150 |
+
|
151 |
+
messageElement.appendChild(imgElement);
|
152 |
+
messageElement.appendChild(spanElement);
|
153 |
+
|
154 |
+
messageBox.appendChild(messageElement);
|
155 |
+
messageBox.scrollTop = messageBox.scrollHeight;
|
156 |
+
}
|
157 |
+
|
158 |
+
function sendMessage() {
|
159 |
+
const message = messageBar.value.trim();
|
160 |
+
if (message) {
|
161 |
+
addMessage(message, true);
|
162 |
+
messageBar.value = '';
|
163 |
+
|
164 |
+
const eventSource = new EventSource(`/chat?message=${encodeURIComponent(message)}`);
|
165 |
+
let aiResponse = '';
|
166 |
+
|
167 |
+
eventSource.onmessage = function(event) {
|
168 |
+
if (event.data === '[DONE]') {
|
169 |
+
eventSource.close();
|
170 |
+
} else {
|
171 |
+
aiResponse += event.data;
|
172 |
+
const aiMessageElement = document.querySelector('.chat.response:last-child span');
|
173 |
+
if (aiMessageElement) {
|
174 |
+
aiMessageElement.textContent = aiResponse;
|
175 |
+
} else {
|
176 |
+
addMessage(aiResponse, false);
|
177 |
+
}
|
178 |
+
}
|
179 |
+
};
|
180 |
+
|
181 |
+
eventSource.onerror = function(error) {
|
182 |
+
console.error('EventSource failed:', error);
|
183 |
+
eventSource.close();
|
184 |
+
};
|
185 |
+
}
|
186 |
+
}
|
187 |
+
|
188 |
+
messageBar.addEventListener('keypress', function(event) {
|
189 |
+
if (event.key === 'Enter') {
|
190 |
+
sendMessage();
|
191 |
+
}
|
192 |
+
});
|
193 |
+
</script>
|
194 |
+
</body>
|
195 |
+
</html>
|
196 |
+
'''
|
197 |
+
|
198 |
+
def download_model():
|
199 |
+
model_name = "lmstudio-community/gemma-2-2b-it-GGUF"
|
200 |
+
model_file = "gemma-2-2b-it-Q6_K.gguf"
|
201 |
+
return hf_hub_download(model_name, filename=model_file)
|
202 |
+
|
203 |
+
def initialize_model(model_path):
|
204 |
+
return Llama(
|
205 |
+
model_path=model_path,
|
206 |
+
n_ctx=4096,
|
207 |
+
n_threads=4,
|
208 |
+
n_gpu_layers=-1 # Use GPU if available
|
209 |
+
)
|
210 |
+
|
211 |
+
model_path = download_model()
|
212 |
+
llm = initialize_model(model_path)
|
213 |
+
|
214 |
+
system_prompt = (
|
215 |
+
"You are a normal AI assistant. Your mission is to help people and respond clearly and friendly."
|
216 |
)
|
217 |
|
218 |
+
chat_history = [{"role": "system", "content": system_prompt}]
|
219 |
+
|
220 |
+
@app.route('/')
|
221 |
+
def index():
|
222 |
+
return HTML_CONTENT
|
223 |
+
|
224 |
+
@app.route('/chat')
|
225 |
+
def chat():
|
226 |
+
global chat_history
|
227 |
+
user_message = request.args.get('message', '')
|
228 |
+
chat_history.append({"role": "user", "content": user_message})
|
229 |
+
|
230 |
+
full_prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in chat_history])
|
231 |
+
full_prompt += "\nAssistant:"
|
232 |
+
|
233 |
+
def generate():
|
234 |
+
ai_response = ""
|
235 |
+
for token in llm(full_prompt, max_tokens=1000, stop=["User:"], stream=True):
|
236 |
+
chunk = token['choices'][0]['text']
|
237 |
+
if chunk:
|
238 |
+
ai_response += chunk
|
239 |
+
yield f"data: {chunk}\n\n"
|
240 |
+
chat_history.append({"role": "assistant", "content": ai_response.strip()})
|
241 |
+
if len(chat_history) > 10: # Limit history to last 10 messages
|
242 |
+
chat_history = chat_history[-10:]
|
243 |
+
yield "data: [DONE]\n\n"
|
244 |
+
|
245 |
+
return Response(generate(), content_type='text/event-stream')
|
246 |
|
247 |
+
if __name__ == '__main__':
|
248 |
+
app.run(debug=True, port=5000)
|