Nymbo commited on
Commit
21137c4
·
verified ·
1 Parent(s): d735dab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -151
app.py CHANGED
@@ -34,15 +34,14 @@ 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
- - model: the model to use for text generation
38
  """
39
 
40
  print(f"Received message: {message}")
41
  print(f"History: {history}")
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"Model: {model}")
46
 
47
  # Convert seed to None if -1 (meaning random)
48
  if seed == -1:
@@ -71,13 +70,13 @@ def respond(
71
 
72
  # Make the streaming request to the HF Inference API via openai-like client
73
  for message_chunk in client.chat.completions.create(
74
- model=model, # Use the selected model
75
  max_tokens=max_tokens,
76
  stream=True, # Stream the response
77
  temperature=temperature,
78
  top_p=top_p,
79
- frequency_penalty=frequency_penalty, # <-- NEW
80
- seed=seed, # <-- NEW
81
  messages=messages,
82
  ):
83
  # Extract the token text from the response chunk
@@ -92,164 +91,125 @@ def respond(
92
  chatbot = gr.Chatbot(height=600)
93
  print("Chatbot interface created.")
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  # Create the Gradio ChatInterface
96
- # We add two new sliders for Frequency Penalty and Seed
97
  demo = gr.ChatInterface(
98
  respond,
99
  additional_inputs=[
100
  gr.Textbox(value="", label="System message"),
101
- gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
102
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
103
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
104
- gr.Slider(
105
- minimum=-2.0,
106
- maximum=2.0,
107
- value=0.0,
108
- step=0.1,
109
- label="Frequency Penalty"
110
- ),
111
- gr.Slider(
112
- minimum=-1,
113
- maximum=65535, # Arbitrary upper limit for demonstration
114
- value=-1,
115
- step=1,
116
- label="Seed (-1 for random)"
117
- ),
118
- gr.Textbox(label="Custom Model", placeholder="Enter a custom model path"),
119
  ],
120
  fill_height=True,
121
  chatbot=chatbot,
122
  theme="Nymbo/Nymbo_Theme",
123
  )
124
- print("Gradio interface initialized.")
125
 
126
- # Define the Gradio interface
127
- with gr.Blocks(theme='Nymbo/Nymbo_Theme_5') as textgen:
128
- # Tab for basic settings
129
- with gr.Tab("Basic Settings"):
130
- with gr.Row():
131
- with gr.Column(elem_id="prompt-container"):
132
- with gr.Row():
133
- # Textbox for user to input the prompt
134
- text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=3, elem_id="prompt-text-input")
135
- with gr.Row():
136
- # Textbox for custom model input
137
- custom_model = gr.Textbox(label="Custom Model", info="Model Hugging Face path (optional)", placeholder="meta-llama/Llama-3.3-70B-Instruct")
138
- with gr.Row():
139
- # Accordion for selecting the model
140
- with gr.Accordion("Featured Models", open=True):
141
- # Textbox for searching models
142
- model_search = gr.Textbox(label="Filter Models", placeholder="Search for a featured model...", lines=1, elem_id="model-search-input")
143
- models_list = (
144
- "meta-llama/Llama-3.3-70B-Instruct",
145
- "meta-llama/Llama-3.3-13B-Instruct",
146
- "meta-llama/Llama-3.3-30B-Instruct",
147
- "meta-llama/Llama-3.3-7B-Instruct",
148
- )
149
-
150
- # Radio buttons to select the desired model
151
- model = gr.Radio(label="Select a model below", value="meta-llama/Llama-3.3-70B-Instruct", choices=models_list, interactive=True, elem_id="model-radio")
152
-
153
- # Filtering models based on search input
154
- def filter_models(search_term):
155
- filtered_models = [m for m in models_list if search_term.lower() in m.lower()]
156
- return gr.update(choices=filtered_models)
157
-
158
- # Update model list when search box is used
159
- model_search.change(filter_models, inputs=model_search, outputs=model)
160
-
161
- # Tab for advanced settings
162
- with gr.Tab("Advanced Settings"):
163
- with gr.Row():
164
- # Slider for setting the maximum number of new tokens
165
- max_tokens = gr.Slider(label="Max new tokens", value=512, minimum=1, maximum=4096, step=1)
166
  with gr.Row():
167
- # Slider for setting the temperature
168
- temperature = gr.Slider(label="Temperature", value=0.7, minimum=0.1, maximum=4.0, step=0.1)
169
- with gr.Row():
170
- # Slider for setting the top-p (nucleus) sampling
171
- top_p = gr.Slider(label="Top-P", value=0.95, minimum=0.1, maximum=1.0, step=0.05)
172
- with gr.Row():
173
- # Slider for setting the frequency penalty
174
- frequency_penalty = gr.Slider(label="Frequency Penalty", value=0.0, minimum=-2.0, maximum=2.0, step=0.1)
175
- with gr.Row():
176
- # Slider for setting the seed for reproducibility
177
- seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=65535, step=1)
178
-
179
- # Tab to provide information to the user
180
  with gr.Tab("Information"):
181
  with gr.Row():
182
- # Display a sample prompt for guidance
183
- gr.Textbox(label="Sample prompt", value="{prompt} | ultra detail, ultra elaboration, ultra quality, perfect.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
 
185
- # Accordion displaying featured models
186
- with gr.Accordion("Featured Models (WiP)", open=False):
187
- gr.HTML(
188
  """
189
- <p><a href="https://huggingface.co/models?inference=warm&pipeline_tag=text-generation&sort=trending">See all available models</a></p>
190
- <table style="width:100%; text-align:center; margin:auto;">
191
- <tr>
192
- <th>Model Name</th>
193
- <th>Typography</th>
194
- <th>Notes</th>
195
- </tr>
196
- <tr>
197
- <td>meta-llama/Llama-3.3-70B-Instruct</td>
198
- <td>✅</td>
199
- <td></td>
200
- </tr>
201
- <tr>
202
- <td>meta-llama/Llama-3.3-13B-Instruct</td>
203
- <td>✅</td>
204
- <td></td>
205
- </tr>
206
- <tr>
207
- <td>meta-llama/Llama-3.3-30B-Instruct</td>
208
- <td>✅</td>
209
- <td></td>
210
- </tr>
211
- <tr>
212
- <td>meta-llama/Llama-3.3-7B-Instruct</td>
213
- <td>✅</td>
214
- <td></td>
215
- </tr>
216
- </table>
217
- """
218
- )
219
-
220
- # Accordion providing an overview of advanced settings
221
- with gr.Accordion("Parameters Overview", open=False):
222
- gr.Markdown(
223
- """
224
- ## Max New Tokens
225
- ###### This slider allows you to specify the maximum number of tokens to generate in the response. The default value is 512, and the maximum output is 4096.
226
-
227
- ## Temperature
228
- ###### The temperature controls the randomness of the output. A higher temperature makes the output more random, while a lower temperature makes it more deterministic. The default value is 0.7.
229
-
230
- ## Top-P
231
- ###### Top-P (nucleus) sampling is a way to control the diversity of the output. A higher value allows for more diverse outputs, while a lower value makes the output more focused. The default value is 0.95.
232
-
233
- ## Frequency Penalty
234
- ###### The frequency penalty penalizes repeated tokens in the output. A higher value makes the output more diverse, while a lower value allows for more repetition. The default value is 0.0.
235
-
236
- ## Seed
237
- ###### The seed is a fixed value for reproducibility. If you find a seed that gives you a result you love, you can use it again to create a similar output. If you leave it at -1, the AI will generate a new seed every time.
238
-
239
- ### Remember, these settings are all about giving you control over the text generation process. Feel free to experiment and see what each one does. And if you're ever in doubt, the default settings are a great place to start. Happy creating!
240
- """
241
- )
242
-
243
- # Row containing the 'Run' button to trigger the text generation
244
- with gr.Row():
245
- text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
246
- # Row for displaying the generated text output
247
- with gr.Row():
248
- text_output = gr.Textbox(label="Text Output", elem_id="text-output")
249
-
250
- # Set up button click event to call the respond function
251
- text_button.click(respond, inputs=[text_prompt, chatbot, gr.Textbox(value="", label="System message"), max_tokens, temperature, top_p, frequency_penalty, seed, model], outputs=text_output)
252
-
253
- print("Launching Gradio interface...") # Debug log
254
- # Launch the Gradio interface without showing the API or sharing externally
255
- textgen.launch(show_api=False, share=False)
 
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
+ - model: the selected model for text generation
38
  """
39
 
40
  print(f"Received message: {message}")
41
  print(f"History: {history}")
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}, Model: {model}")
 
45
 
46
  # Convert seed to None if -1 (meaning random)
47
  if seed == -1:
 
70
 
71
  # Make the streaming request to the HF Inference API via openai-like client
72
  for message_chunk in client.chat.completions.create(
73
+ model=model, # Use the selected model
74
  max_tokens=max_tokens,
75
  stream=True, # Stream the response
76
  temperature=temperature,
77
  top_p=top_p,
78
+ frequency_penalty=frequency_penalty,
79
+ seed=seed,
80
  messages=messages,
81
  ):
82
  # Extract the token text from the response chunk
 
91
  chatbot = gr.Chatbot(height=600)
92
  print("Chatbot interface created.")
93
 
94
+ # List of featured models (placeholder models for now)
95
+ featured_models = [
96
+ "meta-llama/Llama-3.3-70B-Instruct",
97
+ "gpt-3.5-turbo",
98
+ "gpt-4",
99
+ "mistralai/Mistral-7B-Instruct-v0.1",
100
+ "tiiuae/falcon-40b-instruct"
101
+ ]
102
+
103
+ # Function to filter models based on search input
104
+ def filter_models(search_term):
105
+ filtered_models = [m for m in featured_models if search_term.lower() in m.lower()]
106
+ return gr.update(choices=filtered_models)
107
+
108
  # Create the Gradio ChatInterface
 
109
  demo = gr.ChatInterface(
110
  respond,
111
  additional_inputs=[
112
  gr.Textbox(value="", label="System message"),
113
+ gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
114
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
115
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
116
+ gr.Slider(minimum=-2.0, maximum=2.0, value=0.0, step=0.1, label="Frequency Penalty"),
117
+ gr.Slider(minimum=-1, maximum=65535, value=-1, step=1, label="Seed (-1 for random)"),
118
+ gr.Radio(label="Select a model below", value="meta-llama/Llama-3.3-70B-Instruct", choices=featured_models, interactive=True, elem_id="model-radio")
 
 
 
 
 
 
 
 
 
 
 
 
119
  ],
120
  fill_height=True,
121
  chatbot=chatbot,
122
  theme="Nymbo/Nymbo_Theme",
123
  )
 
124
 
125
+ # Add a "Custom Model" text box and "Featured Models" accordion
126
+ with demo:
127
+ with gr.Tab("Model Settings"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  with gr.Row():
129
+ with gr.Column():
130
+ # Textbox for custom model input
131
+ custom_model = gr.Textbox(label="Custom Model", info="Hugging Face model path (optional)", placeholder="username/model-name")
132
+ # Accordion for selecting featured models
133
+ with gr.Accordion("Featured Models", open=True):
134
+ # Textbox for searching models
135
+ model_search = gr.Textbox(label="Filter Models", placeholder="Search for a featured model...", lines=1, elem_id="model-search-input")
136
+ # Radio buttons to select the desired model
137
+ model_radio = gr.Radio(label="Select a model below", value="meta-llama/Llama-3.3-70B-Instruct", choices=featured_models, interactive=True, elem_id="model-radio")
138
+ # Update model list when search box is used
139
+ model_search.change(filter_models, inputs=model_search, outputs=model_radio)
140
+
141
+ # Add an "Information" tab with accordions
142
  with gr.Tab("Information"):
143
  with gr.Row():
144
+ # Accordion for "Featured Models" with a table
145
+ with gr.Accordion("Featured Models (WiP)", open=False):
146
+ gr.HTML(
147
+ """
148
+ <p><a href="https://huggingface.co/models?inference=warm&pipeline_tag=text-generation&sort=trending">See all available models</a></p>
149
+ <table style="width:100%; text-align:center; margin:auto;">
150
+ <tr>
151
+ <th>Model Name</th>
152
+ <th>Typical Use Case</th>
153
+ <th>Notes</th>
154
+ </tr>
155
+ <tr>
156
+ <td>meta-llama/Llama-3.3-70B-Instruct</td>
157
+ <td>General-purpose instruction following</td>
158
+ <td>High-quality, large-scale model</td>
159
+ </tr>
160
+ <tr>
161
+ <td>gpt-3.5-turbo</td>
162
+ <td>Chat and general text generation</td>
163
+ <td>Fast and efficient</td>
164
+ </tr>
165
+ <tr>
166
+ <td>gpt-4</td>
167
+ <td>Advanced text generation</td>
168
+ <td>State-of-the-art performance</td>
169
+ </tr>
170
+ <tr>
171
+ <td>mistralai/Mistral-7B-Instruct-v0.1</td>
172
+ <td>Instruction following</td>
173
+ <td>Lightweight and efficient</td>
174
+ </tr>
175
+ <tr>
176
+ <td>tiiuae/falcon-40b-instruct</td>
177
+ <td>Instruction following</td>
178
+ <td>High-quality, large-scale model</td>
179
+ </tr>
180
+ </table>
181
+ """
182
+ )
183
 
184
+ # Accordion for "Parameters Overview" with markdown
185
+ with gr.Accordion("Parameters Overview", open=False):
186
+ gr.Markdown(
187
  """
188
+ ## System Message
189
+ ###### This is the initial prompt that sets the behavior of the model. It can be used to define the tone, style, or role of the assistant.
190
+
191
+ ## Max Tokens
192
+ ###### This controls the maximum length of the generated response. Higher values allow for longer responses but may take more time to generate.
193
+
194
+ ## Temperature
195
+ ###### This controls the randomness of the output. Lower values make the model more deterministic, while higher values make it more creative.
196
+
197
+ ## Top-P
198
+ ###### This controls the diversity of the output by limiting the model to the most likely tokens. Lower values make the output more focused, while higher values allow for more diversity.
199
+
200
+ ## Frequency Penalty
201
+ ###### This penalizes repeated tokens in the output. Higher values discourage repetition, while lower values allow for more repetitive outputs.
202
+
203
+ ## Seed
204
+ ###### This sets a fixed seed for reproducibility. A value of -1 means the seed is random.
205
+
206
+ ## Model
207
+ ###### This selects the model used for text generation. You can choose from featured models or specify a custom model.
208
+ """
209
+ )
210
+
211
+ print("Gradio interface initialized.")
212
+
213
+ if __name__ == "__main__":
214
+ print("Launching the demo application.")
215
+ demo.launch()