Spaces:
Running
Running
Update app.py
Browse files
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
|
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,
|
75 |
max_tokens=max_tokens,
|
76 |
stream=True, # Stream the response
|
77 |
temperature=temperature,
|
78 |
top_p=top_p,
|
79 |
-
frequency_penalty=frequency_penalty,
|
80 |
-
seed=seed,
|
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,
|
102 |
-
gr.Slider(minimum=0.1, maximum=4.0,
|
103 |
-
gr.Slider(minimum=0.1, maximum=1.0,
|
104 |
-
gr.Slider(
|
105 |
-
|
106 |
-
|
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 |
-
#
|
127 |
-
with
|
128 |
-
|
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 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
#
|
180 |
with gr.Tab("Information"):
|
181 |
with gr.Row():
|
182 |
-
#
|
183 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
"""
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|