Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,43 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
|
|
|
| 4 |
|
| 5 |
-
def respond(message, history
|
| 6 |
-
# Puxa o token do secret do Hugging Face
|
| 7 |
client = InferenceClient(token=os.environ["HF_TOKEN"], model="openai/gpt-oss-20b")
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 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":
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
response = ""
|
| 23 |
-
for
|
| 24 |
-
|
| 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 |
response += token
|
| 35 |
yield response
|
| 36 |
|
| 37 |
-
with gr.Blocks(
|
| 38 |
-
|
| 39 |
-
.
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
padding: 12px 20px;
|
| 51 |
-
background-color: #111;
|
| 52 |
-
color: white;
|
| 53 |
-
font-weight: bold;
|
| 54 |
-
cursor: pointer;
|
| 55 |
-
height: 50px;
|
| 56 |
-
transition: background-color 0.5s;
|
| 57 |
-
}
|
| 58 |
-
.gr-button:active, .gr-chat-send-button:active {
|
| 59 |
-
background-color: white !important;
|
| 60 |
-
color: #111 !important;
|
| 61 |
-
transition: background-color 0.5s;
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
/* Outros botões menores */
|
| 65 |
-
button:not(.gr-chat-send-button) {
|
| 66 |
-
border-radius: 30px;
|
| 67 |
-
padding: 6px 12px;
|
| 68 |
-
background-color: #222;
|
| 69 |
-
color: white;
|
| 70 |
-
height: 40px;
|
| 71 |
-
transition: background-color 0.5s;
|
| 72 |
-
}
|
| 73 |
-
button:not(.gr-chat-send-button):active {
|
| 74 |
-
background-color: white !important;
|
| 75 |
-
color: #111 !important;
|
| 76 |
-
transition: background-color 0.5s;
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
/* Textbox menor */
|
| 80 |
-
textarea {
|
| 81 |
-
height: 40px !important;
|
| 82 |
-
border-radius: 20px !important;
|
| 83 |
-
border: 1px solid #444 !important;
|
| 84 |
-
padding: 8px !important;
|
| 85 |
-
background-color: #111;
|
| 86 |
-
color: white;
|
| 87 |
-
resize: none !important;
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
/* Loader animado embaixo da mensagem da IA */
|
| 91 |
-
#bitai-loader {
|
| 92 |
-
width: 20px;
|
| 93 |
-
height: 20px;
|
| 94 |
-
margin: 10px auto 0 auto;
|
| 95 |
-
border-radius: 50%;
|
| 96 |
-
background: linear-gradient(45deg, #ff6, #f06);
|
| 97 |
-
animation: moveLoader 1s infinite alternate;
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
@keyframes moveLoader {
|
| 101 |
0% { transform: translateY(0px); }
|
| 102 |
50% { transform: translateY(5px); }
|
| 103 |
100% { transform: translateY(0px); }
|
| 104 |
}
|
| 105 |
-
"""
|
| 106 |
-
|
| 107 |
-
with gr.Column():
|
| 108 |
-
gr.HTML("<h2 style='text-align:center; color:white'>BitAI</h2>")
|
| 109 |
-
chatbot = gr.ChatInterface(respond, type="messages")
|
| 110 |
-
gr.HTML("<div id='bitai-loader'></div>") # ícone que se mexe
|
| 111 |
|
| 112 |
-
|
| 113 |
-
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
+
import time
|
| 5 |
|
| 6 |
+
def respond(message, history):
|
|
|
|
| 7 |
client = InferenceClient(token=os.environ["HF_TOKEN"], model="openai/gpt-oss-20b")
|
| 8 |
+
|
| 9 |
+
system_message = "You are BitAI (V1), a friendly chatbot..."
|
| 10 |
+
messages = [{"role":"system","content":system_message}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
messages.extend(history)
|
| 12 |
+
messages.append({"role":"user","content":message})
|
| 13 |
+
|
| 14 |
+
yield "⏳ BitAI is typing..." # aqui mostra o loader
|
| 15 |
+
|
| 16 |
+
# Simulando streaming real
|
| 17 |
response = ""
|
| 18 |
+
for m in client.chat_completion(messages, stream=True):
|
| 19 |
+
token = m.choices[0].delta.content if m.choices else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
response += token
|
| 21 |
yield response
|
| 22 |
|
| 23 |
+
with gr.Blocks() as demo:
|
| 24 |
+
with gr.Column():
|
| 25 |
+
gr.HTML("<h2 style='text-align:center; color:white'>BitAI</h2>")
|
| 26 |
+
chatbot = gr.ChatInterface(respond, type="messages")
|
| 27 |
+
|
| 28 |
+
# Loader fora do chat
|
| 29 |
+
loader = gr.HTML("<div style='text-align:center; margin-top:10px;'>"
|
| 30 |
+
"<div style='width:20px; height:20px; border-radius:50%; "
|
| 31 |
+
"background:linear-gradient(45deg,#ff6,#f06); animation:moveLoader 1s infinite alternate;'></div>"
|
| 32 |
+
"</div>")
|
| 33 |
+
demo.load(lambda: None, [], loader) # placeholder pra ativar animação
|
| 34 |
+
|
| 35 |
+
css_loader = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
@keyframes moveLoader {
|
| 37 |
0% { transform: translateY(0px); }
|
| 38 |
50% { transform: translateY(5px); }
|
| 39 |
100% { transform: translateY(0px); }
|
| 40 |
}
|
| 41 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
demo.launch()
|
|
|