Skip to content

Commit

Permalink
Added custom model cache dir (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish-aesthisia authored Mar 29, 2024
1 parent 3c87287 commit f1076ca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.safetensors
#ignore downloaded models
models/*
17 changes: 14 additions & 3 deletions llm.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import torch
import os, torch
from threading import Thread
from typing import Optional

import gradio as gr
from langchain import PromptTemplate, LLMChain
from langchain.llms.base import LLM
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, AutoConfig

cache_dir = os.path.join(os.getcwd(), "models")

def initialize_model_and_tokenizer(model_name):
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, trust_remote_code=True)
config = AutoConfig.from_pretrained(model_name, cache_dir=cache_dir)

model = AutoModelForCausalLM.from_pretrained(
model_name,
config=config,
cache_dir=cache_dir,
torch_dtype=torch.bfloat16,
trust_remote_code=True)

model.eval()
#model.cuda() #uncomment for cuda

tokenizer = AutoTokenizer.from_pretrained(model_name)
return model, tokenizer

Expand Down

0 comments on commit f1076ca

Please sign in to comment.