Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,6 +13,7 @@ client = OpenAI(
|
|
13 |
)
|
14 |
print("OpenAI client initialized.")
|
15 |
|
|
|
16 |
def respond(
|
17 |
message,
|
18 |
history: list[tuple[str, str]],
|
@@ -25,18 +26,8 @@ def respond(
|
|
25 |
custom_model
|
26 |
):
|
27 |
"""
|
28 |
-
|
29 |
-
- message: the user's new message
|
30 |
-
- history: the list of previous messages, each as a tuple (user_msg, assistant_msg)
|
31 |
-
- system_message: the system prompt
|
32 |
-
- max_tokens: the maximum number of tokens to generate in the response
|
33 |
-
- temperature: sampling temperature
|
34 |
-
- top_p: top-p (nucleus) sampling
|
35 |
-
- frequency_penalty: penalize repeated tokens in the output
|
36 |
-
- seed: a fixed seed for reproducibility; -1 will mean 'random'
|
37 |
-
- custom_model: the final model name in use, which may be set by selecting from the Featured Models radio or by typing a custom model
|
38 |
"""
|
39 |
-
|
40 |
print(f"Received message: {message}")
|
41 |
print(f"History: {history}")
|
42 |
print(f"System message: {system_message}")
|
@@ -44,159 +35,107 @@ def respond(
|
|
44 |
print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
|
45 |
print(f"Selected model (custom_model): {custom_model}")
|
46 |
|
47 |
-
# Convert seed to None if -1 (meaning random)
|
48 |
if seed == -1:
|
49 |
seed = None
|
50 |
|
51 |
-
# Construct the messages array
|
52 |
messages = [{"role": "system", "content": system_message}]
|
53 |
-
print("Initial messages array constructed.")
|
54 |
-
|
55 |
-
# Add conversation history to the context
|
56 |
for val in history:
|
57 |
-
user_part = val[0]
|
58 |
-
assistant_part = val[1]
|
59 |
if user_part:
|
60 |
-
messages.append({"role": "user", "content": user_part})
|
61 |
-
print(f"Added user message to context: {user_part}")
|
62 |
if assistant_part:
|
63 |
-
messages.append({"role": "assistant", "content": assistant_part})
|
64 |
-
print(f"Added assistant message to context: {assistant_part}")
|
65 |
|
66 |
-
# Append the latest user message
|
67 |
messages.append({"role": "user", "content": message})
|
68 |
-
print("Latest user message appended.")
|
69 |
|
70 |
-
# If user provided a model, use
|
71 |
model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.3-70B-Instruct"
|
72 |
-
print(f"Model selected for inference: {model_to_use}")
|
73 |
-
|
74 |
-
# Start with an empty string to build the response as tokens stream in
|
75 |
response = ""
|
76 |
-
print("Sending request to OpenAI API.")
|
77 |
|
78 |
-
# Make the streaming request to the HF Inference API via openai-like client
|
79 |
for message_chunk in client.chat.completions.create(
|
80 |
-
model=model_to_use,
|
81 |
-
max_tokens=max_tokens,
|
82 |
-
stream=True,
|
83 |
-
temperature=temperature,
|
84 |
-
top_p=top_p,
|
85 |
-
frequency_penalty=frequency_penalty,
|
86 |
-
seed=seed,
|
87 |
-
messages=messages,
|
88 |
):
|
89 |
-
# Extract the token text from the response chunk
|
90 |
token_text = message_chunk.choices[0].delta.content
|
91 |
-
print(f"Received token: {token_text}")
|
92 |
response += token_text
|
93 |
-
# Yield the partial response to Gradio so it can display in real-time
|
94 |
yield response
|
95 |
|
96 |
-
print("Completed response generation.")
|
97 |
|
98 |
# -------------------------
|
99 |
# GRADIO UI CONFIGURATION
|
100 |
# -------------------------
|
101 |
|
102 |
-
# Create a Chatbot component
|
103 |
-
chatbot = gr.Chatbot(
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
max_tokens_slider = gr.Slider(
|
110 |
-
minimum=1, # Minimum allowable tokens
|
111 |
-
maximum=4096, # Maximum allowable tokens
|
112 |
-
value=512, # Default value
|
113 |
-
step=1, # Increment step size
|
114 |
-
label="Max new tokens" # Slider label
|
115 |
-
)
|
116 |
-
temperature_slider = gr.Slider(
|
117 |
-
minimum=0.1, # Minimum temperature
|
118 |
-
maximum=4.0, # Maximum temperature
|
119 |
-
value=0.7, # Default value
|
120 |
-
step=0.1, # Increment step size
|
121 |
-
label="Temperature" # Slider label
|
122 |
-
)
|
123 |
-
top_p_slider = gr.Slider(
|
124 |
-
minimum=0.1, # Minimum top-p value
|
125 |
-
maximum=1.0, # Maximum top-p value
|
126 |
-
value=0.95, # Default value
|
127 |
-
step=0.05, # Increment step size
|
128 |
-
label="Top-P" # Slider label
|
129 |
-
)
|
130 |
-
frequency_penalty_slider = gr.Slider(
|
131 |
-
minimum=-2.0, # Minimum penalty
|
132 |
-
maximum=2.0, # Maximum penalty
|
133 |
-
value=0.0, # Default value
|
134 |
-
step=0.1, # Increment step size
|
135 |
-
label="Frequency Penalty" # Slider label
|
136 |
-
)
|
137 |
-
seed_slider = gr.Slider(
|
138 |
-
minimum=-1, # -1 for random seed
|
139 |
-
maximum=65535, # Maximum seed value
|
140 |
-
value=-1, # Default value
|
141 |
-
step=1, # Increment step size
|
142 |
-
label="Seed (-1 for random)" # Slider label
|
143 |
)
|
144 |
|
145 |
-
#
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
)
|
|
|
|
|
|
|
151 |
|
152 |
-
# Define a function that updates the custom model box when a featured model is selected
|
153 |
def set_custom_model_from_radio(selected):
|
154 |
"""
|
155 |
-
|
156 |
-
We will update the Custom Model text box with that selection automatically.
|
157 |
"""
|
158 |
-
print(f"Featured model selected: {selected}")
|
159 |
return selected
|
160 |
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
162 |
demo = gr.ChatInterface(
|
163 |
-
fn=respond,
|
164 |
additional_inputs=[
|
165 |
-
system_message_box,
|
166 |
-
max_tokens_slider,
|
167 |
-
temperature_slider,
|
168 |
-
top_p_slider,
|
169 |
-
frequency_penalty_slider,
|
170 |
-
seed_slider,
|
171 |
-
custom_model_box
|
172 |
],
|
173 |
-
fill_height=True,
|
174 |
-
chatbot=chatbot,
|
175 |
-
textbox=
|
176 |
multimodal=True,
|
177 |
concurrency_limit=20,
|
178 |
-
theme="Nymbo/Nymbo_Theme",
|
179 |
-
examples
|
180 |
-
{"text": "What's your model name and who trained you?",},
|
181 |
-
{"text": "How many R's are there in the word Strawberry?"},],
|
182 |
cache_examples=False
|
183 |
)
|
184 |
-
|
185 |
print("ChatInterface object created.")
|
186 |
|
187 |
-
# -----------
|
188 |
-
# ADDING THE "FEATURED MODELS" ACCORDION
|
189 |
-
# -----------
|
190 |
with demo:
|
191 |
-
|
|
|
192 |
model_search_box = gr.Textbox(
|
193 |
-
label="Filter Models",
|
194 |
-
placeholder="Search for a featured model...",
|
195 |
-
lines=1
|
196 |
)
|
197 |
-
print("Model search box created.")
|
198 |
|
199 |
-
# Sample list of popular text models
|
200 |
models_list = [
|
201 |
"meta-llama/Llama-3.3-70B-Instruct",
|
202 |
"meta-llama/Llama-3.2-3B-Instruct",
|
@@ -216,38 +155,51 @@ with demo:
|
|
216 |
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
217 |
"microsoft/Phi-3.5-mini-instruct",
|
218 |
]
|
219 |
-
print("Models list initialized.")
|
220 |
|
221 |
featured_model_radio = gr.Radio(
|
222 |
-
label="Select a model below",
|
223 |
-
choices=models_list,
|
224 |
-
value="meta-llama/Llama-3.3-70B-Instruct",
|
225 |
-
interactive=True
|
226 |
)
|
227 |
-
print("Featured models radio button created.")
|
228 |
|
229 |
-
# Filter function for the radio button list
|
230 |
def filter_models(search_term):
|
231 |
-
|
232 |
-
filtered = [m for m in models_list if search_term.lower() in m.lower()] # Filter models by search term
|
233 |
-
print(f"Filtered models: {filtered}") # Log filtered models
|
234 |
return gr.update(choices=filtered)
|
235 |
|
236 |
-
# Update the radio list when the search box value changes
|
237 |
model_search_box.change(
|
238 |
-
fn=filter_models,
|
239 |
-
inputs=model_search_box,
|
240 |
-
outputs=featured_model_radio
|
241 |
)
|
242 |
-
print("Model search box change event linked.")
|
243 |
|
244 |
-
# Update the custom model textbox when a featured model is selected
|
245 |
featured_model_radio.change(
|
246 |
-
fn=set_custom_model_from_radio,
|
247 |
-
inputs=featured_model_radio,
|
248 |
-
outputs=custom_model_box
|
249 |
)
|
250 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
251 |
|
252 |
print("Gradio interface initialized.")
|
253 |
|
|
|
13 |
)
|
14 |
print("OpenAI client initialized.")
|
15 |
|
16 |
+
|
17 |
def respond(
|
18 |
message,
|
19 |
history: list[tuple[str, str]],
|
|
|
26 |
custom_model
|
27 |
):
|
28 |
"""
|
29 |
+
Respond function for ChatInterface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
"""
|
|
|
31 |
print(f"Received message: {message}")
|
32 |
print(f"History: {history}")
|
33 |
print(f"System message: {system_message}")
|
|
|
35 |
print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
|
36 |
print(f"Selected model (custom_model): {custom_model}")
|
37 |
|
|
|
38 |
if seed == -1:
|
39 |
seed = None
|
40 |
|
41 |
+
# Construct the messages array
|
42 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
43 |
for val in history:
|
44 |
+
user_part = val[0]
|
45 |
+
assistant_part = val[1]
|
46 |
if user_part:
|
47 |
+
messages.append({"role": "user", "content": user_part})
|
|
|
48 |
if assistant_part:
|
49 |
+
messages.append({"role": "assistant", "content": assistant_part})
|
|
|
50 |
|
|
|
51 |
messages.append({"role": "user", "content": message})
|
|
|
52 |
|
53 |
+
# If user provided a model, use it; else use default
|
54 |
model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.3-70B-Instruct"
|
|
|
|
|
|
|
55 |
response = ""
|
|
|
56 |
|
|
|
57 |
for message_chunk in client.chat.completions.create(
|
58 |
+
model=model_to_use,
|
59 |
+
max_tokens=max_tokens,
|
60 |
+
stream=True,
|
61 |
+
temperature=temperature,
|
62 |
+
top_p=top_p,
|
63 |
+
frequency_penalty=frequency_penalty,
|
64 |
+
seed=seed,
|
65 |
+
messages=messages,
|
66 |
):
|
|
|
67 |
token_text = message_chunk.choices[0].delta.content
|
|
|
68 |
response += token_text
|
|
|
69 |
yield response
|
70 |
|
|
|
71 |
|
72 |
# -------------------------
|
73 |
# GRADIO UI CONFIGURATION
|
74 |
# -------------------------
|
75 |
|
76 |
+
# Create a Chatbot component
|
77 |
+
chatbot = gr.Chatbot(
|
78 |
+
height=600,
|
79 |
+
show_copy_button=True,
|
80 |
+
placeholder="Select a model and begin chatting",
|
81 |
+
likeable=True,
|
82 |
+
layout="panel"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
)
|
84 |
|
85 |
+
# Create textboxes/sliders for system prompt, tokens, etc.
|
86 |
+
system_message_box = gr.Textbox(value="", label="System message")
|
87 |
+
max_tokens_slider = gr.Slider(1, 4096, value=512, step=1, label="Max new tokens")
|
88 |
+
temperature_slider = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature")
|
89 |
+
top_p_slider = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-P")
|
90 |
+
frequency_penalty_slider = gr.Slider(-2.0, 2.0, value=0.0, step=0.1, label="Frequency Penalty")
|
91 |
+
seed_slider = gr.Slider(-1, 65535, value=-1, step=1, label="Seed (-1 for random)")
|
92 |
+
custom_model_box = gr.Textbox(value="", label="Custom Model",
|
93 |
+
info="(Optional) Provide a custom Hugging Face model path. Overrides any selected featured model.")
|
94 |
|
|
|
95 |
def set_custom_model_from_radio(selected):
|
96 |
"""
|
97 |
+
Update the Custom Model textbox when a featured model is selected.
|
|
|
98 |
"""
|
99 |
+
print(f"Featured model selected: {selected}")
|
100 |
return selected
|
101 |
|
102 |
+
|
103 |
+
# Create a user textbox that we can reference
|
104 |
+
# This will become our "Message" input inside the ChatInterface
|
105 |
+
user_textbox = gr.MultimodalTextbox()
|
106 |
+
|
107 |
+
# No 'examples' here—because we want to keep the user's parameters unchanged
|
108 |
demo = gr.ChatInterface(
|
109 |
+
fn=respond,
|
110 |
additional_inputs=[
|
111 |
+
system_message_box,
|
112 |
+
max_tokens_slider,
|
113 |
+
temperature_slider,
|
114 |
+
top_p_slider,
|
115 |
+
frequency_penalty_slider,
|
116 |
+
seed_slider,
|
117 |
+
custom_model_box
|
118 |
],
|
119 |
+
fill_height=True,
|
120 |
+
chatbot=chatbot,
|
121 |
+
textbox=user_textbox,
|
122 |
multimodal=True,
|
123 |
concurrency_limit=20,
|
124 |
+
theme="Nymbo/Nymbo_Theme",
|
125 |
+
# No examples parameter used
|
|
|
|
|
126 |
cache_examples=False
|
127 |
)
|
|
|
128 |
print("ChatInterface object created.")
|
129 |
|
|
|
|
|
|
|
130 |
with demo:
|
131 |
+
# Featured models accordion
|
132 |
+
with gr.Accordion("Featured Models", open=False):
|
133 |
model_search_box = gr.Textbox(
|
134 |
+
label="Filter Models",
|
135 |
+
placeholder="Search for a featured model...",
|
136 |
+
lines=1
|
137 |
)
|
|
|
138 |
|
|
|
139 |
models_list = [
|
140 |
"meta-llama/Llama-3.3-70B-Instruct",
|
141 |
"meta-llama/Llama-3.2-3B-Instruct",
|
|
|
155 |
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
156 |
"microsoft/Phi-3.5-mini-instruct",
|
157 |
]
|
|
|
158 |
|
159 |
featured_model_radio = gr.Radio(
|
160 |
+
label="Select a model below",
|
161 |
+
choices=models_list,
|
162 |
+
value="meta-llama/Llama-3.3-70B-Instruct",
|
163 |
+
interactive=True
|
164 |
)
|
|
|
165 |
|
|
|
166 |
def filter_models(search_term):
|
167 |
+
filtered = [m for m in models_list if search_term.lower() in m.lower()]
|
|
|
|
|
168 |
return gr.update(choices=filtered)
|
169 |
|
|
|
170 |
model_search_box.change(
|
171 |
+
fn=filter_models,
|
172 |
+
inputs=model_search_box,
|
173 |
+
outputs=featured_model_radio
|
174 |
)
|
|
|
175 |
|
|
|
176 |
featured_model_radio.change(
|
177 |
+
fn=set_custom_model_from_radio,
|
178 |
+
inputs=featured_model_radio,
|
179 |
+
outputs=custom_model_box
|
180 |
)
|
181 |
+
|
182 |
+
# Example Prompts accordion
|
183 |
+
with gr.Accordion("Example Prompts", open=False):
|
184 |
+
ex1_btn = gr.Button("Example 1: 'Howdy, partner!'")
|
185 |
+
ex2_btn = gr.Button("Example 2: 'What's your model name and who trained you?'")
|
186 |
+
ex3_btn = gr.Button("Example 3: 'How many R's in Strawberry?'")
|
187 |
+
|
188 |
+
# Helper function that returns an update for user_textbox
|
189 |
+
def load_example(example_text):
|
190 |
+
return gr.update(value=example_text)
|
191 |
+
|
192 |
+
ex1_btn.click(fn=lambda: load_example("Howdy, partner!"),
|
193 |
+
inputs=[],
|
194 |
+
outputs=user_textbox)
|
195 |
+
|
196 |
+
ex2_btn.click(fn=lambda: load_example("What's your model name and who trained you?"),
|
197 |
+
inputs=[],
|
198 |
+
outputs=user_textbox)
|
199 |
+
|
200 |
+
ex3_btn.click(fn=lambda: load_example("How many R's are there in the word Strawberry?"),
|
201 |
+
inputs=[],
|
202 |
+
outputs=user_textbox)
|
203 |
|
204 |
print("Gradio interface initialized.")
|
205 |
|