Spaces:
Sleeping
Sleeping
Download models.py from Afifi00/n8n_Workflow: direct link, hf CLI and curl.
- Browser
- Download file 1.72 kB
-
https://huggingface.co/spaces/Afifi00/n8n_Workflow/resolve/main/models.py
- Command line
-
hf download hf://spaces/Afifi00/n8n_Workflow/models.py
-
curl -L -o models.py https://huggingface.co/spaces/Afifi00/n8n_Workflow/resolve/main/models.py
1.72 kB
| from huggingface_hub import hf_hub_download | |
| from llama_cpp import Llama, LlamaState | |
| from langchain.text_splitter import RecursiveCharacterTextSplitter | |
| from langchain.embeddings import HuggingFaceEmbeddings | |
| from langchain.vectorstores import Chroma | |
| import multiprocessing | |
| import os | |
| import re | |
| def load_llm(): | |
| model_repo = "TheBloke/Mistral-7B-Instruct-v0.1-GGUF" | |
| model_file = "mistral-7b-instruct-v0.1.Q4_K_M.gguf" | |
| model_path = hf_hub_download(repo_id=model_repo, filename=model_file, resume_download=True) | |
| n_threads = min(8, multiprocessing.cpu_count()) | |
| return Llama(model_path=model_path, n_ctx=3000, n_threads=n_threads, n_gpu_layers=35) | |
| text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder( | |
| encoding_name="cl100k_base", | |
| chunk_size=512, | |
| chunk_overlap=50 | |
| ) | |
| embedding_model = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2") | |
| prompt_template = """ | |
| You are a senior automation engineer. Given a user's request, generate a valid n8n JSON workflow with minimal dummy credentials and full working nodes.Create a comprehensive n8n workflow . | |
| {context_block} | |
| Request: {prompt} | |
| Only output the JSON. | |
| """ | |
| def extract_json_block(text): | |
| match = re.search(r'\{.*\}', text, re.DOTALL) | |
| return match.group(0).strip() if match else None | |
| def save_model_state(llm, filename="model_state.bin"): | |
| state = llm.save_state() | |
| with open(filename, "wb") as f: | |
| f.write(state.llama_state) | |
| return filename | |
| def load_model_state(llm, filename="model_state.bin"): | |
| if os.path.exists(filename): | |
| with open(filename, "rb") as f: | |
| state_data = f.read() | |
| llm.load_state(LlamaState(llama_state=state_data)) | |
| return True | |
| return False | |