mariusjabami commited on
Commit
e86214a
·
verified ·
1 Parent(s): fa8b0f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -98
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/lambdai")
6
 
7
- css = """
8
- body {
9
- background-color: #111;
10
- color: #e0e0e0;
11
- font-family: 'JetBrains Mono', monospace;
 
 
 
 
 
12
  }
13
- .gr-button {
14
- background: linear-gradient(to right, #2a2a2a, #1f1f1f);
15
- color: white;
16
- border-radius: 10px;
17
- padding: 8px 16px;
18
- font-weight: bold;
19
- font-family: 'JetBrains Mono', monospace;
20
  }
21
- .gr-button:hover {
22
- background: #333;
 
 
 
 
23
  }
24
- .gr-textbox textarea {
25
- background-color: #181818 !important;
26
- color: #fff !important;
27
- font-family: 'JetBrains Mono', monospace;
28
- border-radius: 8px;
29
  }
30
- .gr-chat-message {
31
- font-family: 'JetBrains Mono', monospace;
 
 
32
  }
33
- .markdown-think {
34
- background-color: #1e1e1e;
35
- border-left: 4px solid #555;
36
- padding: 10px;
37
- margin-bottom: 8px;
38
- font-style: italic;
39
- white-space: pre-wrap;
40
- font-family: 'JetBrains Mono', monospace;
41
- animation: pulse 1.5s infinite ease-in-out;
 
42
  }
43
- @keyframes pulse {
44
- 0% { opacity: 0.6; }
45
- 50% { opacity: 1.0; }
46
- 100% { opacity: 0.6; }
 
 
 
 
 
 
47
  }
48
  """
49
 
50
- def respond(message, history, system_message, max_tokens, temperature, top_p):
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
- for chunk in client.chat_completion(
70
- thinking_prompt,
71
- max_tokens=max_tokens,
72
- stream=True,
73
- temperature=temperature,
74
- top_p=top_p,
75
- ):
76
- token = chunk.choices[0].delta.content or ""
77
- reasoning += token
78
- styled_thought = f'<div class="markdown-think">{reasoning.strip()}</div>'
79
- yield styled_thought
 
80
 
81
- time.sleep(0.5)
 
 
 
 
 
 
 
 
82
 
83
- final_prompt = messages + [
84
- {"role": "user", "content": message},
85
- {"role": "assistant", "content": reasoning.strip()},
86
- {"role": "user", "content": "Now answer based on your reasoning above."}
87
- ]
 
88
 
89
- final_answer = ""
90
- for chunk in client.chat_completion(
91
- final_prompt,
92
- max_tokens=max_tokens,
93
- stream=True,
94
- temperature=temperature,
95
- top_p=top_p,
96
- ):
97
- token = chunk.choices[0].delta.content or ""
98
- final_answer += token
99
- yield final_answer.strip()
 
100
 
101
- demo = gr.ChatInterface(
102
- fn=respond,
103
- title="LENIRΛ",
104
- theme=gr.themes.Base(primary_hue="gray"),
105
- css=css,
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()