Maximofn commited on
Commit
4aaa328
·
1 Parent(s): 34aabf7

Configure cache directories and add system prompt for local model

Browse files

- Set specific cache directories for Transformers and HuggingFace libraries
- Add system prompt to guide model's language-adaptive behavior
- Modify call_model function to include system prompt in message generation
- Improve model initialization with explicit cache directory specification

Files changed (1) hide show
  1. app.py +12 -3
app.py CHANGED
@@ -10,6 +10,10 @@ import os
10
  from dotenv import load_dotenv
11
  load_dotenv()
12
 
 
 
 
 
13
  # HuggingFace token
14
  HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN", os.getenv("HUGGINGFACE_TOKEN"))
15
  print(f"Token HuggingFace: {HUGGINGFACE_TOKEN}")
@@ -21,12 +25,14 @@ MODEL_NAME = "HuggingFaceTB/SmolLM2-1.7B-Instruct"
21
  print(f"Loading model {MODEL_NAME} locally...")
22
  tokenizer = AutoTokenizer.from_pretrained(
23
  MODEL_NAME,
24
- token=HUGGINGFACE_TOKEN # Add token for authentication
 
25
  )
26
  model = AutoModelForCausalLM.from_pretrained(
27
  MODEL_NAME,
28
  device_map="auto",
29
- token=HUGGINGFACE_TOKEN # Add token for authentication
 
30
  )
31
 
32
  # Create a pipeline to facilitate generation
@@ -52,8 +58,11 @@ def call_model(state: MessagesState):
52
  Returns:
53
  dict: A dictionary containing the generated text and the thread ID
54
  """
 
 
 
55
  # Convert LangChain messages to a format that the local model can understand
56
- prompt = ""
57
  for msg in state["messages"]:
58
  if isinstance(msg, HumanMessage):
59
  prompt += f"User: {msg.content}\n"
 
10
  from dotenv import load_dotenv
11
  load_dotenv()
12
 
13
+ # Configurar directorio de caché en un lugar con permisos
14
+ os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
15
+ os.environ["HF_HOME"] = "/tmp/hf_home"
16
+
17
  # HuggingFace token
18
  HUGGINGFACE_TOKEN = os.environ.get("HUGGINGFACE_TOKEN", os.getenv("HUGGINGFACE_TOKEN"))
19
  print(f"Token HuggingFace: {HUGGINGFACE_TOKEN}")
 
25
  print(f"Loading model {MODEL_NAME} locally...")
26
  tokenizer = AutoTokenizer.from_pretrained(
27
  MODEL_NAME,
28
+ token=HUGGINGFACE_TOKEN, # Add token for authentication
29
+ cache_dir="/tmp/transformers_cache" # Especificar directorio de caché
30
  )
31
  model = AutoModelForCausalLM.from_pretrained(
32
  MODEL_NAME,
33
  device_map="auto",
34
+ token=HUGGINGFACE_TOKEN, # Add token for authentication
35
+ cache_dir="/tmp/transformers_cache" # Especificar directorio de caché
36
  )
37
 
38
  # Create a pipeline to facilitate generation
 
58
  Returns:
59
  dict: A dictionary containing the generated text and the thread ID
60
  """
61
+ # System prompt to guide the model's behavior
62
+ system_prompt = "You are a friendly Chatbot. Always reply in the language in which the user is writing to you."
63
+
64
  # Convert LangChain messages to a format that the local model can understand
65
+ prompt = f"System: {system_prompt}\n\n"
66
  for msg in state["messages"]:
67
  if isinstance(msg, HumanMessage):
68
  prompt += f"User: {msg.content}\n"