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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -52
app.py CHANGED
@@ -1,35 +1,28 @@
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
-
8
- # Prompt melhorado
9
- system_message = """You are BitAI (Bit), a friendly chatbot created by the user "Sal".
10
- - Always answer clearly, friendly and playful.
11
- - Refuse unsafe or harmful requests.
12
- - If you don’t know something, admit kindly.
13
- - Never claim to be Sal, only identify as BitAI (Bit).
14
- """
15
 
16
  messages = [{"role": "system", "content": system_message}]
17
  messages.extend(history)
18
  messages.append({"role": "user", "content": message})
19
 
20
  response = ""
21
- for chunk in client.chat_completion(
22
- messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95
23
- ):
24
- token = (
25
- chunk.choices[0].delta.content
26
- if chunk.choices and chunk.choices[0].delta.content
27
- else ""
28
- )
29
  response += token
30
  yield response
31
 
32
- # Script + CSS
 
 
33
  custom_js = """
34
  <script>
35
  function initTitle() {
@@ -67,32 +60,8 @@ function initTitle() {
67
  }
68
  }
69
 
70
- // deixar botão enviar grande sempre
71
- function resizeSendBtn() {
72
- const txtArea = document.querySelector("textarea");
73
- const btn = document.querySelector("button.primary");
74
- if(txtArea && btn){
75
- btn.style.height = txtArea.offsetHeight + "px";
76
- btn.style.width = txtArea.offsetHeight + "px";
77
- btn.style.borderRadius = "50%";
78
- btn.style.fontSize = "16px";
79
- }
80
- }
81
-
82
  window.addEventListener("load", () => {
83
  setTimeout(initTitle, 300);
84
-
85
- // observar textarea
86
- const observer = new MutationObserver(() => {
87
- const txtArea = document.querySelector("textarea");
88
- const btn = document.querySelector("button.primary");
89
- if(txtArea && btn){
90
- resizeSendBtn();
91
- new ResizeObserver(resizeSendBtn).observe(txtArea);
92
- observer.disconnect();
93
- }
94
- });
95
- observer.observe(document.body, { childList: true, subtree: true });
96
  });
97
  </script>
98
 
@@ -121,18 +90,76 @@ window.addEventListener("load", () => {
121
  </style>
122
  """
123
 
124
- # Interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  with gr.Blocks(css="""
126
- body{background:#000;font-family:'Inter','Manrope','Outfit',sans-serif;}
127
- .gradio-container{border-radius:20px;padding:20px;max-width:700px;margin:30px auto;
128
- background:linear-gradient(145deg,#0f0f0f,#1a1a1a);
129
- box-shadow:0 10px 30px rgba(0,0,0,0.4);}
130
- textarea{border:none;border-radius:25px;padding:14px 20px;
131
- background:#1a1a1a;color:#fff;font-size:16px;width:100%;min-height:56px;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
  """) as demo:
133
  gr.LoginButton()
134
- gr.ChatInterface(respond, type="messages")
135
- gr.HTML(custom_js)
136
 
137
- if __name__ == "__main__":
138
  demo.launch()
 
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})
14
 
15
  response = ""
16
+ for chunk in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
17
+ token = ""
18
+ if chunk.choices and chunk.choices[0].delta.content:
19
+ token = chunk.choices[0].delta.content
 
 
 
 
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() {
 
60
  }
61
  }
62
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  window.addEventListener("load", () => {
64
  setTimeout(initTitle, 300);
 
 
 
 
 
 
 
 
 
 
 
 
65
  });
66
  </script>
67
 
 
90
  </style>
91
  """
92
 
93
+ fade_js = """
94
+ <script>
95
+ const observer = new MutationObserver(mutations=>{
96
+ mutations.forEach(m=>{
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()