melissalau commited on
Commit
14c8701
·
1 Parent(s): 30a6991

edited app.py and Dockerfile

Browse files
Files changed (2) hide show
  1. Dockerfile +12 -30
  2. app.py +12 -19
Dockerfile CHANGED
@@ -1,33 +1,17 @@
1
- # Use a slim Python base image
2
- FROM python:3.13.5-slim
3
 
4
- # Set the working directory
5
- WORKDIR /app
6
 
7
- # Install system dependencies
8
- # DEBIAN_FRONTEND is set to noninteractive to prevent prompts
9
- ENV DEBIAN_FRONTEND=noninteractive
10
- RUN apt-get update && apt-get install -y --no-install-recommends \
11
- build-essential \
12
- curl \
13
- git \
14
- ca-certificates \
15
  && rm -rf /var/lib/apt/lists/*
16
 
17
- # --- Manual Ollama Installation ---
18
- # This is the new, robust method.
19
- # Download the latest Linux binary from Ollama's GitHub releases
20
- # The URL below is a generic way to get the latest release
21
- RUN curl -L https://github.com/ollama/ollama/releases/latest/download/ollama-linux-amd64.tgz \
22
- -o /tmp/ollama-linux-amd64.tgz
23
-
24
- # Extract the tarball and place the binary directly in /usr/local/bin/
25
- RUN tar -xzf /tmp/ollama-linux-amd64.tgz -C /usr/local/bin/
26
-
27
- # Remove the temporary file
28
- RUN rm /tmp/ollama-linux-amd64.tgz
29
-
30
  # Copy your application files
 
31
  COPY requirements.txt ./
32
  COPY app.py ./
33
 
@@ -37,8 +21,6 @@ RUN pip3 install -r requirements.txt
37
  # Expose the Streamlit port
38
  EXPOSE 8501
39
 
40
- # Healthcheck for Streamlit (useful for Hugging Face Spaces)
41
- HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
42
-
43
- # Entrypoint to run your Streamlit app
44
- ENTRYPOINT ["streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
 
1
+ # Use the official Ollama image as the base
2
+ FROM ollama/ollama
3
 
4
+ # Switch to the root user to install system and Python packages
5
+ USER root
6
 
7
+ # Install Python and pip (Ollama image doesn't have it by default)
8
+ RUN apt-get update && apt-get install -y \
9
+ python3 \
10
+ python3-pip \
 
 
 
 
11
  && rm -rf /var/lib/apt/lists/*
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # Copy your application files
14
+ WORKDIR /app
15
  COPY requirements.txt ./
16
  COPY app.py ./
17
 
 
21
  # Expose the Streamlit port
22
  EXPOSE 8501
23
 
24
+ # --- Ollama will be running on container startup ---
25
+ # The entrypoint will now start the Streamlit app
26
+ ENTRYPOINT ["python3", "-m", "streamlit", "run", "app.py", "--server.port=8501", "--server.address=0.0.0.0"]
 
 
app.py CHANGED
@@ -4,27 +4,21 @@ 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 subprocess
8
- import time
9
 
10
  # --- Ollama Setup ---
11
- def start_ollama_and_pull_models():
12
- """Starts the Ollama server and pulls all required models."""
13
- st.write("Starting Ollama server...")
14
-
15
- # Use the absolute path to the ollama binary
16
- subprocess.Popen(['/usr/local/bin/ollama', 'serve'])
17
- time.sleep(5) # Give the server time to start
18
-
19
- # Pull all models at once
20
  models_to_pull = ["phi3:mini", "deepseek-coder:1.3b"]
21
  st.write(f"Pulling models: {', '.join(models_to_pull)}...")
 
 
 
22
  for model in models_to_pull:
23
  try:
24
- # Use the absolute path for pulling the model too
25
- subprocess.run(['/usr/local/bin/ollama', 'pull', model], check=True)
26
  st.success(f"Model '{model}' pulled successfully.")
27
- except subprocess.CalledProcessError as e:
28
  st.error(f"Failed to pull model '{model}': {e}")
29
 
30
 
@@ -32,10 +26,10 @@ def start_ollama_and_pull_models():
32
  st.set_page_config(layout="wide")
33
  st.title("My Local Chatbot")
34
 
35
- if "ollama_started" not in st.session_state:
36
  with st.spinner("Setting up the local LLM server... this may take a moment."):
37
- start_ollama_and_pull_models()
38
- st.session_state.ollama_started = True
39
 
40
  # --- Sidebar Inputs ---
41
  st.sidebar.header("Settings")
@@ -50,8 +44,7 @@ TOP_P = st.sidebar.slider("Top-p (nucleus sampling)", 0.0, 1.0, 0.9, 0.05)
50
  TOP_K = st.sidebar.slider("Top-k", 0, 100, 40, 5)
51
  MAX_TOKENS = st.sidebar.number_input("Max Tokens", min_value=256, max_value=16384, value=2048, step=256)
52
 
53
- # ... (rest of the code is the same) ...
54
-
55
  if "chat_history" not in st.session_state:
56
  st.session_state.chat_history = []
57
  if "memory" not in st.session_state:
 
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
  # --- Ollama Setup ---
10
+ def pull_models():
11
+ """Pulls all required models."""
 
 
 
 
 
 
 
12
  models_to_pull = ["phi3:mini", "deepseek-coder:1.3b"]
13
  st.write(f"Pulling models: {', '.join(models_to_pull)}...")
14
+
15
+ # Use the official ollama client to pull models.
16
+ # The `ollama` executable is now in the PATH, so the call works.
17
  for model in models_to_pull:
18
  try:
19
+ os.system(f"ollama pull {model}")
 
20
  st.success(f"Model '{model}' pulled successfully.")
21
+ except Exception as e:
22
  st.error(f"Failed to pull model '{model}': {e}")
23
 
24
 
 
26
  st.set_page_config(layout="wide")
27
  st.title("My Local Chatbot")
28
 
29
+ if "ollama_pulled" not in st.session_state:
30
  with st.spinner("Setting up the local LLM server... this may take a moment."):
31
+ pull_models()
32
+ st.session_state.ollama_pulled = True
33
 
34
  # --- Sidebar Inputs ---
35
  st.sidebar.header("Settings")
 
44
  TOP_K = st.sidebar.slider("Top-k", 0, 100, 40, 5)
45
  MAX_TOKENS = st.sidebar.number_input("Max Tokens", min_value=256, max_value=16384, value=2048, step=256)
46
 
47
+ # ... (remaining code from your app.py) ...
 
48
  if "chat_history" not in st.session_state:
49
  st.session_state.chat_history = []
50
  if "memory" not in st.session_state: