Nymbo commited on
Commit
9e12544
·
verified ·
1 Parent(s): 5292786

alright its too expensive yall, I gotta pull back a bit

Browse files
Files changed (1) hide show
  1. app.py +47 -10
app.py CHANGED
@@ -15,8 +15,9 @@ def respond(
15
  top_p,
16
  frequency_penalty,
17
  seed,
18
- provider, # Moved before custom_model
19
- custom_model, # Moved after provider
 
20
  model_search_term,
21
  selected_model
22
  ):
@@ -25,14 +26,17 @@ def respond(
25
  print(f"System message: {system_message}")
26
  print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
27
  print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
28
- print(f"Selected provider: {provider}") # Updated order
29
- print(f"Selected model (custom_model): {custom_model}") # Updated order
 
30
  print(f"Model search term: {model_search_term}")
31
  print(f"Selected model from radio: {selected_model}")
32
 
33
- # Initialize the Inference Client with the provider
34
- # Provider is specified during initialization, not in the method call
35
- client = InferenceClient(token=ACCESS_TOKEN, provider=provider)
 
 
36
  print(f"Hugging Face Inference Client initialized with {provider} provider.")
37
 
38
  # Convert seed to None if -1 (meaning random)
@@ -105,6 +109,13 @@ def respond(
105
 
106
  print("Completed response generation.")
107
 
 
 
 
 
 
 
 
108
  # GRADIO UI
109
 
110
  chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="Select a model and begin chatting", layout="panel")
@@ -169,6 +180,15 @@ provider_radio = gr.Radio(
169
  info="[View all models here](https://huggingface.co/models?inference_provider=all&pipeline_tag=text-generation&sort=trending)"
170
  )
171
 
 
 
 
 
 
 
 
 
 
172
  # Custom model box
173
  custom_model_box = gr.Textbox(
174
  value="",
@@ -246,8 +266,9 @@ demo = gr.ChatInterface(
246
  top_p_slider,
247
  frequency_penalty_slider,
248
  seed_slider,
249
- provider_radio, # Provider selection (moved up)
250
- custom_model_box, # Custom Model (moved down)
 
251
  model_search_box, # Model search box
252
  featured_model_radio # Featured model radio
253
  ],
@@ -273,9 +294,25 @@ with demo:
273
  outputs=custom_model_box
274
  )
275
  print("Featured model radio button change event linked.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
 
277
  print("Gradio interface initialized.")
278
 
279
  if __name__ == "__main__":
280
  print("Launching the demo application.")
281
- demo.launch(show_api=True) # Fixed typo: demo. Launch -> demo.launch
 
15
  top_p,
16
  frequency_penalty,
17
  seed,
18
+ provider,
19
+ custom_api_key, # New parameter for BYOK
20
+ custom_model,
21
  model_search_term,
22
  selected_model
23
  ):
 
26
  print(f"System message: {system_message}")
27
  print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
28
  print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
29
+ print(f"Selected provider: {provider}")
30
+ print(f"Custom API Key provided: {bool(custom_api_key.strip())}") # Log whether a custom key was provided without printing the key
31
+ print(f"Selected model (custom_model): {custom_model}")
32
  print(f"Model search term: {model_search_term}")
33
  print(f"Selected model from radio: {selected_model}")
34
 
35
+ # Determine which token to use - custom API key if provided, otherwise the ACCESS_TOKEN
36
+ token_to_use = custom_api_key if custom_api_key.strip() != "" else ACCESS_TOKEN
37
+
38
+ # Initialize the Inference Client with the provider and appropriate token
39
+ client = InferenceClient(token=token_to_use, provider=provider)
40
  print(f"Hugging Face Inference Client initialized with {provider} provider.")
41
 
42
  # Convert seed to None if -1 (meaning random)
 
109
 
110
  print("Completed response generation.")
111
 
112
+ # Function to validate provider selection based on BYOK
113
+ def validate_provider(api_key, provider):
114
+ # If no custom API key is provided, only "hf-inference" can be used
115
+ if not api_key.strip() and provider != "hf-inference":
116
+ return gr.update(value="hf-inference")
117
+ return gr.update(value=provider)
118
+
119
  # GRADIO UI
120
 
121
  chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="Select a model and begin chatting", layout="panel")
 
180
  info="[View all models here](https://huggingface.co/models?inference_provider=all&pipeline_tag=text-generation&sort=trending)"
181
  )
182
 
183
+ # New BYOK textbox - Added for the new feature
184
+ byok_textbox = gr.Textbox(
185
+ value="",
186
+ label="BYOK (Bring Your Own Key)",
187
+ info="Enter a custom Hugging Face API key here. When empty, only 'hf-inference' provider can be used.",
188
+ placeholder="Enter your Hugging Face API token",
189
+ type="password" # Hide the API key for security
190
+ )
191
+
192
  # Custom model box
193
  custom_model_box = gr.Textbox(
194
  value="",
 
266
  top_p_slider,
267
  frequency_penalty_slider,
268
  seed_slider,
269
+ provider_radio, # Provider selection
270
+ byok_textbox, # New BYOK textbox
271
+ custom_model_box, # Custom Model
272
  model_search_box, # Model search box
273
  featured_model_radio # Featured model radio
274
  ],
 
294
  outputs=custom_model_box
295
  )
296
  print("Featured model radio button change event linked.")
297
+
298
+ # Connect the BYOK textbox to validate provider selection
299
+ byok_textbox.change(
300
+ fn=validate_provider,
301
+ inputs=[byok_textbox, provider_radio],
302
+ outputs=provider_radio
303
+ )
304
+ print("BYOK textbox change event linked.")
305
+
306
+ # Also validate provider when the radio changes to ensure consistency
307
+ provider_radio.change(
308
+ fn=validate_provider,
309
+ inputs=[byok_textbox, provider_radio],
310
+ outputs=provider_radio
311
+ )
312
+ print("Provider radio button change event linked.")
313
 
314
  print("Gradio interface initialized.")
315
 
316
  if __name__ == "__main__":
317
  print("Launching the demo application.")
318
+ demo.launch(show_api=True)