hyang0503 commited on
Commit
02b33e8
·
1 Parent(s): 25635a4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -1
app.py CHANGED
@@ -3,5 +3,42 @@ import gradio as gr
3
  def greet(name):
4
  return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  iface.launch()
 
3
  def greet(name):
4
  return "Hello " + name + "!!"
5
 
6
+
7
+ import torch
8
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
9
+ from peft import PeftModel, PeftConfig
10
+
11
+ class InferenceFineTunning:
12
+ def __init__(self, model_path):
13
+ peft_model_id = f"hyang0503/{model_path}"
14
+ config = PeftConfig.from_pretrained(peft_model_id)
15
+ bnb_config = BitsAndBytesConfig(
16
+ load_in_4bit=True,
17
+ bnb_4bit_use_double_quant=True,
18
+ bnb_4bit_quant_type="nf4",
19
+ bnb_4bit_compute_dtype=torch.bfloat16
20
+ )
21
+ self.model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, quantization_config=bnb_config, device_map="auto")
22
+ self.model = PeftModel.from_pretrained(self.model, peft_model_id)
23
+
24
+ # self.tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)
25
+ self.tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
26
+ self.tokenizer.pad_token = self.tokenizer.eos_token
27
+ self.model.eval()
28
+
29
+ def generate(self, q): # 실습 노트북과 내용 다름
30
+ outputs = self.model.generate(
31
+ **self.tokenizer(
32
+ f"### 질문: {q}\n\n### 답변:",
33
+ return_tensors='pt',
34
+ return_token_type_ids=False
35
+ ).to("cuda"),
36
+ max_new_tokens=256,
37
+ early_stopping=True,
38
+ do_sample=True,
39
+ eos_token_id=2,
40
+ )
41
+ print(self.tokenizer.decode(outputs[0]))
42
+ ifg = InferenceFineTunning("qlora-koalpaca")
43
+ iface = gr.Interface(fn=ifg.generate, inputs="text", outputs="text")
44
  iface.launch()