Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +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):
|
|
@@ -14,6 +15,9 @@ Keep a simple, approachable, and friendly tone."""
|
|
| 14 |
messages.extend(history)
|
| 15 |
messages.append({"role": "user", "content": message})
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
response = ""
|
| 18 |
for message_chunk in client.chat_completion(
|
| 19 |
messages,
|
|
@@ -27,14 +31,16 @@ Keep a simple, approachable, and friendly tone."""
|
|
| 27 |
if len(choices) and choices[0].delta.content:
|
| 28 |
token = choices[0].delta.content
|
| 29 |
response += token
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
# HTML do ícone da BitAI
|
| 35 |
bit_icon_html = """
|
| 36 |
-
<div style="text-align:center; margin-
|
| 37 |
-
<div class="wrap" id="bit" style="width:
|
| 38 |
<svg viewBox="0 0 160 160">
|
| 39 |
<circle class="face" cx="80" cy="80" r="72"/>
|
| 40 |
<circle class="dot eye-left"/>
|
|
@@ -42,18 +48,14 @@ bit_icon_html = """
|
|
| 42 |
<circle class="dot extra-dot" cx="80" cy="78" r="8"/>
|
| 43 |
</svg>
|
| 44 |
</div>
|
| 45 |
-
<div class="label"><b>bitAI</b> — <span id="mode">normal</span></div>
|
| 46 |
</div>
|
| 47 |
|
| 48 |
<script>
|
| 49 |
const wrap = document.getElementById('bit');
|
| 50 |
-
const
|
| 51 |
-
let thinking = false;
|
| 52 |
-
wrap.addEventListener('click', ()=>{
|
| 53 |
-
thinking = !thinking;
|
| 54 |
wrap.classList.toggle('thinking', thinking);
|
| 55 |
-
|
| 56 |
-
|
| 57 |
</script>
|
| 58 |
|
| 59 |
<style>
|
|
@@ -70,10 +72,19 @@ bit_icon_html = """
|
|
| 70 |
.wrap.thinking .extra-dot{ animation-delay:0.25s; }
|
| 71 |
.wrap.thinking .eye-right{ animation-delay:0.5s; }
|
| 72 |
@keyframes wave { 0%,100%{ transform:translateY(0); } 50%{ transform:translateY(-12%); } }
|
| 73 |
-
.label{ text-align:center; font-family:sans-serif; color:#223; margin-top:4px;}
|
| 74 |
</style>
|
| 75 |
"""
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
with gr.Blocks(css="""
|
| 78 |
body { background-color: #f0f0f0; font-family: 'Arial', sans-serif; }
|
| 79 |
.gradio-container { border-radius: 25px; padding: 20px; max-width: 700px; margin: 20px auto; background-color: #fafafa; box-shadow: 0 4px 20px rgba(0,0,0,0.05);}
|
|
@@ -84,10 +95,9 @@ with gr.Blocks(css="""
|
|
| 84 |
with gr.Sidebar():
|
| 85 |
gr.LoginButton()
|
| 86 |
|
| 87 |
-
# Coloca o ícone no topo da UI
|
| 88 |
-
gr.HTML(bit_icon_html)
|
| 89 |
-
|
| 90 |
chatbot.render()
|
|
|
|
|
|
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
| 93 |
demo.launch()
|
|
|
|
| 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 |
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,
|
|
|
|
| 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;">
|
| 44 |
<svg viewBox="0 0 160 160">
|
| 45 |
<circle class="face" cx="80" cy="80" r="72"/>
|
| 46 |
<circle class="dot eye-left"/>
|
|
|
|
| 48 |
<circle class="dot extra-dot" cx="80" cy="78" r="8"/>
|
| 49 |
</svg>
|
| 50 |
</div>
|
|
|
|
| 51 |
</div>
|
| 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>
|
|
|
|
| 72 |
.wrap.thinking .extra-dot{ animation-delay:0.25s; }
|
| 73 |
.wrap.thinking .eye-right{ animation-delay:0.5s; }
|
| 74 |
@keyframes wave { 0%,100%{ transform:translateY(0); } 50%{ transform:translateY(-12%); } }
|
|
|
|
| 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; }
|
| 90 |
.gradio-container { border-radius: 25px; padding: 20px; max-width: 700px; margin: 20px auto; background-color: #fafafa; box-shadow: 0 4px 20px rgba(0,0,0,0.05);}
|
|
|
|
| 95 |
with gr.Sidebar():
|
| 96 |
gr.LoginButton()
|
| 97 |
|
|
|
|
|
|
|
|
|
|
| 98 |
chatbot.render()
|
| 99 |
+
# Ícone abaixo do chat
|
| 100 |
+
gr.HTML(bit_icon_html)
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.launch()
|