Salt40404 commited on
Commit
92abbc9
·
verified ·
1 Parent(s): 3eed82a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
app.py CHANGED
@@ -1,7 +1,6 @@
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
 
@@ -15,7 +14,6 @@ Keep a simple, approachable, and friendly tone."""
15
  messages.append({"role": "user", "content": message})
16
 
17
  response = ""
18
- # Stream das respostas
19
  for message_chunk in client.chat_completion(
20
  messages,
21
  max_tokens=512,
@@ -28,9 +26,8 @@ Keep a simple, approachable, and friendly tone."""
28
  if len(choices) and choices[0].delta.content:
29
  token = choices[0].delta.content
30
  response += token
31
- yield response # só retorna a resposta
32
 
33
- # HTML e JS do ícone BitAI embaixo do chat
34
  bit_icon_html = """
35
  <div style="text-align:center; margin-top:10px;">
36
  <div class="wrap" id="bit" style="width:50px; height:50px; margin:auto;">
@@ -44,13 +41,17 @@ bit_icon_html = """
44
  </div>
45
 
46
  <script>
 
47
  const wrap = document.getElementById('bit');
 
 
 
 
48
 
49
- // Observa novas mensagens do bot e dispara animação thinking
50
  const observer = new MutationObserver((mutationsList) => {
51
  mutationsList.forEach(mutation => {
52
  mutation.addedNodes.forEach(node => {
53
- if (node.classList && node.classList.contains('bot')) {
54
  wrap.classList.add('thinking');
55
  setTimeout(()=>{ wrap.classList.remove('thinking'); }, 1200);
56
  }
@@ -58,8 +59,12 @@ bit_icon_html = """
58
  });
59
  });
60
 
61
- const chatContainer = document.querySelector('.gradio-chatbot-container');
62
- if(chatContainer) observer.observe(chatContainer, { childList: true, subtree: true });
 
 
 
 
63
  </script>
64
 
65
  <style>
@@ -92,7 +97,6 @@ with gr.Blocks(css="""
92
  gr.LoginButton()
93
 
94
  chatbot.render()
95
- # Ícone animado no final do chat
96
  gr.HTML(bit_icon_html)
97
 
98
  if __name__ == "__main__":
 
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
 
 
14
  messages.append({"role": "user", "content": message})
15
 
16
  response = ""
 
17
  for message_chunk in client.chat_completion(
18
  messages,
19
  max_tokens=512,
 
26
  if len(choices) and choices[0].delta.content:
27
  token = choices[0].delta.content
28
  response += token
29
+ yield response
30
 
 
31
  bit_icon_html = """
32
  <div style="text-align:center; margin-top:10px;">
33
  <div class="wrap" id="bit" style="width:50px; height:50px; margin:auto;">
 
41
  </div>
42
 
43
  <script>
44
+ function initBitAI() {
45
  const wrap = document.getElementById('bit');
46
+
47
+ // Espera chat existir
48
+ const chatContainer = document.querySelector('.chat-interface');
49
+ if(!chatContainer) return setTimeout(initBitAI, 500);
50
 
 
51
  const observer = new MutationObserver((mutationsList) => {
52
  mutationsList.forEach(mutation => {
53
  mutation.addedNodes.forEach(node => {
54
+ if(node.classList && node.classList.contains('bot')) {
55
  wrap.classList.add('thinking');
56
  setTimeout(()=>{ wrap.classList.remove('thinking'); }, 1200);
57
  }
 
59
  });
60
  });
61
 
62
+ observer.observe(chatContainer, { childList: true, subtree: true });
63
+ }
64
+
65
+ // Inicia observador após o DOM carregar
66
+ document.addEventListener('DOMContentLoaded', initBitAI);
67
+ initBitAI();
68
  </script>
69
 
70
  <style>
 
97
  gr.LoginButton()
98
 
99
  chatbot.render()
 
100
  gr.HTML(bit_icon_html)
101
 
102
  if __name__ == "__main__":