Spaces:
Sleeping
Sleeping
adding examples
Browse files
app.py
CHANGED
|
@@ -26,8 +26,18 @@ def respond(
|
|
| 26 |
custom_model
|
| 27 |
):
|
| 28 |
"""
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
"""
|
|
|
|
| 31 |
print(f"Received message: {message}")
|
| 32 |
print(f"History: {history}")
|
| 33 |
print(f"System message: {system_message}")
|
|
@@ -35,25 +45,38 @@ def respond(
|
|
| 35 |
print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
|
| 36 |
print(f"Selected model (custom_model): {custom_model}")
|
| 37 |
|
|
|
|
| 38 |
if seed == -1:
|
| 39 |
seed = None
|
| 40 |
|
| 41 |
-
# Construct the messages array
|
| 42 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
|
| 43 |
for val in history:
|
| 44 |
-
user_part = val[0]
|
| 45 |
-
assistant_part = val[1]
|
| 46 |
if user_part:
|
| 47 |
messages.append({"role": "user", "content": user_part})
|
|
|
|
| 48 |
if assistant_part:
|
| 49 |
messages.append({"role": "assistant", "content": assistant_part})
|
|
|
|
| 50 |
|
|
|
|
| 51 |
messages.append({"role": "user", "content": message})
|
|
|
|
| 52 |
|
| 53 |
-
# If user provided a model, use
|
| 54 |
model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.3-70B-Instruct"
|
|
|
|
|
|
|
|
|
|
| 55 |
response = ""
|
|
|
|
| 56 |
|
|
|
|
| 57 |
for message_chunk in client.chat.completions.create(
|
| 58 |
model=model_to_use,
|
| 59 |
max_tokens=max_tokens,
|
|
@@ -64,47 +87,81 @@ def respond(
|
|
| 64 |
seed=seed,
|
| 65 |
messages=messages,
|
| 66 |
):
|
|
|
|
| 67 |
token_text = message_chunk.choices[0].delta.content
|
|
|
|
| 68 |
response += token_text
|
| 69 |
yield response
|
| 70 |
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# -------------------------
|
| 73 |
# GRADIO UI CONFIGURATION
|
| 74 |
# -------------------------
|
| 75 |
|
| 76 |
-
# Create a Chatbot component
|
| 77 |
-
chatbot = gr.Chatbot(
|
| 78 |
-
|
| 79 |
-
show_copy_button=True,
|
| 80 |
-
placeholder="Select a model and begin chatting",
|
| 81 |
-
likeable=True,
|
| 82 |
-
layout="panel"
|
| 83 |
-
)
|
| 84 |
|
| 85 |
-
# Create textboxes
|
| 86 |
system_message_box = gr.Textbox(value="", label="System message")
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
def set_custom_model_from_radio(selected):
|
| 96 |
"""
|
| 97 |
-
|
|
|
|
| 98 |
"""
|
| 99 |
print(f"Featured model selected: {selected}")
|
| 100 |
return selected
|
| 101 |
|
| 102 |
-
|
| 103 |
-
#
|
| 104 |
-
#
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
# No 'examples' here—because we want to keep the user's parameters unchanged
|
| 108 |
demo = gr.ChatInterface(
|
| 109 |
fn=respond,
|
| 110 |
additional_inputs=[
|
|
@@ -114,27 +171,42 @@ demo = gr.ChatInterface(
|
|
| 114 |
top_p_slider,
|
| 115 |
frequency_penalty_slider,
|
| 116 |
seed_slider,
|
| 117 |
-
custom_model_box
|
| 118 |
],
|
| 119 |
fill_height=True,
|
| 120 |
chatbot=chatbot,
|
| 121 |
-
textbox=user_textbox,
|
| 122 |
-
multimodal=True,
|
| 123 |
-
concurrency_limit=20,
|
| 124 |
theme="Nymbo/Nymbo_Theme",
|
| 125 |
-
# No examples parameter used
|
| 126 |
-
cache_examples=False
|
| 127 |
)
|
| 128 |
print("ChatInterface object created.")
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
with demo:
|
| 131 |
-
# Featured models accordion
|
| 132 |
with gr.Accordion("Featured Models", open=False):
|
| 133 |
model_search_box = gr.Textbox(
|
| 134 |
label="Filter Models",
|
| 135 |
placeholder="Search for a featured model...",
|
| 136 |
lines=1
|
| 137 |
)
|
|
|
|
| 138 |
|
| 139 |
models_list = [
|
| 140 |
"meta-llama/Llama-3.3-70B-Instruct",
|
|
@@ -155,6 +227,7 @@ with demo:
|
|
| 155 |
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 156 |
"microsoft/Phi-3.5-mini-instruct",
|
| 157 |
]
|
|
|
|
| 158 |
|
| 159 |
featured_model_radio = gr.Radio(
|
| 160 |
label="Select a model below",
|
|
@@ -162,9 +235,12 @@ with demo:
|
|
| 162 |
value="meta-llama/Llama-3.3-70B-Instruct",
|
| 163 |
interactive=True
|
| 164 |
)
|
|
|
|
| 165 |
|
| 166 |
def filter_models(search_term):
|
|
|
|
| 167 |
filtered = [m for m in models_list if search_term.lower() in m.lower()]
|
|
|
|
| 168 |
return gr.update(choices=filtered)
|
| 169 |
|
| 170 |
model_search_box.change(
|
|
@@ -172,34 +248,14 @@ with demo:
|
|
| 172 |
inputs=model_search_box,
|
| 173 |
outputs=featured_model_radio
|
| 174 |
)
|
|
|
|
| 175 |
|
| 176 |
featured_model_radio.change(
|
| 177 |
fn=set_custom_model_from_radio,
|
| 178 |
inputs=featured_model_radio,
|
| 179 |
outputs=custom_model_box
|
| 180 |
)
|
| 181 |
-
|
| 182 |
-
# Example Prompts accordion
|
| 183 |
-
with gr.Accordion("Example Prompts", open=False):
|
| 184 |
-
ex1_btn = gr.Button("Example 1: 'Howdy, partner!'")
|
| 185 |
-
ex2_btn = gr.Button("Example 2: 'What's your model name and who trained you?'")
|
| 186 |
-
ex3_btn = gr.Button("Example 3: 'How many R's in Strawberry?'")
|
| 187 |
-
|
| 188 |
-
# Helper function that returns an update for user_textbox
|
| 189 |
-
def load_example(example_text):
|
| 190 |
-
return gr.update(value=example_text)
|
| 191 |
-
|
| 192 |
-
ex1_btn.click(fn=lambda: load_example("Howdy, partner!"),
|
| 193 |
-
inputs=[],
|
| 194 |
-
outputs=user_textbox)
|
| 195 |
-
|
| 196 |
-
ex2_btn.click(fn=lambda: load_example("What's your model name and who trained you?"),
|
| 197 |
-
inputs=[],
|
| 198 |
-
outputs=user_textbox)
|
| 199 |
-
|
| 200 |
-
ex3_btn.click(fn=lambda: load_example("How many R's are there in the word Strawberry?"),
|
| 201 |
-
inputs=[],
|
| 202 |
-
outputs=user_textbox)
|
| 203 |
|
| 204 |
print("Gradio interface initialized.")
|
| 205 |
|
|
|
|
| 26 |
custom_model
|
| 27 |
):
|
| 28 |
"""
|
| 29 |
+
This function handles the chatbot response. It takes in:
|
| 30 |
+
- message: the user's new message
|
| 31 |
+
- history: the list of previous messages, each as a tuple (user_msg, assistant_msg)
|
| 32 |
+
- system_message: the system prompt
|
| 33 |
+
- max_tokens: the maximum number of tokens to generate in the response
|
| 34 |
+
- temperature: sampling temperature
|
| 35 |
+
- top_p: top-p (nucleus) sampling
|
| 36 |
+
- frequency_penalty: penalize repeated tokens in the output
|
| 37 |
+
- seed: a fixed seed for reproducibility; -1 will mean 'random'
|
| 38 |
+
- custom_model: the final model name in use, which may be set by selecting from the Featured Models radio or by typing a custom model
|
| 39 |
"""
|
| 40 |
+
|
| 41 |
print(f"Received message: {message}")
|
| 42 |
print(f"History: {history}")
|
| 43 |
print(f"System message: {system_message}")
|
|
|
|
| 45 |
print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
|
| 46 |
print(f"Selected model (custom_model): {custom_model}")
|
| 47 |
|
| 48 |
+
# Convert seed to None if -1 (meaning random)
|
| 49 |
if seed == -1:
|
| 50 |
seed = None
|
| 51 |
|
| 52 |
+
# Construct the messages array required by the API
|
| 53 |
messages = [{"role": "system", "content": system_message}]
|
| 54 |
+
print("Initial messages array constructed.")
|
| 55 |
+
|
| 56 |
+
# Add conversation history to the context
|
| 57 |
for val in history:
|
| 58 |
+
user_part = val[0] # Extract user message from the tuple
|
| 59 |
+
assistant_part = val[1] # Extract assistant message from the tuple
|
| 60 |
if user_part:
|
| 61 |
messages.append({"role": "user", "content": user_part})
|
| 62 |
+
print(f"Added user message to context: {user_part}")
|
| 63 |
if assistant_part:
|
| 64 |
messages.append({"role": "assistant", "content": assistant_part})
|
| 65 |
+
print(f"Added assistant message to context: {assistant_part}")
|
| 66 |
|
| 67 |
+
# Append the latest user message
|
| 68 |
messages.append({"role": "user", "content": message})
|
| 69 |
+
print("Latest user message appended.")
|
| 70 |
|
| 71 |
+
# If user provided a model, use that; otherwise, fall back to a default model
|
| 72 |
model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.3-70B-Instruct"
|
| 73 |
+
print(f"Model selected for inference: {model_to_use}")
|
| 74 |
+
|
| 75 |
+
# Start with an empty string to build the response as tokens stream in
|
| 76 |
response = ""
|
| 77 |
+
print("Sending request to OpenAI API.")
|
| 78 |
|
| 79 |
+
# Make the streaming request to the HF Inference API via openai-like client
|
| 80 |
for message_chunk in client.chat.completions.create(
|
| 81 |
model=model_to_use,
|
| 82 |
max_tokens=max_tokens,
|
|
|
|
| 87 |
seed=seed,
|
| 88 |
messages=messages,
|
| 89 |
):
|
| 90 |
+
# Extract the token text from the response chunk
|
| 91 |
token_text = message_chunk.choices[0].delta.content
|
| 92 |
+
print(f"Received token: {token_text}")
|
| 93 |
response += token_text
|
| 94 |
yield response
|
| 95 |
|
| 96 |
+
print("Completed response generation.")
|
| 97 |
+
|
| 98 |
|
| 99 |
# -------------------------
|
| 100 |
# GRADIO UI CONFIGURATION
|
| 101 |
# -------------------------
|
| 102 |
|
| 103 |
+
# Create a Chatbot component with a specified height
|
| 104 |
+
chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="Select a model and begin chatting", likeable=True, layout="panel")
|
| 105 |
+
print("Chatbot interface created.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
# Create textboxes and sliders for system prompt, tokens, and other parameters
|
| 108 |
system_message_box = gr.Textbox(value="", label="System message")
|
| 109 |
+
|
| 110 |
+
max_tokens_slider = gr.Slider(
|
| 111 |
+
minimum=1,
|
| 112 |
+
maximum=4096,
|
| 113 |
+
value=512,
|
| 114 |
+
step=1,
|
| 115 |
+
label="Max new tokens"
|
| 116 |
+
)
|
| 117 |
+
temperature_slider = gr.Slider(
|
| 118 |
+
minimum=0.1,
|
| 119 |
+
maximum=4.0,
|
| 120 |
+
value=0.7,
|
| 121 |
+
step=0.1,
|
| 122 |
+
label="Temperature"
|
| 123 |
+
)
|
| 124 |
+
top_p_slider = gr.Slider(
|
| 125 |
+
minimum=0.1,
|
| 126 |
+
maximum=1.0,
|
| 127 |
+
value=0.95,
|
| 128 |
+
step=0.05,
|
| 129 |
+
label="Top-P"
|
| 130 |
+
)
|
| 131 |
+
frequency_penalty_slider = gr.Slider(
|
| 132 |
+
minimum=-2.0,
|
| 133 |
+
maximum=2.0,
|
| 134 |
+
value=0.0,
|
| 135 |
+
step=0.1,
|
| 136 |
+
label="Frequency Penalty"
|
| 137 |
+
)
|
| 138 |
+
seed_slider = gr.Slider(
|
| 139 |
+
minimum=-1,
|
| 140 |
+
maximum=65535,
|
| 141 |
+
value=-1,
|
| 142 |
+
step=1,
|
| 143 |
+
label="Seed (-1 for random)"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# The custom_model_box is what the respond function sees as "custom_model"
|
| 147 |
+
custom_model_box = gr.Textbox(
|
| 148 |
+
value="",
|
| 149 |
+
label="Custom Model",
|
| 150 |
+
info="(Optional) Provide a custom Hugging Face model path. Overrides any selected featured model."
|
| 151 |
+
)
|
| 152 |
|
| 153 |
def set_custom_model_from_radio(selected):
|
| 154 |
"""
|
| 155 |
+
This function will get triggered whenever someone picks a model from the 'Featured Models' radio.
|
| 156 |
+
We will update the Custom Model text box with that selection automatically.
|
| 157 |
"""
|
| 158 |
print(f"Featured model selected: {selected}")
|
| 159 |
return selected
|
| 160 |
|
| 161 |
+
# IMPORTANT: Because we have 1 main user input + 7 additional inputs,
|
| 162 |
+
# each example should be an 8-item list [user_text, system_prompt, max_tokens, temperature,
|
| 163 |
+
# top_p, frequency_penalty, seed, custom_model].
|
| 164 |
+
# You can adjust the default parameter values if desired.
|
|
|
|
|
|
|
| 165 |
demo = gr.ChatInterface(
|
| 166 |
fn=respond,
|
| 167 |
additional_inputs=[
|
|
|
|
| 171 |
top_p_slider,
|
| 172 |
frequency_penalty_slider,
|
| 173 |
seed_slider,
|
| 174 |
+
custom_model_box,
|
| 175 |
],
|
| 176 |
fill_height=True,
|
| 177 |
chatbot=chatbot,
|
|
|
|
|
|
|
|
|
|
| 178 |
theme="Nymbo/Nymbo_Theme",
|
|
|
|
|
|
|
| 179 |
)
|
| 180 |
print("ChatInterface object created.")
|
| 181 |
|
| 182 |
+
# Add examples to the interface
|
| 183 |
+
demo.add_examples(
|
| 184 |
+
examples=[
|
| 185 |
+
["Howdy, partner!", "You are a friendly assistant.", 512, 0.7, 0.95, 0.0, -1, ""],
|
| 186 |
+
["What's your model name and who trained you?", "You are a factual assistant.", 512, 0.7, 0.95, 0.0, -1, ""],
|
| 187 |
+
["How many R's are there in 'Strawberry'?", "You are a playful assistant.", 512, 0.7, 0.95, 0.0, -1, ""],
|
| 188 |
+
],
|
| 189 |
+
inputs=[
|
| 190 |
+
chatbot,
|
| 191 |
+
system_message_box,
|
| 192 |
+
max_tokens_slider,
|
| 193 |
+
temperature_slider,
|
| 194 |
+
top_p_slider,
|
| 195 |
+
frequency_penalty_slider,
|
| 196 |
+
seed_slider,
|
| 197 |
+
custom_model_box,
|
| 198 |
+
],
|
| 199 |
+
)
|
| 200 |
+
print("Examples added to the interface.")
|
| 201 |
+
|
| 202 |
with demo:
|
|
|
|
| 203 |
with gr.Accordion("Featured Models", open=False):
|
| 204 |
model_search_box = gr.Textbox(
|
| 205 |
label="Filter Models",
|
| 206 |
placeholder="Search for a featured model...",
|
| 207 |
lines=1
|
| 208 |
)
|
| 209 |
+
print("Model search box created.")
|
| 210 |
|
| 211 |
models_list = [
|
| 212 |
"meta-llama/Llama-3.3-70B-Instruct",
|
|
|
|
| 227 |
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
| 228 |
"microsoft/Phi-3.5-mini-instruct",
|
| 229 |
]
|
| 230 |
+
print("Models list initialized.")
|
| 231 |
|
| 232 |
featured_model_radio = gr.Radio(
|
| 233 |
label="Select a model below",
|
|
|
|
| 235 |
value="meta-llama/Llama-3.3-70B-Instruct",
|
| 236 |
interactive=True
|
| 237 |
)
|
| 238 |
+
print("Featured models radio button created.")
|
| 239 |
|
| 240 |
def filter_models(search_term):
|
| 241 |
+
print(f"Filtering models with search term: {search_term}")
|
| 242 |
filtered = [m for m in models_list if search_term.lower() in m.lower()]
|
| 243 |
+
print(f"Filtered models: {filtered}")
|
| 244 |
return gr.update(choices=filtered)
|
| 245 |
|
| 246 |
model_search_box.change(
|
|
|
|
| 248 |
inputs=model_search_box,
|
| 249 |
outputs=featured_model_radio
|
| 250 |
)
|
| 251 |
+
print("Model search box change event linked.")
|
| 252 |
|
| 253 |
featured_model_radio.change(
|
| 254 |
fn=set_custom_model_from_radio,
|
| 255 |
inputs=featured_model_radio,
|
| 256 |
outputs=custom_model_box
|
| 257 |
)
|
| 258 |
+
print("Featured model radio button change event linked.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
print("Gradio interface initialized.")
|
| 261 |
|