Gargaz commited on
Commit
78fe226
1 Parent(s): 468dc44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +245 -61
app.py CHANGED
@@ -1,64 +1,248 @@
1
- import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  )
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- demo.launch()
 
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)