PrarthanaTS commited on
Commit
9a57482
·
1 Parent(s): f046987

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +58 -0
  2. requirements.txt +9 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ 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
+ 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
+ \n 🔎 For more details about the finetuning, take a look at the [GitHub](https://github.com/mkthoma/llm_finetuning) code.
15
+ \n ⛔⛔ The model is hosted on a CPU and inference takes a long time. Please feel free to duplicate the space and use it on a GPU ⛔⛔
16
+ """
17
+
18
+ LICENSE = """
19
+ 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).
20
+ """
21
+
22
+ def generate(question, context, max_new_tokens = 200, temperature = 0.6):
23
+
24
+ system_message = "You are a question answering chatbot. Provide a clear and detailed explanation"
25
+ prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n {question} [/INST]" # replace the command here with something relevant to your task
26
+
27
+ # Count the number of tokens in the prompt
28
+ num_prompt_tokens = len(tokenizer(prompt)['input_ids'])
29
+ # Calculate the maximum length for the generation
30
+ max_length = num_prompt_tokens + max_new_tokens
31
+
32
+ gen = pipeline('text-generation', model=model, tokenizer=tokenizer, max_length=max_length, temperature=temperature)
33
+ result = gen(prompt)
34
+ return (result[0]['generated_text'].replace(prompt, ''))
35
+
36
+
37
+ bbchatbot = gr.Chatbot(
38
+ avatar_images=["logo/user logo.png", "logo/bot logo.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
39
+
40
+ examples = [["What is a large language model?"], ["How to calm down a person?"], ["What is aritificial intelligence?"], ["How to write a good resume?"]]
41
+
42
+ additional_inputs = additional_inputs=[gr.Slider(label="Max new tokens",minimum=100,maximum=500,step=10,value=num_new_tokens),
43
+ gr.Slider(label="Temperature",minimum=0.1,maximum=4.0,step=0.1,value=0.6)]
44
+
45
+ chat_interface = gr.ChatInterface(fn=generate,
46
+ additional_inputs=additional_inputs,
47
+ chatbot=bbchatbot,
48
+ title="",
49
+ examples=examples
50
+ )
51
+
52
+ with gr.Blocks(css="style.css") as demo:
53
+ gr.Markdown(DESCRIPTION)
54
+ gr.DuplicateButton(value="Duplicate for private use", elem_id="duplicate-button")
55
+ chat_interface.render()
56
+ gr.Markdown(LICENSE)
57
+
58
+ demo.queue().launch(show_api=False)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ accelerate
2
+ peft
3
+ bitsandbytes
4
+ transformers
5
+ trl
6
+ einops
7
+ huggingface_hub
8
+ gradio
9
+ torch