Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,14 +2,31 @@ import gradio as gr
|
|
2 |
from openai import OpenAI
|
3 |
import os
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
6 |
-
|
7 |
|
8 |
client = OpenAI(
|
9 |
base_url="https://api-inference.huggingface.co/v1/",
|
10 |
api_key=ACCESS_TOKEN,
|
11 |
)
|
12 |
-
|
13 |
|
14 |
|
15 |
def respond(
|
@@ -24,19 +41,19 @@ def respond(
|
|
24 |
custom_model
|
25 |
):
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
|
34 |
# Convert seed to None if -1 (meaning random)
|
35 |
if seed == -1:
|
36 |
seed = None
|
37 |
|
38 |
messages = [{"role": "system", "content": system_message}]
|
39 |
-
|
40 |
|
41 |
# Add conversation history to the context
|
42 |
for val in history:
|
@@ -44,22 +61,22 @@ def respond(
|
|
44 |
assistant_part = val[1]
|
45 |
if user_part:
|
46 |
messages.append({"role": "user", "content": user_part})
|
47 |
-
|
48 |
if assistant_part:
|
49 |
messages.append({"role": "assistant", "content": assistant_part})
|
50 |
-
|
51 |
|
52 |
# Append the latest user message
|
53 |
messages.append({"role": "user", "content": message})
|
54 |
-
|
55 |
|
56 |
# If user provided a model, use that; otherwise, fall back to a default model
|
57 |
model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.3-70B-Instruct"
|
58 |
-
|
59 |
|
60 |
# Start with an empty string to build the response as tokens stream in
|
61 |
response = ""
|
62 |
-
|
63 |
|
64 |
for message_chunk in client.chat.completions.create(
|
65 |
model=model_to_use,
|
@@ -72,16 +89,16 @@ def respond(
|
|
72 |
messages=messages,
|
73 |
):
|
74 |
token_text = message_chunk.choices[0].delta.content
|
75 |
-
|
76 |
response += token_text
|
77 |
yield response
|
78 |
|
79 |
-
|
80 |
|
81 |
# GRADIO UI
|
82 |
|
83 |
-
chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="Select a model and begin chatting", layout="panel")
|
84 |
-
|
85 |
|
86 |
system_message_box = gr.Textbox(value="", placeholder="You are a helpful assistant.", label="System Prompt")
|
87 |
|
@@ -130,11 +147,7 @@ custom_model_box = gr.Textbox(
|
|
130 |
)
|
131 |
|
132 |
def set_custom_model_from_radio(selected):
|
133 |
-
""
|
134 |
-
This function will get triggered whenever someone picks a model from the 'Featured Models' radio.
|
135 |
-
We will update the Custom Model text box with that selection automatically.
|
136 |
-
"""
|
137 |
-
print(f"Featured model selected: {selected}")
|
138 |
return selected
|
139 |
|
140 |
demo = gr.ChatInterface(
|
@@ -152,7 +165,7 @@ demo = gr.ChatInterface(
|
|
152 |
chatbot=chatbot,
|
153 |
theme="Nymbo/Nymbo_Theme",
|
154 |
)
|
155 |
-
|
156 |
|
157 |
with demo:
|
158 |
with gr.Accordion("Model Selection", open=False):
|
@@ -161,7 +174,7 @@ with demo:
|
|
161 |
placeholder="Search for a featured model...",
|
162 |
lines=1
|
163 |
)
|
164 |
-
|
165 |
|
166 |
models_list = [
|
167 |
"meta-llama/Llama-3.3-70B-Instruct",
|
@@ -182,7 +195,7 @@ with demo:
|
|
182 |
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
183 |
"microsoft/Phi-3.5-mini-instruct",
|
184 |
]
|
185 |
-
|
186 |
|
187 |
featured_model_radio = gr.Radio(
|
188 |
label="Select a model below",
|
@@ -190,12 +203,12 @@ with demo:
|
|
190 |
value="meta-llama/Llama-3.3-70B-Instruct",
|
191 |
interactive=True
|
192 |
)
|
193 |
-
|
194 |
|
195 |
def filter_models(search_term):
|
196 |
-
|
197 |
filtered = [m for m in models_list if search_term.lower() in m.lower()]
|
198 |
-
|
199 |
return gr.update(choices=filtered)
|
200 |
|
201 |
model_search_box.change(
|
@@ -203,17 +216,17 @@ with demo:
|
|
203 |
inputs=model_search_box,
|
204 |
outputs=featured_model_radio
|
205 |
)
|
206 |
-
|
207 |
|
208 |
featured_model_radio.change(
|
209 |
fn=set_custom_model_from_radio,
|
210 |
inputs=featured_model_radio,
|
211 |
outputs=custom_model_box
|
212 |
)
|
213 |
-
|
214 |
|
215 |
-
|
216 |
|
217 |
if __name__ == "__main__":
|
218 |
-
|
219 |
demo.launch()
|
|
|
2 |
from openai import OpenAI
|
3 |
import os
|
4 |
|
5 |
+
# A helper function to show pop-up (toast) messages in the Gradio interface
|
6 |
+
# and also keep them in the console for debugging.
|
7 |
+
# Note: gr.toast() only works during or after a Gradio event has started.
|
8 |
+
# If this code runs at the global level (on import), the pop-ups may
|
9 |
+
# not appear. They *will* appear for any messages triggered during
|
10 |
+
# a Gradio event (e.g. when the user sends a message).
|
11 |
+
|
12 |
+
def show_loading_status(msg):
|
13 |
+
# Attempt to show pop-up via gr.toast (works when called inside a running Gradio event).
|
14 |
+
try:
|
15 |
+
gr.toast(msg)
|
16 |
+
except:
|
17 |
+
# If gr.toast() fails (e.g. called outside of an event), just ignore or pass
|
18 |
+
pass
|
19 |
+
# Also print to console for debugging
|
20 |
+
print(msg)
|
21 |
+
|
22 |
ACCESS_TOKEN = os.getenv("HF_TOKEN")
|
23 |
+
show_loading_status("Access token loaded.")
|
24 |
|
25 |
client = OpenAI(
|
26 |
base_url="https://api-inference.huggingface.co/v1/",
|
27 |
api_key=ACCESS_TOKEN,
|
28 |
)
|
29 |
+
show_loading_status("OpenAI client initialized.")
|
30 |
|
31 |
|
32 |
def respond(
|
|
|
41 |
custom_model
|
42 |
):
|
43 |
|
44 |
+
show_loading_status(f"Received message: {message}")
|
45 |
+
show_loading_status(f"History: {history}")
|
46 |
+
show_loading_status(f"System message: {system_message}")
|
47 |
+
show_loading_status(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
|
48 |
+
show_loading_status(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
|
49 |
+
show_loading_status(f"Selected model (custom_model): {custom_model}")
|
50 |
|
51 |
# Convert seed to None if -1 (meaning random)
|
52 |
if seed == -1:
|
53 |
seed = None
|
54 |
|
55 |
messages = [{"role": "system", "content": system_message}]
|
56 |
+
show_loading_status("Initial messages array constructed.")
|
57 |
|
58 |
# Add conversation history to the context
|
59 |
for val in history:
|
|
|
61 |
assistant_part = val[1]
|
62 |
if user_part:
|
63 |
messages.append({"role": "user", "content": user_part})
|
64 |
+
show_loading_status(f"Added user message to context: {user_part}")
|
65 |
if assistant_part:
|
66 |
messages.append({"role": "assistant", "content": assistant_part})
|
67 |
+
show_loading_status(f"Added assistant message to context: {assistant_part}")
|
68 |
|
69 |
# Append the latest user message
|
70 |
messages.append({"role": "user", "content": message})
|
71 |
+
show_loading_status("Latest user message appended.")
|
72 |
|
73 |
# If user provided a model, use that; otherwise, fall back to a default model
|
74 |
model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.3-70B-Instruct"
|
75 |
+
show_loading_status(f"Model selected for inference: {model_to_use}")
|
76 |
|
77 |
# Start with an empty string to build the response as tokens stream in
|
78 |
response = ""
|
79 |
+
show_loading_status("Sending request to OpenAI API.")
|
80 |
|
81 |
for message_chunk in client.chat.completions.create(
|
82 |
model=model_to_use,
|
|
|
89 |
messages=messages,
|
90 |
):
|
91 |
token_text = message_chunk.choices[0].delta.content
|
92 |
+
show_loading_status(f"Received token: {token_text}")
|
93 |
response += token_text
|
94 |
yield response
|
95 |
|
96 |
+
show_loading_status("Completed response generation.")
|
97 |
|
98 |
# GRADIO UI
|
99 |
|
100 |
+
chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="Select a model and begin chatting", likeable=True, layout="panel")
|
101 |
+
show_loading_status("Chatbot interface created.")
|
102 |
|
103 |
system_message_box = gr.Textbox(value="", placeholder="You are a helpful assistant.", label="System Prompt")
|
104 |
|
|
|
147 |
)
|
148 |
|
149 |
def set_custom_model_from_radio(selected):
|
150 |
+
show_loading_status(f"Featured model selected: {selected}")
|
|
|
|
|
|
|
|
|
151 |
return selected
|
152 |
|
153 |
demo = gr.ChatInterface(
|
|
|
165 |
chatbot=chatbot,
|
166 |
theme="Nymbo/Nymbo_Theme",
|
167 |
)
|
168 |
+
show_loading_status("ChatInterface object created.")
|
169 |
|
170 |
with demo:
|
171 |
with gr.Accordion("Model Selection", open=False):
|
|
|
174 |
placeholder="Search for a featured model...",
|
175 |
lines=1
|
176 |
)
|
177 |
+
show_loading_status("Model search box created.")
|
178 |
|
179 |
models_list = [
|
180 |
"meta-llama/Llama-3.3-70B-Instruct",
|
|
|
195 |
"TinyLlama/TinyLlama-1.1B-Chat-v1.0",
|
196 |
"microsoft/Phi-3.5-mini-instruct",
|
197 |
]
|
198 |
+
show_loading_status("Models list initialized.")
|
199 |
|
200 |
featured_model_radio = gr.Radio(
|
201 |
label="Select a model below",
|
|
|
203 |
value="meta-llama/Llama-3.3-70B-Instruct",
|
204 |
interactive=True
|
205 |
)
|
206 |
+
show_loading_status("Featured models radio button created.")
|
207 |
|
208 |
def filter_models(search_term):
|
209 |
+
show_loading_status(f"Filtering models with search term: {search_term}")
|
210 |
filtered = [m for m in models_list if search_term.lower() in m.lower()]
|
211 |
+
show_loading_status(f"Filtered models: {filtered}")
|
212 |
return gr.update(choices=filtered)
|
213 |
|
214 |
model_search_box.change(
|
|
|
216 |
inputs=model_search_box,
|
217 |
outputs=featured_model_radio
|
218 |
)
|
219 |
+
show_loading_status("Model search box change event linked.")
|
220 |
|
221 |
featured_model_radio.change(
|
222 |
fn=set_custom_model_from_radio,
|
223 |
inputs=featured_model_radio,
|
224 |
outputs=custom_model_box
|
225 |
)
|
226 |
+
show_loading_status("Featured model radio button change event linked.")
|
227 |
|
228 |
+
show_loading_status("Gradio interface initialized.")
|
229 |
|
230 |
if __name__ == "__main__":
|
231 |
+
show_loading_status("Launching the demo application.")
|
232 |
demo.launch()
|