|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
from datetime import datetime |
|
import spaces |
|
|
|
""" |
|
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference |
|
""" |
|
|
|
lora_name = "robinhad/UAlpaca-2.0-Mistral-7B" |
|
|
|
from peft import PeftModel, PeftConfig |
|
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig |
|
from torch import bfloat16 |
|
model_name = "mistralai/Mistral-7B-v0.1" |
|
|
|
quant_config = BitsAndBytesConfig( |
|
load_in_4bit=True, |
|
bnb_4bit_quant_type="nf4", |
|
bnb_4bit_use_double_quant=True, |
|
bnb_4bit_compute_dtype=bfloat16 |
|
) |
|
tokenizer = AutoTokenizer.from_pretrained(lora_name, use_fast=True) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_name, |
|
quantization_config=quant_config |
|
) |
|
|
|
model = PeftModel.from_pretrained(model, lora_name, torch_device="cpu") |
|
|
|
model = model.to("cuda") |
|
|
|
from transformers import StoppingCriteriaList, StopStringCriteria, TextIteratorStreamer |
|
from threading import Thread |
|
|
|
stop_criteria = StoppingCriteriaList([StopStringCriteria(tokenizer, stop_strings=["<|im_end|>"])]) |
|
|
|
|
|
@spaces.GPU |
|
def respond( |
|
message, |
|
history: list[tuple[str, str]], |
|
max_tokens, |
|
temperature, |
|
top_p, |
|
): |
|
|
|
messages = [] |
|
|
|
for val in history: |
|
if val[0]: |
|
messages.append({"role": "user", "content": val[0]}) |
|
if val[1]: |
|
messages.append({"role": "assistant", "content": val[1]}) |
|
|
|
messages.append({"role": "user", "content": message}) |
|
|
|
|
|
tokenized = tokenizer.apply_chat_template(messages, return_tensors="pt", add_generation_prompt=True).to("cuda") |
|
|
|
|
|
print(tokenizer.batch_decode(tokenized)[0]) |
|
print("====") |
|
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True) |
|
generation_kwargs = dict(inputs=tokenized, streamer=streamer, max_new_tokens=max_tokens, stopping_criteria=stop_criteria, top_p=top_p, temperature=temperature) |
|
|
|
thread = Thread(target=model.generate, kwargs=generation_kwargs) |
|
|
|
thread.start() |
|
|
|
generated_text = "" |
|
|
|
for new_text in streamer: |
|
|
|
generated_text += new_text |
|
|
|
generated_text = generated_text.replace("<|im_end|>", "") |
|
yield generated_text |
|
|
|
|
|
|
|
""" |
|
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface |
|
""" |
|
demo = gr.ChatInterface( |
|
respond, |
|
additional_inputs=[ |
|
|
|
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), |
|
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), |
|
gr.Slider( |
|
minimum=0.1, |
|
maximum=1.0, |
|
value=0.95, |
|
step=0.05, |
|
label="Top-p (nucleus sampling)", |
|
), |
|
], |
|
description="""### Attribution: ELEKS supported this project through a grant dedicated to the memory of Oleksiy Skrypnyk""", |
|
title=f"Inference demo for '{lora_name}' (alpha) model, instruction-tuned for Ukrainian", |
|
examples=[ |
|
["Напиши історію про Івасика-Телесика"], |
|
["Яка найвища гора в Україні?"], |
|
["Як звали батька Тараса Григоровича Шевченка?"], |
|
|
|
["Яка з цих гір не знаходиться у Європі? Говерла, Монблан, Гран-Парадізо, Еверест"], |
|
[ |
|
"Дай відповідь на питання\nЧому у качки жовті ноги?" |
|
]], |
|
) |
|
|
|
|
|
|
|
|
|
demo.launch() |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|