Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,22 +4,19 @@ from huggingface_hub import InferenceClient
|
|
4 |
# -- 1) DEFINE YOUR MODELS HERE --
|
5 |
models = [
|
6 |
{
|
7 |
-
"name": "Tiny Model",
|
8 |
-
"description": "
|
9 |
"id": "amusktweewt/tiny-model-500M-chat-v2",
|
10 |
"enabled": True
|
11 |
},
|
12 |
{
|
13 |
-
"name": "
|
14 |
-
"description": "
|
15 |
"id": "another-model",
|
16 |
"enabled": False
|
17 |
}
|
18 |
]
|
19 |
|
20 |
-
def get_selected_model_id(evt: gr.SelectData):
|
21 |
-
"""Helper to extract the model ID from dropdown selection"""
|
22 |
-
return models[evt.index]["id"] if models[evt.index]["enabled"] else models[0]["id"]
|
23 |
|
24 |
def respond(message, history: list[tuple[str, str]], model_id, system_message, max_tokens, temperature, top_p):
|
25 |
"""
|
@@ -48,17 +45,21 @@ def respond(message, history: list[tuple[str, str]], model_id, system_message, m
|
|
48 |
response_text = ""
|
49 |
# Stream the response token-by-token.
|
50 |
for resp in client.chat_completion(
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
):
|
57 |
token = resp.choices[0].delta.content
|
58 |
response_text += token
|
59 |
yield response_text
|
60 |
|
61 |
-
|
|
|
|
|
|
|
|
|
62 |
with gr.Blocks(css="""
|
63 |
.container {
|
64 |
max-width: 900px !important;
|
@@ -68,51 +69,58 @@ with gr.Blocks(css="""
|
|
68 |
#chatbot {
|
69 |
height: 600px !important;
|
70 |
}
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
}
|
74 |
""") as demo:
|
75 |
with gr.Row():
|
76 |
with gr.Column(elem_classes="container"):
|
77 |
-
# Create
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
# Hidden textbox to store the current model ID (will be read by 'respond')
|
88 |
model_id = gr.Textbox(
|
89 |
value=models[0]["id"],
|
90 |
visible=False,
|
91 |
-
elem_id="
|
92 |
)
|
93 |
-
|
94 |
-
# Update the hidden model_id when dropdown changes
|
95 |
-
def update_model_id(evt):
|
96 |
-
selected_index = evt.index
|
97 |
-
if models[selected_index]["enabled"]:
|
98 |
-
return models[selected_index]["id"]
|
99 |
-
else:
|
100 |
-
# If disabled model selected, stay with default
|
101 |
-
return models[0]["id"]
|
102 |
-
|
103 |
-
model_dropdown.select(
|
104 |
-
update_model_id,
|
105 |
-
inputs=[],
|
106 |
-
outputs=[model_id]
|
107 |
-
)
|
108 |
-
|
109 |
# System message and parameter controls in a collapsible section
|
110 |
with gr.Accordion("Advanced Settings", open=False):
|
111 |
system_message = gr.Textbox(
|
112 |
value="You are a friendly Chatbot.",
|
113 |
label="System message"
|
114 |
)
|
115 |
-
|
116 |
with gr.Row():
|
117 |
with gr.Column(scale=1):
|
118 |
max_tokens = gr.Slider(
|
@@ -122,7 +130,7 @@ with gr.Blocks(css="""
|
|
122 |
step=1,
|
123 |
label="Max new tokens"
|
124 |
)
|
125 |
-
|
126 |
with gr.Column(scale=1):
|
127 |
temperature = gr.Slider(
|
128 |
minimum=0.1,
|
@@ -131,7 +139,7 @@ with gr.Blocks(css="""
|
|
131 |
step=0.1,
|
132 |
label="Temperature"
|
133 |
)
|
134 |
-
|
135 |
with gr.Column(scale=1):
|
136 |
top_p = gr.Slider(
|
137 |
minimum=0.1,
|
@@ -140,7 +148,7 @@ with gr.Blocks(css="""
|
|
140 |
step=0.05,
|
141 |
label="Top-p (nucleus sampling)"
|
142 |
)
|
143 |
-
|
144 |
# The ChatInterface with a larger chat area and our parameters
|
145 |
chat = gr.ChatInterface(
|
146 |
respond,
|
|
|
4 |
# -- 1) DEFINE YOUR MODELS HERE --
|
5 |
models = [
|
6 |
{
|
7 |
+
"name": "Tiny Model 500M Chat v2",
|
8 |
+
"description": "Original model with a context length of 1024 and single turn capabilities",
|
9 |
"id": "amusktweewt/tiny-model-500M-chat-v2",
|
10 |
"enabled": True
|
11 |
},
|
12 |
{
|
13 |
+
"name": "New Model",
|
14 |
+
"description": "(Disabled)",
|
15 |
"id": "another-model",
|
16 |
"enabled": False
|
17 |
}
|
18 |
]
|
19 |
|
|
|
|
|
|
|
20 |
|
21 |
def respond(message, history: list[tuple[str, str]], model_id, system_message, max_tokens, temperature, top_p):
|
22 |
"""
|
|
|
45 |
response_text = ""
|
46 |
# Stream the response token-by-token.
|
47 |
for resp in client.chat_completion(
|
48 |
+
messages,
|
49 |
+
max_tokens=max_tokens,
|
50 |
+
stream=True,
|
51 |
+
temperature=temperature,
|
52 |
+
top_p=top_p,
|
53 |
):
|
54 |
token = resp.choices[0].delta.content
|
55 |
response_text += token
|
56 |
yield response_text
|
57 |
|
58 |
+
|
59 |
+
# Since Gradio doesn't support disabled options in dropdowns natively,
|
60 |
+
# we'll use a workaround with HTML and JavaScript
|
61 |
+
|
62 |
+
# -- 3) BUILD THE UI WITH CUSTOM DROPDOWN --
|
63 |
with gr.Blocks(css="""
|
64 |
.container {
|
65 |
max-width: 900px !important;
|
|
|
69 |
#chatbot {
|
70 |
height: 600px !important;
|
71 |
}
|
72 |
+
/* CSS for disabling dropdown options */
|
73 |
+
.disabled-option {
|
74 |
+
color: #999 !important;
|
75 |
+
background-color: #f0f0f0 !important;
|
76 |
+
pointer-events: none !important;
|
77 |
+
}
|
78 |
+
/* Dark mode support */
|
79 |
+
@media (prefers-color-scheme: dark) {
|
80 |
+
.disabled-option {
|
81 |
+
color: #666 !important;
|
82 |
+
background-color: #333 !important;
|
83 |
+
}
|
84 |
}
|
85 |
""") as demo:
|
86 |
with gr.Row():
|
87 |
with gr.Column(elem_classes="container"):
|
88 |
+
# Create custom HTML dropdown with properly disabled options
|
89 |
+
dropdown_options = ""
|
90 |
+
for model in models:
|
91 |
+
value = model["id"]
|
92 |
+
label = f"{model['name']}: {model['description']}"
|
93 |
+
disabled = "" if model["enabled"] else 'disabled class="disabled-option"'
|
94 |
+
dropdown_options += f'<option value="{value}" {disabled}>{label}</option>'
|
95 |
+
|
96 |
+
dropdown_html = f"""
|
97 |
+
<div style="margin-bottom: 20px;">
|
98 |
+
<label for="model_select" style="display: block; margin-bottom: 8px; font-weight: bold;">Select Model:</label>
|
99 |
+
<select id="model_select" style="width: 100%; padding: 8px; border-radius: 8px;
|
100 |
+
border: 1px solid var(--border-color, #ccc); background-color: var(--background-fill-primary);"
|
101 |
+
onchange="document.getElementById('hidden_model_id').value = this.value;
|
102 |
+
document.getElementById('hidden_model_id').dispatchEvent(new Event('input'));">
|
103 |
+
{dropdown_options}
|
104 |
+
</select>
|
105 |
+
</div>
|
106 |
+
"""
|
107 |
+
|
108 |
+
gr.HTML(value=dropdown_html)
|
109 |
+
|
110 |
# Hidden textbox to store the current model ID (will be read by 'respond')
|
111 |
model_id = gr.Textbox(
|
112 |
value=models[0]["id"],
|
113 |
visible=False,
|
114 |
+
elem_id="hidden_model_id"
|
115 |
)
|
116 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
# System message and parameter controls in a collapsible section
|
118 |
with gr.Accordion("Advanced Settings", open=False):
|
119 |
system_message = gr.Textbox(
|
120 |
value="You are a friendly Chatbot.",
|
121 |
label="System message"
|
122 |
)
|
123 |
+
|
124 |
with gr.Row():
|
125 |
with gr.Column(scale=1):
|
126 |
max_tokens = gr.Slider(
|
|
|
130 |
step=1,
|
131 |
label="Max new tokens"
|
132 |
)
|
133 |
+
|
134 |
with gr.Column(scale=1):
|
135 |
temperature = gr.Slider(
|
136 |
minimum=0.1,
|
|
|
139 |
step=0.1,
|
140 |
label="Temperature"
|
141 |
)
|
142 |
+
|
143 |
with gr.Column(scale=1):
|
144 |
top_p = gr.Slider(
|
145 |
minimum=0.1,
|
|
|
148 |
step=0.05,
|
149 |
label="Top-p (nucleus sampling)"
|
150 |
)
|
151 |
+
|
152 |
# The ChatInterface with a larger chat area and our parameters
|
153 |
chat = gr.ChatInterface(
|
154 |
respond,
|