Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ css = """
|
|
11 |
font-family: 'JetBrains Mono', monospace !important;
|
12 |
}
|
13 |
|
|
|
14 |
body {
|
15 |
background-color: #111;
|
16 |
color: #e0e0e0;
|
@@ -36,61 +37,61 @@ animation: pulse 1.5s infinite ease-in-out;
|
|
36 |
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
37 |
messages = [{"role": "system", "content": system_message}] if system_message else []
|
38 |
|
39 |
-
for user, assistant in history:
|
40 |
-
if user:
|
41 |
-
messages.append({"role": "user", "content": user})
|
42 |
-
if assistant:
|
43 |
-
messages.append({"role": "assistant", "content": assistant})
|
44 |
-
|
45 |
-
thinking_prompt = messages + [{
|
46 |
-
"role": "user",
|
47 |
-
"content": f"{message}\n\nThink a bit step-by-step before answering."
|
48 |
-
}]
|
49 |
-
|
50 |
-
reasoning = ""
|
51 |
-
yield '<div class="markdown-think">Thinking...</div>'
|
52 |
-
|
53 |
-
start = time.time()
|
54 |
-
|
55 |
-
for chunk in client.chat_completion(
|
56 |
-
thinking_prompt,
|
57 |
-
max_tokens=max_tokens,
|
58 |
-
stream=True,
|
59 |
-
temperature=temperature,
|
60 |
-
top_p=top_p,
|
61 |
-
):
|
62 |
-
token = chunk.choices[0].delta.content or ""
|
63 |
-
reasoning += token
|
64 |
-
styled_thought = f'<div class="markdown-think">{reasoning.strip()}</div>'
|
65 |
-
yield styled_thought
|
66 |
-
|
67 |
-
elapsed = time.time() - start
|
68 |
-
|
69 |
-
yield f"""
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
time.sleep(2)
|
77 |
-
|
78 |
-
final_prompt = messages + [
|
79 |
-
{"role": "user", "content": message},
|
80 |
-
{"role": "assistant", "content": reasoning.strip()},
|
81 |
-
{"role": "user", "content": "Now answer based on your reasoning above."}
|
82 |
-
]
|
83 |
-
|
84 |
-
final_answer = ""
|
85 |
-
for chunk in client.chat_completion(
|
86 |
-
final_prompt,
|
87 |
-
max_tokens=max_tokens,
|
88 |
-
stream=True,
|
89 |
-
temperature=temperature,
|
90 |
-
top_p=top_p,
|
91 |
-
):
|
92 |
-
token = chunk.choices[0].delta.content or ""
|
93 |
-
final_answer += token
|
94 |
yield final_answer.strip()
|
95 |
|
96 |
demo = gr.ChatInterface(
|
@@ -99,12 +100,13 @@ demo = gr.ChatInterface(
|
|
99 |
theme=gr.themes.Base(),
|
100 |
css=css,
|
101 |
additional_inputs=[
|
102 |
-
gr.Textbox(value="",
|
|
|
103 |
gr.Slider(64, 2048, value=512, step=1, label="Max Tokens"),
|
104 |
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
105 |
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
106 |
]
|
107 |
)
|
108 |
|
109 |
-
if
|
110 |
demo.launch()
|
|
|
11 |
font-family: 'JetBrains Mono', monospace !important;
|
12 |
}
|
13 |
|
14 |
+
|
15 |
body {
|
16 |
background-color: #111;
|
17 |
color: #e0e0e0;
|
|
|
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(
|
|
|
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()
|