Update app.py
Browse files
app.py
CHANGED
|
@@ -1,100 +1,113 @@
|
|
| 1 |
-
import os
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
os.makedirs("bitai_data", exist_ok=True)
|
| 8 |
-
|
| 9 |
-
def get_user_file(username):
|
| 10 |
-
if not username:
|
| 11 |
-
username = str(uuid.uuid4())[:8]
|
| 12 |
-
return f"bitai_data/{username}.json"
|
| 13 |
-
|
| 14 |
-
def load_history(username):
|
| 15 |
-
file = get_user_file(username)
|
| 16 |
-
if os.path.exists(file):
|
| 17 |
-
with open(file, "r") as f:
|
| 18 |
-
return json.load(f)
|
| 19 |
-
return []
|
| 20 |
-
|
| 21 |
-
def save_history(username, history):
|
| 22 |
-
with open(get_user_file(username), "w") as f:
|
| 23 |
-
json.dump(history, f)
|
| 24 |
-
|
| 25 |
-
def list_chats():
|
| 26 |
-
files = os.listdir("bitai_data")
|
| 27 |
-
return [f.replace(".json", "") for f in files]
|
| 28 |
-
|
| 29 |
-
def respond(message, history, username):
|
| 30 |
-
history.append({"role": "user", "content": message})
|
| 31 |
-
save_history(username, history)
|
| 32 |
|
|
|
|
| 33 |
system_message = """
|
| 34 |
You are BitAI (V1), a friendly, curious, and talkative chatbot created by the user 'Sal'.
|
| 35 |
You can share opinions, answer casual questions, and chat about personal-style topics in a safe and friendly way.
|
|
|
|
| 36 |
Politely refuse only things that are truly harmful, illegal, or unsafe.
|
|
|
|
| 37 |
"""
|
| 38 |
|
| 39 |
messages = [{"role": "system", "content": system_message}]
|
| 40 |
messages.extend(history)
|
|
|
|
| 41 |
|
| 42 |
response = ""
|
| 43 |
-
for
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
token = ""
|
| 46 |
if len(choices) and choices[0].delta.content:
|
| 47 |
token = choices[0].delta.content
|
| 48 |
response += token
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
.
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
overflow-y: auto;
|
| 66 |
}
|
| 67 |
-
.chat-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
padding: 8px;
|
| 72 |
-
text-align: left;
|
| 73 |
-
background: #2c2c2c;
|
| 74 |
-
color: white;
|
| 75 |
-
border-radius: 6px;
|
| 76 |
-
border: none;
|
| 77 |
-
cursor: pointer;
|
| 78 |
-
transition: 0.2s;
|
| 79 |
}
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
}
|
| 83 |
-
""") as app:
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
|
|
|
|
|
|
| 1 |
+
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
|
| 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.
|
| 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": "user", "content": message})
|
| 21 |
|
| 22 |
response = ""
|
| 23 |
+
for message in client.chat_completion(
|
| 24 |
+
messages,
|
| 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(css="""
|
| 38 |
+
/* Chat arredondado */
|
| 39 |
+
.gr-chat-interface {
|
| 40 |
+
border-radius: 20px !important;
|
| 41 |
+
overflow: hidden !important;
|
| 42 |
+
border: 2px solid #333 !important;
|
| 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;
|
| 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 |
+
""") as demo:
|
| 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 |
+
if __name__ == "__main__":
|
| 113 |
+
demo.launch()
|