Nymbo commited on
Commit
be3f346
·
verified ·
1 Parent(s): 231828d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -68
app.py CHANGED
@@ -34,7 +34,7 @@ def respond(
34
  - top_p: top-p (nucleus) sampling
35
  - frequency_penalty: penalize repeated tokens in the output
36
  - seed: a fixed seed for reproducibility; -1 will mean 'random'
37
- - custom_model: the user-provided custom model name (if any)
38
  """
39
 
40
  print(f"Received message: {message}")
@@ -42,7 +42,7 @@ def respond(
42
  print(f"System message: {system_message}")
43
  print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
44
  print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
45
- print(f"Custom model: {custom_model}")
46
 
47
  # Convert seed to None if -1 (meaning random)
48
  if seed == -1:
@@ -65,7 +65,7 @@ def respond(
65
  # Append the latest user message
66
  messages.append({"role": "user", "content": message})
67
 
68
- # Determine which model to use: either custom_model or a default
69
  model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.3-70B-Instruct"
70
  print(f"Model selected for inference: {model_to_use}")
71
 
@@ -75,7 +75,7 @@ def respond(
75
 
76
  # Make the streaming request to the HF Inference API via openai-like client
77
  for message_chunk in client.chat.completions.create(
78
- model=model_to_use, # Use either the user-provided custom model or default
79
  max_tokens=max_tokens,
80
  stream=True, # Stream the response
81
  temperature=temperature,
@@ -93,104 +93,137 @@ def respond(
93
 
94
  print("Completed response generation.")
95
 
 
 
 
 
96
  # Create a Chatbot component with a specified height
97
  chatbot = gr.Chatbot(height=600)
98
  print("Chatbot interface created.")
99
 
100
- # Create the Gradio ChatInterface
101
- # We add two new sliders for Frequency Penalty, Seed, and now a new "Custom Model" text box.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  demo = gr.ChatInterface(
103
  fn=respond,
 
104
  additional_inputs=[
105
- gr.Textbox(value="", label="System message"),
106
- gr.Slider(
107
- minimum=1,
108
- maximum=4096,
109
- value=512,
110
- step=1,
111
- label="Max new tokens"
112
- ),
113
- gr.Slider(
114
- minimum=0.1,
115
- maximum=4.0,
116
- value=0.7,
117
- step=0.1,
118
- label="Temperature"
119
- ),
120
- gr.Slider(
121
- minimum=0.1,
122
- maximum=1.0,
123
- value=0.95,
124
- step=0.05,
125
- label="Top-P"
126
- ),
127
- gr.Slider(
128
- minimum=-2.0,
129
- maximum=2.0,
130
- value=0.0,
131
- step=0.1,
132
- label="Frequency Penalty"
133
- ),
134
- gr.Slider(
135
- minimum=-1,
136
- maximum=65535,
137
- value=-1,
138
- step=1,
139
- label="Seed (-1 for random)"
140
- ),
141
- gr.Textbox(
142
- value="",
143
- label="Custom Model",
144
- info="(Optional) Provide a custom Hugging Face model path. This will override the default model if not empty."
145
- ),
146
  ],
147
  fill_height=True,
148
  chatbot=chatbot,
149
  theme="Nymbo/Nymbo_Theme",
150
  )
151
- print("Gradio interface initialized.")
152
 
153
- # --------------------------------------------------------
154
- # NEW FEATURE: "Featured Models" Accordion with Filtering
155
- # Adapted from Serverless-ImgGen-Hub's approach
156
- # --------------------------------------------------------
157
  with demo:
158
  with gr.Accordion("Featured Models", open=False):
159
- # Textbox to search/filter models
160
- model_search = gr.Textbox(
161
  label="Filter Models",
162
  placeholder="Search for a featured model...",
163
  lines=1
164
  )
165
-
166
- # For demonstration purposes, here is a sample list of possible text-generation models
167
  models_list = [
168
  "meta-llama/Llama-3.3-70B-Instruct",
169
- "meta-llama/Llama-3.1-8B-Instruct",
170
- "microsoft/Phi-3.5-mini-instruct",
171
- "mistralai/Mistral-7B-Instruct-v0.3",
172
  "tiiuae/falcon-7b-instruct",
173
- "Qwen/Qwen2.5-72B-Instruct",
 
 
 
174
  ]
175
-
176
- # Radio buttons to display and select from the featured models
177
- # This won't directly override the "Custom Model" field, but you can copy it from here
178
- featured_model = gr.Radio(
179
  label="Select a model below",
180
  choices=models_list,
181
  value="meta-llama/Llama-3.3-70B-Instruct",
182
  interactive=True
183
  )
184
 
185
- # Filtering function to update model list based on search input
186
  def filter_models(search_term):
187
- # Filter the list by checking if the search term is in each model name
188
  filtered = [m for m in models_list if search_term.lower() in m.lower()]
189
  return gr.update(choices=filtered)
190
 
191
- # When the user types in the search box, we update the featured_model radio choices
192
- model_search.change(filter_models, inputs=model_search, outputs=featured_model)
 
 
 
 
193
 
 
 
 
 
 
 
 
 
194
 
195
  if __name__ == "__main__":
196
  print("Launching the demo application.")
 
34
  - top_p: top-p (nucleus) sampling
35
  - frequency_penalty: penalize repeated tokens in the output
36
  - seed: a fixed seed for reproducibility; -1 will mean 'random'
37
+ - 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
38
  """
39
 
40
  print(f"Received message: {message}")
 
42
  print(f"System message: {system_message}")
43
  print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
44
  print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
45
+ print(f"Selected model (custom_model): {custom_model}")
46
 
47
  # Convert seed to None if -1 (meaning random)
48
  if seed == -1:
 
65
  # Append the latest user message
66
  messages.append({"role": "user", "content": message})
67
 
68
+ # If user provided a model, use that; otherwise, fall back to a default
69
  model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.3-70B-Instruct"
70
  print(f"Model selected for inference: {model_to_use}")
71
 
 
75
 
76
  # Make the streaming request to the HF Inference API via openai-like client
77
  for message_chunk in client.chat.completions.create(
78
+ model=model_to_use, # Use either the user-provided or default model
79
  max_tokens=max_tokens,
80
  stream=True, # Stream the response
81
  temperature=temperature,
 
93
 
94
  print("Completed response generation.")
95
 
96
+ # -------------------------
97
+ # GRADIO UI CONFIGURATION
98
+ # -------------------------
99
+
100
  # Create a Chatbot component with a specified height
101
  chatbot = gr.Chatbot(height=600)
102
  print("Chatbot interface created.")
103
 
104
+ # We'll create text boxes & sliders for system prompt, tokens, etc.
105
+ system_message_box = gr.Textbox(value="", label="System message")
106
+
107
+ max_tokens_slider = gr.Slider(
108
+ minimum=1,
109
+ maximum=4096,
110
+ value=512,
111
+ step=1,
112
+ label="Max new tokens"
113
+ )
114
+ temperature_slider = gr.Slider(
115
+ minimum=0.1,
116
+ maximum=4.0,
117
+ value=0.7,
118
+ step=0.1,
119
+ label="Temperature"
120
+ )
121
+ top_p_slider = gr.Slider(
122
+ minimum=0.1,
123
+ maximum=1.0,
124
+ value=0.95,
125
+ step=0.05,
126
+ label="Top-P"
127
+ )
128
+ frequency_penalty_slider = gr.Slider(
129
+ minimum=-2.0,
130
+ maximum=2.0,
131
+ value=0.0,
132
+ step=0.1,
133
+ label="Frequency Penalty"
134
+ )
135
+ seed_slider = gr.Slider(
136
+ minimum=-1,
137
+ maximum=65535,
138
+ value=-1,
139
+ step=1,
140
+ label="Seed (-1 for random)"
141
+ )
142
+
143
+ # The custom_model_box is what the respond function sees as "custom_model"
144
+ custom_model_box = gr.Textbox(
145
+ value="",
146
+ label="Custom Model",
147
+ info="(Optional) Provide a custom Hugging Face model path. Overrides any selected featured model."
148
+ )
149
+
150
+ # Define a function that, when a user selects a model from the radio, populates `custom_model_box`
151
+ def set_custom_model_from_radio(selected):
152
+ """
153
+ This function will get triggered whenever someone picks a model from the 'Featured Models' radio.
154
+ We will update the Custom Model text box with that selection automatically.
155
+ """
156
+ return selected
157
+
158
+ # The main ChatInterface object
159
  demo = gr.ChatInterface(
160
  fn=respond,
161
+ # For ChatInterface, we can pass additional inputs in order to feed them into the "respond" function
162
  additional_inputs=[
163
+ system_message_box,
164
+ max_tokens_slider,
165
+ temperature_slider,
166
+ top_p_slider,
167
+ frequency_penalty_slider,
168
+ seed_slider,
169
+ custom_model_box
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  ],
171
  fill_height=True,
172
  chatbot=chatbot,
173
  theme="Nymbo/Nymbo_Theme",
174
  )
 
175
 
176
+ # -----------
177
+ # ADDING THE "FEATURED MODELS" ACCORDION
178
+ # -----------
 
179
  with demo:
180
  with gr.Accordion("Featured Models", open=False):
181
+ model_search_box = gr.Textbox(
 
182
  label="Filter Models",
183
  placeholder="Search for a featured model...",
184
  lines=1
185
  )
186
+
187
+ # Sample list of popular text models
188
  models_list = [
189
  "meta-llama/Llama-3.3-70B-Instruct",
190
+ "bigscience/bloomz-7b1",
191
+ "OpenAssistant/oasst-sft-1-pythia-12b",
192
+ "OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
193
  "tiiuae/falcon-7b-instruct",
194
+ "OpenAI/gpt-3.5-turbo",
195
+ "OpenAI/gpt-4-32k",
196
+ "meta-llama/Llama-2-13B-chat-hf",
197
+ "meta-llama/Llama-2-70B-chat-hf",
198
  ]
199
+
200
+ featured_model_radio = gr.Radio(
 
 
201
  label="Select a model below",
202
  choices=models_list,
203
  value="meta-llama/Llama-3.3-70B-Instruct",
204
  interactive=True
205
  )
206
 
207
+ # Filter function for the radio
208
  def filter_models(search_term):
 
209
  filtered = [m for m in models_list if search_term.lower() in m.lower()]
210
  return gr.update(choices=filtered)
211
 
212
+ # Whenever we type in the search box, update the radio with the filtered list
213
+ model_search_box.change(
214
+ fn=filter_models,
215
+ inputs=model_search_box,
216
+ outputs=featured_model_radio
217
+ )
218
 
219
+ # Whenever we select a featured model, populate the 'Custom Model' textbox
220
+ featured_model_radio.change(
221
+ fn=set_custom_model_from_radio,
222
+ inputs=featured_model_radio,
223
+ outputs=custom_model_box
224
+ )
225
+
226
+ print("Gradio interface initialized.")
227
 
228
  if __name__ == "__main__":
229
  print("Launching the demo application.")