Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,78 +35,78 @@ animation: pulse 1.5s infinite ease-in-out;
|
|
35 |
"""
|
36 |
|
37 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
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 |
<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 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
demo = gr.ChatInterface(
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
)
|
110 |
|
111 |
if name == "main":
|
112 |
-
|
|
|
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()
|