BitAI / app.py
Salt40404's picture
Update app.py
299222a verified
raw
history blame
3.83 kB
import os
import gradio as gr
from huggingface_hub import InferenceClient
def respond(message, history: list[dict[str, str]], saved_chats):
# Puxa o token do secret do Hugging Face
client = InferenceClient(token=os.environ["HF_TOKEN"], model="openai/gpt-oss-20b")
system_message = """
You are BitAI (V1), a friendly, curious, and talkative chatbot created by the user 'Sal'.
You can share opinions, answer casual questions, and chat about personal-style topics in a safe and friendly way.
Avoid repeating the same phrases, and always try to keep the conversation engaging and natural.
Politely refuse only things that are truly harmful, illegal, or unsafe.
If someone asks what you are, clarify politely that you are BitAI, an AI chatbot.
"""
messages = [{"role": "system", "content": system_message}]
messages.extend(history)
messages.append({"role": "user", "content": message})
response = ""
for msg in client.chat_completion(
messages,
max_tokens=2048,
stream=True,
temperature=0.7,
top_p=0.95,
):
choices = msg.choices
token = ""
if len(choices) and choices[0].delta.content:
token = choices[0].delta.content
response += token
yield response
def save_chat(history, saved_chats):
# Salva o histórico atual
if history:
saved_chats.append(history.copy())
return saved_chats, "Chat salvo! ✅"
def load_chat(index, saved_chats):
# Carrega um chat salvo
if 0 <= index < len(saved_chats):
return saved_chats[index], f"Chat {index+1} carregado! 📂"
return [], "Índice inválido."
with gr.Blocks(css="""
/* Mesmo CSS que você fez */
.gr-chat-interface {
border-radius: 20px !important;
overflow: hidden !important;
border: 2px solid #333 !important;
background-color: #1a1a1a !important;
color: white;
}
.gr-button, .gr-chat-send-button {
border-radius: 50px;
padding: 12px 20px;
background-color: #111;
color: white;
font-weight: bold;
cursor: pointer;
height: 50px;
transition: background-color 0.5s;
}
.gr-button:active, .gr-chat-send-button:active {
background-color: white !important;
color: #111 !important;
transition: background-color 0.5s;
}
button:not(.gr-chat-send-button) {
border-radius: 30px;
padding: 6px 12px;
background-color: #222;
color: white;
height: 40px;
transition: background-color 0.5s;
}
button:not(.gr-chat-send-button):active {
background-color: white !important;
color: #111 !important;
transition: background-color 0.5s;
}
textarea {
height: 40px !important;
border-radius: 20px !important;
border: 1px solid #444 !important;
padding: 8px !important;
background-color: #111;
color: white;
resize: none !important;
}
""") as demo:
saved_chats = gr.State([]) # Lista com todos os chats salvos
with gr.Column():
gr.HTML("<h2 style='text-align:center; color:white'>BitAI</h2>")
chatbot = gr.ChatInterface(fn=respond, type="messages", additional_inputs=[saved_chats])
with gr.Row():
save_btn = gr.Button("💾 Salvar Chat")
load_index = gr.Number(label="Número do chat pra carregar", precision=0)
load_btn = gr.Button("📂 Carregar Chat")
status = gr.Textbox(label="Status", interactive=False)
# Liga os botões
save_btn.click(save_chat, [chatbot.chatbot, saved_chats], [saved_chats, status])
load_btn.click(load_chat, [load_index, saved_chats], [chatbot.chatbot, status])
if __name__ == "__main__":
demo.launch()