Salt40404 commited on
Commit
abda3c6
·
verified ·
1 Parent(s): b415ca1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -62
app.py CHANGED
@@ -1,10 +1,10 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # Função principal do chatbot
5
  def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
6
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
7
- system_message = """You are BitAI (or Bit for short), a friendly chatbot created by "Sal".
8
  You respond politely, refuse inappropriate requests, and keep a friendly tone."""
9
 
10
  messages = [{"role": "system", "content": system_message}]
@@ -17,12 +17,11 @@ You respond politely, refuse inappropriate requests, and keep a friendly tone.""
17
  response += token
18
  yield response
19
 
20
- # JS + HTML para título animado e botão de fechar
21
- fade_js = """
22
  <script>
23
- document.addEventListener("DOMContentLoaded", () => {
24
  const grContainer = document.querySelector(".gradio-container");
25
-
26
  if(grContainer && !document.querySelector(".chat-title")) {
27
  const titleContainer = document.createElement("div");
28
  titleContainer.className = "title-container";
@@ -31,7 +30,7 @@ document.addEventListener("DOMContentLoaded", () => {
31
  <h1 class="chat-title">Talk with BitAI (W.I.P)</h1>
32
  <p class="chat-subtitle">Still updating, might not know everything, filter slightly broken</p>
33
  <button class="close-title-btn">
34
- <svg width="24" height="24" viewBox="0 0 24 24">
35
  <path fill="#fff" d="M12 16l-6-6h12z"/>
36
  </svg>
37
  </button>
@@ -45,7 +44,7 @@ document.addEventListener("DOMContentLoaded", () => {
45
  titleContainer.style.transform = "translateY(0)";
46
  }, 100);
47
 
48
- // botão de fechar
49
  const closeBtn = titleContainer.querySelector(".close-title-btn");
50
  closeBtn.addEventListener("click", () => {
51
  titleContainer.style.transition = "all 0.4s ease";
@@ -54,78 +53,73 @@ document.addEventListener("DOMContentLoaded", () => {
54
  setTimeout(() => titleContainer.remove(), 400);
55
  });
56
  }
 
57
 
58
- // Startup message
59
- const chatContainer = document.querySelector(".chat-interface");
60
- if(chatContainer && chatContainer.children.length===0){
61
- const startupMsg = document.createElement("div");
62
- startupMsg.className="startup-message";
63
- startupMsg.innerHTML='<div class="startup-content"><div class="startup-icon">🤖</div><div class="startup-text"><div class="startup-title">BitAI</div><div class="startup-subtitle">How was your day? Good morning!</div><div class="startup-note">Entirely AI</div></div></div>';
64
- chatContainer.appendChild(startupMsg);
65
- setTimeout(()=>{startupMsg.style.opacity="1"; startupMsg.style.transform="translateY(0)";},100);
66
- }
67
-
68
- // Observador para animação de novas mensagens
69
- const observer = new MutationObserver(mutations => {
70
- mutations.forEach(m => {
71
- m.addedNodes.forEach(node => {
72
- if(node.classList?.contains('chat-message')){
73
- const startupMsg = document.querySelector(".startup-message");
74
- if(startupMsg){startupMsg.style.opacity="0"; startupMsg.style.transform="translateY(20px)"; setTimeout(()=>startupMsg.remove(),300);}
75
- node.style.opacity=0; node.style.transform='translateY(20px) scale(0.95)';
76
- node.style.transition='all 0.5s ease, box-shadow 0.3s ease';
77
- requestAnimationFrame(()=>{node.style.opacity=1; node.style.transform='translateY(0) scale(1)'; setTimeout(()=>node.style.boxShadow='0 4px 15px rgba(0,0,0,0.2)',500);});
78
- }
79
- });
80
- });
81
- });
82
- if(chatContainer) observer.observe(chatContainer, {childList:true, subtree:true});
83
 
84
- // Button dentro do textarea
 
 
85
  const btn = document.querySelector(".send-btn");
86
- const txtCont = document.querySelector(".input-container");
87
- if(btn && txtCont){txtCont.style.position="relative"; btn.style.position="absolute"; btn.style.right="8px"; btn.style.top="8px"; btn.style.width="40px"; btn.style.height="40px";}
88
-
89
- // Função genérica de bounce
90
- const bounce = el=>{
91
- if(!el || el.hasAttribute("data-bounce-added")) return;
92
- el.setAttribute("data-bounce-added","true");
93
- el.addEventListener("click",()=>{el.style.transition="transform 0.5s cubic-bezier(0.68,-0.55,0.27,1.55)"; el.style.transform="scale(0.95)"; setTimeout(()=>el.style.transform="scale(1)",500);});
94
- };
95
- document.querySelectorAll("button, .chat-message, textarea, .gr-button.gr-login, .chat-title, .chat-subtitle, .startup-message, .gr-chatbot").forEach(bounce);
96
  });
97
  </script>
98
 
99
  <style>
100
- .title-container { text-align:center; margin-bottom:25px; opacity:0; transform:translateY(-20px); transition:0.5s; position:relative; }
 
 
 
 
 
 
 
101
  .title-inner { display:inline-block; position:relative; }
102
  .chat-title { font-size:32px; color:#fff; margin-bottom:10px; cursor:pointer; }
103
  .chat-subtitle { font-size:14px; color:#aaa; margin-bottom:10px; }
104
- .close-title-btn { position:absolute; right:-10px; top:-10px; background:transparent; border:none; cursor:pointer; padding:5px; transition: transform 0.2s; }
 
 
 
 
 
 
 
 
105
  .close-title-btn:hover { transform:scale(1.2); }
106
- .startup-message { opacity:0; transform:translateY(20px); transition:0.5s; margin:20px 0; display:flex; justify-content:center; cursor:pointer; }
107
- .startup-content { display:flex; align-items:center; background:linear-gradient(145deg,#2e2e2e,#252525); padding:20px; border-radius:20px; box-shadow:0 4px 15px rgba(0,0,0,0.2); }
108
- .startup-icon { font-size:32px; margin-right:15px; }
109
- .startup-title { font-weight:600; font-size:18px; color:#fff; }
110
- .startup-subtitle { font-size:16px; color:#eee; }
111
- .startup-note { font-size:12px; color:#aaa; font-style:italic; }
 
 
112
  </style>
113
  """
114
 
115
- # Gradio interface completa
116
  with gr.Blocks(css="""
117
- body{background:#000;margin:0;padding:0;font-family:'Inter','Manrope','Outfit',sans-serif;}
118
- .gradio-container{border-radius:20px;padding:20px;max-width:700px;margin:30px auto;background:linear-gradient(145deg,#0f0f0f,#1a1a1a);box-shadow:0 10px 30px rgba(0,0,0,0.4);transition:transform 0.3s;}
119
- textarea{border:none;border-radius:25px;padding:14px 20px 14px 20px;background:#1a1a1a;color:#fff;font-size:16px;width:100%;height:56px;transition:0.3s;}
120
- .send-btn{border:none;border-radius:20px;background:linear-gradient(145deg,#555,#444);color:#fff;width:40px;height:40px;position:absolute;right:8px;top:8px;cursor:pointer;transition:0.3s;}
121
- .chat-message{border-radius:20px;padding:16px;margin:12px 0;opacity:0;transition:0.5s;box-shadow:0 4px 10px rgba(0,0,0,0.15);}
122
- .chat-message.user{background:linear-gradient(145deg,#252525,#1f1f1f);color:#fff;align-items:flex-end;margin-left:40px;}
123
- .chat-message.bot{background:linear-gradient(145deg,#2e2e2e,#252525);color:#fff;align-items:flex-start;margin-right:40px;}
124
  """) as demo:
125
-
126
  gr.LoginButton()
127
  gr.ChatInterface(respond, type="messages")
128
- gr.HTML(fade_js)
129
 
130
  if __name__ == "__main__":
131
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Função do bot
5
  def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
6
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
7
+ system_message = """You are BitAI (or Bit for short), a friendly chatbot created by the user "Sal".
8
  You respond politely, refuse inappropriate requests, and keep a friendly tone."""
9
 
10
  messages = [{"role": "system", "content": system_message}]
 
17
  response += token
18
  yield response
19
 
20
+ # Script + CSS
21
+ custom_js = """
22
  <script>
23
+ function initTitle() {
24
  const grContainer = document.querySelector(".gradio-container");
 
25
  if(grContainer && !document.querySelector(".chat-title")) {
26
  const titleContainer = document.createElement("div");
27
  titleContainer.className = "title-container";
 
30
  <h1 class="chat-title">Talk with BitAI (W.I.P)</h1>
31
  <p class="chat-subtitle">Still updating, might not know everything, filter slightly broken</p>
32
  <button class="close-title-btn">
33
+ <svg width="28" height="28" viewBox="0 0 24 24">
34
  <path fill="#fff" d="M12 16l-6-6h12z"/>
35
  </svg>
36
  </button>
 
44
  titleContainer.style.transform = "translateY(0)";
45
  }, 100);
46
 
47
+ // fechar com a seta
48
  const closeBtn = titleContainer.querySelector(".close-title-btn");
49
  closeBtn.addEventListener("click", () => {
50
  titleContainer.style.transition = "all 0.4s ease";
 
53
  setTimeout(() => titleContainer.remove(), 400);
54
  });
55
  }
56
+ }
57
 
58
+ // esperar carregar tudo
59
+ window.addEventListener("load", () => {
60
+ setTimeout(initTitle, 300);
61
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ // deixar botão de enviar grande
64
+ window.addEventListener("load", () => {
65
+ const txtArea = document.querySelector("textarea");
66
  const btn = document.querySelector(".send-btn");
67
+ if(txtArea && btn){
68
+ const resize = () => {
69
+ btn.style.height = txtArea.offsetHeight + "px";
70
+ btn.style.width = txtArea.offsetHeight + "px";
71
+ };
72
+ resize();
73
+ new ResizeObserver(resize).observe(txtArea);
74
+ }
 
 
75
  });
76
  </script>
77
 
78
  <style>
79
+ .title-container {
80
+ text-align:center;
81
+ margin-bottom:25px;
82
+ opacity:0;
83
+ transform:translateY(-20px);
84
+ transition:0.5s;
85
+ position:relative;
86
+ }
87
  .title-inner { display:inline-block; position:relative; }
88
  .chat-title { font-size:32px; color:#fff; margin-bottom:10px; cursor:pointer; }
89
  .chat-subtitle { font-size:14px; color:#aaa; margin-bottom:10px; }
90
+ .close-title-btn {
91
+ position:absolute;
92
+ right:-15px; top:-15px;
93
+ background:transparent;
94
+ border:none;
95
+ cursor:pointer;
96
+ padding:5px;
97
+ transition: transform 0.2s;
98
+ }
99
  .close-title-btn:hover { transform:scale(1.2); }
100
+ .send-btn {
101
+ border:none;
102
+ border-radius:50%;
103
+ background:linear-gradient(145deg,#555,#444);
104
+ color:#fff;
105
+ cursor:pointer;
106
+ transition:0.3s;
107
+ }
108
  </style>
109
  """
110
 
111
+ # Interface
112
  with gr.Blocks(css="""
113
+ body{background:#000;font-family:'Inter','Manrope','Outfit',sans-serif;}
114
+ .gradio-container{border-radius:20px;padding:20px;max-width:700px;margin:30px auto;
115
+ background:linear-gradient(145deg,#0f0f0f,#1a1a1a);
116
+ box-shadow:0 10px 30px rgba(0,0,0,0.4);}
117
+ textarea{border:none;border-radius:25px;padding:14px 20px;
118
+ background:#1a1a1a;color:#fff;font-size:16px;width:100%;min-height:56px;}
 
119
  """) as demo:
 
120
  gr.LoginButton()
121
  gr.ChatInterface(respond, type="messages")
122
+ gr.HTML(custom_js)
123
 
124
  if __name__ == "__main__":
125
  demo.launch()