Salt40404 commited on
Commit
7b62253
·
verified ·
1 Parent(s): 444353e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -48
app.py CHANGED
@@ -1,35 +1,26 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # Função principal da IA
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
  system_message = """You are BitAI (or Bit for short), a friendly chatbot created by the user "Sal".
9
  If someone claims that you are "Sal", politely clarify that you are BitAI.
10
  Respond naturally and casually, without repeating your identity or visual appearance unless asked.
11
- Keep a simple, approachable, and friendly tone. do not modify anything else on user thing unless they say what they want to modify on the thing or code"""
12
 
13
  messages = [{"role": "system", "content": system_message}]
14
  messages.extend(history)
15
  messages.append({"role": "user", "content": message})
16
 
17
  response = ""
18
- for message_chunk in client.chat_completion(
19
- messages,
20
- max_tokens=512,
21
- stream=True,
22
- temperature=0.7,
23
- top_p=0.95,
24
- ):
25
  token = ""
26
- choices = message_chunk.choices
27
- if len(choices) and choices[0].delta.content:
28
- token = choices[0].delta.content
29
  response += token
30
  yield response
31
 
32
- # HTML do ícone da BitAI
33
  bit_icon_html = """
34
  <svg class="bit-icon" viewBox="0 0 160 160">
35
  <circle class="face" cx="80" cy="80" r="72"/>
@@ -39,42 +30,67 @@ bit_icon_html = """
39
  </svg>
40
 
41
  <script>
42
- function animateEyes(node){
43
- const wrap = node.querySelector('.bit-icon');
44
- wrap.classList.add('thinking');
45
- setTimeout(()=> wrap.classList.remove('thinking'), 1200);
46
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- // Observa novas mensagens do bot
49
- const chatContainer = document.querySelector('.chat-interface');
50
- if(chatContainer){
51
- const observer = new MutationObserver(mutations => {
52
- mutations.forEach(m => {
53
- m.addedNodes.forEach(node => {
54
- if(node.classList && node.classList.contains('bot')){
55
- animateEyes(node);
56
- // move o ícone para o topo da mensagem
57
- node.insertAdjacentElement('afterbegin', wrap.cloneNode(true));
 
 
 
 
 
 
 
58
  }
59
  });
60
  });
61
  });
62
- observer.observe(chatContainer, {childList:true, subtree:true});
 
63
  }
 
 
 
64
  </script>
65
 
66
  <style>
67
- .bit-icon{ width:30px; height:30px; margin-right:5px; vertical-align:middle;}
68
- .face{ fill:#0b1020; }
69
- .dot{ fill:#fff; transition: all 500ms ease; }
70
- .eye-left { cx:56; cy:62; r:10;}
71
- .eye-right{ cx:104; cy:62; r:10;}
72
- .extra-dot{ opacity:0; transform:translateY(10%); transition: all 500ms ease;}
73
- .wrap.thinking .extra-dot{ opacity:1; transform:translateY(0);}
74
- .wrap.thinking .dot{ animation: wave 1.4s infinite ease-in-out;}
75
- .wrap.thinking .eye-left { animation-delay:0s; }
76
- .wrap.thinking .extra-dot{ animation-delay:0.25s;}
77
- .wrap.thinking .eye-right{ animation-delay:0.5s;}
78
  @keyframes wave{0%,100%{transform:translateY(0);}50%{transform:translateY(-12%);}}
79
  </style>
80
  """
@@ -82,17 +98,17 @@ if(chatContainer){
82
  chatbot = gr.ChatInterface(respond, type="messages")
83
 
84
  with gr.Blocks(css="""
85
- body { background-color: #000; font-family: 'Arial', sans-serif; }
86
- .gradio-container { border-radius: 20px; padding: 15px; max-width:700px; margin:20px auto; background-color:#fff; }
87
- .chat-message { border-radius: 15px; padding:10px 15px; margin:5px 0; display:flex; align-items:flex-start;}
88
- .chat-message.user { background-color: #dceefc; }
89
- .chat-message.bot { background-color: #f0f0f0; justify-content:flex-start; }
90
  """) as demo:
91
  with gr.Sidebar():
92
  gr.LoginButton()
93
-
94
  chatbot.render()
95
  gr.HTML(bit_icon_html)
96
 
97
- if __name__ == "__main__":
98
  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
+ # Ícone do BitAI que vai dentro de cada balão do bot
24
  bit_icon_html = """
25
  <svg class="bit-icon" viewBox="0 0 160 160">
26
  <circle class="face" cx="80" cy="80" r="72"/>
 
30
  </svg>
31
 
32
  <script>
33
+ function addBitIconToBotMessages(){
34
+ const chatContainer = document.querySelector('.chat-interface');
35
+ if(!chatContainer) return setTimeout(addBitIconToBotMessages,500);
36
+
37
+ const observer = new MutationObserver(mutations=>{
38
+ mutations.forEach(m=>{
39
+ m.addedNodes.forEach(node=>{
40
+ if(node.classList && node.classList.contains('bot') && !node.querySelector('.bit-icon')){
41
+ const svg = document.createElementNS("http://www.w3.org/2000/svg","svg");
42
+ svg.innerHTML = `<circle class="face" cx="80" cy="80" r="72"/>
43
+ <circle class="dot eye-left"/>
44
+ <circle class="dot eye-right"/>
45
+ <circle class="dot extra-dot" cx="80" cy="78" r="8"/>`;
46
+ svg.setAttribute('class','bit-icon');
47
+ svg.setAttribute('viewBox','0 0 160 160');
48
+ svg.style.width='30px';
49
+ svg.style.height='30px';
50
+ svg.style.marginTop='5px';
51
+ svg.style.display='block';
52
+ node.appendChild(svg);
53
+
54
+ // animação de "lendo"
55
+ const left = svg.querySelector('.eye-left');
56
+ const right = svg.querySelector('.eye-right');
57
+ const extra = svg.querySelector('.extra-dot');
58
 
59
+ let interval = setInterval(()=>{
60
+ const dx = (Math.random()*4)-2;
61
+ const dy = (Math.random()*4)-2;
62
+ left.setAttribute('cx',56+dx);
63
+ left.setAttribute('cy',62+dy);
64
+ right.setAttribute('cx',104+dx);
65
+ right.setAttribute('cy',62+dy);
66
+ extra.setAttribute('cx',80+dx);
67
+ extra.setAttribute('cy',78+dy);
68
+ },100);
69
+
70
+ setTimeout(()=>{
71
+ clearInterval(interval);
72
+ left.setAttribute('cx',56); left.setAttribute('cy',62);
73
+ right.setAttribute('cx',104); right.setAttribute('cy',62);
74
+ extra.setAttribute('cx',80); extra.setAttribute('cy',78);
75
+ },1200);
76
  }
77
  });
78
  });
79
  });
80
+
81
+ observer.observe(chatContainer,{childList:true, subtree:true});
82
  }
83
+
84
+ document.addEventListener('DOMContentLoaded',addBitIconToBotMessages);
85
+ addBitIconToBotMessages();
86
  </script>
87
 
88
  <style>
89
+ .bit-icon .face{fill:#0b1020;}
90
+ .bit-icon .dot{fill:#fff; transition: all 500ms ease;}
91
+ .bit-icon .extra-dot{opacity:0; transform:translateY(10%); transition: all 500ms ease;}
92
+ .bit-icon.thinking .extra-dot{opacity:1; transform:translateY(0);}
93
+ .bit-icon.thinking .dot{animation: wave 1.4s infinite ease-in-out;}
 
 
 
 
 
 
94
  @keyframes wave{0%,100%{transform:translateY(0);}50%{transform:translateY(-12%);}}
95
  </style>
96
  """
 
98
  chatbot = gr.ChatInterface(respond, type="messages")
99
 
100
  with gr.Blocks(css="""
101
+ body{background-color:#000;font-family:'Arial',sans-serif;}
102
+ .gradio-container{border-radius:20px;padding:15px;max-width:700px;margin:20px auto;background-color:#fff;}
103
+ .chat-message{border-radius:15px;padding:10px 15px;margin:5px 0; display:flex; flex-direction:column;}
104
+ .chat-message.user{background-color:#dceefc; align-items:flex-end;}
105
+ .chat-message.bot{background-color:#f0f0f0; align-items:flex-start;}
106
  """) as demo:
107
  with gr.Sidebar():
108
  gr.LoginButton()
109
+
110
  chatbot.render()
111
  gr.HTML(bit_icon_html)
112
 
113
+ if __name__=="__main__":
114
  demo.launch()