Update app.py
Browse files
app.py
CHANGED
|
@@ -8,21 +8,20 @@ You always respond politely and helpfully. If a user requests something appropri
|
|
| 8 |
If a user requests something harmful, illegal, or inappropriate, politely refuse.
|
| 9 |
If a user keeps insisting on harmful requests, firmly tell them to stop and that they cannot use the service for that purpose.
|
| 10 |
Keep a simple, approachable, and friendly tone otherwise."""
|
| 11 |
-
|
| 12 |
messages = [{"role": "system", "content": system_message}]
|
| 13 |
messages.extend(history)
|
| 14 |
messages.append({"role": "user", "content": message})
|
| 15 |
|
| 16 |
response = ""
|
| 17 |
for chunk in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
|
| 18 |
-
token = ""
|
| 19 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 20 |
-
|
| 21 |
-
response += token
|
| 22 |
yield response
|
| 23 |
|
| 24 |
fade_js = """
|
| 25 |
<script>
|
|
|
|
| 26 |
const observer = new MutationObserver(mutations=>{
|
| 27 |
mutations.forEach(m=>{
|
| 28 |
m.addedNodes.forEach(node=>{
|
|
@@ -38,21 +37,20 @@ const observer = new MutationObserver(mutations=>{
|
|
| 38 |
});
|
| 39 |
});
|
| 40 |
});
|
|
|
|
|
|
|
| 41 |
document.addEventListener("DOMContentLoaded", ()=>{
|
| 42 |
const chatContainer = document.querySelector(".chat-interface");
|
| 43 |
if(chatContainer) observer.observe(chatContainer, {childList:true, subtree:true});
|
| 44 |
|
| 45 |
-
// auto resize do textarea
|
| 46 |
const txt = document.querySelector("textarea");
|
| 47 |
if(txt){
|
| 48 |
-
txt.style.transition = "height 0.2s ease";
|
| 49 |
txt.addEventListener("input", ()=>{
|
| 50 |
txt.style.height = "auto";
|
| 51 |
-
txt.style.height =
|
| 52 |
});
|
| 53 |
}
|
| 54 |
|
| 55 |
-
// animação de clique do botão
|
| 56 |
const btn = document.querySelector(".send-btn");
|
| 57 |
if(btn){
|
| 58 |
btn.addEventListener("click", ()=>{
|
|
@@ -72,30 +70,60 @@ body { background-color:#000; font-family:'Arial',sans-serif; margin:0; padding:
|
|
| 72 |
.chat-message.bot { background-color:#2b2b2b; color:#fff; align-items:flex-start;}
|
| 73 |
|
| 74 |
/* input com botão dentro */
|
| 75 |
-
.input-
|
| 76 |
-
display:flex;
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
}
|
| 80 |
textarea {
|
| 81 |
-
border:none;
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
.send-btn {
|
| 87 |
-
position:absolute;
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
}
|
| 92 |
.send-btn:hover { background-color:#555; }
|
|
|
|
| 93 |
.gr-button.gr-login { border-radius:45px !important; background-color:#222 !important; color:#fff !important; margin-bottom:15px;}
|
| 94 |
.gr-button.gr-login:hover { background-color:#444 !important;}
|
| 95 |
""") as demo:
|
| 96 |
|
| 97 |
gr.LoginButton()
|
| 98 |
chatbot = gr.ChatInterface(respond, type="messages")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
gr.HTML(fade_js)
|
| 100 |
|
| 101 |
if __name__=="__main__":
|
|
|
|
| 8 |
If a user requests something harmful, illegal, or inappropriate, politely refuse.
|
| 9 |
If a user keeps insisting on harmful requests, firmly tell them to stop and that they cannot use the service for that purpose.
|
| 10 |
Keep a simple, approachable, and friendly tone otherwise."""
|
| 11 |
+
|
| 12 |
messages = [{"role": "system", "content": system_message}]
|
| 13 |
messages.extend(history)
|
| 14 |
messages.append({"role": "user", "content": message})
|
| 15 |
|
| 16 |
response = ""
|
| 17 |
for chunk in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
|
|
|
|
| 18 |
if chunk.choices and chunk.choices[0].delta.content:
|
| 19 |
+
response += chunk.choices[0].delta.content
|
|
|
|
| 20 |
yield response
|
| 21 |
|
| 22 |
fade_js = """
|
| 23 |
<script>
|
| 24 |
+
// Fade-in das mensagens
|
| 25 |
const observer = new MutationObserver(mutations=>{
|
| 26 |
mutations.forEach(m=>{
|
| 27 |
m.addedNodes.forEach(node=>{
|
|
|
|
| 37 |
});
|
| 38 |
});
|
| 39 |
});
|
| 40 |
+
|
| 41 |
+
// Auto-expand textarea
|
| 42 |
document.addEventListener("DOMContentLoaded", ()=>{
|
| 43 |
const chatContainer = document.querySelector(".chat-interface");
|
| 44 |
if(chatContainer) observer.observe(chatContainer, {childList:true, subtree:true});
|
| 45 |
|
|
|
|
| 46 |
const txt = document.querySelector("textarea");
|
| 47 |
if(txt){
|
|
|
|
| 48 |
txt.addEventListener("input", ()=>{
|
| 49 |
txt.style.height = "auto";
|
| 50 |
+
txt.style.height = txt.scrollHeight + "px"; // cresce suavemente
|
| 51 |
});
|
| 52 |
}
|
| 53 |
|
|
|
|
| 54 |
const btn = document.querySelector(".send-btn");
|
| 55 |
if(btn){
|
| 56 |
btn.addEventListener("click", ()=>{
|
|
|
|
| 70 |
.chat-message.bot { background-color:#2b2b2b; color:#fff; align-items:flex-start;}
|
| 71 |
|
| 72 |
/* input com botão dentro */
|
| 73 |
+
.input-wrapper {
|
| 74 |
+
display: flex;
|
| 75 |
+
align-items: flex-end;
|
| 76 |
+
position: relative;
|
| 77 |
+
margin-top: 10px;
|
| 78 |
+
border-radius:45px;
|
| 79 |
+
background-color:#1a1a1a;
|
| 80 |
+
padding-right:60px; /* espaço pro botão */
|
| 81 |
}
|
| 82 |
textarea {
|
| 83 |
+
border:none;
|
| 84 |
+
outline:none;
|
| 85 |
+
border-radius:45px;
|
| 86 |
+
padding:12px 16px;
|
| 87 |
+
background:transparent;
|
| 88 |
+
color:#fff;
|
| 89 |
+
font-size:16px;
|
| 90 |
+
width:100%;
|
| 91 |
+
resize:none;
|
| 92 |
+
transition: height 0.2s ease;
|
| 93 |
}
|
| 94 |
.send-btn {
|
| 95 |
+
position:absolute;
|
| 96 |
+
right:8px;
|
| 97 |
+
bottom:8px;
|
| 98 |
+
border:none;
|
| 99 |
+
border-radius:45px;
|
| 100 |
+
background-color:#444;
|
| 101 |
+
color:#fff;
|
| 102 |
+
width:48px;
|
| 103 |
+
height:48px;
|
| 104 |
+
display:flex;
|
| 105 |
+
align-items:center;
|
| 106 |
+
justify-content:center;
|
| 107 |
+
cursor:pointer;
|
| 108 |
+
transition: transform 0.1s ease, background-color 0.2s ease;
|
| 109 |
}
|
| 110 |
.send-btn:hover { background-color:#555; }
|
| 111 |
+
|
| 112 |
.gr-button.gr-login { border-radius:45px !important; background-color:#222 !important; color:#fff !important; margin-bottom:15px;}
|
| 113 |
.gr-button.gr-login:hover { background-color:#444 !important;}
|
| 114 |
""") as demo:
|
| 115 |
|
| 116 |
gr.LoginButton()
|
| 117 |
chatbot = gr.ChatInterface(respond, type="messages")
|
| 118 |
+
|
| 119 |
+
# Container de input + botão
|
| 120 |
+
with gr.Row(elem_classes=["input-wrapper"]):
|
| 121 |
+
textbox = gr.Textbox(label="", placeholder="Pergunte qualquer coisa...", lines=1)
|
| 122 |
+
send = gr.Button("→", elem_classes=["send-btn"])
|
| 123 |
+
|
| 124 |
+
# Conecta botão ao chatbot
|
| 125 |
+
send.click(chatbot.submit, inputs=[textbox], outputs=[chatbot])
|
| 126 |
+
|
| 127 |
gr.HTML(fade_js)
|
| 128 |
|
| 129 |
if __name__=="__main__":
|