Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
# Função
|
| 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 |
-
system_message = """You are BitAI (or Bit for short), a friendly chatbot created by "Sal".
|
| 8 |
You respond politely, refuse inappropriate requests, and keep a friendly tone."""
|
| 9 |
|
| 10 |
messages = [{"role": "system", "content": system_message}]
|
|
@@ -17,12 +17,11 @@ You respond politely, refuse inappropriate requests, and keep a friendly tone.""
|
|
| 17 |
response += token
|
| 18 |
yield response
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
<script>
|
| 23 |
-
|
| 24 |
const grContainer = document.querySelector(".gradio-container");
|
| 25 |
-
|
| 26 |
if(grContainer && !document.querySelector(".chat-title")) {
|
| 27 |
const titleContainer = document.createElement("div");
|
| 28 |
titleContainer.className = "title-container";
|
|
@@ -31,7 +30,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 31 |
<h1 class="chat-title">Talk with BitAI (W.I.P)</h1>
|
| 32 |
<p class="chat-subtitle">Still updating, might not know everything, filter slightly broken</p>
|
| 33 |
<button class="close-title-btn">
|
| 34 |
-
<svg width="
|
| 35 |
<path fill="#fff" d="M12 16l-6-6h12z"/>
|
| 36 |
</svg>
|
| 37 |
</button>
|
|
@@ -45,7 +44,7 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 45 |
titleContainer.style.transform = "translateY(0)";
|
| 46 |
}, 100);
|
| 47 |
|
| 48 |
-
//
|
| 49 |
const closeBtn = titleContainer.querySelector(".close-title-btn");
|
| 50 |
closeBtn.addEventListener("click", () => {
|
| 51 |
titleContainer.style.transition = "all 0.4s ease";
|
|
@@ -54,78 +53,73 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 54 |
setTimeout(() => titleContainer.remove(), 400);
|
| 55 |
});
|
| 56 |
}
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
startupMsg.className="startup-message";
|
| 63 |
-
startupMsg.innerHTML='<div class="startup-content"><div class="startup-icon">🤖</div><div class="startup-text"><div class="startup-title">BitAI</div><div class="startup-subtitle">How was your day? Good morning!</div><div class="startup-note">Entirely AI</div></div></div>';
|
| 64 |
-
chatContainer.appendChild(startupMsg);
|
| 65 |
-
setTimeout(()=>{startupMsg.style.opacity="1"; startupMsg.style.transform="translateY(0)";},100);
|
| 66 |
-
}
|
| 67 |
-
|
| 68 |
-
// Observador para animação de novas mensagens
|
| 69 |
-
const observer = new MutationObserver(mutations => {
|
| 70 |
-
mutations.forEach(m => {
|
| 71 |
-
m.addedNodes.forEach(node => {
|
| 72 |
-
if(node.classList?.contains('chat-message')){
|
| 73 |
-
const startupMsg = document.querySelector(".startup-message");
|
| 74 |
-
if(startupMsg){startupMsg.style.opacity="0"; startupMsg.style.transform="translateY(20px)"; setTimeout(()=>startupMsg.remove(),300);}
|
| 75 |
-
node.style.opacity=0; node.style.transform='translateY(20px) scale(0.95)';
|
| 76 |
-
node.style.transition='all 0.5s ease, box-shadow 0.3s ease';
|
| 77 |
-
requestAnimationFrame(()=>{node.style.opacity=1; node.style.transform='translateY(0) scale(1)'; setTimeout(()=>node.style.boxShadow='0 4px 15px rgba(0,0,0,0.2)',500);});
|
| 78 |
-
}
|
| 79 |
-
});
|
| 80 |
-
});
|
| 81 |
-
});
|
| 82 |
-
if(chatContainer) observer.observe(chatContainer, {childList:true, subtree:true});
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
const btn = document.querySelector(".send-btn");
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
};
|
| 95 |
-
document.querySelectorAll("button, .chat-message, textarea, .gr-button.gr-login, .chat-title, .chat-subtitle, .startup-message, .gr-chatbot").forEach(bounce);
|
| 96 |
});
|
| 97 |
</script>
|
| 98 |
|
| 99 |
<style>
|
| 100 |
-
.title-container {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
.title-inner { display:inline-block; position:relative; }
|
| 102 |
.chat-title { font-size:32px; color:#fff; margin-bottom:10px; cursor:pointer; }
|
| 103 |
.chat-subtitle { font-size:14px; color:#aaa; margin-bottom:10px; }
|
| 104 |
-
.close-title-btn {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
.close-title-btn:hover { transform:scale(1.2); }
|
| 106 |
-
.
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
| 112 |
</style>
|
| 113 |
"""
|
| 114 |
|
| 115 |
-
#
|
| 116 |
with gr.Blocks(css="""
|
| 117 |
-
body{background:#000;
|
| 118 |
-
.gradio-container{border-radius:20px;padding:20px;max-width:700px;margin:30px auto;
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
.chat-message.bot{background:linear-gradient(145deg,#2e2e2e,#252525);color:#fff;align-items:flex-start;margin-right:40px;}
|
| 124 |
""") as demo:
|
| 125 |
-
|
| 126 |
gr.LoginButton()
|
| 127 |
gr.ChatInterface(respond, type="messages")
|
| 128 |
-
gr.HTML(
|
| 129 |
|
| 130 |
if __name__ == "__main__":
|
| 131 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Função do bot
|
| 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 |
+
system_message = """You are BitAI (or Bit for short), a friendly chatbot created by the user "Sal".
|
| 8 |
You respond politely, refuse inappropriate requests, and keep a friendly tone."""
|
| 9 |
|
| 10 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
| 17 |
response += token
|
| 18 |
yield response
|
| 19 |
|
| 20 |
+
# Script + CSS
|
| 21 |
+
custom_js = """
|
| 22 |
<script>
|
| 23 |
+
function initTitle() {
|
| 24 |
const grContainer = document.querySelector(".gradio-container");
|
|
|
|
| 25 |
if(grContainer && !document.querySelector(".chat-title")) {
|
| 26 |
const titleContainer = document.createElement("div");
|
| 27 |
titleContainer.className = "title-container";
|
|
|
|
| 30 |
<h1 class="chat-title">Talk with BitAI (W.I.P)</h1>
|
| 31 |
<p class="chat-subtitle">Still updating, might not know everything, filter slightly broken</p>
|
| 32 |
<button class="close-title-btn">
|
| 33 |
+
<svg width="28" height="28" viewBox="0 0 24 24">
|
| 34 |
<path fill="#fff" d="M12 16l-6-6h12z"/>
|
| 35 |
</svg>
|
| 36 |
</button>
|
|
|
|
| 44 |
titleContainer.style.transform = "translateY(0)";
|
| 45 |
}, 100);
|
| 46 |
|
| 47 |
+
// fechar com a seta
|
| 48 |
const closeBtn = titleContainer.querySelector(".close-title-btn");
|
| 49 |
closeBtn.addEventListener("click", () => {
|
| 50 |
titleContainer.style.transition = "all 0.4s ease";
|
|
|
|
| 53 |
setTimeout(() => titleContainer.remove(), 400);
|
| 54 |
});
|
| 55 |
}
|
| 56 |
+
}
|
| 57 |
|
| 58 |
+
// esperar carregar tudo
|
| 59 |
+
window.addEventListener("load", () => {
|
| 60 |
+
setTimeout(initTitle, 300);
|
| 61 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
// deixar botão de enviar grande
|
| 64 |
+
window.addEventListener("load", () => {
|
| 65 |
+
const txtArea = document.querySelector("textarea");
|
| 66 |
const btn = document.querySelector(".send-btn");
|
| 67 |
+
if(txtArea && btn){
|
| 68 |
+
const resize = () => {
|
| 69 |
+
btn.style.height = txtArea.offsetHeight + "px";
|
| 70 |
+
btn.style.width = txtArea.offsetHeight + "px";
|
| 71 |
+
};
|
| 72 |
+
resize();
|
| 73 |
+
new ResizeObserver(resize).observe(txtArea);
|
| 74 |
+
}
|
|
|
|
|
|
|
| 75 |
});
|
| 76 |
</script>
|
| 77 |
|
| 78 |
<style>
|
| 79 |
+
.title-container {
|
| 80 |
+
text-align:center;
|
| 81 |
+
margin-bottom:25px;
|
| 82 |
+
opacity:0;
|
| 83 |
+
transform:translateY(-20px);
|
| 84 |
+
transition:0.5s;
|
| 85 |
+
position:relative;
|
| 86 |
+
}
|
| 87 |
.title-inner { display:inline-block; position:relative; }
|
| 88 |
.chat-title { font-size:32px; color:#fff; margin-bottom:10px; cursor:pointer; }
|
| 89 |
.chat-subtitle { font-size:14px; color:#aaa; margin-bottom:10px; }
|
| 90 |
+
.close-title-btn {
|
| 91 |
+
position:absolute;
|
| 92 |
+
right:-15px; top:-15px;
|
| 93 |
+
background:transparent;
|
| 94 |
+
border:none;
|
| 95 |
+
cursor:pointer;
|
| 96 |
+
padding:5px;
|
| 97 |
+
transition: transform 0.2s;
|
| 98 |
+
}
|
| 99 |
.close-title-btn:hover { transform:scale(1.2); }
|
| 100 |
+
.send-btn {
|
| 101 |
+
border:none;
|
| 102 |
+
border-radius:50%;
|
| 103 |
+
background:linear-gradient(145deg,#555,#444);
|
| 104 |
+
color:#fff;
|
| 105 |
+
cursor:pointer;
|
| 106 |
+
transition:0.3s;
|
| 107 |
+
}
|
| 108 |
</style>
|
| 109 |
"""
|
| 110 |
|
| 111 |
+
# Interface
|
| 112 |
with gr.Blocks(css="""
|
| 113 |
+
body{background:#000;font-family:'Inter','Manrope','Outfit',sans-serif;}
|
| 114 |
+
.gradio-container{border-radius:20px;padding:20px;max-width:700px;margin:30px auto;
|
| 115 |
+
background:linear-gradient(145deg,#0f0f0f,#1a1a1a);
|
| 116 |
+
box-shadow:0 10px 30px rgba(0,0,0,0.4);}
|
| 117 |
+
textarea{border:none;border-radius:25px;padding:14px 20px;
|
| 118 |
+
background:#1a1a1a;color:#fff;font-size:16px;width:100%;min-height:56px;}
|
|
|
|
| 119 |
""") as demo:
|
|
|
|
| 120 |
gr.LoginButton()
|
| 121 |
gr.ChatInterface(respond, type="messages")
|
| 122 |
+
gr.HTML(custom_js)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
demo.launch()
|