Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,53 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
client = InferenceClient("lambdaindie/lambdai")
|
| 5 |
|
| 6 |
css = """
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
display: flex;
|
| 9 |
align-items: center;
|
| 10 |
-
gap:
|
| 11 |
-
|
| 12 |
-
font-style: italic;
|
| 13 |
-
margin-bottom: 5px;
|
| 14 |
-
animation: pulse 1.5s infinite;
|
| 15 |
}
|
| 16 |
.loader {
|
| 17 |
width: 14px;
|
| 18 |
height: 14px;
|
| 19 |
-
border: 2px solid #
|
| 20 |
-
border-top: 2px solid #
|
| 21 |
border-radius: 50%;
|
| 22 |
animation: spin 1s linear infinite;
|
| 23 |
}
|
|
@@ -25,41 +55,78 @@ css = """
|
|
| 25 |
to { transform: rotate(360deg); }
|
| 26 |
}
|
| 27 |
@keyframes pulse {
|
| 28 |
-
0% { opacity:
|
| 29 |
-
50% { opacity:
|
| 30 |
-
100% { opacity:
|
| 31 |
}
|
| 32 |
"""
|
| 33 |
|
| 34 |
-
def respond(message, history):
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
reasoning, answer = output.split("\n\n", 1)
|
| 49 |
-
else:
|
| 50 |
-
reasoning, answer = "No reasoning provided.", output
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
| 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: #000000;
|
| 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: #000000;
|
| 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 |
display: flex;
|
| 42 |
align-items: center;
|
| 43 |
+
gap: 10px;
|
| 44 |
+
animation: pulse 1.5s infinite ease-in-out;
|
|
|
|
|
|
|
|
|
|
| 45 |
}
|
| 46 |
.loader {
|
| 47 |
width: 14px;
|
| 48 |
height: 14px;
|
| 49 |
+
border: 2px solid #888;
|
| 50 |
+
border-top: 2px solid #e0e0e0;
|
| 51 |
border-radius: 50%;
|
| 52 |
animation: spin 1s linear infinite;
|
| 53 |
}
|
|
|
|
| 55 |
to { transform: rotate(360deg); }
|
| 56 |
}
|
| 57 |
@keyframes pulse {
|
| 58 |
+
0% { opacity: 0.6; }
|
| 59 |
+
50% { opacity: 1.0; }
|
| 60 |
+
100% { opacity: 0.6; }
|
| 61 |
}
|
| 62 |
"""
|
| 63 |
|
| 64 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 65 |
+
messages = [{"role": "system", "content": system_message}] if system_message else []
|
| 66 |
+
|
| 67 |
+
for user, assistant in history:
|
| 68 |
+
if user:
|
| 69 |
+
messages.append({"role": "user", "content": user})
|
| 70 |
+
if assistant:
|
| 71 |
+
messages.append({"role": "assistant", "content": assistant})
|
| 72 |
+
|
| 73 |
+
thinking_prompt = messages + [
|
| 74 |
+
{
|
| 75 |
+
"role": "user",
|
| 76 |
+
"content": f"{message}\n\nThink step-by-step before answering."
|
| 77 |
+
}
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
reasoning = ""
|
| 81 |
+
yield '<div class="markdown-think"><div class="loader"></div>Thinking...</div>'
|
| 82 |
|
| 83 |
+
for chunk in client.chat_completion(
|
| 84 |
+
thinking_prompt,
|
| 85 |
+
max_tokens=max_tokens,
|
| 86 |
+
stream=True,
|
| 87 |
+
temperature=temperature,
|
| 88 |
+
top_p=top_p,
|
| 89 |
+
):
|
| 90 |
+
token = chunk.choices[0].delta.content or ""
|
| 91 |
+
reasoning += token
|
| 92 |
+
styled_thought = f'<div class="markdown-think"><div class="loader"></div>{reasoning.strip()}</div>'
|
| 93 |
+
yield styled_thought
|
| 94 |
|
| 95 |
+
time.sleep(0.5)
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
final_prompt = messages + [
|
| 98 |
+
{"role": "user", "content": message},
|
| 99 |
+
{"role": "assistant", "content": reasoning.strip()},
|
| 100 |
+
{"role": "user", "content": "Now answer based on your reasoning above."}
|
| 101 |
+
]
|
| 102 |
|
| 103 |
+
final_answer = ""
|
| 104 |
+
for chunk in client.chat_completion(
|
| 105 |
+
final_prompt,
|
| 106 |
+
max_tokens=max_tokens,
|
| 107 |
+
stream=True,
|
| 108 |
+
temperature=temperature,
|
| 109 |
+
top_p=top_p,
|
| 110 |
+
):
|
| 111 |
+
token = chunk.choices[0].delta.content or ""
|
| 112 |
+
final_answer += token
|
| 113 |
+
yield final_answer.strip()
|
| 114 |
|
| 115 |
+
demo = gr.ChatInterface(
|
| 116 |
+
fn=respond,
|
| 117 |
+
title="λambdAI",
|
| 118 |
+
theme=gr.themes.Base(primary_hue="gray"),
|
| 119 |
+
css=css,
|
| 120 |
+
additional_inputs=[
|
| 121 |
+
gr.Textbox(
|
| 122 |
+
value="You are a concise, logical AI that explains its reasoning clearly before answering.",
|
| 123 |
+
label="System Message"
|
| 124 |
+
),
|
| 125 |
+
gr.Slider(64, 2048, value=512, step=1, label="Max Tokens"),
|
| 126 |
+
gr.Slider(0.1, 2.0, value=0.7, step=0.1, label="Temperature"),
|
| 127 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
|
| 128 |
+
]
|
| 129 |
+
)
|
| 130 |
|
| 131 |
+
if __name__ == "__main__":
|
| 132 |
+
demo.launch()
|