lalanikarim commited on
Commit
1e62eb5
1 Parent(s): c7dfe89

added hugging face hub for model download and caching

Browse files
Files changed (2) hide show
  1. main.py +6 -16
  2. requirements.txt +1 -0
main.py CHANGED
@@ -1,6 +1,4 @@
1
  import streamlit as st
2
- import os
3
- from urllib.request import urlretrieve
4
  from operator import itemgetter
5
  from langchain.llms import LlamaCpp
6
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
@@ -8,6 +6,7 @@ from langchain.memory import ConversationBufferMemory
8
  # from langchain.callbacks.manager import CallbackManager
9
  from langchain.callbacks.base import BaseCallbackHandler
10
  from langchain.schema.runnable import RunnablePassthrough, RunnableLambda
 
11
 
12
 
13
  # StreamHandler to intercept streaming output from the LLM.
@@ -38,21 +37,12 @@ def create_chain(system_prompt):
38
  # responses in real time.
39
  # callback_manager = CallbackManager([stream_handler])
40
 
41
- # url and model name to download
42
- (url, model_name) = (
43
- "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_0.gguf",
44
- "mistral-7b-instruct-v0.1.Q4_0.gguf")
45
 
46
- # create models folder if missing
47
- if not os.path.exists("models"):
48
- os.mkdir("models")
49
-
50
- # set model_path before providing to LlamaCpp
51
- model_path = "models/" + model_name
52
-
53
- # download model to model_path if missing
54
- if not os.path.exists(model_path):
55
- urlretrieve(url, model_path)
56
 
57
  # initialize LlamaCpp llm model
58
  llm = LlamaCpp(
 
1
  import streamlit as st
 
 
2
  from operator import itemgetter
3
  from langchain.llms import LlamaCpp
4
  from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
 
6
  # from langchain.callbacks.manager import CallbackManager
7
  from langchain.callbacks.base import BaseCallbackHandler
8
  from langchain.schema.runnable import RunnablePassthrough, RunnableLambda
9
+ from huggingface_hub import hf_hub_download
10
 
11
 
12
  # StreamHandler to intercept streaming output from the LLM.
 
37
  # responses in real time.
38
  # callback_manager = CallbackManager([stream_handler])
39
 
40
+ (repo_id, model_file_name) = ("TheBloke/Mistral-7B-Instruct-v0.1-GGUF",
41
+ "mistral-7b-instruct-v0.1.Q5_0.gguf")
 
 
42
 
43
+ model_path = hf_hub_download(repo_id=repo_id,
44
+ filename=model_file_name,
45
+ repo_type="model")
 
 
 
 
 
 
 
46
 
47
  # initialize LlamaCpp llm model
48
  llm = LlamaCpp(
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  langchain==0.0.321
2
  llama_cpp_python==0.2.11
 
3
  streamlit==1.27.2
 
1
  langchain==0.0.321
2
  llama_cpp_python==0.2.11
3
+ huggingface_hub==0.18.0
4
  streamlit==1.27.2