Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
from transformers import AutoTokenizer
|
4 |
|
5 |
# Use the appropriate tokenizer for your model.
|
6 |
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta")
|
@@ -134,26 +134,25 @@ def respond(
|
|
134 |
temperature,
|
135 |
top_p,
|
136 |
):
|
137 |
-
"""Responds to a user message, maintaining conversation history
|
138 |
|
139 |
-
formatted_system_message =
|
140 |
|
141 |
-
truncated_history = truncate_history(history, formatted_system_message, MAX_CONTEXT_LENGTH - max_tokens - 100)
|
142 |
|
143 |
-
messages = [{"role": "system", "content": formatted_system_message}]
|
144 |
for user_msg, assistant_msg in truncated_history:
|
145 |
if user_msg:
|
146 |
-
messages.append({"role": "user", "content": f"<|user|>\n{user_msg}</s>"})
|
147 |
if assistant_msg:
|
148 |
-
messages.append({"role": "assistant", "content": f"<|assistant|>\n{assistant_msg}</s>"})
|
149 |
-
|
150 |
-
messages.append({"role": "user", "content": f"<|user|>\n{message}</s>"}) # Format current user message
|
151 |
|
|
|
152 |
|
153 |
response = ""
|
154 |
try:
|
155 |
for chunk in client.chat_completion(
|
156 |
-
messages,
|
157 |
max_tokens=max_tokens,
|
158 |
stream=True,
|
159 |
temperature=temperature,
|
@@ -161,10 +160,14 @@ def respond(
|
|
161 |
):
|
162 |
token = chunk.choices[0].delta.content
|
163 |
response += token
|
164 |
-
|
|
|
|
|
165 |
except Exception as e:
|
166 |
-
print(f"An error occurred: {e}")
|
167 |
-
|
|
|
|
|
168 |
|
169 |
|
170 |
def clear_memory(history, chat_history):
|
@@ -176,11 +179,14 @@ def clear_memory(history, chat_history):
|
|
176 |
|
177 |
|
178 |
# --- Gradio Interface ---
|
179 |
-
with gr.Blocks() as demo:
|
180 |
-
chatbot = gr.Chatbot(label="Roos NVC Chatbot")
|
181 |
-
msg = gr.Textbox(label="Your Message")
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
184 |
system_message = gr.Textbox(value=nvc_prompt_template, label="System message")
|
185 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
186 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
@@ -191,11 +197,12 @@ with gr.Blocks() as demo: # Use gr.Blocks for more control
|
|
191 |
step=0.05,
|
192 |
label="Top-p (nucleus sampling)",
|
193 |
)
|
194 |
-
clear_btn = gr.Button("Clear Memory") #Clear memory button
|
195 |
|
196 |
-
msg.submit(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p], chatbot)
|
197 |
-
clear_btn.click(clear_memory, [chatbot, chatbot], [msg, chatbot])
|
198 |
|
|
|
|
|
|
|
|
|
199 |
|
200 |
if __name__ == "__main__":
|
201 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from transformers import AutoTokenizer
|
4 |
|
5 |
# Use the appropriate tokenizer for your model.
|
6 |
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta")
|
|
|
134 |
temperature,
|
135 |
top_p,
|
136 |
):
|
137 |
+
"""Responds to a user message, maintaining conversation history. Returns history."""
|
138 |
|
139 |
+
formatted_system_message = system_message # Use the provided system message
|
140 |
|
141 |
+
truncated_history = truncate_history(history, formatted_system_message, MAX_CONTEXT_LENGTH - max_tokens - 100)
|
142 |
|
143 |
+
messages = [{"role": "system", "content": formatted_system_message}]
|
144 |
for user_msg, assistant_msg in truncated_history:
|
145 |
if user_msg:
|
146 |
+
messages.append({"role": "user", "content": f"<|user|>\n{user_msg}</s>"})
|
147 |
if assistant_msg:
|
148 |
+
messages.append({"role": "assistant", "content": f"<|assistant|>\n{assistant_msg}</s>"})
|
|
|
|
|
149 |
|
150 |
+
messages.append({"role": "user", "content": f"<|user|>\n{message}</s>"})
|
151 |
|
152 |
response = ""
|
153 |
try:
|
154 |
for chunk in client.chat_completion(
|
155 |
+
messages,
|
156 |
max_tokens=max_tokens,
|
157 |
stream=True,
|
158 |
temperature=temperature,
|
|
|
160 |
):
|
161 |
token = chunk.choices[0].delta.content
|
162 |
response += token
|
163 |
+
# Crucial: Yield the *updated* history. Append the *current* response.
|
164 |
+
yield history + [(message, response)] # Build the (user, bot) tuple.
|
165 |
+
|
166 |
except Exception as e:
|
167 |
+
print(f"An error occurred: {e}")
|
168 |
+
error_message = "I'm sorry, I encountered an error. Please try again."
|
169 |
+
# Also yield the history, even in case of error, to preserve context.
|
170 |
+
yield history + [(message, error_message)]
|
171 |
|
172 |
|
173 |
def clear_memory(history, chat_history):
|
|
|
179 |
|
180 |
|
181 |
# --- Gradio Interface ---
|
182 |
+
with gr.Blocks() as demo:
|
183 |
+
chatbot = gr.Chatbot(label="Roos NVC Chatbot")
|
184 |
+
msg = gr.Textbox(label="Your Message")
|
185 |
+
with gr.Row(): # Put the Send button and Clear button on the same row
|
186 |
+
send_btn = gr.Button("Send") # Add a Send button
|
187 |
+
clear_btn = gr.Button("Clear Memory")
|
188 |
+
|
189 |
+
with gr.Accordion("Settings", open=False):
|
190 |
system_message = gr.Textbox(value=nvc_prompt_template, label="System message")
|
191 |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
192 |
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
|
|
197 |
step=0.05,
|
198 |
label="Top-p (nucleus sampling)",
|
199 |
)
|
|
|
200 |
|
|
|
|
|
201 |
|
202 |
+
# Connect both Enter key *and* Send button to the respond function
|
203 |
+
msg.submit(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p], chatbot)
|
204 |
+
send_btn.click(respond, [msg, chatbot, system_message, max_tokens, temperature, top_p], chatbot)
|
205 |
+
clear_btn.click(clear_memory, [chatbot, chatbot], [msg, chatbot]) #Corrected
|
206 |
|
207 |
if __name__ == "__main__":
|
208 |
demo.launch()
|