Entz commited on
Commit
31011ed
·
verified ·
1 Parent(s): f771c73

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +23 -6
  2. dockerfile +10 -10
app.py CHANGED
@@ -8,6 +8,7 @@ from dotenv import load_dotenv
8
  import asyncio
9
  import httpx
10
  import time
 
11
 
12
  # Load environment variables
13
  load_dotenv()
@@ -16,6 +17,18 @@ load_dotenv()
16
  st.title("Math Reasoning Chatbot")
17
  st.write("Select a provider and chat with the bot to solve math problems!")
18
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # Function to check if the Ollama server is running with retries
20
  async def check_ollama_health(max_retries=5, retry_delay=5):
21
  for attempt in range(max_retries):
@@ -44,14 +57,16 @@ def setup_client(provider):
44
  display_model = "OpenAI (gpt-4o-mini)"
45
  elif provider == "ollama":
46
  from openai import AsyncOpenAI as OllamaClient
47
- # Check if Ollama server is running with retries
48
- if not asyncio.run(check_ollama_health()):
49
- st.error("Ollama server is not running or not accessible at http://localhost:11434 after multiple attempts. Please try again later or select a different provider.")
50
- return None, None, None
 
 
51
  client = instructor.from_openai(
52
  OllamaClient(base_url="http://localhost:11434/v1", api_key="ollama"), mode=instructor.Mode.JSON
53
  )
54
- model = "llama3.2:1b" # Updated to use the smaller model
55
  display_model = "Ollama (llama3.2:1b)"
56
  else:
57
  st.error(f"Unsupported provider: {provider}")
@@ -91,7 +106,9 @@ if "agent" not in st.session_state or st.session_state.get("current_model") != m
91
  client=client,
92
  model=model,
93
  system_prompt_generator=system_prompt_generator,
94
- memory=st.session_state.memory,
 
 
95
  system_role="developer",
96
  ))
97
  st.session_state.current_model = model # Track the current model to detect changes
 
8
  import asyncio
9
  import httpx
10
  import time
11
+ import subprocess
12
 
13
  # Load environment variables
14
  load_dotenv()
 
17
  st.title("Math Reasoning Chatbot")
18
  st.write("Select a provider and chat with the bot to solve math problems!")
19
 
20
+ # Function to start Ollama server if not already running
21
+ def start_ollama_server():
22
+ try:
23
+ # Check if Ollama is already running
24
+ response = httpx.get("http://localhost:11434/v1")
25
+ if response.status_code == 200:
26
+ return True
27
+ except httpx.RequestError:
28
+ # Start Ollama server in the background
29
+ subprocess.Popen(["ollama", "serve"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30
+ return False
31
+
32
  # Function to check if the Ollama server is running with retries
33
  async def check_ollama_health(max_retries=5, retry_delay=5):
34
  for attempt in range(max_retries):
 
57
  display_model = "OpenAI (gpt-4o-mini)"
58
  elif provider == "ollama":
59
  from openai import AsyncOpenAI as OllamaClient
60
+ # Start Ollama server if not running
61
+ if not start_ollama_server():
62
+ # Wait and check health
63
+ if not asyncio.run(check_ollama_health()):
64
+ st.error("Failed to start Ollama server or connect to it at http://localhost:11434 after multiple attempts.")
65
+ return None, None, None
66
  client = instructor.from_openai(
67
  OllamaClient(base_url="http://localhost:11434/v1", api_key="ollama"), mode=instructor.Mode.JSON
68
  )
69
+ model = "llama3.2:1b"
70
  display_model = "Ollama (llama3.2:1b)"
71
  else:
72
  st.error(f"Unsupported provider: {provider}")
 
106
  client=client,
107
  model=model,
108
  system_prompt_generator=system_prompt_generator,
109
+ memory ~
110
+
111
+ =st.session_state.memory,
112
  system_role="developer",
113
  ))
114
  st.session_state.current_model = model # Track the current model to detect changes
dockerfile CHANGED
@@ -4,18 +4,18 @@ FROM python:3.10-slim
4
  # Set working directory
5
  WORKDIR /app
6
 
7
- # Copy the requirements file
8
- COPY requirements.txt .
9
-
10
- # Install dependencies
11
- RUN pip install --no-cache-dir -r requirements.txt
12
 
13
  # Install Ollama
14
- RUN apt-get update && apt-get install -y curl
15
  RUN curl -fsSL https://ollama.com/install.sh | sh
16
 
17
- # Pre-download the llama3.2:1b model and debug
18
- RUN ollama pull llama3.2:1b && ollama list
 
 
 
 
19
 
20
  # Copy the app code
21
  COPY app.py .
@@ -23,5 +23,5 @@ COPY app.py .
23
  # Expose the Streamlit port
24
  EXPOSE 8501
25
 
26
- # Start Ollama in the background, log output, wait 10 seconds, then run Streamlit
27
- CMD ollama serve > ollama.log 2>&1 & sleep 10 && streamlit run app.py --server.port 8501 --server.address 0.0.0.0
 
4
  # Set working directory
5
  WORKDIR /app
6
 
7
+ # Install system dependencies
8
+ RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
 
 
 
9
 
10
  # Install Ollama
 
11
  RUN curl -fsSL https://ollama.com/install.sh | sh
12
 
13
+ # Pre-download the llama3.2:1b model
14
+ RUN ollama pull llama3.2:1b
15
+
16
+ # Copy the requirements file and install Python dependencies
17
+ COPY requirements.txt .
18
+ RUN pip install --no-cache-dir -r requirements.txt
19
 
20
  # Copy the app code
21
  COPY app.py .
 
23
  # Expose the Streamlit port
24
  EXPOSE 8501
25
 
26
+ # Run Streamlit (Ollama will be started by the app code)
27
+ CMD ["streamlit", "run", "app.py", "--server.port", "8501", "--server.address", "0.0.0.0"]