wgetdd commited on
Commit
321de10
β€’
1 Parent(s): 3814244

inference code

Browse files
Files changed (3) hide show
  1. app.py +67 -0
  2. inference.py +28 -0
  3. requirments.txt +8 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from torchvision import transforms
3
+ import torch
4
+ from inference import run_inference
5
+
6
+
7
+ description_zero_shot_training = """ ### Zero Shot Training
8
+
9
+ 1. Choose a Dataset MNIST/CIFAR10
10
+
11
+ 2. Output will be class accuracy
12
+
13
+ """
14
+
15
+ # Description
16
+ title = "<center><strong><font size='8'>πŸ“Ž THE CLIP PLAYGROUND πŸ“Ž</font></strong></center>"
17
+
18
+ text_input = gr.Text(label="Enter text")
19
+ text_input2 = gr.Text(label="Generated Response")
20
+
21
+ css = "h1 { text-align: center } .about { text-align: justify; padding-left: 10%; padding-right: 10%; }"
22
+
23
+ with gr.Blocks(css=css, title='Play with CLIP') as demo:
24
+ with gr.Row():
25
+ with gr.Column(scale=1):
26
+ # Title
27
+ gr.Markdown(title)
28
+
29
+ with gr.Tab("chat_with_phi2"):
30
+ # Images
31
+ with gr.Row(variant="panel"):
32
+ with gr.Column(scale=1):
33
+ text_input.render()
34
+
35
+ with gr.Column(scale=1):
36
+ text_input2.render()
37
+
38
+ # Submit & Clear
39
+ with gr.Row():
40
+ with gr.Column():
41
+ run_chat_with_phi2_button = gr.Button("chat_with_phi2", variant='primary')
42
+ clear_btn_text_to_image = gr.Button("Clear", variant="secondary")
43
+ gr.Markdown(description_zero_shot_training)
44
+ gr.Examples(examples = ["What is Large Language models ?", "Can you write a short introduction about the relevance of the term monopsony in economics? Please use examples related to potential monopsonies in the labour market and cite relevant research.", "I want to start doing astrophotography as a hobby, any suggestions what could i do?"],
45
+ inputs=[text_input],
46
+ outputs=text_input2,
47
+ fn=run_inference,
48
+ cache_examples=True,
49
+ examples_per_page=4)
50
+
51
+ run_chat_with_phi2_button.click(run_inference,
52
+ inputs=[
53
+ text_input,
54
+ ],
55
+ outputs=text_input2)
56
+
57
+ #######################################################################################################################
58
+ def clear():
59
+ return None, None
60
+
61
+ def clear_text():
62
+ return None, None, None
63
+
64
+ clear_btn_text_to_image.click(clear, outputs=[text_input, text_input2])
65
+
66
+ demo.queue()
67
+ demo.launch()
inference.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, AutoTokenizer,pipeline
3
+
4
+ model_name = "trained_model/content/results/checkpoint-500"
5
+
6
+ bnb_config = BitsAndBytesConfig(
7
+ load_in_4bit=True,
8
+ bnb_4bit_quant_type="nf4",
9
+ bnb_4bit_compute_dtype=torch.float16,
10
+ )
11
+
12
+ model = AutoModelForCausalLM.from_pretrained(
13
+ model_name,
14
+ quantization_config=bnb_config,
15
+ trust_remote_code=True
16
+ )
17
+ model.config.use_cache = False
18
+
19
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
20
+ tokenizer.pad_token = tokenizer.eos_token
21
+
22
+ # Run text generation pipeline with our next model
23
+ pipe = pipeline(task="text-generation", model=model, tokenizer=tokenizer, max_length=200)
24
+
25
+
26
+ def run_inference(prompt):
27
+ result = pipe(f"<s>[INST] {prompt} [/INST]")
28
+ return result[0]['generated_text']
requirments.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ trl
2
+ transformers
3
+ accelerate
4
+ git+https://github.com/huggingface/peft.git
5
+ datasets
6
+ bitsandbytes
7
+ einops
8
+ wandb