amusktweewt commited on
Commit
9a55f2c
·
verified ·
1 Parent(s): 3079ffd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -18
app.py CHANGED
@@ -1,31 +1,58 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # Initialize the InferenceClient with your chat model.
5
- client = InferenceClient("amusktweewt/tiny-model-500M-chat-v2")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p):
8
- """
9
- Builds a chat prompt using a simple template:
10
- - Optionally includes a system message.
11
- - Iterates over conversation history (each exchange as a tuple of (user, assistant)).
12
- - Adds the new user message and appends an empty assistant turn.
13
- Then it streams the response from the model.
14
- """
15
- messages = []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- # Include the system prompt if provided.
18
  if system_message:
19
  messages.append({"role": "system", "content": system_message})
20
 
21
- # Append conversation history.
22
  if history:
23
  for user_msg, bot_msg in history:
24
  messages.append({"role": "user", "content": user_msg})
25
  messages.append({"role": "assistant", "content": bot_msg})
26
 
27
- # Add the new user message and an empty assistant response
28
- # (this mimics your template where the assistant turn is empty to be filled).
29
  messages.append({"role": "user", "content": message})
30
  messages.append({"role": "assistant", "content": ""})
31
 
@@ -42,15 +69,27 @@ def respond(message, history: list[tuple[str, str]], system_message, max_tokens,
42
  response_text += token
43
  yield response_text
44
 
45
- # Create a Gradio ChatInterface.
 
 
 
 
 
 
 
 
46
  demo = gr.ChatInterface(
47
- respond,
48
  additional_inputs=[
 
 
49
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
53
  ],
 
 
54
  )
55
 
56
  if __name__ == "__main__":
 
1
  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",
9
+ "description": "A small chat model.",
10
+ "id": "amusktweewt/tiny-model-500M-chat-v2",
11
+ "enabled": True
12
+ },
13
+ {
14
+ "name": "Another Model",
15
+ "description": "A bigger chat model (disabled).",
16
+ "id": "another-model",
17
+ "enabled": False
18
+ }
19
+ ]
20
 
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>
36
+ <select id="model_select" onchange="document.getElementById('hidden_model').value = this.value;">
37
+ {dropdown_options}
38
+ </select>
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
  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__":