Salt40404 commited on
Commit
e33aa88
·
verified ·
1 Parent(s): 0c73e02

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -54
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
4
  def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
5
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
6
  system_message = """You are BitAI (or Bit for short), a friendly chatbot created by the user "Sal".
@@ -15,13 +16,15 @@ Keep a simple, approachable, and friendly tone otherwise."""
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=>{
@@ -38,19 +41,21 @@ const observer = new MutationObserver(mutations=>{
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", ()=>{
@@ -68,61 +73,18 @@ body { background-color:#000; font-family:'Arial',sans-serif; margin:0; padding:
68
  .chat-message { border-radius:45px; padding:14px 18px; margin:8px 0; display:flex; flex-direction:column; opacity:0;}
69
  .chat-message.user { background-color:#1f1f1f; color:#fff; align-items:flex-end;}
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
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # Função principal que chama o modelo
5
  def respond(message, history: list[dict[str, str]], hf_token: gr.OAuthToken):
6
  client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
7
  system_message = """You are BitAI (or Bit for short), a friendly chatbot created by the user "Sal".
 
16
 
17
  response = ""
18
  for chunk in client.chat_completion(messages, max_tokens=512, stream=True, temperature=0.7, top_p=0.95):
19
+ token = ""
20
  if chunk.choices and chunk.choices[0].delta.content:
21
+ token = chunk.choices[0].delta.content
22
+ response += token
23
  yield response
24
 
25
+ # JS para animações suaves
26
  fade_js = """
27
  <script>
 
28
  const observer = new MutationObserver(mutations=>{
29
  mutations.forEach(m=>{
30
  m.addedNodes.forEach(node=>{
 
41
  });
42
  });
43
 
 
44
  document.addEventListener("DOMContentLoaded", ()=>{
45
  const chatContainer = document.querySelector(".chat-interface");
46
  if(chatContainer) observer.observe(chatContainer, {childList:true, subtree:true});
47
 
48
+ // Animação sutil ao digitar na textarea
49
  const txt = document.querySelector("textarea");
50
  if(txt){
51
  txt.addEventListener("input", ()=>{
52
+ txt.style.transition = "background-color 0.15s ease";
53
+ txt.style.backgroundColor = "#222";
54
+ setTimeout(()=> txt.style.backgroundColor="#1a1a1a",150);
55
  });
56
  }
57
 
58
+ // Clique no botão anima
59
  const btn = document.querySelector(".send-btn");
60
  if(btn){
61
  btn.addEventListener("click", ()=>{
 
73
  .chat-message { border-radius:45px; padding:14px 18px; margin:8px 0; display:flex; flex-direction:column; opacity:0;}
74
  .chat-message.user { background-color:#1f1f1f; color:#fff; align-items:flex-end;}
75
  .chat-message.bot { background-color:#2b2b2b; color:#fff; align-items:flex-start;}
76
+ textarea { border:none; outline:none; border-radius:45px; padding:12px; background-color:#1a1a1a; color:#fff; font-size:16px; flex:1; height:48px; box-sizing:border-box;}
77
+ .send-btn { border:none; border-radius:45px; background-color:#444; color:#fff; width:48px; height:48px; font-size:18px; display:flex; align-items:center; justify-content:center; cursor:pointer; margin-left:8px; transition:transform 0.1s ease;}
78
+ .send-btn:hover { background-color:#555;}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  .gr-button.gr-login { border-radius:45px !important; background-color:#222 !important; color:#fff !important; margin-bottom:15px;}
80
  .gr-button.gr-login:hover { background-color:#444 !important;}
81
+ .input-container { display:flex; margin-top:10px; }
82
  """) as demo:
83
 
84
+ # Login direto na página
85
  gr.LoginButton()
 
86
 
87
+ chatbot = gr.ChatInterface(respond, type="messages")
 
 
 
 
 
 
88
 
89
  gr.HTML(fade_js)
90