Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,9 @@ client = InferenceClient(
|
|
6 |
)
|
7 |
|
8 |
# Formats the prompt to hold all of the past messages
|
9 |
-
def
|
10 |
prompt = "<s>"
|
11 |
-
|
12 |
-
# String to add before every prompt
|
13 |
-
prompt_prefix = ""
|
14 |
-
#prompt_prefix = "Please correct the grammar in the following sentence:"
|
15 |
-
prompt_template = "[INST] " + prompt_prefix + " {} [/INST]"
|
16 |
-
|
17 |
|
18 |
# Iterates through every past user input and response to be added to the prompt
|
19 |
for user_prompt, bot_response in history:
|
@@ -21,67 +16,43 @@ def format_prompt_basic(message, history):
|
|
21 |
prompt += f" {bot_response}</s> "
|
22 |
|
23 |
prompt += prompt_template.format(message)
|
24 |
-
print("\nPROMPT: \n\t" + prompt)
|
25 |
-
|
26 |
return prompt
|
27 |
|
28 |
-
# Formats the prompt to hold all of the past messages
|
29 |
-
def format_prompt_grammar1(message, history):
|
30 |
-
prompt = "<s>"
|
31 |
-
|
32 |
-
# String to add before every prompt
|
33 |
-
prompt_prefix = "Correct any grammatical errors in the following sentence and provide the corrected version:\n\nSentence: "
|
34 |
-
prompt_template = "[INST] " + prompt_prefix + ' {} [/INST]'
|
35 |
-
|
36 |
-
|
37 |
-
myList = [["It is my friends house in England.", "Corrected Sentence: It is my friend's house in England."], ["Every girl must bring their books to school.", "Corrected Sentence: Every girl must bring her books to school."]]
|
38 |
-
myList = myList + history
|
39 |
-
|
40 |
-
|
41 |
-
# Iterates through every past user input and response to be added to the prompt
|
42 |
-
for user_prompt, bot_response in myList:
|
43 |
-
prompt += prompt_template.format(user_prompt)
|
44 |
-
prompt += f" {bot_response}</s> \n"
|
45 |
-
|
46 |
-
prompt += prompt_template.format(message)
|
47 |
-
print("PROMPT: \n\t{}\n".format(prompt))
|
48 |
-
return prompt
|
49 |
|
50 |
# Don't track actual history
|
51 |
-
def format_prompt_grammar(message):
|
52 |
prompt = "<s>"
|
53 |
|
54 |
# String to add before every prompt
|
55 |
prompt_prefix = "Correct any grammatical errors in the following sentence and provide the corrected version:\n\nSentence:"
|
56 |
prompt_template = "[INST] " + prompt_prefix + ' {} [/INST]'
|
57 |
|
58 |
-
|
59 |
-
["Every girl must bring their books to school.", "Every girl must bring her books to school."]]# ["I have been to New York last summer.", "I went to New York last summer."]]
|
60 |
|
61 |
# Iterates through every past user input and response to be added to the prompt
|
62 |
-
for user_prompt, bot_response in
|
63 |
prompt += prompt_template.format(user_prompt)
|
64 |
prompt += f" {bot_response}</s> \n"
|
65 |
|
66 |
prompt += prompt_template.format(message)
|
67 |
-
print("PROMPT: \n\t{}\n".format(prompt))
|
68 |
return prompt
|
69 |
|
70 |
|
71 |
|
72 |
def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
|
73 |
print("\n\nSystem Prompt: '{}'".format(system_prompt))
|
|
|
74 |
temperature = float(temperature)
|
75 |
-
if temperature < 1e-2:
|
76 |
-
temperature = 1e-2
|
77 |
top_p = float(top_p)
|
78 |
|
|
|
79 |
generate_kwargs = dict(temperature=temperature, max_new_tokens=max_new_tokens, top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42,)
|
80 |
|
81 |
-
|
82 |
-
#formatted_prompt = format_prompt_grammar(f"
|
83 |
-
|
84 |
-
|
85 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
86 |
output = ""
|
87 |
|
@@ -93,14 +64,13 @@ def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256
|
|
93 |
|
94 |
|
95 |
additional_inputs=[
|
96 |
-
gr.Textbox( label="System Prompt", value="
|
97 |
gr.Slider( label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs", ),
|
98 |
gr.Slider( label="Max new tokens", value=256, minimum=0, maximum=1048, step=64, interactive=True, info="The maximum numbers of new tokens", ),
|
99 |
gr.Slider( label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens", ),
|
100 |
gr.Slider( label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens", )
|
101 |
]
|
102 |
|
103 |
-
|
104 |
examples=['Give me the grammatically correct version of the sentence: "We shood buy an car"', "Give me an example exam question testing students on square roots on basic integers", "Would this block of HTML code run?\n```\n\n```", "I have been to New York last summer.", "We shood buy an car.", "People is coming to my party.", "She is more taller.", "Their were lot of sheeps.", "I want to speak English good.", "I must to buy a new cartoon book."]
|
105 |
examples = [[x, None, None, None, None, None] for x in examples]
|
106 |
|
|
|
6 |
)
|
7 |
|
8 |
# Formats the prompt to hold all of the past messages
|
9 |
+
def format_prompt(message, history):
|
10 |
prompt = "<s>"
|
11 |
+
prompt_template = "[INST] {} [/INST]"
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Iterates through every past user input and response to be added to the prompt
|
14 |
for user_prompt, bot_response in history:
|
|
|
16 |
prompt += f" {bot_response}</s> "
|
17 |
|
18 |
prompt += prompt_template.format(message)
|
|
|
|
|
19 |
return prompt
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Don't track actual history
|
23 |
+
def format_prompt_grammar(message, history):
|
24 |
prompt = "<s>"
|
25 |
|
26 |
# String to add before every prompt
|
27 |
prompt_prefix = "Correct any grammatical errors in the following sentence and provide the corrected version:\n\nSentence:"
|
28 |
prompt_template = "[INST] " + prompt_prefix + ' {} [/INST]'
|
29 |
|
30 |
+
myHistory = [["It is my friends house in England.", "It is my friend's house in England."], ["Every girl must bring their books to school.", "Every girl must bring her books to school."]]
|
|
|
31 |
|
32 |
# Iterates through every past user input and response to be added to the prompt
|
33 |
+
for user_prompt, bot_response in myHistory:
|
34 |
prompt += prompt_template.format(user_prompt)
|
35 |
prompt += f" {bot_response}</s> \n"
|
36 |
|
37 |
prompt += prompt_template.format(message)
|
|
|
38 |
return prompt
|
39 |
|
40 |
|
41 |
|
42 |
def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
|
43 |
print("\n\nSystem Prompt: '{}'".format(system_prompt))
|
44 |
+
|
45 |
temperature = float(temperature)
|
46 |
+
if temperature < 1e-2: temperature = 1e-2
|
|
|
47 |
top_p = float(top_p)
|
48 |
|
49 |
+
|
50 |
generate_kwargs = dict(temperature=temperature, max_new_tokens=max_new_tokens, top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42,)
|
51 |
|
52 |
+
formatted_prompt = format_prompt(f"{system_prompt} {prompt}", history)
|
53 |
+
#formatted_prompt = format_prompt_grammar(f"Corrected Sentence: {prompt}", history)
|
54 |
+
print("\nPROMPT: \n\t" + formatted_prompt)
|
55 |
+
|
56 |
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
57 |
output = ""
|
58 |
|
|
|
64 |
|
65 |
|
66 |
additional_inputs=[
|
67 |
+
gr.Textbox( label="System Prompt", value="" , max_lines=1, interactive=True, ),
|
68 |
gr.Slider( label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs", ),
|
69 |
gr.Slider( label="Max new tokens", value=256, minimum=0, maximum=1048, step=64, interactive=True, info="The maximum numbers of new tokens", ),
|
70 |
gr.Slider( label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens", ),
|
71 |
gr.Slider( label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens", )
|
72 |
]
|
73 |
|
|
|
74 |
examples=['Give me the grammatically correct version of the sentence: "We shood buy an car"', "Give me an example exam question testing students on square roots on basic integers", "Would this block of HTML code run?\n```\n\n```", "I have been to New York last summer.", "We shood buy an car.", "People is coming to my party.", "She is more taller.", "Their were lot of sheeps.", "I want to speak English good.", "I must to buy a new cartoon book."]
|
75 |
examples = [[x, None, None, None, None, None] for x in examples]
|
76 |
|