Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -16,13 +16,13 @@ pipeline = transformers.pipeline(
|
|
16 |
)
|
17 |
|
18 |
def chat_function(message, history, system_prompt, max_new_tokens, temperature):
|
19 |
-
# Assuming history is a list of tuples (user_message, assistant_message)
|
20 |
messages = []
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
24 |
|
25 |
-
#
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
# Construct the prompt using the pipeline's tokenizer
|
@@ -32,12 +32,6 @@ def chat_function(message, history, system_prompt, max_new_tokens, temperature):
|
|
32 |
add_generation_prompt=True
|
33 |
)
|
34 |
|
35 |
-
# Generate the response
|
36 |
-
terminators = [
|
37 |
-
pipeline.tokenizer.eos_token_id,
|
38 |
-
pipeline.tokenizer.convert_tokens_to_ids("")
|
39 |
-
]
|
40 |
-
|
41 |
# Adjust the temperature slightly above given to ensure variety
|
42 |
adjusted_temp = temperature + 0.1
|
43 |
|
@@ -45,7 +39,6 @@ def chat_function(message, history, system_prompt, max_new_tokens, temperature):
|
|
45 |
outputs = pipeline(
|
46 |
prompt,
|
47 |
max_new_tokens=max_new_tokens,
|
48 |
-
eos_token_id=terminators,
|
49 |
do_sample=True,
|
50 |
temperature=adjusted_temp,
|
51 |
top_p=0.9
|
@@ -55,12 +48,12 @@ def chat_function(message, history, system_prompt, max_new_tokens, temperature):
|
|
55 |
generated_text = outputs[0]["generated_text"]
|
56 |
return generated_text[len(prompt):] # Return the new part of the conversation
|
57 |
|
58 |
-
#Gradio interface
|
59 |
gr.Interface(
|
60 |
fn=chat_function,
|
61 |
inputs=[
|
62 |
gr.Textbox(placeholder="Enter your message here", label="Your Message"),
|
63 |
-
gr.JSON(label="Conversation History (format as [[user, assistant], ...])"),
|
64 |
gr.Textbox(label="System Prompt"),
|
65 |
gr.Slider(512, 4096, label="Max New Tokens"),
|
66 |
gr.Slider(0.0, 1.0, step=0.1, label="Temperature")
|
@@ -69,6 +62,7 @@ gr.Interface(
|
|
69 |
).launch()
|
70 |
|
71 |
|
|
|
72 |
# def chat_function(message, history, system_prompt,max_new_tokens,temperature):
|
73 |
# messages = [
|
74 |
# {"role": "system", "content": system_prompt},
|
|
|
16 |
)
|
17 |
|
18 |
def chat_function(message, history, system_prompt, max_new_tokens, temperature):
|
|
|
19 |
messages = []
|
20 |
+
if history is not None:
|
21 |
+
for user_msg, assistant_msg in history:
|
22 |
+
messages.append({"role": "user", "content": user_msg})
|
23 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
24 |
|
25 |
+
# Always add the current user message
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
# Construct the prompt using the pipeline's tokenizer
|
|
|
32 |
add_generation_prompt=True
|
33 |
)
|
34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
# Adjust the temperature slightly above given to ensure variety
|
36 |
adjusted_temp = temperature + 0.1
|
37 |
|
|
|
39 |
outputs = pipeline(
|
40 |
prompt,
|
41 |
max_new_tokens=max_new_tokens,
|
|
|
42 |
do_sample=True,
|
43 |
temperature=adjusted_temp,
|
44 |
top_p=0.9
|
|
|
48 |
generated_text = outputs[0]["generated_text"]
|
49 |
return generated_text[len(prompt):] # Return the new part of the conversation
|
50 |
|
51 |
+
# Update Gradio interface to reflect the potential nullability of history
|
52 |
gr.Interface(
|
53 |
fn=chat_function,
|
54 |
inputs=[
|
55 |
gr.Textbox(placeholder="Enter your message here", label="Your Message"),
|
56 |
+
gr.JSON(label="Conversation History (format as [[user, assistant], ...])", optional=True), # Marked as optional
|
57 |
gr.Textbox(label="System Prompt"),
|
58 |
gr.Slider(512, 4096, label="Max New Tokens"),
|
59 |
gr.Slider(0.0, 1.0, step=0.1, label="Temperature")
|
|
|
62 |
).launch()
|
63 |
|
64 |
|
65 |
+
|
66 |
# def chat_function(message, history, system_prompt,max_new_tokens,temperature):
|
67 |
# messages = [
|
68 |
# {"role": "system", "content": system_prompt},
|