Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,228 +1,96 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
custom_model
|
25 |
-
):
|
26 |
-
|
27 |
-
print(f"Received message: {message}")
|
28 |
-
print(f"History: {history}")
|
29 |
-
print(f"System message: {system_message}")
|
30 |
-
print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
|
31 |
-
print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
|
32 |
-
print(f"Selected model (custom_model): {custom_model}")
|
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 |
-
print("Initial messages array constructed.")
|
40 |
-
|
41 |
-
# Add conversation history to the context
|
42 |
-
for val in history:
|
43 |
-
user_part = val[0]
|
44 |
-
assistant_part = val[1]
|
45 |
-
if user_part:
|
46 |
-
messages.append({"role": "user", "content": user_part})
|
47 |
-
print(f"Added user message to context: {user_part}")
|
48 |
-
if assistant_part:
|
49 |
-
messages.append({"role": "assistant", "content": assistant_part})
|
50 |
-
print(f"Added assistant message to context: {assistant_part}")
|
51 |
-
|
52 |
-
# Append the latest user message
|
53 |
-
messages.append({"role": "user", "content": message})
|
54 |
-
print("Latest user message appended.")
|
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 |
-
print(f"Model selected for inference: {model_to_use}")
|
59 |
-
|
60 |
-
# Start with an empty string to build the response as tokens stream in
|
61 |
-
response = ""
|
62 |
-
print("Sending request to OpenAI API.")
|
63 |
-
|
64 |
-
for message_chunk in client.chat.completions.create(
|
65 |
-
model=model_to_use,
|
66 |
max_tokens=max_tokens,
|
67 |
-
stream=
|
68 |
temperature=temperature,
|
69 |
top_p=top_p,
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
# GRADIO UI
|
82 |
-
|
83 |
-
chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="Select a model and begin chatting", likeable=True, layout="panel")
|
84 |
-
print("Chatbot interface created.")
|
85 |
-
|
86 |
-
system_message_box = gr.Textbox(value="", placeholder="You are a helpful assistant.", label="System Prompt")
|
87 |
-
|
88 |
-
max_tokens_slider = gr.Slider(
|
89 |
-
minimum=1,
|
90 |
-
maximum=4096,
|
91 |
-
value=512,
|
92 |
-
step=1,
|
93 |
-
label="Max new tokens"
|
94 |
-
)
|
95 |
-
temperature_slider = gr.Slider(
|
96 |
-
minimum=0.1,
|
97 |
-
maximum=4.0,
|
98 |
-
value=0.7,
|
99 |
-
step=0.1,
|
100 |
-
label="Temperature"
|
101 |
-
)
|
102 |
-
top_p_slider = gr.Slider(
|
103 |
-
minimum=0.1,
|
104 |
-
maximum=1.0,
|
105 |
-
value=0.95,
|
106 |
-
step=0.05,
|
107 |
-
label="Top-P"
|
108 |
-
)
|
109 |
-
frequency_penalty_slider = gr.Slider(
|
110 |
-
minimum=-2.0,
|
111 |
-
maximum=2.0,
|
112 |
-
value=0.0,
|
113 |
-
step=0.1,
|
114 |
-
label="Frequency Penalty"
|
115 |
-
)
|
116 |
-
seed_slider = gr.Slider(
|
117 |
-
minimum=-1,
|
118 |
-
maximum=65535,
|
119 |
-
value=-1,
|
120 |
-
step=1,
|
121 |
-
label="Seed (-1 for random)"
|
122 |
-
)
|
123 |
-
|
124 |
-
# The custom_model_box is what the respond function sees as "custom_model"
|
125 |
-
custom_model_box = gr.Textbox(
|
126 |
-
value="",
|
127 |
-
label="Custom Model",
|
128 |
-
info="(Optional) Provide a custom Hugging Face model path. Overrides any selected featured model.",
|
129 |
-
placeholder="meta-llama/Llama-3.3-70B-Instruct"
|
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(
|
141 |
-
fn=respond,
|
142 |
-
additional_inputs=[
|
143 |
-
system_message_box,
|
144 |
-
max_tokens_slider,
|
145 |
-
temperature_slider,
|
146 |
-
top_p_slider,
|
147 |
-
frequency_penalty_slider,
|
148 |
-
seed_slider,
|
149 |
-
custom_model_box,
|
150 |
-
],
|
151 |
-
fill_height=True,
|
152 |
-
chatbot=chatbot,
|
153 |
-
theme="Nymbo/Nymbo_Theme",
|
154 |
-
)
|
155 |
-
print("ChatInterface object created.")
|
156 |
|
157 |
with demo:
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
inputs=model_search_box,
|
213 |
-
outputs=featured_model_radio
|
214 |
-
)
|
215 |
-
print("Model search box change event linked.")
|
216 |
-
|
217 |
-
featured_model_radio.change(
|
218 |
-
fn=set_custom_model_from_radio,
|
219 |
-
inputs=featured_model_radio,
|
220 |
-
outputs=custom_model_box
|
221 |
-
)
|
222 |
-
print("Featured model radio button change event linked.")
|
223 |
-
|
224 |
-
print("Gradio interface initialized.")
|
225 |
|
226 |
if __name__ == "__main__":
|
227 |
-
print("Launching the demo application.")
|
228 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import json
|
4 |
+
|
5 |
+
# Список доступных моделей
|
6 |
+
models_list = [
|
7 |
+
"google/gemma-3-27b-it",
|
8 |
+
"meta-llama/Llama-3.3-70B-Instruct",
|
9 |
+
"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
10 |
+
"Qwen/Qwen2.5-72B-Instruct"
|
11 |
+
]
|
12 |
+
|
13 |
+
def add_message(role, content, messages):
|
14 |
+
messages.append({"role": role, "content": content})
|
15 |
+
return messages, len(messages), str(messages)
|
16 |
+
|
17 |
+
def clear_messages(messages):
|
18 |
+
return [], 0, "[]"
|
19 |
+
|
20 |
+
def start_conversation(model, messages, max_tokens, temperature, top_p):
|
21 |
+
client = InferenceClient(model)
|
22 |
+
response = client.chat_completion(
|
23 |
+
messages,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
max_tokens=max_tokens,
|
25 |
+
stream=False,
|
26 |
temperature=temperature,
|
27 |
top_p=top_p,
|
28 |
+
)
|
29 |
+
return response.choices[0].message.content
|
30 |
+
|
31 |
+
def show_messages(messages):
|
32 |
+
return str(messages)
|
33 |
+
|
34 |
+
def get_messages_api(messages):
|
35 |
+
return json.dumps(messages, indent=4)
|
36 |
+
|
37 |
+
demo = gr.Blocks()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
with demo:
|
40 |
+
gr.Markdown("# Chat Interface")
|
41 |
+
role_input = gr.Textbox(label="Role")
|
42 |
+
content_input = gr.Textbox(label="Content")
|
43 |
+
messages_state = gr.State(value=[])
|
44 |
+
messages_output = gr.Textbox(label="Messages", value="[]")
|
45 |
+
count_output = gr.Number(label="Count", value=0)
|
46 |
+
response_output = gr.Textbox(label="Response")
|
47 |
+
messages_api_output = gr.Textbox(label="Messages API")
|
48 |
+
|
49 |
+
add_button = gr.Button("Add")
|
50 |
+
clear_button = gr.Button("Clear")
|
51 |
+
start_button = gr.Button("Start")
|
52 |
+
show_button = gr.Button("Show messages")
|
53 |
+
get_api_button = gr.Button("Get messages API")
|
54 |
+
|
55 |
+
model_input = gr.Radio(
|
56 |
+
label="Select a model",
|
57 |
+
choices=models_list,
|
58 |
+
value=models_list[0],
|
59 |
+
)
|
60 |
+
|
61 |
+
max_tokens_slider = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
62 |
+
temperature_slider = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
63 |
+
top_p_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
64 |
+
|
65 |
+
add_button.click(
|
66 |
+
add_message,
|
67 |
+
inputs=[role_input, content_input, messages_state],
|
68 |
+
outputs=[messages_state, count_output, messages_output],
|
69 |
+
)
|
70 |
+
|
71 |
+
clear_button.click(
|
72 |
+
clear_messages,
|
73 |
+
inputs=[messages_state],
|
74 |
+
outputs=[messages_state, count_output, messages_output],
|
75 |
+
)
|
76 |
+
|
77 |
+
start_button.click(
|
78 |
+
start_conversation,
|
79 |
+
inputs=[model_input, messages_state, max_tokens_slider, temperature_slider, top_p_slider],
|
80 |
+
outputs=[response_output],
|
81 |
+
)
|
82 |
+
|
83 |
+
show_button.click(
|
84 |
+
show_messages,
|
85 |
+
inputs=[messages_state],
|
86 |
+
outputs=[messages_output],
|
87 |
+
)
|
88 |
+
|
89 |
+
get_api_button.click(
|
90 |
+
get_messages_api,
|
91 |
+
inputs=[messages_state],
|
92 |
+
outputs=[messages_api_output],
|
93 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
if __name__ == "__main__":
|
|
|
96 |
demo.launch()
|