| from transformers import AutoModelForCausalLM, AutoTokenizer |
| import torch |
| import spaces |
| import gradio as gr |
| from transformers import BitsAndBytesConfig |
| import os |
| from huggingface_hub import login |
|
|
| login(token=os.environ["HUGGINGFACE_TOKEN"]) |
|
|
| model_name = "mistralai/Mistral-7B-v0.1" |
|
|
|
|
| tokenizer = AutoTokenizer.from_pretrained(model_name,use_auth_token=True) |
|
|
| quantization_config = BitsAndBytesConfig( |
| load_in_4bit=True, |
| bnb_4bit_compute_dtype=torch.float16, |
| bnb_4bit_use_double_quant=True |
| ) |
| model = AutoModelForCausalLM.from_pretrained( |
| model_name, |
| quantization_config=quantization_config, |
| device_map="auto" |
| ) |
|
|
|
|
| |
| @spaces.GPU |
| def generate_text(prompt): |
| inputs = tokenizer(prompt, return_tensors="pt").to("cuda") |
| output = model.generate(**inputs, max_new_tokens=100) |
| return tokenizer.decode(output[0], skip_special_tokens=True) |
|
|
| |
|
|
| @spaces.GPU |
| def chat_with_llm(prompt): |
| return generate_text(prompt) |
|
|
| gr.Interface(fn=chat_with_llm, inputs="text", outputs="text", title="Mistral Chatbot").launch() |
|
|
|
|