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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -25
app.py CHANGED
@@ -1,12 +1,12 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # Define available models.
5
  models = [
6
  {
7
  "name": "Tiny Model",
8
  "description": "A small chat model.",
9
- "id": "amusktweewt/tiny-model-500M-chat-v2",
10
  "enabled": True
11
  },
12
  {
@@ -17,38 +17,62 @@ models = [
17
  }
18
  ]
19
 
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>
32
- <select id="model_select" onchange="document.getElementById('hidden_model').value = this.value;">
33
- {dropdown_options}
34
- </select>
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
-
52
  response_text = ""
53
  # Stream the response token-by-token.
54
  for resp in client.chat_completion(
@@ -62,23 +86,49 @@ def respond(message, history: list[tuple[str, str]], model_id, system_message, m
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
 
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
+ # -- 1) DEFINE YOUR MODELS HERE --
5
  models = [
6
  {
7
  "name": "Tiny Model",
8
  "description": "A small chat model.",
9
+ "id": "amusktweewt/tiny-model-500M-chat-v2", # The one you say works already
10
  "enabled": True
11
  },
12
  {
 
17
  }
18
  ]
19
 
20
+ # Build the custom HTML for a disabled-capable <select>.
21
  dropdown_options = ""
22
  for model in models:
 
23
  label = f"{model['name']}: {model['description']}"
24
+ disabled_attr = "disabled" if not model["enabled"] else ""
25
  if not model["enabled"]:
26
+ # Mark label visually so the user sees it's disabled
27
  label = f"{model['name']} (Disabled): {model['description']}"
28
  dropdown_options += f'<option value="{model["id"]}" {disabled_attr}>{label}</option>\n'
29
 
30
+ # Minimal inline styling to help match Gradio's background.
31
  dropdown_html = f"""
32
+ <style>
33
+ .custom-select {{
34
+ background-color: var(--background-color, #FFF);
35
+ color: var(--text-color, #000);
36
+ border: 1px solid var(--border-color, #CCC);
37
+ padding: 8px;
38
+ border-radius: 4px;
39
+ font-size: 1rem;
40
+ width: 100%;
41
+ box-sizing: border-box;
42
+ margin-bottom: 1rem;
43
+ }}
44
+ </style>
45
+ <label for="model_select"><strong>Select Model:</strong></label>
46
+ <select id="model_select" class="custom-select"
47
+ onchange="document.getElementById('hidden_model').value = this.value;">
48
+ {dropdown_options}
49
+ </select>
50
  """
51
 
52
  def respond(message, history: list[tuple[str, str]], model_id, system_message, max_tokens, temperature, top_p):
53
+ """
54
+ Builds a chat prompt using a simple template:
55
+ - Optionally includes a system message.
56
+ - Iterates over conversation history (each exchange as a tuple of (user, assistant)).
57
+ - Adds the new user message and appends an empty assistant turn.
58
+ Then it streams the response from the model.
59
+ """
60
+ # -- 2) Instantiate the InferenceClient using the chosen model --
61
  client = InferenceClient(model_id)
62
+
63
+ # Build the messages list.
64
  messages = []
65
  if system_message:
66
  messages.append({"role": "system", "content": system_message})
67
+
68
  if history:
69
  for user_msg, bot_msg in history:
70
  messages.append({"role": "user", "content": user_msg})
71
  messages.append({"role": "assistant", "content": bot_msg})
72
+
73
  messages.append({"role": "user", "content": message})
74
  messages.append({"role": "assistant", "content": ""})
75
+
76
  response_text = ""
77
  # Stream the response token-by-token.
78
  for resp in client.chat_completion(
 
86
  response_text += token
87
  yield response_text
88
 
89
+ # -- 3) BUILD THE UI IN A BLOCKS CONTEXT (so we can add custom HTML above the chat) --
90
  with gr.Blocks() as demo:
91
+ # Our custom HTML dropdown (shows model + description, supports disabled)
92
  gr.HTML(value=dropdown_html)
93
+
94
+ # Hidden textbox to store the current model ID (will be read by 'respond').
95
+ hidden_model = gr.Textbox(
96
+ value=models[0]["id"], # Default to the first model
97
+ visible=False,
98
+ elem_id="hidden_model"
99
+ )
100
 
101
+ # The ChatInterface is almost the same as your original code.
102
+ # We simply add `hidden_model` as one more input argument.
103
+ chat = gr.ChatInterface(
104
+ respond,
105
  additional_inputs=[
 
106
  hidden_model,
107
+ gr.Textbox(
108
+ value="You are a friendly Chatbot.",
109
+ label="System message"
110
+ ),
111
+ gr.Slider(
112
+ minimum=1,
113
+ maximum=2048,
114
+ value=512,
115
+ step=1,
116
+ label="Max new tokens"
117
+ ),
118
+ gr.Slider(
119
+ minimum=0.1,
120
+ maximum=4.0,
121
+ value=0.7,
122
+ step=0.1,
123
+ label="Temperature"
124
+ ),
125
+ gr.Slider(
126
+ minimum=0.1,
127
+ maximum=1.0,
128
+ value=0.95,
129
+ step=0.05,
130
+ label="Top-p (nucleus sampling)"
131
+ ),
132
  ]
133
  )
134