Update app.py
Browse files
app.py
CHANGED
|
@@ -3,27 +3,19 @@ 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 |
-
|
| 7 |
system_message = """You are BitAI (or Bit for short), a friendly chatbot created by the user "Sal".
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
Keep a simple, approachable, and friendly tone otherwise."""
|
| 12 |
-
|
| 13 |
-
messages = [{"role": "system", "content": system_message}]
|
| 14 |
messages.extend(history)
|
| 15 |
-
messages.append({"role":
|
| 16 |
|
| 17 |
response = ""
|
| 18 |
for chunk in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
|
| 19 |
-
token = ""
|
| 20 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 21 |
-
|
| 22 |
-
response += token
|
| 23 |
yield response
|
| 24 |
|
| 25 |
-
chatbot = gr.ChatInterface(respond, type="messages")
|
| 26 |
-
|
| 27 |
fade_js = """
|
| 28 |
<script>
|
| 29 |
const observer = new MutationObserver(mutations=>{
|
|
@@ -31,99 +23,51 @@ const observer = new MutationObserver(mutations=>{
|
|
| 31 |
m.addedNodes.forEach(node=>{
|
| 32 |
if(node.classList && node.classList.contains('chat-message')){
|
| 33 |
node.style.opacity = 0;
|
| 34 |
-
node.style.
|
|
|
|
| 35 |
requestAnimationFrame(()=>{
|
| 36 |
node.style.opacity = 1;
|
|
|
|
| 37 |
});
|
| 38 |
}
|
| 39 |
});
|
| 40 |
});
|
| 41 |
});
|
| 42 |
-
|
| 43 |
document.addEventListener("DOMContentLoaded", ()=>{
|
| 44 |
const chatContainer = document.querySelector(".chat-interface");
|
| 45 |
-
if(chatContainer) observer.observe(chatContainer,
|
| 46 |
});
|
| 47 |
</script>
|
| 48 |
"""
|
| 49 |
|
| 50 |
with gr.Blocks(css="""
|
| 51 |
-
body {
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
}
|
| 57 |
-
.
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
background-color:#121212;
|
| 63 |
-
box-shadow:0 6px 25px rgba(0,0,0,0.3);
|
| 64 |
-
}
|
| 65 |
-
.chat-message {
|
| 66 |
-
border-radius:45px;
|
| 67 |
-
padding:14px 18px;
|
| 68 |
-
margin:8px 0;
|
| 69 |
-
display:flex;
|
| 70 |
-
flex-direction:column;
|
| 71 |
-
opacity:0;
|
| 72 |
-
}
|
| 73 |
-
.chat-message.user {
|
| 74 |
-
background-color:#1f1f1f;
|
| 75 |
-
color:#fff;
|
| 76 |
-
align-items:flex-end;
|
| 77 |
-
}
|
| 78 |
-
.chat-message.bot {
|
| 79 |
-
background-color:#2b2b2b;
|
| 80 |
-
color:#fff;
|
| 81 |
-
align-items:flex-start;
|
| 82 |
-
}
|
| 83 |
-
textarea {
|
| 84 |
-
border:none;
|
| 85 |
-
outline:none;
|
| 86 |
-
border-radius:45px;
|
| 87 |
-
padding:12px;
|
| 88 |
-
background-color:#1a1a1a;
|
| 89 |
-
color:#fff;
|
| 90 |
-
font-size:16px;
|
| 91 |
-
width:100%;
|
| 92 |
-
box-sizing:border-box;
|
| 93 |
-
}
|
| 94 |
-
.send-btn {
|
| 95 |
-
border:none;
|
| 96 |
-
border-radius:45px;
|
| 97 |
-
background-color:#444;
|
| 98 |
-
color:#fff;
|
| 99 |
-
width:48px;
|
| 100 |
-
height:48px;
|
| 101 |
-
font-size:18px;
|
| 102 |
-
display:flex;
|
| 103 |
-
align-items:center;
|
| 104 |
-
justify-content:center;
|
| 105 |
-
cursor:pointer;
|
| 106 |
-
margin-left:8px;
|
| 107 |
-
}
|
| 108 |
-
.send-btn:hover {
|
| 109 |
-
background-color:#555;
|
| 110 |
-
}
|
| 111 |
-
/* login button default Gradio */
|
| 112 |
-
.gr-button.gr-login {
|
| 113 |
-
border-radius:45px !important;
|
| 114 |
-
background-color:#444 !important;
|
| 115 |
-
color:#fff !important;
|
| 116 |
-
margin-bottom:15px;
|
| 117 |
-
}
|
| 118 |
-
.gr-button.gr-login:hover {
|
| 119 |
-
background-color:#555 !important;
|
| 120 |
-
}
|
| 121 |
""") as demo:
|
| 122 |
|
| 123 |
# Login direto na página
|
| 124 |
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
gr.HTML(fade_js)
|
| 128 |
|
| 129 |
if __name__=="__main__":
|
|
|
|
| 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 |
system_message = """You are BitAI (or Bit for short), a friendly chatbot created by the user "Sal".
|
| 7 |
+
Respond naturally. Respectfully refuse harmful requests, and if user insists, tell them to stop using the service."""
|
| 8 |
+
|
| 9 |
+
messages = [{"role":"system","content":system_message}]
|
|
|
|
|
|
|
|
|
|
| 10 |
messages.extend(history)
|
| 11 |
+
messages.append({"role":"user","content":message})
|
| 12 |
|
| 13 |
response = ""
|
| 14 |
for chunk in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
|
|
|
|
| 15 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 16 |
+
response += chunk.choices[0].delta.content
|
|
|
|
| 17 |
yield response
|
| 18 |
|
|
|
|
|
|
|
| 19 |
fade_js = """
|
| 20 |
<script>
|
| 21 |
const observer = new MutationObserver(mutations=>{
|
|
|
|
| 23 |
m.addedNodes.forEach(node=>{
|
| 24 |
if(node.classList && node.classList.contains('chat-message')){
|
| 25 |
node.style.opacity = 0;
|
| 26 |
+
node.style.transform = 'translateY(20px)';
|
| 27 |
+
node.style.transition = 'opacity 0.3s ease, transform 0.3s ease';
|
| 28 |
requestAnimationFrame(()=>{
|
| 29 |
node.style.opacity = 1;
|
| 30 |
+
node.style.transform = 'translateY(0)';
|
| 31 |
});
|
| 32 |
}
|
| 33 |
});
|
| 34 |
});
|
| 35 |
});
|
|
|
|
| 36 |
document.addEventListener("DOMContentLoaded", ()=>{
|
| 37 |
const chatContainer = document.querySelector(".chat-interface");
|
| 38 |
+
if(chatContainer) observer.observe(chatContainer,{childList:true,subtree:true});
|
| 39 |
});
|
| 40 |
</script>
|
| 41 |
"""
|
| 42 |
|
| 43 |
with gr.Blocks(css="""
|
| 44 |
+
body { background-color:#000; font-family:'Arial',sans-serif; margin:0; padding:0; }
|
| 45 |
+
.gradio-container { border-radius:45px; padding:20px; max-width:700px; margin:30px auto; background-color:#121212; box-shadow:0 6px 25px rgba(0,0,0,0.3);}
|
| 46 |
+
.chat-message { border-radius:45px; padding:14px 18px; margin:8px 0; display:flex; flex-direction:column; opacity:0;}
|
| 47 |
+
.chat-message.user { background-color:#1f1f1f; color:#fff; align-items:flex-end;}
|
| 48 |
+
.chat-message.bot { background-color:#2b2b2b; color:#fff; align-items:flex-start;}
|
| 49 |
+
textarea { border:none; outline:none; border-radius:45px; padding:12px; background-color:#1a1a1a; color:#fff; font-size:16px; width:calc(100% - 60px); box-sizing:border-box; }
|
| 50 |
+
.send-btn { border:none; border-radius:45px; background-color:#444; color:#fff; width:60px; height:60px; font-size:18px; display:flex; align-items:center; justify-content:center; cursor:pointer; margin-left:8px;}
|
| 51 |
+
.send-btn:hover { background-color:#555;}
|
| 52 |
+
.gr-button.gr-login { border-radius:45px !important; background-color:#222 !important; color:#fff !important; margin-bottom:15px;}
|
| 53 |
+
.gr-button.gr-login:hover { background-color:#444 !important;}
|
| 54 |
+
.input-container { display:flex; margin-top:10px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
""") as demo:
|
| 56 |
|
| 57 |
# Login direto na página
|
| 58 |
gr.LoginButton()
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
chatbot = gr.ChatInterface(respond, type="messages")
|
| 62 |
|
| 63 |
+
# Campo de texto + botão enviar
|
| 64 |
+
with gr.Row(elem_classes=["input-container"]):
|
| 65 |
+
textbox = gr.Textbox(label="", placeholder="Escreva algo...", lines=1)
|
| 66 |
+
send = gr.Button("→", elem_classes=["send-btn"])
|
| 67 |
+
|
| 68 |
+
# Conecta botão ao chatbot
|
| 69 |
+
send.click(chatbot.submit, inputs=[textbox], outputs=[chatbot])
|
| 70 |
+
|
| 71 |
gr.HTML(fade_js)
|
| 72 |
|
| 73 |
if __name__=="__main__":
|