Spaces:
Paused
Paused
Update model_utils.py
Browse files- model_utils.py +59 -27
model_utils.py
CHANGED
@@ -1,37 +1,48 @@
|
|
1 |
-
import os
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
-
from langchain.llms import HuggingFacePipeline
|
4 |
-
from langchain import PromptTemplate, LLMChain
|
5 |
-
from dotenv import load_dotenv
|
|
|
6 |
|
7 |
# Define the model directory and name
|
8 |
-
MODEL_DIR = "/home/user/model"
|
9 |
# MODEL_NAME = "Giang07/Llama-2-7b-chat-QLoRa"
|
10 |
# MODEL_NAME = "meta-llama/Meta-Llama-3-8B"
|
11 |
-
MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct"
|
12 |
|
13 |
# Load environment variables from .env file
|
14 |
-
load_dotenv()
|
15 |
|
16 |
# Now you can use the token
|
17 |
-
api_token = os.getenv('HF_TOKEN')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
def load_model():
|
20 |
"""
|
21 |
Load or download the model and tokenizer.
|
22 |
"""
|
23 |
-
config_path = os.path.join(MODEL_DIR, "config.json")
|
24 |
-
if not os.path.exists(config_path):
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
else:
|
33 |
-
|
34 |
-
|
|
|
|
|
35 |
|
36 |
return model, tokenizer
|
37 |
|
@@ -39,17 +50,38 @@ def create_pipeline(model, tokenizer):
|
|
39 |
"""
|
40 |
Create a text-generation pipeline.
|
41 |
"""
|
42 |
-
hf_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
return hf_pipeline
|
44 |
|
45 |
def generate_text(hf_pipeline, input_text):
|
46 |
"""
|
47 |
Generate text using the Hugging Face pipeline.
|
48 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
prompt_template = PromptTemplate(
|
50 |
input_variables=["input_text"],
|
51 |
-
template="
|
52 |
)
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import os
|
2 |
+
# from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
+
# from langchain.llms import HuggingFacePipeline
|
4 |
+
# from langchain import PromptTemplate, LLMChain
|
5 |
+
# from dotenv import load_dotenv
|
6 |
+
|
7 |
|
8 |
# Define the model directory and name
|
9 |
+
# MODEL_DIR = "/home/user/model"
|
10 |
# MODEL_NAME = "Giang07/Llama-2-7b-chat-QLoRa"
|
11 |
# MODEL_NAME = "meta-llama/Meta-Llama-3-8B"
|
12 |
+
# MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct"
|
13 |
|
14 |
# Load environment variables from .env file
|
15 |
+
# load_dotenv()
|
16 |
|
17 |
# Now you can use the token
|
18 |
+
# api_token = os.getenv('HF_TOKEN')
|
19 |
+
|
20 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
21 |
+
from langchain.llms import HuggingFacePipeline
|
22 |
+
from langchain.prompts import PromptTemplate
|
23 |
+
from langchain.schemas import LangChainConfig
|
24 |
+
|
25 |
+
|
26 |
+
MODEL_NAME = "microsoft/Phi-3-mini-4k-instruct"
|
27 |
|
28 |
def load_model():
|
29 |
"""
|
30 |
Load or download the model and tokenizer.
|
31 |
"""
|
32 |
+
# config_path = os.path.join(MODEL_DIR, "config.json")
|
33 |
+
# if not os.path.exists(config_path):
|
34 |
+
# os.makedirs(MODEL_DIR, exist_ok=True)
|
35 |
+
# # model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, use_auth_token=api_token)
|
36 |
+
# model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
37 |
+
# model.save_pretrained(MODEL_DIR)
|
38 |
+
# # tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_auth_token=api_token)
|
39 |
+
# tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
40 |
+
# tokenizer.save_pretrained(MODEL_DIR)
|
41 |
+
# else:
|
42 |
+
# model = AutoModelForCausalLM.from_pretrained(MODEL_DIR)
|
43 |
+
# tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)
|
44 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
45 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
46 |
|
47 |
return model, tokenizer
|
48 |
|
|
|
50 |
"""
|
51 |
Create a text-generation pipeline.
|
52 |
"""
|
53 |
+
# hf_pipeline = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
54 |
+
# Initialize the Hugging Face pipeline
|
55 |
+
hf_pipeline = HuggingFacePipeline(
|
56 |
+
model=model,
|
57 |
+
tokenizer=tokenizer,
|
58 |
+
hf_pipeline_kwargs={"return_full_text": False} # Adjust based on the function you need
|
59 |
+
)
|
60 |
return hf_pipeline
|
61 |
|
62 |
def generate_text(hf_pipeline, input_text):
|
63 |
"""
|
64 |
Generate text using the Hugging Face pipeline.
|
65 |
+
# """
|
66 |
+
# prompt_template = PromptTemplate(
|
67 |
+
# input_variables=["input_text"],
|
68 |
+
# template="Translate the following English text to French: {input_text}"
|
69 |
+
# )
|
70 |
+
# llm = HuggingFacePipeline(pipeline=hf_pipeline)
|
71 |
+
# llm_chain = LLMChain(prompt_template=prompt_template, llm=llm)
|
72 |
+
# return llm_chain.run({"input_text": input_text})
|
73 |
+
# Define a prompt template if needed (this is an example, adjust accordingly)
|
74 |
+
|
75 |
prompt_template = PromptTemplate(
|
76 |
input_variables=["input_text"],
|
77 |
+
template="Please summarize the following text: {input_text}"
|
78 |
)
|
79 |
+
# Configuration for LangChain (adjust max_tokens, temperature, etc., as needed)
|
80 |
+
config = LangChainConfig(max_tokens=50, temperature=0.7)
|
81 |
+
|
82 |
+
# Example input text for the task
|
83 |
+
# input_text = "LangChain is a library that facilitates the development of applications using language models."
|
84 |
+
|
85 |
+
# Run the LangChain pipeline
|
86 |
+
output = hf_pipeline.generate(input_text, prompt_template=prompt_template, config=config)
|
87 |
+
return output
|