File size: 1,310 Bytes
8fd1ef9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import transformers
from peft import PeftModel

# Quantization config
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype="float16",
)

model_name = "TinyPixel/Llama-2-7B-bf16-sharded"

# loading the model with quantization config
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    quantization_config=bnb_config,
    trust_remote_code=True,
    device_map='auto'
)

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

model = PeftModel.from_pretrained(model,"shenoy/DialogSumLlama2_qlora", device_map="auto")

#gradio fields
input_text = gr.inputs.Textbox(label="Input Text", type="text")
output_text = gr.outputs.Textbox(label="Output Text", type="text")

def predict(text):
    inputs = tokenizer(text, return_tensors="pt")
    outputs = model.generate(input_ids=inputs['input_ids'], attention_mask=inputs['attention_mask'], max_new_tokens=100 ,repetition_penalty=1.2)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

#gradio interface
interface = gr.Interface(fn=predict, inputs=input_text, outputs=output_text)
interface.launch()