Update app.py
Browse files
app.py
CHANGED
|
@@ -1,135 +1,132 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
def respond(message, history: list[dict[str, str]]):
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
You
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
messages.extend(history)
|
| 17 |
-
messages.append({"role": "user", "content": message})
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
max_tokens=2048,
|
| 23 |
-
stream=True,
|
| 24 |
-
temperature=0.7,
|
| 25 |
-
top_p=0.95,
|
| 26 |
-
):
|
| 27 |
-
token = message.choices[0].delta.content if message.choices else ""
|
| 28 |
-
response += token
|
| 29 |
-
yield response
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
transition: background-color 0.5s;
|
| 51 |
-
}
|
| 52 |
-
.gr-button:active, .gr-chat-send-button:active {
|
| 53 |
-
background-color: white !important;
|
| 54 |
-
color: #111 !important;
|
| 55 |
-
transition: background-color 0.5s;
|
| 56 |
-
}
|
| 57 |
|
| 58 |
-
/*
|
| 59 |
-
button
|
| 60 |
-
border-radius:
|
| 61 |
-
padding:
|
| 62 |
-
background-color: #
|
| 63 |
-
color: white;
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
/*
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
/*
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
background:
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
}
|
| 94 |
-
#pupil {
|
| 95 |
-
width: 10px;
|
| 96 |
-
height: 10px;
|
| 97 |
-
background: black;
|
| 98 |
-
border-radius: 50%;
|
| 99 |
-
position: absolute;
|
| 100 |
-
top: 10px;
|
| 101 |
-
left: 10px;
|
| 102 |
-
animation: lookAround 3s infinite;
|
| 103 |
-
}
|
| 104 |
-
@keyframes lookAround {
|
| 105 |
-
0% { top: 8px; left: 8px; }
|
| 106 |
-
25% { top: 8px; left: 12px; }
|
| 107 |
-
50% { top: 12px; left: 12px; }
|
| 108 |
-
75% { top: 12px; left: 8px; }
|
| 109 |
-
100% { top: 8px; left: 8px; }
|
| 110 |
-
}
|
| 111 |
-
""") as demo:
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
// Mostrar a bolinha depois que a IA termina de digitar
|
| 123 |
-
const observer = new MutationObserver(() => {
|
| 124 |
-
const messages = document.querySelectorAll('.gr-chat-message');
|
| 125 |
-
const lastMsg = messages[messages.length-1];
|
| 126 |
-
if(lastMsg && lastMsg.classList.contains('gr-chat-message-assistant')){
|
| 127 |
-
document.getElementById('bitai-eye').style.display = 'block';
|
| 128 |
-
}
|
| 129 |
-
});
|
| 130 |
-
observer.observe(document.querySelector('.gr-chat-interface'), {childList: true});
|
| 131 |
-
</script>
|
| 132 |
-
""")
|
| 133 |
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
def respond(message, history: list[dict[str, str]]):
|
| 6 |
+
# Puxa o token do secret do Hugging Face
|
| 7 |
+
client = InferenceClient(token=os.environ["HF_TOKEN"], model="openai/gpt-oss-20b")
|
| 8 |
|
| 9 |
+
# System prompt amigável
|
| 10 |
+
system_message = """
|
| 11 |
+
You are BitAI (V1), a friendly, curious, and talkative chatbot created by the user 'Sal'.
|
| 12 |
+
You can share opinions, answer casual questions, and chat about personal-style topics in a safe and friendly way.
|
| 13 |
+
Avoid repeating the same phrases, and always try to keep the conversation engaging and natural.
|
| 14 |
+
Politely refuse only things that are truly harmful, illegal, or unsafe.
|
| 15 |
+
If someone asks what you are, clarify politely that you are BitAI, an AI chatbot.
|
| 16 |
+
"""
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
messages = [{"role": "system", "content": system_message}]
|
| 19 |
+
messages.extend(history)
|
| 20 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
response = ""
|
| 23 |
+
for message in client.chat_completion(
|
| 24 |
+
messages,
|
| 25 |
+
max_tokens=2048,
|
| 26 |
+
stream=True,
|
| 27 |
+
temperature=0.7,
|
| 28 |
+
top_p=0.95,
|
| 29 |
+
):
|
| 30 |
+
choices = message.choices
|
| 31 |
+
token = ""
|
| 32 |
+
if len(choices) and choices[0].delta.content:
|
| 33 |
+
token = choices[0].delta.content
|
| 34 |
+
if token is not None: # evita TypeError
|
| 35 |
+
response += token
|
| 36 |
+
yield response
|
| 37 |
|
| 38 |
+
with gr.Blocks(css="""
|
| 39 |
+
/* Chat arredondado */
|
| 40 |
+
.gr-chat-interface {
|
| 41 |
+
border-radius: 20px !important;
|
| 42 |
+
overflow: hidden !important;
|
| 43 |
+
border: 2px solid #333 !important;
|
| 44 |
+
background-color: #1a1a1a !important;
|
| 45 |
+
color: white;
|
| 46 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
/* Botões grandes escuros com cantos muito arredondados */
|
| 49 |
+
.gr-button, .gr-chat-send-button {
|
| 50 |
+
border-radius: 50px;
|
| 51 |
+
padding: 12px 20px;
|
| 52 |
+
background-color: #111;
|
| 53 |
+
color: white;
|
| 54 |
+
font-weight: bold;
|
| 55 |
+
cursor: pointer;
|
| 56 |
+
height: 50px;
|
| 57 |
+
transition: background-color 0.5s;
|
| 58 |
+
}
|
| 59 |
+
.gr-button:active, .gr-chat-send-button:active {
|
| 60 |
+
background-color: white !important;
|
| 61 |
+
color: #111 !important;
|
| 62 |
+
transition: background-color 0.5s;
|
| 63 |
+
}
|
| 64 |
|
| 65 |
+
/* Outros botões menores */
|
| 66 |
+
button:not(.gr-chat-send-button) {
|
| 67 |
+
border-radius: 30px;
|
| 68 |
+
padding: 6px 12px;
|
| 69 |
+
background-color: #222;
|
| 70 |
+
color: white;
|
| 71 |
+
height: 40px;
|
| 72 |
+
transition: background-color 0.5s;
|
| 73 |
+
}
|
| 74 |
+
button:not(.gr-chat-send-button):active {
|
| 75 |
+
background-color: white !important;
|
| 76 |
+
color: #111 !important;
|
| 77 |
+
transition: background-color 0.5s;
|
| 78 |
+
}
|
| 79 |
|
| 80 |
+
/* Textbox menor */
|
| 81 |
+
textarea {
|
| 82 |
+
height: 40px !important;
|
| 83 |
+
border-radius: 20px !important;
|
| 84 |
+
border: 1px solid #444 !important;
|
| 85 |
+
padding: 8px !important;
|
| 86 |
+
background-color: #111;
|
| 87 |
+
color: white;
|
| 88 |
+
resize: none !important;
|
| 89 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
/* Bolinha animada da IA olhando pro usuário */
|
| 92 |
+
#bitai-eye {
|
| 93 |
+
width: 30px;
|
| 94 |
+
height: 30px;
|
| 95 |
+
margin: 10px auto 0 auto;
|
| 96 |
+
border-radius: 50%;
|
| 97 |
+
background: radial-gradient(circle at 10px 10px, #fff, #ccc);
|
| 98 |
+
position: relative;
|
| 99 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
+
#pupil {
|
| 102 |
+
width: 10px;
|
| 103 |
+
height: 10px;
|
| 104 |
+
background: black;
|
| 105 |
+
border-radius: 50%;
|
| 106 |
+
position: absolute;
|
| 107 |
+
top: 10px;
|
| 108 |
+
left: 10px;
|
| 109 |
+
animation: lookAround 3s infinite;
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
@keyframes lookAround {
|
| 113 |
+
0% { top: 8px; left: 8px; }
|
| 114 |
+
25% { top: 8px; left: 12px; }
|
| 115 |
+
50% { top: 12px; left: 12px; }
|
| 116 |
+
75% { top: 12px; left: 8px; }
|
| 117 |
+
100% { top: 8px; left: 8px; }
|
| 118 |
+
}
|
| 119 |
+
""") as demo:
|
| 120 |
+
|
| 121 |
+
with gr.Column():
|
| 122 |
+
gr.HTML("<h2 style='text-align:center; color:white'>BitAI</h2>")
|
| 123 |
+
chatbot = gr.ChatInterface(respond, type="messages")
|
| 124 |
+
# Bolinha da IA aparecendo sempre embaixo do chat
|
| 125 |
+
gr.HTML("""
|
| 126 |
+
<div id="bitai-eye">
|
| 127 |
+
<div id="pupil"></div>
|
| 128 |
+
</div>
|
| 129 |
+
""")
|
| 130 |
+
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
demo.launch()
|