File size: 1,784 Bytes
eaa8416
 
 
 
 
 
 
4657191
eaa8416
 
 
 
 
 
 
 
 
d0e6c3b
 
 
 
eaa8416
 
 
b9d49cf
2ac2871
eaa8416
5e2e076
4657191
5e2e076
d497b84
5e2e076
d497b84
5e2e076
eaa8416
5e2e076
 
 
 
 
 
 
 
 
 
5c0dd13
5e2e076
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, logging
import gradio as gr


model_name = "microsoft/phi-2"
model = AutoModelForCausalLM.from_pretrained(
    model_name, low_cpu_mem_usage=True,
    trust_remote_code=True
)
model.config.use_cache = False


tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
tokenizer.pad_token = tokenizer.eos_token

# Loading adapter (trained LORA weights)
# ckpt = '/content/drive/MyDrive/S27/results/checkpoint-500'
# model.load_adapter(ckpt)
adapter_path = 'checkpoint-500'
model.load_adapter(adapter_path)

def inference(prompt):

  pipe = pipeline(task="text-generation",model=model,tokenizer=tokenizer,max_length = 100)
  result = pipe(f"<s>[INST] {prompt} [/INST]")
  return result[0]['generated_text']

INTERFACE = gr.Interface(fn=inference, inputs=[gr.Textbox(label= "Prompt", value= 'write a note on Shakuntala Devi')],
                    
                    outputs=gr.Text(label= "Generated Text"), title="Language Model Phi-2 fine-tuned with OpenAssistant/oasst-1 dataset using QLoRA strategy",
                 
                 examples = [['explain transpiration in plants'],]
                        ).launch(debug=True)
    
# with gr.Blocks() as demo:

#     gr.Markdown(
#     """
#     # Phi2 trained on OpenAssistant/oasst1  dataset
#     Start typing below to see the output.
#     """)
#     prompt = gr.Textbox(label="Prompt")
#     output = gr.Textbox(label="Output Box")
#     greet_btn = gr.Button("Generate")
#     examples = gr.Examples(examples=['write a note on Shakuntala Devi'], ['Tell me about Amitabh Bachchan'], inputs = [prompt], cache_examples=False)
#     greet_btn.click(fn=inference, inputs=prompt, outputs=output)

# demo.launch(debug=True)