Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,109 +1,112 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import torch
|
4 |
-
import threading
|
5 |
import time
|
6 |
|
7 |
-
|
8 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
9 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
10 |
-
|
11 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
-
model.to(device)
|
13 |
|
14 |
css = """
|
15 |
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap');
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
.markdown-think {
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
}
|
|
|
27 |
@keyframes pulse {
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
}
|
32 |
"""
|
33 |
|
34 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
yield final_answer.strip()
|
94 |
|
95 |
demo = gr.ChatInterface(
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
|
|
106 |
)
|
107 |
|
108 |
-
if
|
109 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
3 |
import time
|
4 |
|
5 |
+
client = InferenceClient("lambdaindie/lambdai")
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
css = """
|
8 |
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono&display=swap');
|
9 |
+
|
10 |
+
{
|
11 |
+
font-family: 'JetBrains Mono', monospace !important;
|
12 |
+
}
|
13 |
+
|
14 |
+
|
15 |
+
body {
|
16 |
+
background-color: #111;
|
17 |
+
color: #e0e0e0;
|
18 |
+
}
|
19 |
+
|
20 |
.markdown-think {
|
21 |
+
background-color: #1e1e1e;
|
22 |
+
border-left: 4px solid #555;
|
23 |
+
padding: 10px;
|
24 |
+
margin-bottom: 8px;
|
25 |
+
font-style: italic;
|
26 |
+
white-space: pre-wrap;
|
27 |
+
animation: pulse 1.5s infinite ease-in-out;
|
28 |
}
|
29 |
+
|
30 |
@keyframes pulse {
|
31 |
+
0% { opacity: 0.6; }
|
32 |
+
50% { opacity: 1.0; }
|
33 |
+
100% { opacity: 0.6; }
|
34 |
}
|
35 |
"""
|
36 |
|
37 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
38 |
+
messages = [{"role": "system", "content": system_message}] if system_message else []
|
39 |
+
|
40 |
+
for user, assistant in history:
|
41 |
+
if user:
|
42 |
+
messages.append({"role": "user", "content": user})
|
43 |
+
if assistant:
|
44 |
+
messages.append({"role": "assistant", "content": assistant})
|
45 |
+
|
46 |
+
thinking_prompt = messages + [{
|
47 |
+
"role": "user",
|
48 |
+
"content": f"{message}\n\nThink a bit step-by-step before answering."
|
49 |
+
}]
|
50 |
+
|
51 |
+
reasoning = ""
|
52 |
+
yield '<div class="markdown-think">Thinking...</div>'
|
53 |
+
|
54 |
+
start = time.time()
|
55 |
+
|
56 |
+
for chunk in client.chat_completion(
|
57 |
+
thinking_prompt,
|
58 |
+
max_tokens=max_tokens,
|
59 |
+
stream=True,
|
60 |
+
temperature=temperature,
|
61 |
+
top_p=top_p,
|
62 |
+
):
|
63 |
+
token = chunk.choices[0].delta.content or ""
|
64 |
+
reasoning += token
|
65 |
+
styled_thought = f'<div class="markdown-think">{reasoning.strip()}</div>'
|
66 |
+
yield styled_thought
|
67 |
+
|
68 |
+
elapsed = time.time() - start
|
69 |
+
|
70 |
+
yield f"""
|
71 |
+
<div style="margin-top:12px;padding:8px 12px;background-color:#222;border-left:4px solid #888;
|
72 |
+
font-family:'JetBrains Mono', monospace;color:#ccc;font-size:14px;">
|
73 |
+
Pensou por {elapsed:.1f} segundos
|
74 |
+
</div>
|
75 |
+
"""
|
76 |
+
|
77 |
+
time.sleep(2)
|
78 |
+
|
79 |
+
final_prompt = messages + [
|
80 |
+
{"role": "user", "content": message},
|
81 |
+
{"role": "assistant", "content": reasoning.strip()},
|
82 |
+
{"role": "user", "content": "Now answer based on your reasoning above."}
|
83 |
+
]
|
84 |
+
|
85 |
+
final_answer = ""
|
86 |
+
for chunk in client.chat_completion(
|
87 |
+
final_prompt,
|
88 |
+
max_tokens=max_tokens,
|
89 |
+
stream=True,
|
90 |
+
temperature=temperature,
|
91 |
+
top_p=top_p,
|
92 |
+
):
|
93 |
+
token = chunk.choices[0].delta.content or ""
|
94 |
+
final_answer += token
|
95 |
+
yield final_answer.strip()
|
|
|
96 |
|
97 |
demo = gr.ChatInterface(
|
98 |
+
fn=respond,
|
99 |
+
title="位ambdAI",
|
100 |
+
theme=gr.themes.Base(),
|
101 |
+
css=css,
|
102 |
+
additional_inputs=[
|
103 |
+
gr.Textbox(value="",
|
104 |
+
label="System Message"),
|
105 |
+
gr.Slider(64, 2048, value=512, step=1, label="Max Tokens"),
|
106 |
+
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
107 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
108 |
+
]
|
109 |
)
|
110 |
|
111 |
+
if name == "main":
|
112 |
+
demo.launch()
|