File size: 1,036 Bytes
f357513
4297655
 
f357513
277c222
4297655
f357513
 
 
277c222
f357513
 
 
277c222
 
 
 
 
 
 
 
 
 
 
 
06ee17b
277c222
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
import torch
import gradio as gr

from peft import PeftModel, PeftConfig
from transformers import AutoModelForCausalLM, AutoTokenizer

device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
peft_model_id = "kimmeoungjun/qlora-koalpaca"
config = PeftConfig.from_pretrained(peft_model_id)
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path)
model = PeftModel.from_pretrained(model, peft_model_id).to(device)
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)

def generate(q):
    inputs = tokenizer(f"### 질문: {q}\n\n### 답변:", return_tensors='pt', return_token_type_ids=False)
    outputs = model.generate(
        **{k: v.to(device) for k, v in inputs.items()},
        max_new_tokens=256,
        do_sample=True,
        eos_token_id=2,
    )
    result = tokenizer.decode(outputs[0])
    answer_idx = result.find("### 답변:")
    answer = result[answer_idx + 7:].strip()
    return answer

gr.Interface(generate, "text", "text").launch(share=True)