amusktweewt commited on
Commit
8eb0bec
·
verified ·
1 Parent(s): 9a55f2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -30
app.py CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
  # Define available models.
5
- # Each model has a name, description, model ID, and an "enabled" flag.
6
  models = [
7
  {
8
  "name": "Tiny Model",
@@ -21,15 +20,12 @@ models = [
21
  # Build the HTML for the custom dropdown.
22
  dropdown_options = ""
23
  for model in models:
24
- # If a model is disabled, add the "disabled" attribute and modify its label.
25
  disabled_attr = "disabled" if not model["enabled"] else ""
26
  label = f"{model['name']}: {model['description']}"
27
  if not model["enabled"]:
28
  label = f"{model['name']} (Disabled): {model['description']}"
29
  dropdown_options += f'<option value="{model["id"]}" {disabled_attr}>{label}</option>\n'
30
 
31
- # This HTML dropdown will be displayed. When the user selects an option,
32
- # an inline JavaScript updates the value of the hidden textbox with the chosen model ID.
33
  dropdown_html = f"""
34
  <div>
35
  <label for="model_select"><strong>Select Model:</strong></label>
@@ -39,20 +35,17 @@ dropdown_html = f"""
39
  </div>
40
  """
41
 
42
- # The respond function now accepts the model_id as one of its inputs.
43
  def respond(message, history: list[tuple[str, str]], model_id, system_message, max_tokens, temperature, top_p):
44
- # Instantiate the InferenceClient with the selected model.
45
  client = InferenceClient(model_id)
46
 
47
  messages = []
48
  if system_message:
49
  messages.append({"role": "system", "content": system_message})
50
-
51
  if history:
52
  for user_msg, bot_msg in history:
53
  messages.append({"role": "user", "content": user_msg})
54
  messages.append({"role": "assistant", "content": bot_msg})
55
-
56
  messages.append({"role": "user", "content": message})
57
  messages.append({"role": "assistant", "content": ""})
58
 
@@ -69,28 +62,25 @@ def respond(message, history: list[tuple[str, str]], model_id, system_message, m
69
  response_text += token
70
  yield response_text
71
 
72
- # Create Gradio components.
73
- # The HTML component displays our custom dropdown.
74
- html_dropdown = gr.HTML(value=dropdown_html)
75
- # The hidden textbox will hold the selected model ID.
76
- hidden_model = gr.Textbox(value=models[0]["id"], visible=False, elem_id="hidden_model")
77
-
78
- # Create the Gradio ChatInterface.
79
- # Note: Only components that supply a value are passed to the function.
80
- # The HTML component is for display only.
81
- demo = gr.ChatInterface(
82
- fn=respond,
83
- additional_inputs=[
84
- # hidden_model supplies the model_id.
85
- hidden_model,
86
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
87
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
88
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
89
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
90
- ],
91
- # You can include the HTML dropdown in a layout so that it is visible to the user.
92
- layout=[html_dropdown]
93
- )
94
 
95
  if __name__ == "__main__":
96
  demo.launch()
 
2
  from huggingface_hub import InferenceClient
3
 
4
  # Define available models.
 
5
  models = [
6
  {
7
  "name": "Tiny Model",
 
20
  # Build the HTML for the custom dropdown.
21
  dropdown_options = ""
22
  for model in models:
 
23
  disabled_attr = "disabled" if not model["enabled"] else ""
24
  label = f"{model['name']}: {model['description']}"
25
  if not model["enabled"]:
26
  label = f"{model['name']} (Disabled): {model['description']}"
27
  dropdown_options += f'<option value="{model["id"]}" {disabled_attr}>{label}</option>\n'
28
 
 
 
29
  dropdown_html = f"""
30
  <div>
31
  <label for="model_select"><strong>Select Model:</strong></label>
 
35
  </div>
36
  """
37
 
 
38
  def respond(message, history: list[tuple[str, str]], model_id, system_message, max_tokens, temperature, top_p):
39
+ # Instantiate the InferenceClient using the selected model.
40
  client = InferenceClient(model_id)
41
 
42
  messages = []
43
  if system_message:
44
  messages.append({"role": "system", "content": system_message})
 
45
  if history:
46
  for user_msg, bot_msg in history:
47
  messages.append({"role": "user", "content": user_msg})
48
  messages.append({"role": "assistant", "content": bot_msg})
 
49
  messages.append({"role": "user", "content": message})
50
  messages.append({"role": "assistant", "content": ""})
51
 
 
62
  response_text += token
63
  yield response_text
64
 
65
+ # Build the interface using Gradio Blocks.
66
+ with gr.Blocks() as demo:
67
+ # Display the custom dropdown.
68
+ gr.HTML(value=dropdown_html)
69
+ # Hidden textbox to capture the selected model ID.
70
+ hidden_model = gr.Textbox(value=models[0]["id"], visible=False, elem_id="hidden_model")
71
+
72
+ # Create the ChatInterface.
73
+ chat_interface = gr.ChatInterface(
74
+ fn=respond,
75
+ additional_inputs=[
76
+ # Pass the hidden model selector.
77
+ hidden_model,
78
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
79
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
80
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
81
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
82
+ ]
83
+ )
 
 
 
84
 
85
  if __name__ == "__main__":
86
  demo.launch()