Spaces:
Sleeping
Sleeping
Commit
·
6cac78c
1
Parent(s):
cd64317
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,23 +9,20 @@ model_list = {
|
|
| 9 |
}
|
| 10 |
|
| 11 |
def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
|
| 15 |
# Build conversation messages for the client
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
for user_msg, assistant_msg in history:
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
messages.append({"role": "user", "content": message})
|
| 21 |
|
| 22 |
-
# Append new user message to history with an empty assistant response
|
| 23 |
-
history = history + [(message, "")]
|
| 24 |
response = ""
|
| 25 |
|
| 26 |
-
# Create an InferenceClient for the selected model
|
| 27 |
-
client = InferenceClient(model_list.get(selected_model, "HuggingFaceH4/zephyr-7b-beta"))
|
| 28 |
-
|
| 29 |
# Stream the response from the client
|
| 30 |
for token_message in client.chat_completion(
|
| 31 |
messages,
|
|
@@ -35,21 +32,113 @@ def respond(message, history, system_message, max_tokens, temperature, top_p, se
|
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
token = token_message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Yield two outputs: clear the input and update the chat history
|
| 42 |
-
yield "", history
|
| 43 |
|
| 44 |
-
# Custom CSS for
|
| 45 |
css = """
|
| 46 |
-
body {
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
"""
|
| 51 |
|
| 52 |
with gr.Blocks(css=css) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
with gr.Row():
|
| 54 |
# Left sidebar: Model selector
|
| 55 |
with gr.Column(scale=1):
|
|
@@ -57,31 +146,110 @@ with gr.Blocks(css=css) as demo:
|
|
| 57 |
model_dropdown = gr.Dropdown(
|
| 58 |
choices=list(model_list.keys()),
|
| 59 |
label="Select Model",
|
| 60 |
-
value="Safe LM"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
-
|
|
|
|
| 63 |
with gr.Column(scale=3):
|
| 64 |
-
gr.
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
with gr.Row():
|
| 68 |
-
send_button = gr.Button("Send")
|
| 69 |
clear_button = gr.Button("Clear Chat")
|
| 70 |
-
gr.Markdown("### Chat Settings")
|
| 71 |
-
system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System Message")
|
| 72 |
-
max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens")
|
| 73 |
-
temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 74 |
-
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 75 |
|
| 76 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
send_button.click(
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
-
# Clear the chat history
|
| 84 |
clear_button.click(lambda: None, None, chatbot, queue=False)
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
| 87 |
-
demo.launch()
|
|
|
|
| 9 |
}
|
| 10 |
|
| 11 |
def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
|
| 12 |
+
# Create an InferenceClient for the selected model
|
| 13 |
+
client = InferenceClient(model_list.get(selected_model, "HuggingFaceH4/zephyr-7b-beta"))
|
| 14 |
|
| 15 |
# Build conversation messages for the client
|
| 16 |
messages = [{"role": "system", "content": system_message}]
|
| 17 |
for user_msg, assistant_msg in history:
|
| 18 |
+
if user_msg: # Only add non-empty messages
|
| 19 |
+
messages.append({"role": "user", "content": user_msg})
|
| 20 |
+
if assistant_msg: # Only add non-empty messages
|
| 21 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 22 |
messages.append({"role": "user", "content": message})
|
| 23 |
|
|
|
|
|
|
|
| 24 |
response = ""
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
# Stream the response from the client
|
| 27 |
for token_message in client.chat_completion(
|
| 28 |
messages,
|
|
|
|
| 32 |
top_p=top_p,
|
| 33 |
):
|
| 34 |
token = token_message.choices[0].delta.content
|
| 35 |
+
if token is not None: # Handle potential None values
|
| 36 |
+
response += token
|
| 37 |
+
yield response
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Custom CSS for branding with blue, teal and gold accents
|
| 40 |
css = """
|
| 41 |
+
body {
|
| 42 |
+
background-color: #f5f7fa;
|
| 43 |
+
font-family: 'Inter', 'Segoe UI', sans-serif;
|
| 44 |
+
}
|
| 45 |
+
.gradio-container {
|
| 46 |
+
background-color: #FFFFFF;
|
| 47 |
+
border-radius: 16px;
|
| 48 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
| 49 |
+
max-width: 95%;
|
| 50 |
+
margin: 20px auto;
|
| 51 |
+
}
|
| 52 |
+
.app-header {
|
| 53 |
+
background: linear-gradient(135deg, #0a76d8, #36b3c2);
|
| 54 |
+
padding: 20px;
|
| 55 |
+
border-radius: 16px 16px 0 0;
|
| 56 |
+
position: relative;
|
| 57 |
+
color: white;
|
| 58 |
+
overflow: hidden;
|
| 59 |
+
}
|
| 60 |
+
.app-header::before {
|
| 61 |
+
content: "🔒";
|
| 62 |
+
position: absolute;
|
| 63 |
+
font-size: 120px;
|
| 64 |
+
opacity: 0.1;
|
| 65 |
+
right: -20px;
|
| 66 |
+
top: -30px;
|
| 67 |
+
transform: rotate(15deg);
|
| 68 |
+
}
|
| 69 |
+
.app-title {
|
| 70 |
+
font-size: 28px;
|
| 71 |
+
font-weight: 800;
|
| 72 |
+
margin: 0;
|
| 73 |
+
display: flex;
|
| 74 |
+
align-items: center;
|
| 75 |
+
}
|
| 76 |
+
.app-title .safe {
|
| 77 |
+
font-weight: 900;
|
| 78 |
+
margin-right: 5px;
|
| 79 |
+
}
|
| 80 |
+
.app-title .lm {
|
| 81 |
+
font-weight: 300;
|
| 82 |
+
letter-spacing: 1px;
|
| 83 |
+
}
|
| 84 |
+
.app-title .shield {
|
| 85 |
+
margin-right: 10px;
|
| 86 |
+
font-size: 24px;
|
| 87 |
+
}
|
| 88 |
+
.app-subtitle {
|
| 89 |
+
font-size: 14px;
|
| 90 |
+
opacity: 0.9;
|
| 91 |
+
margin-top: 5px;
|
| 92 |
+
}
|
| 93 |
+
.golden-accent {
|
| 94 |
+
border-top: 3px solid #e6c200;
|
| 95 |
+
margin: 0;
|
| 96 |
+
}
|
| 97 |
+
button, .gradio-button {
|
| 98 |
+
background: linear-gradient(to right, #0a76d8, #36b3c2) !important;
|
| 99 |
+
color: white !important;
|
| 100 |
+
border: none !important;
|
| 101 |
+
border-radius: 12px !important;
|
| 102 |
+
padding: 10px 20px !important;
|
| 103 |
+
font-weight: 600 !important;
|
| 104 |
+
transition: all 0.3s ease !important;
|
| 105 |
+
}
|
| 106 |
+
button:hover, .gradio-button:hover {
|
| 107 |
+
transform: translateY(-2px) !important;
|
| 108 |
+
box-shadow: 0 4px 8px rgba(10, 118, 216, 0.3) !important;
|
| 109 |
+
}
|
| 110 |
+
.gradio-dropdown, .gradio-slider, .gradio-textbox, .gradio-checkbox {
|
| 111 |
+
border-radius: 12px !important;
|
| 112 |
+
border: 1px solid #e1e5eb !important;
|
| 113 |
+
}
|
| 114 |
+
.model-select {
|
| 115 |
+
border-left: 4px solid #e6c200 !important;
|
| 116 |
+
padding-left: 10px !important;
|
| 117 |
+
}
|
| 118 |
+
.footer {
|
| 119 |
+
text-align: center;
|
| 120 |
+
color: #666;
|
| 121 |
+
font-size: 12px;
|
| 122 |
+
margin-top: 20px;
|
| 123 |
+
padding: 10px;
|
| 124 |
+
}
|
| 125 |
"""
|
| 126 |
|
| 127 |
with gr.Blocks(css=css) as demo:
|
| 128 |
+
# Custom header with branding
|
| 129 |
+
with gr.HTML():
|
| 130 |
+
gr.HTML("""
|
| 131 |
+
<div class="app-header">
|
| 132 |
+
<h1 class="app-title">
|
| 133 |
+
<span class="shield">🔒</span>
|
| 134 |
+
<span class="safe">Safe</span>
|
| 135 |
+
<span class="lm">Playground</span>
|
| 136 |
+
</h1>
|
| 137 |
+
<p class="app-subtitle">Responsible AI for everyone</p>
|
| 138 |
+
</div>
|
| 139 |
+
<hr class="golden-accent">
|
| 140 |
+
""")
|
| 141 |
+
|
| 142 |
with gr.Row():
|
| 143 |
# Left sidebar: Model selector
|
| 144 |
with gr.Column(scale=1):
|
|
|
|
| 146 |
model_dropdown = gr.Dropdown(
|
| 147 |
choices=list(model_list.keys()),
|
| 148 |
label="Select Model",
|
| 149 |
+
value="Safe LM",
|
| 150 |
+
elem_classes=["model-select"]
|
| 151 |
+
)
|
| 152 |
+
|
| 153 |
+
# Settings
|
| 154 |
+
gr.Markdown("### Settings")
|
| 155 |
+
system_message = gr.Textbox(
|
| 156 |
+
value="You are a friendly and safe assistant.",
|
| 157 |
+
label="System Message",
|
| 158 |
+
lines=2
|
| 159 |
+
)
|
| 160 |
+
max_tokens_slider = gr.Slider(
|
| 161 |
+
minimum=1, maximum=2048, value=512, step=1,
|
| 162 |
+
label="Max New Tokens"
|
| 163 |
+
)
|
| 164 |
+
temperature_slider = gr.Slider(
|
| 165 |
+
minimum=0.1, maximum=4.0, value=0.7, step=0.1,
|
| 166 |
+
label="Temperature"
|
| 167 |
+
)
|
| 168 |
+
top_p_slider = gr.Slider(
|
| 169 |
+
minimum=0.1, maximum=1.0, value=0.95, step=0.05,
|
| 170 |
+
label="Top-p (nucleus sampling)"
|
| 171 |
)
|
| 172 |
+
|
| 173 |
+
# Main area: Chat interface
|
| 174 |
with gr.Column(scale=3):
|
| 175 |
+
chatbot = gr.Chatbot(
|
| 176 |
+
label="Conversation",
|
| 177 |
+
show_label=True,
|
| 178 |
+
avatar_images=("👤", "🔒"),
|
| 179 |
+
height=500
|
| 180 |
+
)
|
| 181 |
+
with gr.Row():
|
| 182 |
+
user_input = gr.Textbox(
|
| 183 |
+
placeholder="Type your message here...",
|
| 184 |
+
label="Your Message",
|
| 185 |
+
show_label=False,
|
| 186 |
+
scale=9
|
| 187 |
+
)
|
| 188 |
+
send_button = gr.Button(
|
| 189 |
+
"Send",
|
| 190 |
+
scale=1,
|
| 191 |
+
variant="primary"
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
with gr.Row():
|
|
|
|
| 195 |
clear_button = gr.Button("Clear Chat")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
|
| 197 |
+
# Footer with branding
|
| 198 |
+
with gr.HTML():
|
| 199 |
+
gr.HTML("""
|
| 200 |
+
<div class="footer">
|
| 201 |
+
<p>Safe Playground™ | Responsible AI Technology | © 2025</p>
|
| 202 |
+
</div>
|
| 203 |
+
""")
|
| 204 |
+
|
| 205 |
+
# Fix 1: Correct event handling for the chatbot interface
|
| 206 |
+
def user(user_message, history):
|
| 207 |
+
# Return the user's message and add it to history
|
| 208 |
+
return "", history + [[user_message, None]]
|
| 209 |
+
|
| 210 |
+
def bot(history, system_message, max_tokens, temperature, top_p, selected_model):
|
| 211 |
+
# Get the last user message from history
|
| 212 |
+
user_message = history[-1][0]
|
| 213 |
+
# Call respond function with the message
|
| 214 |
+
response_generator = respond(
|
| 215 |
+
user_message,
|
| 216 |
+
history[:-1], # Pass history without the current message
|
| 217 |
+
system_message,
|
| 218 |
+
max_tokens,
|
| 219 |
+
temperature,
|
| 220 |
+
top_p,
|
| 221 |
+
selected_model
|
| 222 |
+
)
|
| 223 |
+
# Update history as responses come in
|
| 224 |
+
for response in response_generator:
|
| 225 |
+
history[-1][1] = response
|
| 226 |
+
yield history
|
| 227 |
+
|
| 228 |
+
# Wire up the event chain
|
| 229 |
+
user_input.submit(
|
| 230 |
+
user,
|
| 231 |
+
[user_input, chatbot],
|
| 232 |
+
[user_input, chatbot],
|
| 233 |
+
queue=False
|
| 234 |
+
).then(
|
| 235 |
+
bot,
|
| 236 |
+
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 237 |
+
[chatbot]
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
send_button.click(
|
| 241 |
+
user,
|
| 242 |
+
[user_input, chatbot],
|
| 243 |
+
[user_input, chatbot],
|
| 244 |
+
queue=False
|
| 245 |
+
).then(
|
| 246 |
+
bot,
|
| 247 |
+
[chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 248 |
+
[chatbot]
|
| 249 |
)
|
| 250 |
|
| 251 |
+
# Clear the chat history
|
| 252 |
clear_button.click(lambda: None, None, chatbot, queue=False)
|
| 253 |
|
| 254 |
if __name__ == "__main__":
|
| 255 |
+
demo.launch()
|