rasyosef commited on
Commit
25721d0
1 Parent(s): 3d927d1

added slider to configure max_new_tokens

Browse files
Files changed (1) hide show
  1. app.py +6 -5
app.py CHANGED
@@ -14,7 +14,7 @@ model = AutoModelForCausalLM.from_pretrained(checkpoint, torch_dtype=torch.float
14
  phi2 = pipeline("text-generation", tokenizer=tokenizer, model=model)
15
 
16
  # Function that accepts a prompt and generates text using the phi2 pipeline
17
- def generate(prompt, chat_history):
18
 
19
  instruction = "You are a helpful assistant to 'User'. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'."
20
  final_prompt = f"Instruction: {instruction}\n"
@@ -26,7 +26,7 @@ def generate(prompt, chat_history):
26
  final_prompt += "User: " + prompt + "\n"
27
  final_prompt += "Output:"
28
 
29
- generated_text = phi2(final_prompt, max_new_tokens=42)[0]["generated_text"]
30
  response = generated_text.split("Output:")[1].split("User:")[0]
31
 
32
  if "Assistant:" in response:
@@ -40,16 +40,17 @@ def generate(prompt, chat_history):
40
  with gr.Blocks() as demo:
41
  gr.Markdown("""
42
  # Phi-2 Chatbot Demo
43
-
44
  This chatbot was created using Microsoft's 2.7 billion parameter [phi-2](https://huggingface.co/microsoft/phi-2) Transformer model.
45
 
46
- In order to reduce the response time on this hardware, `max_new_tokens` has been set to `42` in the text generation pipeline. It takes up to `150 seconds` for each response to be generated.
47
  """)
48
 
 
 
49
  chatbot = gr.Chatbot()
50
  msg = gr.Textbox()
51
 
52
  clear = gr.ClearButton([msg, chatbot])
53
- msg.submit(fn=generate, inputs=[msg, chatbot], outputs=[msg, chatbot])
54
 
55
  demo.launch()
 
14
  phi2 = pipeline("text-generation", tokenizer=tokenizer, model=model)
15
 
16
  # Function that accepts a prompt and generates text using the phi2 pipeline
17
+ def generate(prompt, chat_history, max_new_tokens):
18
 
19
  instruction = "You are a helpful assistant to 'User'. You do not respond as 'User' or pretend to be 'User'. You only respond once as 'Assistant'."
20
  final_prompt = f"Instruction: {instruction}\n"
 
26
  final_prompt += "User: " + prompt + "\n"
27
  final_prompt += "Output:"
28
 
29
+ generated_text = phi2(final_prompt, max_new_tokens=max_new_tokens)[0]["generated_text"]
30
  response = generated_text.split("Output:")[1].split("User:")[0]
31
 
32
  if "Assistant:" in response:
 
40
  with gr.Blocks() as demo:
41
  gr.Markdown("""
42
  # Phi-2 Chatbot Demo
 
43
  This chatbot was created using Microsoft's 2.7 billion parameter [phi-2](https://huggingface.co/microsoft/phi-2) Transformer model.
44
 
45
+ In order to reduce the response time on this hardware, `max_new_tokens` has been set to `42` in the text generation pipeline. With the default configuration, takes approximately `150 seconds` for each response to be generated. Use the slider below to increase or decrease the length of the generated text.
46
  """)
47
 
48
+ tokens_slider = gr.Slider(8, 128, value=42, label="Maximum new tokens", info="A larger `max_new_tokens` parameter value gives you longer text responses but at the cost of a slower response time.")
49
+
50
  chatbot = gr.Chatbot()
51
  msg = gr.Textbox()
52
 
53
  clear = gr.ClearButton([msg, chatbot])
54
+ msg.submit(fn=generate, inputs=[msg, chatbot, tokens_slider], outputs=[msg, chatbot])
55
 
56
  demo.launch()