Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,6 @@ client = OpenAI(
|
|
| 11 |
)
|
| 12 |
print("OpenAI client initialized.")
|
| 13 |
|
| 14 |
-
|
| 15 |
def respond(
|
| 16 |
message,
|
| 17 |
history: list[tuple[str, str]],
|
|
@@ -23,44 +22,27 @@ def respond(
|
|
| 23 |
seed,
|
| 24 |
custom_model
|
| 25 |
):
|
| 26 |
-
|
| 27 |
print(f"Received message: {message}")
|
| 28 |
print(f"History: {history}")
|
| 29 |
print(f"System message: {system_message}")
|
| 30 |
-
|
| 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 |
-
|
| 40 |
-
|
| 41 |
-
# Add conversation history to the context
|
| 42 |
for val in history:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
if
|
| 46 |
-
messages.append({"role": "
|
| 47 |
-
|
| 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 |
-
|
| 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.1-8B-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,
|
| 66 |
max_tokens=max_tokens,
|
|
@@ -72,75 +54,24 @@ def respond(
|
|
| 72 |
messages=messages,
|
| 73 |
):
|
| 74 |
token_text = message_chunk.choices[0].delta.content
|
| 75 |
-
print(f"Received token: {token_text}")
|
| 76 |
response += token_text
|
| 77 |
yield response
|
| 78 |
|
| 79 |
-
print("Completed response generation.")
|
| 80 |
-
|
| 81 |
-
# GRADIO UI
|
| 82 |
-
|
| 83 |
chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="ChatGPT is initializing...", likeable=True, layout="panel")
|
| 84 |
-
print("Chatbot interface created.")
|
| 85 |
|
| 86 |
system_message_box = gr.Label(value="You can select Max Tokens, Temperature, Top-P, Seed")
|
| 87 |
|
| 88 |
-
max_tokens_slider = gr.Slider(
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
label="Max new tokens"
|
| 94 |
-
)
|
| 95 |
-
temperature_slider = gr.Slider(
|
| 96 |
-
minimum=0.1,
|
| 97 |
-
maximum=1.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 |
-
|
| 125 |
-
custom_model_box = gr.Textbox(
|
| 126 |
-
value="meta-llama/Llama-3.2-3B-Instruct",
|
| 127 |
-
label="AI Mode is ",
|
| 128 |
-
# info="(Optional) Provide a custom Hugging Face model path. Overrides any selected featured model.",
|
| 129 |
-
# info="meta-llama/Llama-3.2-3B-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 |
-
|
| 144 |
system_message_box,
|
| 145 |
max_tokens_slider,
|
| 146 |
temperature_slider,
|
|
@@ -148,49 +79,13 @@ demo = gr.ChatInterface(
|
|
| 148 |
frequency_penalty_slider,
|
| 149 |
seed_slider,
|
| 150 |
custom_model_box,
|
| 151 |
-
|
| 152 |
],
|
| 153 |
fill_height=True,
|
| 154 |
chatbot=chatbot,
|
| 155 |
theme="Nymbo/Nymbo_Theme",
|
| 156 |
)
|
| 157 |
-
print("Chat Interface object created.")
|
| 158 |
-
|
| 159 |
-
with demo:
|
| 160 |
-
with gr.Accordion("", open=False):
|
| 161 |
-
|
| 162 |
-
print("")
|
| 163 |
-
|
| 164 |
-
models_list = [
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
]
|
| 168 |
-
# print("")
|
| 169 |
-
featured_model_radio = gr.Radio(
|
| 170 |
-
value="meta-llama/Llama-3.2-3B-Instruct",
|
| 171 |
-
interactive=True
|
| 172 |
-
)
|
| 173 |
-
|
| 174 |
-
# print("Featured models radio button created.")
|
| 175 |
-
|
| 176 |
-
def filter_models(search_term):
|
| 177 |
-
print(f"Filtering models with search term: {search_term}")
|
| 178 |
-
filtered = [m for m in models_list if search_term.lower() in m.lower()]
|
| 179 |
-
print(f"Filtered models: {filtered}")
|
| 180 |
-
return gr.update(choices=filtered)
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
# print("Model search box change event linked.")
|
| 184 |
-
|
| 185 |
-
featured_model_radio.change(
|
| 186 |
-
fn=set_custom_model_from_radio,
|
| 187 |
-
inputs=featured_model_radio,
|
| 188 |
-
outputs=custom_model_box
|
| 189 |
-
)
|
| 190 |
-
# print("Featured model radio button change event linked.")
|
| 191 |
-
|
| 192 |
-
# print("Gradio interface initialized.")
|
| 193 |
|
| 194 |
if __name__ == "__main__":
|
| 195 |
-
print("Launching the ChatGPT-Llama...
|
| 196 |
demo.launch()
|
|
|
|
|
|
| 11 |
)
|
| 12 |
print("OpenAI client initialized.")
|
| 13 |
|
|
|
|
| 14 |
def respond(
|
| 15 |
message,
|
| 16 |
history: list[tuple[str, str]],
|
|
|
|
| 22 |
seed,
|
| 23 |
custom_model
|
| 24 |
):
|
|
|
|
| 25 |
print(f"Received message: {message}")
|
| 26 |
print(f"History: {history}")
|
| 27 |
print(f"System message: {system_message}")
|
| 28 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
if seed == -1:
|
| 30 |
seed = None
|
| 31 |
+
|
| 32 |
messages = [{"role": "system", "content": system_message}]
|
| 33 |
+
|
|
|
|
|
|
|
| 34 |
for val in history:
|
| 35 |
+
if val[0]:
|
| 36 |
+
messages.append({"role": "user", "content": val[0]})
|
| 37 |
+
if val[1]:
|
| 38 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 39 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
messages.append({"role": "user", "content": message})
|
| 41 |
+
|
|
|
|
|
|
|
| 42 |
model_to_use = custom_model.strip() if custom_model.strip() != "" else "meta-llama/Llama-3.1-8B-Instruct"
|
| 43 |
+
|
|
|
|
|
|
|
| 44 |
response = ""
|
| 45 |
+
|
|
|
|
| 46 |
for message_chunk in client.chat.completions.create(
|
| 47 |
model=model_to_use,
|
| 48 |
max_tokens=max_tokens,
|
|
|
|
| 54 |
messages=messages,
|
| 55 |
):
|
| 56 |
token_text = message_chunk.choices[0].delta.content
|
|
|
|
| 57 |
response += token_text
|
| 58 |
yield response
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
chatbot = gr.Chatbot(height=600, show_copy_button=True, placeholder="ChatGPT is initializing...", likeable=True, layout="panel")
|
|
|
|
| 61 |
|
| 62 |
system_message_box = gr.Label(value="You can select Max Tokens, Temperature, Top-P, Seed")
|
| 63 |
|
| 64 |
+
max_tokens_slider = gr.Slider(1024, 2048, value=1024, step=100, label="Max new tokens")
|
| 65 |
+
temperature_slider = gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature")
|
| 66 |
+
top_p_slider = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-P")
|
| 67 |
+
frequency_penalty_slider = gr.Slider(-2.0, 2.0, value=0.0, step=0.1, label="Frequency Penalty")
|
| 68 |
+
seed_slider = gr.Slider(-1, 65535, value=-1, step=1, label="Seed (-1 for random)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
custom_model_box = gr.Textbox(value="meta-llama/Llama-3.2-3B-Instruct", label="AI Mode is ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
demo = gr.ChatInterface(
|
| 73 |
fn=respond,
|
| 74 |
additional_inputs=[
|
|
|
|
| 75 |
system_message_box,
|
| 76 |
max_tokens_slider,
|
| 77 |
temperature_slider,
|
|
|
|
| 79 |
frequency_penalty_slider,
|
| 80 |
seed_slider,
|
| 81 |
custom_model_box,
|
|
|
|
| 82 |
],
|
| 83 |
fill_height=True,
|
| 84 |
chatbot=chatbot,
|
| 85 |
theme="Nymbo/Nymbo_Theme",
|
| 86 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
+
print("Launching the ChatGPT-Llama...")
|
| 90 |
demo.launch()
|
| 91 |
+
|