Salt40404 commited on
Commit
3eed82a
·
verified ·
1 Parent(s): 4a07a53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -24
app.py CHANGED
@@ -1,6 +1,5 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- import asyncio
4
 
5
  # Função principal da IA
6
  def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
@@ -15,10 +14,8 @@ Keep a simple, approachable, and friendly tone."""
15
  messages.extend(history)
16
  messages.append({"role": "user", "content": message})
17
 
18
- # Primeiro: avisa que a IA está pensando
19
- yield {"response": "", "mode": "thinking"}
20
-
21
  response = ""
 
22
  for message_chunk in client.chat_completion(
23
  messages,
24
  max_tokens=512,
@@ -31,13 +28,9 @@ Keep a simple, approachable, and friendly tone."""
31
  if len(choices) and choices[0].delta.content:
32
  token = choices[0].delta.content
33
  response += token
34
- # Atualiza resposta e mantém “thinking”
35
- yield {"response": response, "mode": "thinking"}
36
-
37
- # Quando terminou, volta para normal
38
- yield {"response": response, "mode": "normal"}
39
 
40
- # HTML do ícone da BitAI (menor)
41
  bit_icon_html = """
42
  <div style="text-align:center; margin-top:10px;">
43
  <div class="wrap" id="bit" style="width:50px; height:50px; margin:auto;">
@@ -52,10 +45,21 @@ bit_icon_html = """
52
 
53
  <script>
54
  const wrap = document.getElementById('bit');
55
- const setThinking = (thinking) => {
56
- wrap.classList.toggle('thinking', thinking);
57
- }
58
- window.setThinking = setThinking; // expõe global pra JS
 
 
 
 
 
 
 
 
 
 
 
59
  </script>
60
 
61
  <style>
@@ -75,15 +79,7 @@ bit_icon_html = """
75
  </style>
76
  """
77
 
78
- # Função wrapper pra atualizar o modo do ícone via JS
79
- def respond_wrapper(message, history, hf_token):
80
- for output in respond(message, history, hf_token):
81
- # Cada yield retorna dict com resposta e modo
82
- yield gr.update(value=output["response"]), gr.HTML.update(value=f"<script>window.setThinking({str(output['mode']=='thinking').lower()});</script>")
83
-
84
- chatbot = gr.ChatInterface(
85
- respond_wrapper, type="messages"
86
- )
87
 
88
  with gr.Blocks(css="""
89
  body { background-color: #f0f0f0; font-family: 'Arial', sans-serif; }
@@ -96,7 +92,7 @@ with gr.Blocks(css="""
96
  gr.LoginButton()
97
 
98
  chatbot.render()
99
- # Ícone abaixo do chat
100
  gr.HTML(bit_icon_html)
101
 
102
  if __name__ == "__main__":
 
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):
 
14
  messages.extend(history)
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
  if len(choices) and choices[0].delta.content:
29
  token = choices[0].delta.content
30
  response += token
31
+ yield response # 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;">
 
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
+ }
57
+ });
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>
 
79
  </style>
80
  """
81
 
82
+ chatbot = gr.ChatInterface(respond, type="messages")
 
 
 
 
 
 
 
 
83
 
84
  with gr.Blocks(css="""
85
  body { background-color: #f0f0f0; font-family: 'Arial', sans-serif; }
 
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__":