amusktweewt commited on
Commit
2471f2f
·
verified ·
1 Parent(s): b1bd07a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -27
app.py CHANGED
@@ -6,7 +6,7 @@ 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
  {
@@ -27,21 +27,30 @@ for model in models:
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;">
@@ -51,30 +60,19 @@ dropdown_html = f"""
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(
79
  messages,
80
  max_tokens=max_tokens,
@@ -86,20 +84,19 @@ def respond(message, history: list[tuple[str, str]], model_id, system_message, m
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=[
@@ -129,7 +126,8 @@ with gr.Blocks() as demo:
129
  step=0.05,
130
  label="Top-p (nucleus sampling)"
131
  ),
132
- ]
 
133
  )
134
 
135
  if __name__ == "__main__":
 
6
  {
7
  "name": "Tiny Model",
8
  "description": "A small chat model.",
9
+ "id": "amusktweewt/tiny-model-500M-chat-v2",
10
  "enabled": True
11
  },
12
  {
 
27
  label = f"{model['name']} (Disabled): {model['description']}"
28
  dropdown_options += f'<option value="{model["id"]}" {disabled_attr}>{label}</option>\n'
29
 
30
+ # Updated CSS to follow system theme and enlarge chat area.
31
+ custom_css = """
32
  <style>
33
+ /* Style for the custom dropdown, using inherited colors */
34
+ .custom-select {
35
+ background-color: transparent; /* Inherit system background */
36
+ color: inherit; /* Inherit system text color */
37
+ border: 1px solid var(--border-color, #ccc);
38
  padding: 8px;
39
  border-radius: 4px;
40
  font-size: 1rem;
41
  width: 100%;
42
  box-sizing: border-box;
43
  margin-bottom: 1rem;
44
+ }
45
+ /* Increase the minimum height of the chat area */
46
+ #chat_interface .chatbox {
47
+ min-height: 500px;
48
+ }
49
  </style>
50
+ """
51
+
52
+ dropdown_html = f"""
53
+ {custom_css}
54
  <label for="model_select"><strong>Select Model:</strong></label>
55
  <select id="model_select" class="custom-select"
56
  onchange="document.getElementById('hidden_model').value = this.value;">
 
60
 
61
  def respond(message, history: list[tuple[str, str]], model_id, system_message, max_tokens, temperature, top_p):
62
  """
63
+ Build a chat prompt and stream the response token-by-token from the model.
 
 
 
 
64
  """
 
65
  client = InferenceClient(model_id)
 
 
66
  messages = []
67
  if system_message:
68
  messages.append({"role": "system", "content": system_message})
 
69
  if history:
70
  for user_msg, bot_msg in history:
71
  messages.append({"role": "user", "content": user_msg})
72
  messages.append({"role": "assistant", "content": bot_msg})
 
73
  messages.append({"role": "user", "content": message})
74
  messages.append({"role": "assistant", "content": ""})
 
75
  response_text = ""
 
76
  for resp in client.chat_completion(
77
  messages,
78
  max_tokens=max_tokens,
 
84
  response_text += token
85
  yield response_text
86
 
87
+ # -- 3) BUILD THE UI IN A BLOCKS CONTEXT --
88
  with gr.Blocks() as demo:
89
+ # Custom HTML dropdown for model selection.
90
  gr.HTML(value=dropdown_html)
91
+
92
+ # Hidden textbox to store the current model ID.
93
  hidden_model = gr.Textbox(
94
  value=models[0]["id"], # Default to the first model
95
  visible=False,
96
  elem_id="hidden_model"
97
  )
98
 
99
+ # ChatInterface with an element ID for styling.
 
100
  chat = gr.ChatInterface(
101
  respond,
102
  additional_inputs=[
 
126
  step=0.05,
127
  label="Top-p (nucleus sampling)"
128
  ),
129
+ ],
130
+ elem_id="chat_interface"
131
  )
132
 
133
  if __name__ == "__main__":