Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
def respond(message, history, hf_token):
|
| 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 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 17 |
yield response
|
| 18 |
|
|
|
|
|
|
|
| 19 |
fade_js = """
|
| 20 |
<script>
|
| 21 |
const observer = new MutationObserver(mutations=>{
|
|
@@ -23,50 +31,101 @@ 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.
|
| 27 |
-
node.style.
|
| 28 |
requestAnimationFrame(()=>{
|
| 29 |
node.style.opacity = 1;
|
| 30 |
-
node.style.transform =
|
| 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 {
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
.
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
""") as demo:
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
chatbot = gr.Chatbot(elem_classes=["chat-interface"])
|
| 60 |
-
textbox = gr.Textbox(label="", placeholder="Escreva algo...", lines=1)
|
| 61 |
-
send = gr.Button("→", elem_classes=["send-btn"])
|
| 62 |
-
|
| 63 |
-
# quando clicar enviar, atualiza o chatbot
|
| 64 |
-
def submit(message, history, hf_token):
|
| 65 |
-
for r in respond(message, history or [], hf_token):
|
| 66 |
-
yield history + [[message, r]]
|
| 67 |
-
|
| 68 |
-
send.click(submit, inputs=[textbox, chatbot, gr.State()], outputs=[chatbot])
|
| 69 |
|
|
|
|
| 70 |
gr.HTML(fade_js)
|
| 71 |
|
| 72 |
if __name__=="__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
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 |
+
You always respond politely and helpfully. If a user requests something appropriate, fulfill it.
|
| 9 |
+
If a user requests something harmful, illegal, or inappropriate, politely refuse.
|
| 10 |
+
If a user keeps insisting on harmful requests, firmly tell them to stop and that they cannot use the service for that purpose.
|
| 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": "user", "content": message})
|
| 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 |
+
token = chunk.choices[0].delta.content
|
| 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 |
m.addedNodes.forEach(node=>{
|
| 32 |
if(node.classList && node.classList.contains('chat-message')){
|
| 33 |
node.style.opacity = 0;
|
| 34 |
+
node.style.transition = "opacity 0.4s ease, transform 0.4s ease";
|
| 35 |
+
node.style.transform = "translateY(10px)";
|
| 36 |
requestAnimationFrame(()=>{
|
| 37 |
node.style.opacity = 1;
|
| 38 |
+
node.style.transform = "translateY(0)";
|
| 39 |
});
|
| 40 |
}
|
| 41 |
});
|
| 42 |
});
|
| 43 |
});
|
| 44 |
+
|
| 45 |
document.addEventListener("DOMContentLoaded", ()=>{
|
| 46 |
const chatContainer = document.querySelector(".chat-interface");
|
| 47 |
+
if(chatContainer) observer.observe(chatContainer, {childList:true, subtree:true});
|
| 48 |
});
|
| 49 |
</script>
|
| 50 |
"""
|
| 51 |
|
| 52 |
with gr.Blocks(css="""
|
| 53 |
+
body {
|
| 54 |
+
background-color:#000;
|
| 55 |
+
font-family:'Arial',sans-serif;
|
| 56 |
+
margin:0;
|
| 57 |
+
padding:0;
|
| 58 |
+
}
|
| 59 |
+
.gradio-container {
|
| 60 |
+
border-radius:45px;
|
| 61 |
+
padding:20px;
|
| 62 |
+
max-width:700px;
|
| 63 |
+
margin:30px auto;
|
| 64 |
+
background-color:#121212;
|
| 65 |
+
box-shadow:0 6px 25px rgba(0,0,0,0.3);
|
| 66 |
+
}
|
| 67 |
+
.chat-message {
|
| 68 |
+
border-radius:45px;
|
| 69 |
+
padding:14px 18px;
|
| 70 |
+
margin:8px 0;
|
| 71 |
+
display:flex;
|
| 72 |
+
flex-direction:column;
|
| 73 |
+
opacity:0;
|
| 74 |
+
}
|
| 75 |
+
.chat-message.user {
|
| 76 |
+
background-color:#1f1f1f;
|
| 77 |
+
color:#fff;
|
| 78 |
+
align-items:flex-end;
|
| 79 |
+
}
|
| 80 |
+
.chat-message.bot {
|
| 81 |
+
background-color:#2b2b2b;
|
| 82 |
+
color:#fff;
|
| 83 |
+
align-items:flex-start;
|
| 84 |
+
}
|
| 85 |
+
textarea {
|
| 86 |
+
border:none;
|
| 87 |
+
outline:none;
|
| 88 |
+
border-radius:45px;
|
| 89 |
+
padding:12px;
|
| 90 |
+
background-color:#1a1a1a;
|
| 91 |
+
color:#fff;
|
| 92 |
+
font-size:16px;
|
| 93 |
+
width:100%;
|
| 94 |
+
box-sizing:border-box;
|
| 95 |
+
}
|
| 96 |
+
.send-btn {
|
| 97 |
+
border:none;
|
| 98 |
+
border-radius:45px;
|
| 99 |
+
background-color:#444;
|
| 100 |
+
color:#fff;
|
| 101 |
+
width:48px;
|
| 102 |
+
height:48px;
|
| 103 |
+
font-size:18px;
|
| 104 |
+
display:flex;
|
| 105 |
+
align-items:center;
|
| 106 |
+
justify-content:center;
|
| 107 |
+
cursor:pointer;
|
| 108 |
+
margin-left:8px;
|
| 109 |
+
}
|
| 110 |
+
.send-btn:hover {
|
| 111 |
+
background-color:#555;
|
| 112 |
+
}
|
| 113 |
+
/* login button default Gradio */
|
| 114 |
+
.gr-button.gr-login {
|
| 115 |
+
border-radius:45px !important;
|
| 116 |
+
background-color:#444 !important;
|
| 117 |
+
color:#fff !important;
|
| 118 |
+
margin-bottom:15px;
|
| 119 |
+
}
|
| 120 |
+
.gr-button.gr-login:hover {
|
| 121 |
+
background-color:#555 !important;
|
| 122 |
+
}
|
| 123 |
""") as demo:
|
| 124 |
|
| 125 |
+
# Login direto na página
|
| 126 |
+
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
+
chatbot.render()
|
| 129 |
gr.HTML(fade_js)
|
| 130 |
|
| 131 |
if __name__=="__main__":
|