Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment #13

Merged
merged 4 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: deploy with docker completed
  • Loading branch information
ForYourEyesOnlyyy committed Oct 25, 2024
commit 836d2056dd744b5e8a874279957b8711ac59f52a
22 changes: 22 additions & 0 deletions deployment/Dockerfile.fastapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Dockerfile for FastAPI
FROM python:3.11-slim

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app

# Set working directory
WORKDIR /app

# Copy the requirements and install dependencies
COPY deployment/requirements/fastapi-requirements.txt .
RUN pip install --no-cache-dir -r fastapi-requirements.txt

# Copy code only for production (comment this out if using only volume mounting)
# COPY . .

# Expose the port FastAPI will run on
EXPOSE 8000

# Command to run FastAPI
CMD ["uvicorn", "deployment.api:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]
22 changes: 22 additions & 0 deletions deployment/Dockerfile.streamlit
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Dockerfile for Streamlit
FROM python:3.11-slim

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONPATH=/app

# Set working directory
WORKDIR /app

# Copy the requirements and install dependencies
COPY deployment/requirements/streamlit-requirements.txt .
RUN pip install --no-cache-dir -r streamlit-requirements.txt

# Copy application code
COPY ./deployment/app.py /deployment/app.py

# Expose the port Streamlit will run on
EXPOSE 8501

# Command to run Streamlit
CMD ["streamlit", "run", "deployment/app.py", "--server.port=8501", "--server.address=0.0.0.0"]
2 changes: 1 addition & 1 deletion deployment/api/api.py → deployment/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def lifespan(app: FastAPI):
import torch
from models.ssam.simple_sentiment_analysis_model import SentimentAnalysisModel
model = SentimentAnalysisModel()
model.load_state_dict(torch.load('models/ssam/model_weights.pth'))
model.load_state_dict(torch.load('models/ssam/model_weights.pth', map_location=config.device))
logging.info(f"Model {config.model_name} loaded successfully at startup.")

tokenizer = data.get_tokenizer(config.tokenizer_name)
Expand Down
2 changes: 1 addition & 1 deletion deployment/app/app.py → deployment/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
tweet = st.text_area("Enter the tweet you want to analyze:", "", height=150)

# API endpoint URL (adjust this URL based on where your FastAPI is hosted)
api_url = "http://127.0.0.1:8000/predict-sentiment/"
api_url = "http://fastapi:8000/predict-sentiment/"

# Create a button to send the request
if st.button("Predict Sentiment"):
Expand Down
33 changes: 33 additions & 0 deletions deployment/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
version: '3.8'

services:
fastapi:
build:
context: ..
dockerfile: deployment/Dockerfile.fastapi
environment:
- PYTHONPATH=/app # Set project root as PYTHONPATH
volumes:
- ../:/app # Mount the project root directory
ports:
- "8000:8000"
networks:
- app-network

streamlit:
build:
context: ..
dockerfile: deployment/Dockerfile.streamlit
environment:
- PYTHONPATH=/app # Set project root as PYTHONPATH
- FASTAPI_URL=http://fastapi:8000 # Connects Streamlit to FastAPI
volumes:
- ../:/app # Mount the project root directory
ports:
- "8501:8501"
networks:
- app-network

networks:
app-network:
driver: bridge
10 changes: 10 additions & 0 deletions deployment/requirements/fastapi-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
datasets==3.0.0
mlflow==2.16.2
mlflow_skinny==2.16.2
pandas==2.2.3
scikit_learn==1.5.2
torch==2.4.1
transformers==4.44.2
pydantic==1.10.2
fastapi
uvicorn
2 changes: 2 additions & 0 deletions deployment/requirements/streamlit-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests
streamlit
Loading