melissalau commited on
Commit
4e7895f
·
1 Parent(s): 83d5914

edited app.py and Dockerfile

Browse files
Files changed (2) hide show
  1. Dockerfile +2 -13
  2. app.py +27 -13
Dockerfile CHANGED
@@ -1,6 +1,6 @@
1
  FROM python:3.13.5-slim
2
 
3
- # Install system dependencies for ctransformers
4
  RUN apt-get update && apt-get install -y \
5
  build-essential \
6
  libssl-dev \
@@ -9,19 +9,8 @@ RUN apt-get update && apt-get install -y \
9
  git \
10
  && rm -rf /var/lib/apt/lists/*
11
 
12
- # Create a directory for the model files
13
  WORKDIR /app
14
- RUN mkdir -p /app/models
15
-
16
- # Download the tinyllama model during the build
17
- RUN curl -L https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v0.4-GGUF/resolve/main/tinyllama-1.1b-chat-v0.4.Q4_0.gguf \
18
- -o /app/models/tinyllama.gguf
19
-
20
- # Download the Deepseek model during the build
21
- RUN curl -L https://huggingface.co/TheBloke/deepseek-coder-1.3B-base-GGUF/resolve/main/deepseek-coder-1.3b-base-q4_K_M.gguf \
22
- -o /app/models/deepseek-coder-1.3b.gguf
23
-
24
- # Copy your application files
25
  COPY requirements.txt ./
26
  COPY app.py ./
27
 
 
1
  FROM python:3.13.5-slim
2
 
3
+ # Install system dependencies
4
  RUN apt-get update && apt-get install -y \
5
  build-essential \
6
  libssl-dev \
 
9
  git \
10
  && rm -rf /var/lib/apt/lists/*
11
 
12
+ # Set the work directory and copy your application files
13
  WORKDIR /app
 
 
 
 
 
 
 
 
 
 
 
14
  COPY requirements.txt ./
15
  COPY app.py ./
16
 
app.py CHANGED
@@ -4,41 +4,55 @@ from langchain.memory import ConversationBufferMemory
4
  from langchain.memory.chat_message_histories import ChatMessageHistory
5
  from langchain.prompts import PromptTemplate
6
  from langchain_core.runnables import RunnableSequence
 
7
  import os
8
 
9
  # --- Model Definitions ---
10
- # A dictionary mapping display names to model file paths and types
11
  MODEL_MAP = {
12
  "TinyLlama (1.1B)": {
13
- "path": "/app/models/tinyllama.gguf",
 
14
  "type": "llama"
15
  },
16
  "Deepseek-Coder (1.3B)": {
17
- "path": "/app/models/deepseek-coder-1.3b.gguf",
 
18
  "type": "deepseek"
19
  }
20
  }
21
 
22
- # ----------------- Streamlit UI and Logic -----------------
23
- st.set_page_config(layout="wide")
24
- st.title("My Local Chatbot")
25
-
26
- # Sidebar Inputs
27
- st.sidebar.header("Settings")
28
- selected_model_name = st.sidebar.selectbox("Choose a Model", list(MODEL_MAP.keys()))
29
 
30
- # Use st.cache_resource to load the LLM once
31
  @st.cache_resource
32
  def load_llm(model_name):
33
- st.write(f"Loading the '{model_name}' model... this may take a moment.")
34
  model_info = MODEL_MAP[model_name]
 
 
 
 
 
35
  llm = CTransformers(
36
- model=model_info["path"],
37
  model_type=model_info["type"],
38
  config={'max_new_tokens': 2048, 'temperature': 0.7}
39
  )
40
  return llm
41
 
 
 
 
 
 
 
 
 
42
  # Load the selected model
43
  llm = load_llm(selected_model_name)
44
  st.success(f"Model '{selected_model_name}' loaded successfully!")
 
4
  from langchain.memory.chat_message_histories import ChatMessageHistory
5
  from langchain.prompts import PromptTemplate
6
  from langchain_core.runnables import RunnableSequence
7
+ from huggingface_hub import hf_hub_download
8
  import os
9
 
10
  # --- Model Definitions ---
11
+ # A dictionary mapping display names to model info
12
  MODEL_MAP = {
13
  "TinyLlama (1.1B)": {
14
+ "repo_id": "TheBloke/TinyLlama-1.1B-Chat-v0.4-GGUF",
15
+ "filename": "tinyllama-1.1b-chat-v0.4.Q4_0.gguf",
16
  "type": "llama"
17
  },
18
  "Deepseek-Coder (1.3B)": {
19
+ "repo_id": "TheBloke/deepseek-coder-1.3B-base-GGUF",
20
+ "filename": "deepseek-coder-1.3b-base-q4_K_M.gguf",
21
  "type": "deepseek"
22
  }
23
  }
24
 
25
+ # --- Model Loading ---
26
+ @st.cache_resource
27
+ def download_model_from_hub(repo_id, filename):
28
+ st.write(f"Downloading model '{filename}' from Hugging Face Hub...")
29
+ model_path = hf_hub_download(repo_id=repo_id, filename=filename)
30
+ return model_path
 
31
 
 
32
  @st.cache_resource
33
  def load_llm(model_name):
34
+ """Loads the model once and caches it."""
35
  model_info = MODEL_MAP[model_name]
36
+
37
+ # Download the model file first
38
+ model_path = download_model_from_hub(model_info["repo_id"], model_info["filename"])
39
+
40
+ # Now load the model using the local file path
41
  llm = CTransformers(
42
+ model=model_path,
43
  model_type=model_info["type"],
44
  config={'max_new_tokens': 2048, 'temperature': 0.7}
45
  )
46
  return llm
47
 
48
+ # ----------------- Streamlit UI and Logic -----------------
49
+ st.set_page_config(layout="wide")
50
+ st.title("My Local Chatbot")
51
+
52
+ # Sidebar Inputs
53
+ st.sidebar.header("Settings")
54
+ selected_model_name = st.sidebar.selectbox("Choose a Model", list(MODEL_MAP.keys()))
55
+
56
  # Load the selected model
57
  llm = load_llm(selected_model_name)
58
  st.success(f"Model '{selected_model_name}' loaded successfully!")