Spaces:
Running
Running
custom models
Browse files
app.py
CHANGED
@@ -21,7 +21,8 @@ def respond(
|
|
21 |
temperature,
|
22 |
top_p,
|
23 |
frequency_penalty,
|
24 |
-
seed
|
|
|
25 |
):
|
26 |
"""
|
27 |
This function handles the chatbot response. It takes in:
|
@@ -33,6 +34,7 @@ def respond(
|
|
33 |
- top_p: top-p (nucleus) sampling
|
34 |
- frequency_penalty: penalize repeated tokens in the output
|
35 |
- seed: a fixed seed for reproducibility; -1 will mean 'random'
|
|
|
36 |
"""
|
37 |
|
38 |
print(f"Received message: {message}")
|
@@ -40,6 +42,7 @@ def respond(
|
|
40 |
print(f"System message: {system_message}")
|
41 |
print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
|
42 |
print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
|
|
|
43 |
|
44 |
# Convert seed to None if -1 (meaning random)
|
45 |
if seed == -1:
|
@@ -62,26 +65,30 @@ def respond(
|
|
62 |
# Append the latest user message
|
63 |
messages.append({"role": "user", "content": message})
|
64 |
|
|
|
|
|
|
|
|
|
65 |
# Start with an empty string to build the response as tokens stream in
|
66 |
response = ""
|
67 |
print("Sending request to OpenAI API.")
|
68 |
|
69 |
# Make the streaming request to the HF Inference API via openai-like client
|
70 |
for message_chunk in client.chat.completions.create(
|
71 |
-
model=
|
72 |
max_tokens=max_tokens,
|
73 |
-
stream=True,
|
74 |
temperature=temperature,
|
75 |
top_p=top_p,
|
76 |
-
frequency_penalty=frequency_penalty,
|
77 |
-
seed=seed,
|
78 |
messages=messages,
|
79 |
):
|
80 |
# Extract the token text from the response chunk
|
81 |
token_text = message_chunk.choices[0].delta.content
|
82 |
print(f"Received token: {token_text}")
|
83 |
response += token_text
|
84 |
-
#
|
85 |
yield response
|
86 |
|
87 |
print("Completed response generation.")
|
@@ -90,69 +97,57 @@ def respond(
|
|
90 |
chatbot = gr.Chatbot(height=600)
|
91 |
print("Chatbot interface created.")
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
temperature,
|
145 |
-
top_p,
|
146 |
-
frequency_penalty,
|
147 |
-
seed
|
148 |
-
],
|
149 |
-
fill_height=True,
|
150 |
-
chatbot=chatbot,
|
151 |
-
theme="Nymbo/Nymbo_Theme",
|
152 |
-
title="Serverless-TextGen-Hub",
|
153 |
-
description="A comprehensive UI for text generation using the HF Inference API."
|
154 |
-
)
|
155 |
-
|
156 |
print("Gradio interface initialized.")
|
157 |
|
158 |
if __name__ == "__main__":
|
|
|
21 |
temperature,
|
22 |
top_p,
|
23 |
frequency_penalty,
|
24 |
+
seed,
|
25 |
+
custom_model
|
26 |
):
|
27 |
"""
|
28 |
This function handles the chatbot response. It takes in:
|
|
|
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 user-provided custom model name (if any)
|
38 |
"""
|
39 |
|
40 |
print(f"Received message: {message}")
|
|
|
42 |
print(f"System message: {system_message}")
|
43 |
print(f"Max tokens: {max_tokens}, Temperature: {temperature}, Top-P: {top_p}")
|
44 |
print(f"Frequency Penalty: {frequency_penalty}, Seed: {seed}")
|
45 |
+
print(f"Custom model: {custom_model}")
|
46 |
|
47 |
# Convert seed to None if -1 (meaning random)
|
48 |
if seed == -1:
|
|
|
65 |
# Append the latest user message
|
66 |
messages.append({"role": "user", "content": message})
|
67 |
|
68 |
+
# Determine which model to use: either custom_model or a default
|
69 |
+
model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.3-70B-Instruct"
|
70 |
+
print(f"Model selected for inference: {model_to_use}")
|
71 |
+
|
72 |
# Start with an empty string to build the response as tokens stream in
|
73 |
response = ""
|
74 |
print("Sending request to OpenAI API.")
|
75 |
|
76 |
# Make the streaming request to the HF Inference API via openai-like client
|
77 |
for message_chunk in client.chat.completions.create(
|
78 |
+
model=model_to_use, # Use either the user-provided custom model or default
|
79 |
max_tokens=max_tokens,
|
80 |
+
stream=True, # Stream the response
|
81 |
temperature=temperature,
|
82 |
top_p=top_p,
|
83 |
+
frequency_penalty=frequency_penalty,
|
84 |
+
seed=seed,
|
85 |
messages=messages,
|
86 |
):
|
87 |
# Extract the token text from the response chunk
|
88 |
token_text = message_chunk.choices[0].delta.content
|
89 |
print(f"Received token: {token_text}")
|
90 |
response += token_text
|
91 |
+
# Yield the partial response to Gradio so it can display in real-time
|
92 |
yield response
|
93 |
|
94 |
print("Completed response generation.")
|
|
|
97 |
chatbot = gr.Chatbot(height=600)
|
98 |
print("Chatbot interface created.")
|
99 |
|
100 |
+
# Create the Gradio ChatInterface
|
101 |
+
# We add two new sliders for Frequency Penalty, Seed, and now a new "Custom Model" text box.
|
102 |
+
demo = gr.ChatInterface(
|
103 |
+
fn=respond,
|
104 |
+
additional_inputs=[
|
105 |
+
gr.Textbox(value="", label="System message"),
|
106 |
+
gr.Slider(
|
107 |
+
minimum=1,
|
108 |
+
maximum=4096,
|
109 |
+
value=512,
|
110 |
+
step=1,
|
111 |
+
label="Max new tokens"
|
112 |
+
),
|
113 |
+
gr.Slider(
|
114 |
+
minimum=0.1,
|
115 |
+
maximum=4.0,
|
116 |
+
value=0.7,
|
117 |
+
step=0.1,
|
118 |
+
label="Temperature"
|
119 |
+
),
|
120 |
+
gr.Slider(
|
121 |
+
minimum=0.1,
|
122 |
+
maximum=1.0,
|
123 |
+
value=0.95,
|
124 |
+
step=0.05,
|
125 |
+
label="Top-P"
|
126 |
+
),
|
127 |
+
gr.Slider(
|
128 |
+
minimum=-2.0,
|
129 |
+
maximum=2.0,
|
130 |
+
value=0.0,
|
131 |
+
step=0.1,
|
132 |
+
label="Frequency Penalty"
|
133 |
+
),
|
134 |
+
gr.Slider(
|
135 |
+
minimum=-1,
|
136 |
+
maximum=65535, # Arbitrary upper limit for demonstration
|
137 |
+
value=-1,
|
138 |
+
step=1,
|
139 |
+
label="Seed (-1 for random)"
|
140 |
+
),
|
141 |
+
gr.Textbox(
|
142 |
+
value="",
|
143 |
+
label="Custom Model",
|
144 |
+
info="(Optional) Provide a custom Hugging Face model path. This will override the default model if not empty."
|
145 |
+
),
|
146 |
+
],
|
147 |
+
fill_height=True,
|
148 |
+
chatbot=chatbot,
|
149 |
+
theme="Nymbo/Nymbo_Theme",
|
150 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
print("Gradio interface initialized.")
|
152 |
|
153 |
if __name__ == "__main__":
|