Bhaddy392 commited on
Commit
20e20b7
Β·
verified Β·
1 Parent(s): c0ac905

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +267 -100
app.py CHANGED
@@ -1,8 +1,241 @@
1
  import gradio as gr
2
  import torch
3
  from transformers import pipeline
4
- from thinking_btn import CUSTOM_CSS, THINKING_JS
5
- from logo import get_logo_html
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  pipe = pipeline(
8
  "text-generation",
@@ -19,22 +252,18 @@ def chat(message, history):
19
  message = message[:1000] + "... [texto cortado]"
20
 
21
  system_msg = (
22
- "VocΓͺ Γ© BrainGPT β€” uma inteligΓͺncia artificial poderosa, direta e precisa. "
23
- "Responda sempre em portuguΓͺs de forma clara, concisa e inteligente. "
24
- "Nunca invente factos. Se nΓ£o souber, diga claramente."
25
  )
26
 
27
  msgs = [{"role": "system", "content": system_msg}]
28
- for user_msg, bot_msg in history[-6:]:
29
- if user_msg:
30
- msgs.append({"role": "user", "content": user_msg})
31
- if bot_msg:
32
- msgs.append({"role": "assistant", "content": bot_msg})
33
  msgs.append({"role": "user", "content": message})
34
 
35
- prompt = pipe.tokenizer.apply_chat_template(
36
- msgs, tokenize=False, add_generation_prompt=True
37
- )
38
 
39
  if len(prompt) > 3500:
40
  return "Conversa muito longa. Clica em Limpar e recomeΓ§a."
@@ -49,10 +278,8 @@ def chat(message, history):
49
  truncation=True,
50
  )
51
 
52
- full = result[0]["generated_text"]
53
- resposta = full.split("<|im_start|>assistant\n")[-1].strip()
54
  resposta = resposta.replace("<|im_end|>", "").strip()
55
-
56
  return resposta if resposta else "Resposta vazia. Tenta reformular."
57
 
58
  except torch.cuda.OutOfMemoryError:
@@ -61,36 +288,32 @@ def chat(message, history):
61
  return f"Erro: {str(e)[:150]}"
62
 
63
 
64
- with gr.Blocks(
65
- css=CUSTOM_CSS,
66
- js=THINKING_JS,
67
- title="BrainGPT",
68
- theme=gr.themes.Base(
69
- primary_hue="orange",
70
- neutral_hue="stone",
71
- ),
72
- ) as demo:
 
73
 
74
- gr.HTML(get_logo_html("logo.png"))
 
 
 
75
 
76
  gr.HTML("""
77
  <div id="thinking-bar" class="thinking-bar">
78
- <div class="thinking-dots">
79
- <span></span><span></span><span></span>
80
- </div>
81
  <span>THINKING...</span>
82
  </div>
83
  """)
84
 
85
- chatbot = gr.Chatbot(
86
- label="",
87
- height=480,
88
- show_copy_button=True,
89
- bubble_full_width=False,
90
- avatar_images=(None, "logo.png"),
91
- )
92
 
93
- with gr.Row(elem_classes="input-row"):
94
  msg = gr.Textbox(
95
  placeholder="Escreve a tua mensagem...",
96
  show_label=False,
@@ -99,73 +322,17 @@ with gr.Blocks(
99
  elem_id="msg-input",
100
  scale=8,
101
  )
102
- send_btn = gr.Button(
103
- "Enviar",
104
- elem_id="send-btn",
105
- variant="primary",
106
- scale=2,
107
- )
108
- clear_btn = gr.Button(
109
- "Limpar",
110
- elem_id="clear-btn",
111
- variant="secondary",
112
- scale=1,
113
- )
114
 
115
  gr.Examples(
116
- examples=[
117
- "Quem es tu?",
118
- "Explica inteligencia artificial em 3 frases",
119
- "Diferenca entre machine learning e deep learning?",
120
- "5 dicas para ser mais produtivo",
121
- "Como funciona o Bitcoin?",
122
- ],
123
  inputs=msg,
124
  label="Exemplos",
125
  )
126
 
127
- def user_submit(message, history):
128
- if not message.strip():
129
- return "", history
130
- history = history + [[message, None]]
131
- return "", history
132
-
133
- def bot_respond(history):
134
- if not history or history[-1][1] is not None:
135
- return history
136
- message = history[-1][0]
137
- response = chat(message, history[:-1])
138
- history[-1][1] = response
139
- return history
140
-
141
- msg.submit(
142
- user_submit,
143
- inputs=[msg, chatbot],
144
- outputs=[msg, chatbot],
145
- queue=False,
146
- ).then(
147
- bot_respond,
148
- inputs=[chatbot],
149
- outputs=[chatbot],
150
- )
151
-
152
- send_btn.click(
153
- user_submit,
154
- inputs=[msg, chatbot],
155
- outputs=[msg, chatbot],
156
- queue=False,
157
- ).then(
158
- bot_respond,
159
- inputs=[chatbot],
160
- outputs=[chatbot],
161
- )
162
-
163
- clear_btn.click(
164
- lambda: ([], ""),
165
- outputs=[chatbot, msg],
166
- queue=False,
167
- )
168
 
169
- if __name__ == "__main__":
170
- demo.queue(max_size=5).launch()
171
-
 
1
  import gradio as gr
2
  import torch
3
  from transformers import pipeline
4
+ import base64
5
+ import os
6
+
7
+ def get_logo_html():
8
+ if os.path.exists("logo.png"):
9
+ with open("logo.png", "rb") as f:
10
+ b64 = base64.b64encode(f.read()).decode("utf-8")
11
+ img = f'<img src="data:image/png;base64,{b64}" style="width:64px;height:64px;border-radius:12px;box-shadow:0 0 24px #ff6a0055;">'
12
+ else:
13
+ img = ""
14
+ return f"""
15
+ <div class="braingpt-header">
16
+ {img}
17
+ <div>
18
+ <div class="braingpt-title">BrainGPT</div>
19
+ <div class="braingpt-sub">NEURAL ENGINE ONLINE - POWERED BY QWEN2</div>
20
+ </div>
21
+ </div>
22
+ """
23
+
24
+ CSS = """
25
+ @import url('https://fonts.googleapis.com/css2?family=Rajdhani:wght@400;600;700&family=Share+Tech+Mono&display=swap');
26
+
27
+ *, *::before, *::after { box-sizing: border-box; }
28
+
29
+ body, .gradio-container {
30
+ background: #0a0a0a !important;
31
+ font-family: 'Rajdhani', sans-serif !important;
32
+ color: #e8e0d0 !important;
33
+ }
34
+
35
+ .braingpt-header {
36
+ display: flex;
37
+ align-items: center;
38
+ gap: 18px;
39
+ padding: 18px 24px 10px 24px;
40
+ border-bottom: 1px solid #2a1a00;
41
+ background: linear-gradient(180deg, #110900 0%, #0a0a0a 100%);
42
+ margin-bottom: 8px;
43
+ }
44
+
45
+ .braingpt-title {
46
+ font-family: 'Rajdhani', sans-serif;
47
+ font-size: 2rem;
48
+ font-weight: 700;
49
+ letter-spacing: 3px;
50
+ background: linear-gradient(90deg, #ff6a00, #ffb347, #ff6a00);
51
+ background-size: 200%;
52
+ -webkit-background-clip: text;
53
+ -webkit-text-fill-color: transparent;
54
+ background-clip: text;
55
+ animation: shimmer 3s linear infinite;
56
+ text-transform: uppercase;
57
+ }
58
+
59
+ .braingpt-sub {
60
+ font-family: 'Share Tech Mono', monospace;
61
+ font-size: 0.72rem;
62
+ color: #664422;
63
+ letter-spacing: 2px;
64
+ margin-top: 2px;
65
+ }
66
+
67
+ @keyframes shimmer {
68
+ 0% { background-position: 0% }
69
+ 100% { background-position: 200% }
70
+ }
71
+
72
+ .chatbot {
73
+ background: #0d0d0d !important;
74
+ border: 1px solid #1f1208 !important;
75
+ border-radius: 12px !important;
76
+ font-family: 'Rajdhani', sans-serif !important;
77
+ font-size: 1.05rem !important;
78
+ }
79
+
80
+ .message.user {
81
+ background: linear-gradient(135deg, #1a0d00, #2a1500) !important;
82
+ border: 1px solid #3d1f00 !important;
83
+ border-radius: 12px 12px 2px 12px !important;
84
+ color: #ffcc88 !important;
85
+ font-weight: 600 !important;
86
+ }
87
+
88
+ .message.bot {
89
+ background: linear-gradient(135deg, #0f0f0f, #161208) !important;
90
+ border: 1px solid #2b1f00 !important;
91
+ border-radius: 12px 12px 12px 2px !important;
92
+ color: #e8d8b8 !important;
93
+ }
94
+
95
+ .thinking-bar {
96
+ display: none;
97
+ align-items: center;
98
+ gap: 10px;
99
+ padding: 8px 16px;
100
+ margin: 4px 0 8px 0;
101
+ background: linear-gradient(90deg, #1a0d00, #0d0d0d);
102
+ border-left: 3px solid #ff6a00;
103
+ border-radius: 0 8px 8px 0;
104
+ font-family: 'Share Tech Mono', monospace;
105
+ font-size: 0.8rem;
106
+ color: #ff8c33;
107
+ letter-spacing: 1px;
108
+ }
109
+
110
+ .thinking-bar.active { display: flex; }
111
+
112
+ .thinking-dots span {
113
+ display: inline-block;
114
+ width: 6px;
115
+ height: 6px;
116
+ background: #ff6a00;
117
+ border-radius: 50%;
118
+ margin: 0 2px;
119
+ animation: bounce-dot 1.2s infinite;
120
+ }
121
+ .thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
122
+ .thinking-dots span:nth-child(3) { animation-delay: 0.4s; }
123
+
124
+ @keyframes bounce-dot {
125
+ 0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
126
+ 40% { transform: translateY(-6px); opacity: 1; }
127
+ }
128
+
129
+ #msg-input textarea {
130
+ background: #111 !important;
131
+ border: 1px solid #2b1800 !important;
132
+ border-radius: 10px !important;
133
+ color: #ffcc88 !important;
134
+ font-family: 'Rajdhani', sans-serif !important;
135
+ font-size: 1rem !important;
136
+ caret-color: #ff6a00 !important;
137
+ resize: none !important;
138
+ padding: 12px 14px !important;
139
+ transition: border-color 0.2s;
140
+ }
141
+
142
+ #msg-input textarea:focus {
143
+ border-color: #ff6a00 !important;
144
+ box-shadow: 0 0 12px #ff6a0033 !important;
145
+ outline: none !important;
146
+ }
147
+
148
+ #send-btn, #clear-btn {
149
+ font-family: 'Rajdhani', sans-serif !important;
150
+ font-weight: 700 !important;
151
+ letter-spacing: 1.5px !important;
152
+ border-radius: 10px !important;
153
+ border: none !important;
154
+ cursor: pointer !important;
155
+ transition: all 0.18s ease !important;
156
+ text-transform: uppercase !important;
157
+ font-size: 0.9rem !important;
158
+ padding: 12px 22px !important;
159
+ }
160
+
161
+ #send-btn {
162
+ background: linear-gradient(135deg, #ff6a00, #cc4400) !important;
163
+ color: #fff !important;
164
+ box-shadow: 0 0 16px #ff6a0044 !important;
165
+ min-width: 110px !important;
166
+ }
167
+
168
+ #send-btn:hover {
169
+ background: linear-gradient(135deg, #ff8c33, #ff5500) !important;
170
+ box-shadow: 0 0 28px #ff6a0077 !important;
171
+ transform: translateY(-1px) !important;
172
+ }
173
+
174
+ #send-btn.thinking {
175
+ background: linear-gradient(135deg, #2a1500, #1a0d00) !important;
176
+ color: #ff8c33 !important;
177
+ pointer-events: none !important;
178
+ }
179
+
180
+ #clear-btn {
181
+ background: #161208 !important;
182
+ color: #664422 !important;
183
+ border: 1px solid #2b1800 !important;
184
+ }
185
+
186
+ #clear-btn:hover {
187
+ background: #1f1208 !important;
188
+ color: #ff8c33 !important;
189
+ border-color: #ff6a00 !important;
190
+ }
191
+
192
+ .examples-table td {
193
+ background: #111 !important;
194
+ border: 1px solid #1f1200 !important;
195
+ color: #cc7733 !important;
196
+ font-family: 'Rajdhani', sans-serif !important;
197
+ border-radius: 6px !important;
198
+ cursor: pointer !important;
199
+ transition: background 0.15s, color 0.15s !important;
200
+ }
201
+
202
+ .examples-table td:hover {
203
+ background: #1f1000 !important;
204
+ color: #ff8c33 !important;
205
+ border-color: #ff6a00 !important;
206
+ }
207
+
208
+ ::-webkit-scrollbar { width: 5px; }
209
+ ::-webkit-scrollbar-track { background: #0a0a0a; }
210
+ ::-webkit-scrollbar-thumb { background: #2b1800; border-radius: 4px; }
211
+ ::-webkit-scrollbar-thumb:hover { background: #ff6a00; }
212
+ """
213
+
214
+ JS = """
215
+ () => {
216
+ function setup() {
217
+ const sendBtn = document.getElementById('send-btn');
218
+ const bar = document.getElementById('thinking-bar');
219
+ if (!sendBtn || !bar) return;
220
+ new MutationObserver(() => {
221
+ const loading = document.querySelector('.generating') !== null
222
+ || document.querySelector('.eta-bar') !== null;
223
+ if (loading) {
224
+ sendBtn.classList.add('thinking');
225
+ const s = sendBtn.querySelector('span');
226
+ if (s) s.innerText = 'Thinking...';
227
+ bar.classList.add('active');
228
+ } else {
229
+ sendBtn.classList.remove('thinking');
230
+ const s = sendBtn.querySelector('span');
231
+ if (s) s.innerText = 'Enviar';
232
+ bar.classList.remove('active');
233
+ }
234
+ }).observe(document.body, { childList: true, subtree: true });
235
+ }
236
+ setTimeout(setup, 1500);
237
+ }
238
+ """
239
 
240
  pipe = pipeline(
241
  "text-generation",
 
252
  message = message[:1000] + "... [texto cortado]"
253
 
254
  system_msg = (
255
+ "Voce e BrainGPT, uma inteligencia artificial poderosa, direta e precisa. "
256
+ "Responda sempre em portugues de forma clara e concisa. "
257
+ "Nunca invente factos. Se nao souber, diga claramente."
258
  )
259
 
260
  msgs = [{"role": "system", "content": system_msg}]
261
+ for u, b in history[-6:]:
262
+ if u: msgs.append({"role": "user", "content": u})
263
+ if b: msgs.append({"role": "assistant", "content": b})
 
 
264
  msgs.append({"role": "user", "content": message})
265
 
266
+ prompt = pipe.tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
 
 
267
 
268
  if len(prompt) > 3500:
269
  return "Conversa muito longa. Clica em Limpar e recomeΓ§a."
 
278
  truncation=True,
279
  )
280
 
281
+ resposta = result[0]["generated_text"].split("<|im_start|>assistant\n")[-1].strip()
 
282
  resposta = resposta.replace("<|im_end|>", "").strip()
 
283
  return resposta if resposta else "Resposta vazia. Tenta reformular."
284
 
285
  except torch.cuda.OutOfMemoryError:
 
288
  return f"Erro: {str(e)[:150]}"
289
 
290
 
291
+ def user_submit(message, history):
292
+ if not message.strip():
293
+ return "", history
294
+ return "", history + [[message, None]]
295
+
296
+ def bot_respond(history):
297
+ if not history or history[-1][1] is not None:
298
+ return history
299
+ history[-1][1] = chat(history[-1][0], history[:-1])
300
+ return history
301
 
302
+
303
+ with gr.Blocks(title="BrainGPT") as demo:
304
+
305
+ gr.HTML(get_logo_html())
306
 
307
  gr.HTML("""
308
  <div id="thinking-bar" class="thinking-bar">
309
+ <div class="thinking-dots"><span></span><span></span><span></span></div>
 
 
310
  <span>THINKING...</span>
311
  </div>
312
  """)
313
 
314
+ chatbot = gr.Chatbot(label="", height=480, bubble_full_width=False)
 
 
 
 
 
 
315
 
316
+ with gr.Row():
317
  msg = gr.Textbox(
318
  placeholder="Escreve a tua mensagem...",
319
  show_label=False,
 
322
  elem_id="msg-input",
323
  scale=8,
324
  )
325
+ send_btn = gr.Button("Enviar", elem_id="send-btn", variant="primary", scale=2)
326
+ clear_btn = gr.Button("Limpar", elem_id="clear-btn", variant="secondary", scale=1)
 
 
 
 
 
 
 
 
 
 
327
 
328
  gr.Examples(
329
+ examples=["Quem es tu?", "Explica IA em 3 frases", "Como funciona o Bitcoin?", "5 dicas de produtividade"],
 
 
 
 
 
 
330
  inputs=msg,
331
  label="Exemplos",
332
  )
333
 
334
+ msg.submit(user_submit, [msg, chatbot], [msg, chatbot], queue=False).then(bot_respond, chatbot, chatbot)
335
+ send_btn.click(user_submit, [msg, chatbot], [msg, chatbot], queue=False).then(bot_respond, chatbot, chatbot)
336
+ clear_btn.click(lambda: ([], ""), outputs=[chatbot, msg], queue=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
 
338
+ demo.queue(max_size=5).launch(css=CSS, js=JS)