Entz commited on
Commit
9fbe5ee
·
verified ·
1 Parent(s): 7149301

Upload 2 files

Browse files
Files changed (2) hide show
  1. Dockerfile +7 -9
  2. app.py +30 -15
Dockerfile CHANGED
@@ -1,5 +1,5 @@
1
- # Use an official Python runtime as the base image
2
- FROM python:3.11-slim
3
 
4
  # Set working directory
5
  WORKDIR /app
@@ -13,17 +13,15 @@ RUN apt-get update && apt-get install -y \
13
  # Install Ollama
14
  RUN curl -fsSL https://ollama.com/install.sh | sh
15
 
16
- ########################################################
17
- # Pull llama3 model during build to avoid runtime delays
18
  RUN ollama serve & \
19
  until curl -s http://localhost:11434 > /dev/null; do \
20
  echo 'Waiting for Ollama...'; sleep 1; \
21
  done && \
22
  ollama pull llama3 && \
 
23
  ollama list > /app/models.txt && \
24
  cat /app/models.txt
25
- ########################################################
26
-
27
 
28
  # Copy requirements file first (optimization for caching)
29
  COPY requirements.txt .
@@ -31,7 +29,7 @@ COPY requirements.txt .
31
  # Install Python dependencies
32
  RUN pip install --no-cache-dir -r requirements.txt
33
 
34
- # Copy only necessary application files (exclude .env)
35
  COPY app.py .
36
 
37
  # Expose the port Hugging Face Spaces expects
@@ -41,5 +39,5 @@ EXPOSE 7860
41
  ENV OLLAMA_HOST=0.0.0.0
42
  ENV OLLAMA_PORT=11434
43
 
44
- # Start Ollama and run Streamlit
45
- CMD bash -c "ollama serve & until curl -s http://localhost:11434 > /dev/null; do echo 'Waiting for Ollama...'; sleep 1; done && streamlit run app.py --server.port 7860 --server.address 0.0.0.0"
 
1
+ # Use an official Python runtime matching Hugging Face's environment
2
+ FROM python:3.10-slim
3
 
4
  # Set working directory
5
  WORKDIR /app
 
13
  # Install Ollama
14
  RUN curl -fsSL https://ollama.com/install.sh | sh
15
 
16
+ # Pre-pull llama3 during build to avoid runtime delays
 
17
  RUN ollama serve & \
18
  until curl -s http://localhost:11434 > /dev/null; do \
19
  echo 'Waiting for Ollama...'; sleep 1; \
20
  done && \
21
  ollama pull llama3 && \
22
+ echo "Model pulled successfully" || echo "Model pull failed" && \
23
  ollama list > /app/models.txt && \
24
  cat /app/models.txt
 
 
25
 
26
  # Copy requirements file first (optimization for caching)
27
  COPY requirements.txt .
 
29
  # Install Python dependencies
30
  RUN pip install --no-cache-dir -r requirements.txt
31
 
32
+ # Copy only the app file
33
  COPY app.py .
34
 
35
  # Expose the port Hugging Face Spaces expects
 
39
  ENV OLLAMA_HOST=0.0.0.0
40
  ENV OLLAMA_PORT=11434
41
 
42
+ # Start Ollama and Streamlit with a more robust wait
43
+ CMD bash -c "ollama serve & sleep 5 && until curl -s http://localhost:11434 > /dev/null; do echo 'Waiting for Ollama...'; sleep 1; done && streamlit run app.py --server.port 7860 --server.address 0.0.0.0"
app.py CHANGED
@@ -6,8 +6,13 @@ from atomic_agents.lib.components.system_prompt_generator import SystemPromptGen
6
  from atomic_agents.agents.base_agent import BaseAgent, BaseAgentConfig, BaseAgentInputSchema, BaseAgentOutputSchema
7
  from dotenv import load_dotenv
8
  import asyncio
 
9
 
10
- # Load environment variables (optional if using Hugging Face Secrets)
 
 
 
 
11
  load_dotenv()
12
 
13
  # Initialize Streamlit app
@@ -20,18 +25,24 @@ def setup_client(provider):
20
  from openai import AsyncOpenAI
21
  api_key = os.getenv("OPENAI_API_KEY")
22
  if not api_key:
23
- st.warning("OpenAI provider unavailable: OPENAI_API_KEY not set. Falling back to Ollama.")
24
- return setup_client("ollama") # Fallback to Ollama
25
  client = instructor.from_openai(AsyncOpenAI(api_key=api_key))
26
  model = "gpt-4o-mini"
27
  display_model = "OpenAI (gpt-4o-mini)"
28
  elif provider == "ollama":
29
  from openai import AsyncOpenAI as OllamaClient
30
- client = instructor.from_openai(
31
- OllamaClient(base_url="http://localhost:11434/v1", api_key="ollama"), mode=instructor.Mode.JSON
32
- )
33
- model = "llama3"
34
- display_model = "Ollama (llama3)"
 
 
 
 
 
 
35
  else:
36
  st.error(f"Unsupported provider: {provider}")
37
  return None, None, None
@@ -50,7 +61,7 @@ system_prompt_generator = SystemPromptGenerator(
50
  )
51
 
52
  # Provider selection
53
- providers_list = ["ollama", "openai"] # Prioritize Ollama since it’s guaranteed to work
54
  selected_provider = st.selectbox("Choose a provider:", providers_list, key="provider_select")
55
 
56
  # Set up client and agent based on the selected provider
@@ -73,7 +84,7 @@ if "agent" not in st.session_state or st.session_state.get("current_model") != m
73
  memory=st.session_state.memory,
74
  system_role="developer",
75
  ))
76
- st.session_state.current_model = model # Track the current model to detect changes
77
 
78
  # Display the selected model
79
  st.markdown(f"**Selected Model:** {st.session_state.display_model}")
@@ -107,11 +118,15 @@ if user_input:
107
  response_container = st.empty()
108
  async def stream_response():
109
  current_response = ""
110
- async for partial_response in st.session_state.agent.run_async(input_schema):
111
- if hasattr(partial_response, "chat_message") and partial_response.chat_message:
112
- if partial_response.chat_message != current_response:
113
- current_response = partial_response.chat_message
114
- response_container.markdown(current_response)
 
 
 
 
115
 
116
  # After streaming completes, add the final response to conversation and memory
117
  st.session_state.conversation.append(("assistant", current_response))
 
6
  from atomic_agents.agents.base_agent import BaseAgent, BaseAgentConfig, BaseAgentInputSchema, BaseAgentOutputSchema
7
  from dotenv import load_dotenv
8
  import asyncio
9
+ import logging
10
 
11
+ # Set up logging
12
+ logging.basicConfig(level=logging.INFO)
13
+ logger = logging.getLogger(__name__)
14
+
15
+ # Load environment variables (optional for Hugging Face Secrets)
16
  load_dotenv()
17
 
18
  # Initialize Streamlit app
 
25
  from openai import AsyncOpenAI
26
  api_key = os.getenv("OPENAI_API_KEY")
27
  if not api_key:
28
+ st.warning("OpenAI unavailable: OPENAI_API_KEY not set. Using Ollama.")
29
+ return setup_client("ollama")
30
  client = instructor.from_openai(AsyncOpenAI(api_key=api_key))
31
  model = "gpt-4o-mini"
32
  display_model = "OpenAI (gpt-4o-mini)"
33
  elif provider == "ollama":
34
  from openai import AsyncOpenAI as OllamaClient
35
+ try:
36
+ client = instructor.from_openai(
37
+ OllamaClient(base_url="http://localhost:11434/v1", api_key="ollama"), mode=instructor.Mode.JSON
38
+ )
39
+ model = "llama3"
40
+ display_model = "Ollama (llama3)"
41
+ logger.info("Ollama client initialized successfully")
42
+ except Exception as e:
43
+ logger.error(f"Failed to initialize Ollama client: {e}")
44
+ st.error(f"Ollama connection failed: {e}")
45
+ return None, None, None
46
  else:
47
  st.error(f"Unsupported provider: {provider}")
48
  return None, None, None
 
61
  )
62
 
63
  # Provider selection
64
+ providers_list = ["ollama", "openai"]
65
  selected_provider = st.selectbox("Choose a provider:", providers_list, key="provider_select")
66
 
67
  # Set up client and agent based on the selected provider
 
84
  memory=st.session_state.memory,
85
  system_role="developer",
86
  ))
87
+ st.session_state.current_model = model
88
 
89
  # Display the selected model
90
  st.markdown(f"**Selected Model:** {st.session_state.display_model}")
 
118
  response_container = st.empty()
119
  async def stream_response():
120
  current_response = ""
121
+ try:
122
+ async for partial_response in st.session_state.agent.run_async(input_schema):
123
+ if hasattr(partial_response, "chat_message") and partial_response.chat_message:
124
+ if partial_response.chat_message != current_response:
125
+ current_response = partial_response.chat_message
126
+ response_container.markdown(current_response)
127
+ except Exception as e:
128
+ logger.error(f"Error streaming response: {e}")
129
+ response_container.error(f"Error: {e}")
130
 
131
  # After streaming completes, add the final response to conversation and memory
132
  st.session_state.conversation.append(("assistant", current_response))