mkthoma commited on
Commit
7c2adcf
1 Parent(s): 281c8a9

app update

Browse files
Files changed (1) hide show
  1. app.py +33 -10
app.py CHANGED
@@ -6,29 +6,52 @@ model_path = "finetuned_phi2"
6
  model = AutoModelForCausalLM.from_pretrained(model_path, low_cpu_mem_usage=True, trust_remote_code=True)
7
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
8
 
 
9
 
 
 
 
 
 
10
 
11
- def generate(question, context):
 
 
 
 
 
12
  system_message = "You are a question answering chatbot. Provide a clear and detailed explanation"
13
  prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n {question} [/INST]" # replace the command here with something relevant to your task
14
-
15
- num_new_tokens = 200 # change to the number of new tokens you want to generate
16
  # Count the number of tokens in the prompt
17
  num_prompt_tokens = len(tokenizer(prompt)['input_ids'])
18
  # Calculate the maximum length for the generation
19
- max_length = num_prompt_tokens + num_new_tokens
20
-
21
- gen = pipeline('text-generation', model=model, tokenizer=tokenizer, max_length=max_length)
22
  result = gen(prompt)
23
  return (result[0]['generated_text'].replace(prompt, ''))
24
-
25
 
26
  bbchatbot = gr.Chatbot(
27
- avatar_images=["logo/user logo.png", "logo/bot logo.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
28
 
29
- demo = gr.ChatInterface(fn=generate,
 
 
 
 
 
 
30
  chatbot=bbchatbot,
31
- title="🧑🏽‍💻Microsoft Phi2 Chatbot🤖"
 
32
  )
33
 
 
 
 
 
 
 
34
  demo.queue().launch(show_api=False)
 
6
  model = AutoModelForCausalLM.from_pretrained(model_path, low_cpu_mem_usage=True, trust_remote_code=True)
7
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
8
 
9
+ num_new_tokens = 200 # change to the number of new tokens you want to generate
10
 
11
+ DESCRIPTION = """\
12
+ # 🧑🏽‍💻Microsoft Phi2 Chatbot🤖
13
+ This Space demonstrates model [Microsoft Phi2 2.7B](https://huggingface.co/microsoft/phi-2), a model with 2.78B parameters fine-tuned for chat instructions. Feel free to play with it, or duplicate to run generations without a queue! If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://huggingface.co/inference-endpoints).
14
+ 🔎 For more details about the finetuning, take a look at the [GitHub](https://github.com/mkthoma/llm_finetuning) code.
15
+ """
16
 
17
+ LICENSE = """
18
+ As a derivate work of [Microsoft Phi2 2.7B](https://huggingface.co/microsoft/phi-2), this demo is governed by the original [license](https://huggingface.co/microsoft/phi-2/resolve/main/LICENSE).
19
+ """
20
+
21
+ def generate(question, context, max_new_tokens = 200, temperature = 0.6):
22
+
23
  system_message = "You are a question answering chatbot. Provide a clear and detailed explanation"
24
  prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n {question} [/INST]" # replace the command here with something relevant to your task
25
+
 
26
  # Count the number of tokens in the prompt
27
  num_prompt_tokens = len(tokenizer(prompt)['input_ids'])
28
  # Calculate the maximum length for the generation
29
+ max_length = num_prompt_tokens + max_new_tokens
30
+
31
+ gen = pipeline('text-generation', model=model, tokenizer=tokenizer, max_length=max_length, temperature=temperature)
32
  result = gen(prompt)
33
  return (result[0]['generated_text'].replace(prompt, ''))
34
+
35
 
36
  bbchatbot = gr.Chatbot(
37
+ avatar_images=["/content/logo/user logo.png", "/content/logo/bot logo.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
38
 
39
+ examples = [["What is a large language model?"], ["How to calm down a person?"], ["What is aritificial intelligence?"], ["How to write a good resume?"]]
40
+
41
+ additional_inputs = additional_inputs=[gr.Slider(label="Max new tokens",minimum=100,maximum=2048,step=50,value=num_new_tokens),
42
+ gr.Slider(label="Temperature",minimum=0.1,maximum=4.0,step=0.1,value=0.6)]
43
+
44
+ chat_interface = gr.ChatInterface(fn=generate,
45
+ additional_inputs=additional_inputs,
46
  chatbot=bbchatbot,
47
+ title="",
48
+ examples=examples
49
  )
50
 
51
+ with gr.Blocks(css="style.css") as demo:
52
+ gr.Markdown(DESCRIPTION)
53
+ gr.DuplicateButton(value="Duplicate for private use", elem_id="duplicate-button")
54
+ chat_interface.render()
55
+ gr.Markdown(LICENSE)
56
+
57
  demo.queue().launch(show_api=False)