|
import gradio as gr |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig,BitsAndBytesConfig |
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0", trust_remote_code=True) |
|
tokenizer = AutoTokenizer.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0", trust_remote_code=True) |
|
|
|
generation_config = GenerationConfig( |
|
penalty_alpha=0.6, |
|
do_sample = True, |
|
top_k=5, |
|
temperature=0.5, |
|
repetition_penalty=1.2, |
|
max_new_tokens=500, |
|
pad_token_id=tokenizer.eos_token_id |
|
) |
|
|
|
def generate_text(input): |
|
input_text = f'<|system|>\n You are a chatbot who can help code!</s> \n <|user|> \n {input} \n <|assistant|> \n' |
|
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device) |
|
output_ids = model.generate(input_ids, generation_config=generation_config) |
|
output_text = tokenizer.decode(output_ids[0],skip_special_tokens=True) |
|
return output_text |
|
|
|
|
|
iface = gr.Interface(fn=generate_text, inputs="text", outputs="text") |
|
iface.launch() |
|
|