Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,10 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
def respond(message, history: list[dict[str, str]]):
|
| 6 |
# Puxa o token do secret do Hugging Face
|
| 7 |
client = InferenceClient(token=os.environ["HF_TOKEN"], model="openai/gpt-oss-20b")
|
| 8 |
|
| 9 |
-
# System prompt amigável, dá opinião e conversa de forma natural
|
| 10 |
system_message = """
|
| 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.
|
|
@@ -20,22 +19,34 @@ If someone asks what you are, clarify politely that you are BitAI, an AI chatbot
|
|
| 20 |
messages.append({"role": "user", "content": message})
|
| 21 |
|
| 22 |
response = ""
|
| 23 |
-
for
|
| 24 |
messages,
|
| 25 |
max_tokens=2048,
|
| 26 |
stream=True,
|
| 27 |
temperature=0.7,
|
| 28 |
top_p=0.95,
|
| 29 |
):
|
| 30 |
-
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(css="""
|
| 38 |
-
/*
|
| 39 |
.gr-chat-interface {
|
| 40 |
border-radius: 20px !important;
|
| 41 |
overflow: hidden !important;
|
|
@@ -43,8 +54,6 @@ with gr.Blocks(css="""
|
|
| 43 |
background-color: #1a1a1a !important;
|
| 44 |
color: white;
|
| 45 |
}
|
| 46 |
-
|
| 47 |
-
/* Botões grandes escuros com cantos muito arredondados */
|
| 48 |
.gr-button, .gr-chat-send-button {
|
| 49 |
border-radius: 50px;
|
| 50 |
padding: 12px 20px;
|
|
@@ -55,15 +64,11 @@ with gr.Blocks(css="""
|
|
| 55 |
height: 50px;
|
| 56 |
transition: background-color 0.5s;
|
| 57 |
}
|
| 58 |
-
|
| 59 |
-
/* Quando clicado */
|
| 60 |
.gr-button:active, .gr-chat-send-button:active {
|
| 61 |
background-color: white !important;
|
| 62 |
color: #111 !important;
|
| 63 |
transition: background-color 0.5s;
|
| 64 |
}
|
| 65 |
-
|
| 66 |
-
/* Outros botões menores */
|
| 67 |
button:not(.gr-chat-send-button) {
|
| 68 |
border-radius: 30px;
|
| 69 |
padding: 6px 12px;
|
|
@@ -77,8 +82,6 @@ button:not(.gr-chat-send-button):active {
|
|
| 77 |
color: #111 !important;
|
| 78 |
transition: background-color 0.5s;
|
| 79 |
}
|
| 80 |
-
|
| 81 |
-
/* Textbox menor */
|
| 82 |
textarea {
|
| 83 |
height: 40px !important;
|
| 84 |
border-radius: 20px !important;
|
|
@@ -90,10 +93,22 @@ textarea {
|
|
| 90 |
}
|
| 91 |
""") as demo:
|
| 92 |
|
|
|
|
|
|
|
| 93 |
with gr.Column():
|
| 94 |
gr.HTML("<h2 style='text-align:center; color:white'>BitAI</h2>")
|
| 95 |
-
chatbot = gr.ChatInterface(respond, type="messages")
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
def respond(message, history: list[dict[str, str]], saved_chats):
|
| 6 |
# Puxa o token do secret do Hugging Face
|
| 7 |
client = InferenceClient(token=os.environ["HF_TOKEN"], model="openai/gpt-oss-20b")
|
| 8 |
|
|
|
|
| 9 |
system_message = """
|
| 10 |
You are BitAI (V1), a friendly, curious, and talkative chatbot created by the user 'Sal'.
|
| 11 |
You can share opinions, answer casual questions, and chat about personal-style topics in a safe and friendly way.
|
|
|
|
| 19 |
messages.append({"role": "user", "content": message})
|
| 20 |
|
| 21 |
response = ""
|
| 22 |
+
for msg in client.chat_completion(
|
| 23 |
messages,
|
| 24 |
max_tokens=2048,
|
| 25 |
stream=True,
|
| 26 |
temperature=0.7,
|
| 27 |
top_p=0.95,
|
| 28 |
):
|
| 29 |
+
choices = msg.choices
|
| 30 |
token = ""
|
| 31 |
if len(choices) and choices[0].delta.content:
|
| 32 |
token = choices[0].delta.content
|
| 33 |
response += token
|
| 34 |
yield response
|
| 35 |
|
| 36 |
+
def save_chat(history, saved_chats):
|
| 37 |
+
# Salva o histórico atual
|
| 38 |
+
if history:
|
| 39 |
+
saved_chats.append(history.copy())
|
| 40 |
+
return saved_chats, "Chat salvo! ✅"
|
| 41 |
+
|
| 42 |
+
def load_chat(index, saved_chats):
|
| 43 |
+
# Carrega um chat salvo
|
| 44 |
+
if 0 <= index < len(saved_chats):
|
| 45 |
+
return saved_chats[index], f"Chat {index+1} carregado! 📂"
|
| 46 |
+
return [], "Índice inválido."
|
| 47 |
+
|
| 48 |
with gr.Blocks(css="""
|
| 49 |
+
/* Mesmo CSS que você fez */
|
| 50 |
.gr-chat-interface {
|
| 51 |
border-radius: 20px !important;
|
| 52 |
overflow: hidden !important;
|
|
|
|
| 54 |
background-color: #1a1a1a !important;
|
| 55 |
color: white;
|
| 56 |
}
|
|
|
|
|
|
|
| 57 |
.gr-button, .gr-chat-send-button {
|
| 58 |
border-radius: 50px;
|
| 59 |
padding: 12px 20px;
|
|
|
|
| 64 |
height: 50px;
|
| 65 |
transition: background-color 0.5s;
|
| 66 |
}
|
|
|
|
|
|
|
| 67 |
.gr-button:active, .gr-chat-send-button:active {
|
| 68 |
background-color: white !important;
|
| 69 |
color: #111 !important;
|
| 70 |
transition: background-color 0.5s;
|
| 71 |
}
|
|
|
|
|
|
|
| 72 |
button:not(.gr-chat-send-button) {
|
| 73 |
border-radius: 30px;
|
| 74 |
padding: 6px 12px;
|
|
|
|
| 82 |
color: #111 !important;
|
| 83 |
transition: background-color 0.5s;
|
| 84 |
}
|
|
|
|
|
|
|
| 85 |
textarea {
|
| 86 |
height: 40px !important;
|
| 87 |
border-radius: 20px !important;
|
|
|
|
| 93 |
}
|
| 94 |
""") as demo:
|
| 95 |
|
| 96 |
+
saved_chats = gr.State([]) # Lista com todos os chats salvos
|
| 97 |
+
|
| 98 |
with gr.Column():
|
| 99 |
gr.HTML("<h2 style='text-align:center; color:white'>BitAI</h2>")
|
| 100 |
+
chatbot = gr.ChatInterface(fn=respond, type="messages", additional_inputs=[saved_chats])
|
| 101 |
+
|
| 102 |
+
with gr.Row():
|
| 103 |
+
save_btn = gr.Button("💾 Salvar Chat")
|
| 104 |
+
load_index = gr.Number(label="Número do chat pra carregar", precision=0)
|
| 105 |
+
load_btn = gr.Button("📂 Carregar Chat")
|
| 106 |
+
|
| 107 |
+
status = gr.Textbox(label="Status", interactive=False)
|
| 108 |
+
|
| 109 |
+
# Liga os botões
|
| 110 |
+
save_btn.click(save_chat, [chatbot.chatbot, saved_chats], [saved_chats, status])
|
| 111 |
+
load_btn.click(load_chat, [load_index, saved_chats], [chatbot.chatbot, status])
|
| 112 |
|
| 113 |
if __name__ == "__main__":
|
| 114 |
demo.launch()
|