Spaces:
Sleeping
Sleeping
Commit
·
2931efa
1
Parent(s):
8d4cdea
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,82 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
def respond(
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
response = ""
|
| 29 |
-
|
| 30 |
-
for
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
| 33 |
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
-
token =
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
"""
|
| 46 |
-
demo = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Define available models (update with your actual model IDs)
|
| 5 |
+
model_list = {
|
| 6 |
+
"Safe LM": "HuggingFaceH4/zephyr-7b-beta", # Replace with your Safe LM model ID
|
| 7 |
+
"Zephyr Beta": "HuggingFaceH4/zephyr-7b-beta",
|
| 8 |
+
"Another Model": "HuggingFaceH4/zephyr-7b-beta"
|
| 9 |
+
}
|
| 10 |
|
| 11 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p, selected_model):
|
| 12 |
+
# Look up the model ID from our list based on the dropdown selection
|
| 13 |
+
model_id = model_list.get(selected_model, "HuggingFaceH4/zephyr-7b-beta")
|
| 14 |
+
# Create an InferenceClient for the selected model
|
| 15 |
+
client = InferenceClient(model_id)
|
| 16 |
+
|
| 17 |
+
# Build the conversation history into the message list
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
+
for user_msg, assistant_msg in history or []:
|
| 20 |
+
if user_msg:
|
| 21 |
+
messages.append({"role": "user", "content": user_msg})
|
| 22 |
+
if assistant_msg:
|
| 23 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
| 24 |
messages.append({"role": "user", "content": message})
|
| 25 |
+
|
| 26 |
response = ""
|
| 27 |
+
# Stream the response from the client
|
| 28 |
+
for token_message in client.chat_completion(
|
| 29 |
messages,
|
| 30 |
max_tokens=max_tokens,
|
| 31 |
stream=True,
|
| 32 |
temperature=temperature,
|
| 33 |
top_p=top_p,
|
| 34 |
):
|
| 35 |
+
token = token_message.choices[0].delta.content
|
|
|
|
| 36 |
response += token
|
| 37 |
yield response
|
| 38 |
|
| 39 |
+
# CSS styling: pastel backgrounds, gentle light colors, and rounded corners for a safe vibe
|
| 40 |
+
css = """
|
| 41 |
+
body { background-color: #FAF3E0; }
|
| 42 |
+
.gradio-container { background-color: #FFFFFF; border-radius: 16px; padding: 20px; }
|
| 43 |
+
button, input, .gradio-dropdown, .gradio-slider, textarea { border-radius: 16px; }
|
| 44 |
+
.gradio-chat { border-radius: 16px; }
|
| 45 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
with gr.Blocks(css=css) as demo:
|
| 48 |
+
with gr.Row():
|
| 49 |
+
# Left sidebar: Model selector
|
| 50 |
+
with gr.Column(scale=1):
|
| 51 |
+
gr.Markdown("## Models")
|
| 52 |
+
model_dropdown = gr.Dropdown(
|
| 53 |
+
choices=list(model_list.keys()),
|
| 54 |
+
label="Select Model",
|
| 55 |
+
value="Safe LM"
|
| 56 |
+
)
|
| 57 |
+
# Main area: Chat interface and settings
|
| 58 |
+
with gr.Column(scale=3):
|
| 59 |
+
gr.Markdown("## Chat Interface")
|
| 60 |
+
chatbot = gr.Chatbot(label="Chat with your Model")
|
| 61 |
+
user_input = gr.Textbox(placeholder="Enter your message...", label="Your Message")
|
| 62 |
+
with gr.Row():
|
| 63 |
+
send_button = gr.Button("Send")
|
| 64 |
+
clear_button = gr.Button("Clear Chat")
|
| 65 |
+
gr.Markdown("### Chat Settings")
|
| 66 |
+
system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System Message")
|
| 67 |
+
max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens")
|
| 68 |
+
temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
| 69 |
+
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 70 |
+
|
| 71 |
+
# When "Send" is clicked, run the respond() function and update the chat interface.
|
| 72 |
+
send_button.click(
|
| 73 |
+
fn=respond,
|
| 74 |
+
inputs=[user_input, chatbot, system_message, max_tokens_slider, temperature_slider, top_p_slider, model_dropdown],
|
| 75 |
+
outputs=[user_input, chatbot],
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Clear the chat history when "Clear Chat" is clicked.
|
| 79 |
+
clear_button.click(lambda: None, None, chatbot, queue=False)
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
demo.launch()
|