Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,118 +1,114 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
import time
|
4 |
|
5 |
-
client = InferenceClient("lambdaindie/
|
6 |
|
7 |
-
css = """
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
12 |
}
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
font-weight: bold;
|
19 |
-
font-family: 'JetBrains Mono', monospace;
|
20 |
}
|
21 |
-
.
|
22 |
-
background: #
|
|
|
|
|
|
|
|
|
23 |
}
|
24 |
-
|
25 |
-
|
26 |
-
color: #
|
27 |
-
font-family: 'JetBrains Mono', monospace;
|
28 |
-
border-radius: 8px;
|
29 |
}
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
}
|
33 |
-
.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
42 |
}
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
}
|
48 |
"""
|
49 |
|
50 |
-
|
51 |
-
messages = [{"role": "system", "content": system_message}] if system_message else []
|
52 |
-
|
53 |
-
for user, assistant in history:
|
54 |
-
if user:
|
55 |
-
messages.append({"role": "user", "content": user})
|
56 |
-
if assistant:
|
57 |
-
messages.append({"role": "assistant", "content": assistant})
|
58 |
-
|
59 |
-
thinking_prompt = messages + [
|
60 |
-
{
|
61 |
-
"role": "user",
|
62 |
-
"content": f"{message}\n\nThink step-by-step before answering."
|
63 |
-
}
|
64 |
-
]
|
65 |
-
|
66 |
-
reasoning = ""
|
67 |
-
yield '<div class="markdown-think">Thinking...</div>'
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
80 |
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
|
84 |
-
{"role": "
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
additional_inputs=[
|
107 |
-
gr.Textbox(
|
108 |
-
value="You are a concise, logical AI that explains its reasoning clearly before answering.",
|
109 |
-
label="System Message"
|
110 |
-
),
|
111 |
-
gr.Slider(64, 2048, value=512, step=1, label="Max Tokens"),
|
112 |
-
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
113 |
-
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
114 |
-
]
|
115 |
-
)
|
116 |
|
117 |
-
if __name__ == "__main__":
|
118 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
+
client = InferenceClient("lambdaindie/lambda")
|
5 |
|
6 |
+
css = r"""
|
7 |
+
/* Fonte e cores gerais */
|
8 |
+
* { font-family: 'JetBrains Mono', monospace; }
|
9 |
+
.gradio-container { background-color: #0d0d0d; color: #e0e0e0; }
|
10 |
+
/* Inputs e chat bubbles */
|
11 |
+
textarea, input, .block, .wrap, .chatbot, .scroll-hide {
|
12 |
+
background-color: #1a1a1a !important;
|
13 |
+
color: #e0e0e0 !important;
|
14 |
+
border: 1px solid #333 !important;
|
15 |
+
border-radius: 12px;
|
16 |
}
|
17 |
+
/* Botão com pulse animation */
|
18 |
+
@keyframes pulse {
|
19 |
+
0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(255,255,255,0.5); }
|
20 |
+
70% { transform: scale(1.05); box-shadow: 0 0 0 10px rgba(255,255,255,0); }
|
21 |
+
100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(255,255,255,0); }
|
|
|
|
|
22 |
}
|
23 |
+
button.pulse {
|
24 |
+
background-color: #272727 !important;
|
25 |
+
border: 1px solid #444 !important;
|
26 |
+
color: #e0e0e0 !important;
|
27 |
+
border-radius: 12px;
|
28 |
+
animation: pulse 2s infinite;
|
29 |
}
|
30 |
+
/* Hover no botão */
|
31 |
+
button.pulse:hover {
|
32 |
+
background-color: #444 !important;
|
|
|
|
|
33 |
}
|
34 |
+
/* Spinner de thinking */
|
35 |
+
@keyframes spin {
|
36 |
+
0% { transform: rotate(0deg); }
|
37 |
+
100% { transform: rotate(360deg); }
|
38 |
}
|
39 |
+
.loader {
|
40 |
+
border: 3px solid #2b2b2b;
|
41 |
+
border-top: 3px solid #e0e0e0;
|
42 |
+
border-radius: 50%;
|
43 |
+
width: 18px;
|
44 |
+
height: 18px;
|
45 |
+
animation: spin 1s linear infinite;
|
46 |
+
display: inline-block;
|
47 |
+
margin-right: 8px;
|
48 |
+
vertical-align: middle;
|
49 |
}
|
50 |
+
/* Markdown de thinking dentro do chat */
|
51 |
+
.thinking-html {
|
52 |
+
background-color: #2b2b2b;
|
53 |
+
padding: 10px;
|
54 |
+
border-radius: 8px;
|
55 |
+
margin-bottom: 8px;
|
56 |
+
font-style: italic;
|
57 |
+
color: #aaaaaa;
|
58 |
+
display: flex;
|
59 |
+
align-items: center;
|
60 |
}
|
61 |
"""
|
62 |
|
63 |
+
with gr.Blocks(css=css, theme=gr.themes.Base()) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
+
gr.Markdown("<h1 style='text-align:center;color:#e0e0e0;'>🅻 Lambdai-v1-1B Chat</h1>")
|
66 |
+
chatbot = gr.Chatbot(elem_id="chatbot", height=480, render_markdown=True, show_copy_button=True)
|
67 |
+
|
68 |
+
with gr.Row():
|
69 |
+
system_message = gr.Textbox(value="You are a helpful AI assistant.", label="System message", lines=1)
|
70 |
+
with gr.Row():
|
71 |
+
max_tokens = gr.Slider(128, 2048, value=512, step=1, label="Max tokens")
|
72 |
+
temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
|
73 |
+
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top‑p")
|
74 |
+
with gr.Row():
|
75 |
+
user_input = gr.Textbox(show_label=False, placeholder="Type your message here...", lines=2)
|
76 |
+
send_button = gr.Button("Λ Think", elem_classes="pulse")
|
77 |
|
78 |
+
def respond(message, chat_history, system_message, max_tokens, temperature, top_p):
|
79 |
+
# 1) exibe o spinner + texto de thinking
|
80 |
+
thinking_html = (
|
81 |
+
f"<div class='thinking-html'>"
|
82 |
+
f"<div class='loader'></div>"
|
83 |
+
f"Thinking… generating reasoning path…"
|
84 |
+
f"</div>"
|
85 |
+
)
|
86 |
+
yield chat_history + [[message, thinking_html]]
|
87 |
|
88 |
+
# 2) prepara payload para API
|
89 |
+
messages = [{"role": "system", "content": system_message}]
|
90 |
+
for u, a in chat_history:
|
91 |
+
if u: messages.append({"role":"user", "content":u})
|
92 |
+
if a: messages.append({"role":"assistant","content":a})
|
93 |
+
messages.append({"role": "user", "content": message})
|
94 |
|
95 |
+
# 3) chama streaming da API
|
96 |
+
response = ""
|
97 |
+
for chunk in client.chat_completion(
|
98 |
+
messages,
|
99 |
+
max_tokens=max_tokens,
|
100 |
+
temperature=temperature,
|
101 |
+
top_p=top_p,
|
102 |
+
stream=True
|
103 |
+
):
|
104 |
+
delta = chunk.choices[0].delta.content or ""
|
105 |
+
response += delta
|
106 |
+
yield chat_history + [[message, response]]
|
107 |
|
108 |
+
send_button.click(
|
109 |
+
fn=respond,
|
110 |
+
inputs=[user_input, chatbot, system_message, max_tokens, temperature, top_p],
|
111 |
+
outputs=chatbot
|
112 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
|
|
114 |
demo.launch()
|