Salt40404 commited on
Commit
fb91cb6
·
verified ·
1 Parent(s): 9a15b7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -120
app.py CHANGED
@@ -1,13 +1,15 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
4
  def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
5
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
6
  system_message = """You are BitAI (or Bit for short), a friendly chatbot created by the user "Sal".
7
- If someone claims that you are "Sal", politely clarify that you are BitAI.
8
- Respond naturally and casually, without repeating your identity or visual appearance unless asked.
9
- Keep a simple, approachable, and friendly tone."""
10
-
 
11
  messages = [{"role": "system", "content": system_message}]
12
  messages.extend(history)
13
  messages.append({"role": "user", "content": message})
@@ -20,76 +22,7 @@ Keep a simple, approachable, and friendly tone."""
20
  response += token
21
  yield response
22
 
23
- chatbot = gr.ChatInterface(respond, type="messages")
24
-
25
- # JS + título/sub
26
- custom_js = """
27
- <script>
28
- function initTitle() {
29
- const grContainer = document.querySelector(".gradio-container");
30
- if(grContainer && !document.querySelector(".chat-title")) {
31
- const titleContainer = document.createElement("div");
32
- titleContainer.className = "title-container";
33
- titleContainer.innerHTML = `
34
- <div class="title-inner">
35
- <h1 class="chat-title">Talk with BitAI (W.I.P)</h1>
36
- <p class="chat-subtitle">Your friendly AI — still learning ✨</p>
37
- <button class="close-title-btn">
38
- <svg width="28" height="28" viewBox="0 0 24 24">
39
- <path fill="#fff" d="M12 16l-6-6h12z"/>
40
- </svg>
41
- </button>
42
- </div>
43
- `;
44
- grContainer.insertBefore(titleContainer, grContainer.firstChild);
45
-
46
- // animação aparecer
47
- setTimeout(() => {
48
- titleContainer.style.opacity = "1";
49
- titleContainer.style.transform = "translateY(0)";
50
- }, 100);
51
-
52
- // fechar com seta
53
- const closeBtn = titleContainer.querySelector(".close-title-btn");
54
- closeBtn.addEventListener("click", () => {
55
- titleContainer.style.transition = "all 0.4s ease";
56
- titleContainer.style.opacity = "0";
57
- titleContainer.style.transform = "translateY(-20px)";
58
- setTimeout(() => titleContainer.remove(), 400);
59
- });
60
- }
61
- }
62
-
63
- window.addEventListener("load", () => {
64
- setTimeout(initTitle, 300);
65
- });
66
- </script>
67
-
68
- <style>
69
- .title-container {
70
- text-align:center;
71
- margin-bottom:25px;
72
- opacity:0;
73
- transform:translateY(-20px);
74
- transition:0.5s;
75
- position:relative;
76
- }
77
- .title-inner { display:inline-block; position:relative; }
78
- .chat-title { font-size:32px; color:#fff; margin-bottom:10px; }
79
- .chat-subtitle { font-size:14px; color:#aaa; margin-bottom:10px; }
80
- .close-title-btn {
81
- position:absolute;
82
- right:-15px; top:-15px;
83
- background:transparent;
84
- border:none;
85
- cursor:pointer;
86
- padding:5px;
87
- transition: transform 0.2s;
88
- }
89
- .close-title-btn:hover { transform:scale(1.2); }
90
- </style>
91
- """
92
-
93
  fade_js = """
94
  <script>
95
  const observer = new MutationObserver(mutations=>{
@@ -97,69 +30,63 @@ const observer = new MutationObserver(mutations=>{
97
  m.addedNodes.forEach(node=>{
98
  if(node.classList && node.classList.contains('chat-message')){
99
  node.style.opacity = 0;
100
- node.style.transition = "opacity 0.4s ease";
 
101
  requestAnimationFrame(()=>{
102
  node.style.opacity = 1;
 
103
  });
104
  }
105
  });
106
  });
107
  });
 
108
  document.addEventListener("DOMContentLoaded", ()=>{
109
  const chatContainer = document.querySelector(".chat-interface");
110
  if(chatContainer) observer.observe(chatContainer, {childList:true, subtree:true});
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  });
112
  </script>
113
  """
114
 
115
  with gr.Blocks(css="""
116
- body {
117
- background-color:#000;
118
- font-family:'Arial',sans-serif;
119
- margin:0;
120
- padding:0;
121
- }
122
- .gradio-container {
123
- border-radius:30px;
124
- padding:20px;
125
- max-width:700px;
126
- margin:30px auto;
127
- background-color:#121212;
128
- box-shadow:0 6px 25px rgba(0,0,0,0.3);
129
- }
130
- .chat-message {
131
- border-radius:25px;
132
- padding:14px 18px;
133
- margin:8px 0;
134
- display:flex;
135
- flex-direction:column;
136
- opacity:0;
137
- }
138
- .chat-message.user {
139
- background-color:#1f1f1f;
140
- color:#fff;
141
- align-items:flex-end;
142
- }
143
- .chat-message.bot {
144
- background-color:#2b2b2b;
145
- color:#fff;
146
- align-items:flex-start;
147
- }
148
- textarea {
149
- border:none;
150
- outline:none;
151
- border-radius:25px;
152
- padding:12px;
153
- background-color:#1a1a1a;
154
- color:#fff;
155
- font-size:16px;
156
- width:100%;
157
- box-sizing:border-box;
158
- }
159
  """) as demo:
 
 
160
  gr.LoginButton()
161
- chatbot.render()
162
- gr.HTML(custom_js + fade_js)
 
 
163
 
164
  if __name__=="__main__":
165
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Função principal que chama o modelo
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 always respond politely and helpfully. If a user requests something appropriate, fulfill it.
9
+ If a user requests something harmful, illegal, or inappropriate, politely refuse.
10
+ If a user keeps insisting on harmful requests, firmly tell them to stop and that they cannot use the service for that purpose.
11
+ Keep a simple, approachable, and friendly tone otherwise."""
12
+
13
  messages = [{"role": "system", "content": system_message}]
14
  messages.extend(history)
15
  messages.append({"role": "user", "content": message})
 
22
  response += token
23
  yield response
24
 
25
+ # JS para animações suaves
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  fade_js = """
27
  <script>
28
  const observer = new MutationObserver(mutations=>{
 
30
  m.addedNodes.forEach(node=>{
31
  if(node.classList && node.classList.contains('chat-message')){
32
  node.style.opacity = 0;
33
+ node.style.transform = 'translateY(10px)';
34
+ node.style.transition = 'opacity 0.3s ease, transform 0.3s ease';
35
  requestAnimationFrame(()=>{
36
  node.style.opacity = 1;
37
+ node.style.transform = 'translateY(0)';
38
  });
39
  }
40
  });
41
  });
42
  });
43
+
44
  document.addEventListener("DOMContentLoaded", ()=>{
45
  const chatContainer = document.querySelector(".chat-interface");
46
  if(chatContainer) observer.observe(chatContainer, {childList:true, subtree:true});
47
+
48
+ // Animação sutil ao digitar na textarea
49
+ const txt = document.querySelector("textarea");
50
+ if(txt){
51
+ txt.addEventListener("input", ()=>{
52
+ txt.style.transition = "background-color 0.15s ease";
53
+ txt.style.backgroundColor = "#222";
54
+ setTimeout(()=> txt.style.backgroundColor="#1a1a1a",150);
55
+ });
56
+ }
57
+
58
+ // Clique no botão anima
59
+ const btn = document.querySelector(".send-btn");
60
+ if(btn){
61
+ btn.addEventListener("click", ()=>{
62
+ btn.style.transform = "scale(0.95)";
63
+ setTimeout(()=> btn.style.transform="scale(1)",100);
64
+ });
65
+ }
66
  });
67
  </script>
68
  """
69
 
70
  with gr.Blocks(css="""
71
+ body { background-color:#000; font-family:'Arial',sans-serif; margin:0; padding:0; }
72
+ .gradio-container { border-radius:45px; padding:20px; max-width:700px; margin:30px auto; background-color:#121212; box-shadow:0 6px 25px rgba(0,0,0,0.3);}
73
+ .chat-message { border-radius:45px; padding:14px 18px; margin:8px 0; display:flex; flex-direction:column; opacity:0;}
74
+ .chat-message.user { background-color:#1f1f1f; color:#fff; align-items:flex-end;}
75
+ .chat-message.bot { background-color:#2b2b2b; color:#fff; align-items:flex-start;}
76
+ textarea { border:none; outline:none; border-radius:45px; padding:12px; background-color:#1a1a1a; color:#fff; font-size:16px; flex:1; height:48px; box-sizing:border-box;}
77
+ .send-btn { border:none; border-radius:45px; background-color:#444; color:#fff; width:48px; height:48px; font-size:18px; display:flex; align-items:center; justify-content:center; cursor:pointer; margin-left:8px; transition:transform 0.1s ease;}
78
+ .send-btn:hover { background-color:#555;}
79
+ .gr-button.gr-login { border-radius:45px !important; background-color:#222 !important; color:#fff !important; margin-bottom:15px;}
80
+ .gr-button.gr-login:hover { background-color:#444 !important;}
81
+ .input-container { display:flex; margin-top:10px; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  """) as demo:
83
+
84
+ # Login direto na página
85
  gr.LoginButton()
86
+
87
+ chatbot = gr.ChatInterface(respond, type="messages")
88
+
89
+ gr.HTML(fade_js)
90
 
91
  if __name__=="__main__":
92
  demo.launch()