Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -21,9 +21,18 @@ import tqdm
|
|
21 |
import accelerate
|
22 |
|
23 |
|
|
|
24 |
|
25 |
llm_name0 = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
list_llm_simple = [os.path.basename(llm) for llm in list_llm]
|
28 |
|
29 |
# Load PDF document and create doc splits
|
@@ -69,13 +78,59 @@ def load_db():
|
|
69 |
# Initialize langchain LLM chain
|
70 |
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
|
71 |
progress(0.1, desc="Initializing HF tokenizer...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
progress(0.5, desc="Initializing HF Hub...")
|
|
|
|
|
73 |
# URL: https://github.com/langchain-ai/langchain/issues/6080
|
74 |
if llm_model == "mistralai/Mixtral-8x7B-Instruct-v0.1":
|
75 |
llm = HuggingFaceHub(
|
76 |
repo_id=llm_model,
|
77 |
model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "load_in_8bit": True}
|
78 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
|
80 |
progress(0.75, desc="Defining buffer memory...")
|
81 |
memory = ConversationBufferMemory(
|
@@ -256,4 +311,5 @@ def demo():
|
|
256 |
|
257 |
|
258 |
if __name__ == "__main__":
|
259 |
-
demo()
|
|
|
|
21 |
import accelerate
|
22 |
|
23 |
|
24 |
+
# default_persist_directory = './chroma_HF/'
|
25 |
|
26 |
llm_name0 = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
27 |
+
llm_name1 = "mistralai/Mistral-7B-Instruct-v0.2"
|
28 |
+
llm_name2 = "mistralai/Mistral-7B-Instruct-v0.1"
|
29 |
+
llm_name3 = "meta-llama/Llama-2-7b-chat-hf"
|
30 |
+
llm_name4 = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
31 |
+
llm_name5 = "microsoft/phi-2"
|
32 |
+
llm_name6 = "mosaicml/mpt-7b-instruct"
|
33 |
+
llm_name7 = "tiiuae/falcon-7b-instruct"
|
34 |
+
llm_name8 = "google/flan-t5-xxl"
|
35 |
+
list_llm = [llm_name0, llm_name1, llm_name2, llm_name3, llm_name4, llm_name5, llm_name6, llm_name7, llm_name8]
|
36 |
list_llm_simple = [os.path.basename(llm) for llm in list_llm]
|
37 |
|
38 |
# Load PDF document and create doc splits
|
|
|
78 |
# Initialize langchain LLM chain
|
79 |
def initialize_llmchain(llm_model, temperature, max_tokens, top_k, vector_db, progress=gr.Progress()):
|
80 |
progress(0.1, desc="Initializing HF tokenizer...")
|
81 |
+
# HuggingFacePipeline uses local model
|
82 |
+
# Note: it will download model locally...
|
83 |
+
# tokenizer=AutoTokenizer.from_pretrained(llm_model)
|
84 |
+
# progress(0.5, desc="Initializing HF pipeline...")
|
85 |
+
# pipeline=transformers.pipeline(
|
86 |
+
# "text-generation",
|
87 |
+
# model=llm_model,
|
88 |
+
# tokenizer=tokenizer,
|
89 |
+
# torch_dtype=torch.bfloat16,
|
90 |
+
# trust_remote_code=True,
|
91 |
+
# device_map="auto",
|
92 |
+
# # max_length=1024,
|
93 |
+
# max_new_tokens=max_tokens,
|
94 |
+
# do_sample=True,
|
95 |
+
# top_k=top_k,
|
96 |
+
# num_return_sequences=1,
|
97 |
+
# eos_token_id=tokenizer.eos_token_id
|
98 |
+
# )
|
99 |
+
# llm = HuggingFacePipeline(pipeline=pipeline, model_kwargs={'temperature': temperature})
|
100 |
+
|
101 |
+
# HuggingFaceHub uses HF inference endpoints
|
102 |
progress(0.5, desc="Initializing HF Hub...")
|
103 |
+
# Use of trust_remote_code as model_kwargs
|
104 |
+
# Warning: langchain issue
|
105 |
# URL: https://github.com/langchain-ai/langchain/issues/6080
|
106 |
if llm_model == "mistralai/Mixtral-8x7B-Instruct-v0.1":
|
107 |
llm = HuggingFaceHub(
|
108 |
repo_id=llm_model,
|
109 |
model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "load_in_8bit": True}
|
110 |
)
|
111 |
+
elif llm_model == "microsoft/phi-2":
|
112 |
+
raise gr.Error("phi-2 model requires 'trust_remote_code=True', currently not supported by langchain HuggingFaceHub...")
|
113 |
+
llm = HuggingFaceHub(
|
114 |
+
repo_id=llm_model,
|
115 |
+
model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
|
116 |
+
)
|
117 |
+
elif llm_model == "TinyLlama/TinyLlama-1.1B-Chat-v1.0":
|
118 |
+
llm = HuggingFaceHub(
|
119 |
+
repo_id=llm_model,
|
120 |
+
model_kwargs={"temperature": temperature, "max_new_tokens": 250, "top_k": top_k}
|
121 |
+
)
|
122 |
+
elif llm_model == "meta-llama/Llama-2-7b-chat-hf":
|
123 |
+
raise gr.Error("Llama-2-7b-chat-hf model requires a Pro subscription...")
|
124 |
+
llm = HuggingFaceHub(
|
125 |
+
repo_id=llm_model,
|
126 |
+
model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k}
|
127 |
+
)
|
128 |
+
else:
|
129 |
+
llm = HuggingFaceHub(
|
130 |
+
repo_id=llm_model,
|
131 |
+
# model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k, "trust_remote_code": True, "torch_dtype": "auto"}
|
132 |
+
model_kwargs={"temperature": temperature, "max_new_tokens": max_tokens, "top_k": top_k}
|
133 |
+
)
|
134 |
|
135 |
progress(0.75, desc="Defining buffer memory...")
|
136 |
memory = ConversationBufferMemory(
|
|
|
311 |
|
312 |
|
313 |
if __name__ == "__main__":
|
314 |
+
demo()
|
315 |
+
|