saifisvibin commited on
Commit
ee093d4
·
1 Parent(s): 6f2b9f4

first commit

Browse files
Files changed (9) hide show
  1. DEPLOYMENT.md +0 -225
  2. Dockerfile +10 -10
  3. QUICK_DEPLOY.md +0 -116
  4. README.md +27 -16
  5. fly.toml +0 -30
  6. main.py +1 -1
  7. railway.json +0 -13
  8. render.yaml +0 -10
  9. start.sh +0 -7
DEPLOYMENT.md DELETED
@@ -1,225 +0,0 @@
1
- # Deployment Guide
2
-
3
- This guide will help you deploy your FastAPI backend to a free hosting service so you can share it with others.
4
-
5
- ## Quick Deploy Options
6
-
7
- ### Option 1: Railway (Recommended - Easiest) ⭐
8
-
9
- **Railway** is the easiest option with a free tier.
10
-
11
- #### Steps:
12
-
13
- 1. **Create a Railway account:**
14
- - Go to https://railway.app
15
- - Sign up with GitHub (recommended) or email
16
-
17
- 2. **Create a new project:**
18
- - Click "New Project"
19
- - Select "Deploy from GitHub repo" (if you have GitHub) OR
20
- - Select "Empty Project" and upload files manually
21
-
22
- 3. **If using GitHub:**
23
- - Push your code to GitHub first
24
- - Connect your repository
25
- - Railway will auto-detect the Dockerfile
26
-
27
- 4. **If uploading manually:**
28
- - Click "New" → "GitHub Repo" or "Empty Project"
29
- - Upload these files:
30
- - `main.py`
31
- - `requirements.txt`
32
- - `Dockerfile`
33
- - `railway.json`
34
- - Railway will automatically build and deploy
35
-
36
- 5. **Configure the service:**
37
- - Railway will auto-detect Python
38
- - Set start command: `uvicorn main:app --host 0.0.0.0 --port $PORT`
39
- - Or it will use the Dockerfile automatically
40
-
41
- 6. **Get your URL:**
42
- - Once deployed, Railway gives you a URL like: `your-app.railway.app`
43
- - Share this URL with developers!
44
-
45
- **Railway Free Tier:**
46
- - $5 free credit per month
47
- - Perfect for testing and sharing
48
- - Auto-deploys on git push
49
-
50
- ---
51
-
52
- ### Option 2: Render (Also Easy)
53
-
54
- **Render** offers free hosting with easy setup.
55
-
56
- #### Steps:
57
-
58
- 1. **Create a Render account:**
59
- - Go to https://render.com
60
- - Sign up with GitHub (recommended)
61
-
62
- 2. **Create a new Web Service:**
63
- - Click "New" → "Web Service"
64
- - Connect your GitHub repository OR upload files
65
-
66
- 3. **Configure the service:**
67
- - **Name:** `fastapi-backend` (or any name)
68
- - **Environment:** Python 3
69
- - **Build Command:** `pip install -r requirements.txt`
70
- - **Start Command:** `uvicorn main:app --host 0.0.0.0 --port $PORT`
71
- - **Plan:** Free
72
-
73
- 4. **Deploy:**
74
- - Click "Create Web Service"
75
- - Render will build and deploy automatically
76
- - You'll get a URL like: `your-app.onrender.com`
77
-
78
- **Render Free Tier:**
79
- - Free tier available
80
- - Spins down after 15 minutes of inactivity (wakes up on first request)
81
- - Perfect for testing and sharing
82
-
83
- ---
84
-
85
- ### Option 3: Fly.io (Good Performance)
86
-
87
- **Fly.io** offers global deployment with good performance.
88
-
89
- #### Steps:
90
-
91
- 1. **Install Fly CLI:**
92
- ```bash
93
- # Windows (PowerShell)
94
- powershell -Command "iwr https://fly.io/install.ps1 -useb | iex"
95
-
96
- # Or download from https://fly.io/docs/getting-started/installing-flyctl/
97
- ```
98
-
99
- 2. **Login:**
100
- ```bash
101
- fly auth login
102
- ```
103
-
104
- 3. **Initialize:**
105
- ```bash
106
- fly launch
107
- ```
108
- - Follow the prompts
109
- - Don't deploy yet (we'll configure first)
110
-
111
- 4. **Create `fly.toml`** (I'll create this for you)
112
-
113
- 5. **Deploy:**
114
- ```bash
115
- fly deploy
116
- ```
117
-
118
- ---
119
-
120
- ## Files Needed for Deployment
121
-
122
- Make sure you have these files in your project:
123
-
124
- ✅ `main.py` - Your FastAPI application
125
- ✅ `requirements.txt` - Python dependencies
126
- ✅ `Dockerfile` - For containerized deployment
127
- ✅ `.dockerignore` - Excludes unnecessary files
128
- ✅ `railway.json` - Railway configuration (optional)
129
- ✅ `render.yaml` - Render configuration (optional)
130
-
131
- ---
132
-
133
- ## After Deployment
134
-
135
- ### Test Your Deployed API
136
-
137
- Once deployed, test your endpoints:
138
-
139
- **Status endpoint:**
140
- ```bash
141
- curl https://your-app.railway.app/status
142
- ```
143
-
144
- **Register endpoint:**
145
- ```bash
146
- curl -X POST https://your-app.railway.app/register \
147
- -H "Content-Type: application/json" \
148
- -d '{"name": "John Doe", "email": "john@example.com", "age": 25}'
149
- ```
150
-
151
- **View API docs:**
152
- - Swagger UI: `https://your-app.railway.app/docs`
153
- - ReDoc: `https://your-app.railway.app/redoc`
154
-
155
- ### Share with Developers
156
-
157
- Send them:
158
- 1. **API Base URL:** `https://your-app.railway.app`
159
- 2. **API Documentation:** `https://your-app.railway.app/docs`
160
- 3. **Quick Start Guide:** Share `FASTAPI_README.md`
161
-
162
- ---
163
-
164
- ## Environment Variables (Optional)
165
-
166
- If you need to configure settings, add environment variables in your hosting platform:
167
-
168
- - `PORT` - Automatically set by hosting platform
169
- - `ENVIRONMENT` - Set to "production" for production mode
170
-
171
- ---
172
-
173
- ## Troubleshooting
174
-
175
- ### Common Issues:
176
-
177
- 1. **Port binding error:**
178
- - Make sure you're using `--host 0.0.0.0` and `--port $PORT`
179
- - The `$PORT` variable is set automatically by hosting platforms
180
-
181
- 2. **Build fails:**
182
- - Check that `requirements.txt` has all dependencies
183
- - Make sure Python version is compatible (3.8+)
184
-
185
- 3. **API not accessible:**
186
- - Check that the service is running (not sleeping)
187
- - Verify the URL is correct
188
- - Check logs in your hosting platform dashboard
189
-
190
- 4. **CORS errors:**
191
- - If calling from a browser, you may need to add CORS middleware
192
- - Add this to `main.py` if needed:
193
- ```python
194
- from fastapi.middleware.cors import CORSMiddleware
195
-
196
- app.add_middleware(
197
- CORSMiddleware,
198
- allow_origins=["*"], # In production, specify actual origins
199
- allow_credentials=True,
200
- allow_methods=["*"],
201
- allow_headers=["*"],
202
- )
203
- ```
204
-
205
- ---
206
-
207
- ## Recommended: Railway
208
-
209
- For sharing with developers, **Railway** is recommended because:
210
- - ✅ Easiest setup
211
- - ✅ Free tier with $5 credit/month
212
- - ✅ Auto-deploys from GitHub
213
- - ✅ Good performance
214
- - ✅ Easy to share URLs
215
-
216
- ---
217
-
218
- ## Need Help?
219
-
220
- If you run into issues:
221
- 1. Check the logs in your hosting platform dashboard
222
- 2. Verify all files are uploaded correctly
223
- 3. Make sure `requirements.txt` includes all dependencies
224
- 4. Test locally first: `uvicorn main:app --reload`
225
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Dockerfile CHANGED
@@ -1,9 +1,11 @@
1
  # Use Python 3.11 slim image
2
  FROM python:3.11-slim
3
 
4
- # Set working directory
5
  WORKDIR /app
6
 
 
 
 
7
  # Copy requirements first for better caching
8
  COPY requirements.txt .
9
 
@@ -15,16 +17,14 @@ COPY main.py .
15
  COPY model_loader.py .
16
  COPY best_lung_cancer_model.joblib .
17
  COPY scaler.joblib .
18
- COPY start.sh .
19
 
20
- # Make startup script executable
21
- RUN chmod +x start.sh
22
 
23
- # Expose port (Railway/Render will set PORT env variable)
24
- EXPOSE 8000
25
 
26
- # Run the application using the startup script
27
- # This properly handles the PORT environment variable
28
- # Using shell form to execute the bash script
29
- CMD ./start.sh
30
 
 
 
 
1
  # Use Python 3.11 slim image
2
  FROM python:3.11-slim
3
 
 
4
  WORKDIR /app
5
 
6
+ # Create a non-root user for Hugging Face
7
+ RUN useradd -m -u 1000 user
8
+
9
  # Copy requirements first for better caching
10
  COPY requirements.txt .
11
 
 
17
  COPY model_loader.py .
18
  COPY best_lung_cancer_model.joblib .
19
  COPY scaler.joblib .
 
20
 
21
+ # Set ownership for Hugging Face user
22
+ RUN chown -R user:user /app
23
 
24
+ USER user
 
25
 
26
+ # Hugging Face Spaces uses port 7860
27
+ EXPOSE 7860
 
 
28
 
29
+ # Run the application
30
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
QUICK_DEPLOY.md DELETED
@@ -1,116 +0,0 @@
1
- # Quick Deploy Guide - Railway (5 Minutes)
2
-
3
- The fastest way to deploy your FastAPI backend and share it with developers.
4
-
5
- ## Step-by-Step Instructions
6
-
7
- ### 1. Create Railway Account (1 minute)
8
-
9
- 1. Go to **https://railway.app**
10
- 2. Click **"Start a New Project"**
11
- 3. Sign up with **GitHub** (recommended) or email
12
-
13
- ### 2. Deploy Your App (2 minutes)
14
-
15
- **Option A: Deploy from GitHub (Recommended)**
16
-
17
- 1. **Push your code to GitHub first:**
18
- ```bash
19
- git init
20
- git add .
21
- git commit -m "Initial commit"
22
- git remote add origin https://github.com/yourusername/your-repo.git
23
- git push -u origin main
24
- ```
25
-
26
- 2. **In Railway:**
27
- - Click **"New Project"**
28
- - Select **"Deploy from GitHub repo"**
29
- - Choose your repository
30
- - Railway will auto-detect and deploy!
31
-
32
- **Option B: Deploy Manually**
33
-
34
- 1. In Railway, click **"New Project"** → **"Empty Project"**
35
- 2. Click **"New"** → **"GitHub Repo"** or **"Empty Project"**
36
- 3. Upload these files:
37
- - `main.py`
38
- - `requirements.txt`
39
- - `Dockerfile`
40
- 4. Railway will automatically build and deploy
41
-
42
- ### 3. Get Your URL (Instant)
43
-
44
- Once deployed, Railway gives you a URL like:
45
- ```
46
- https://your-app-name.railway.app
47
- ```
48
-
49
- **That's it!** Your API is live and ready to share! 🎉
50
-
51
- ### 4. Test Your Deployed API
52
-
53
- **Status endpoint:**
54
- ```bash
55
- curl https://your-app-name.railway.app/status
56
- ```
57
-
58
- **API Documentation:**
59
- - Swagger UI: `https://your-app-name.railway.app/docs`
60
- - ReDoc: `https://your-app-name.railway.app/redoc`
61
-
62
- **Register endpoint:**
63
- ```bash
64
- curl -X POST https://your-app-name.railway.app/register \
65
- -H "Content-Type: application/json" \
66
- -d '{"name": "John Doe", "email": "john@example.com", "age": 25}'
67
- ```
68
-
69
- ### 5. Share with Developers
70
-
71
- Send them:
72
- 1. **API Base URL:** `https://your-app-name.railway.app`
73
- 2. **API Docs:** `https://your-app-name.railway.app/docs`
74
- 3. **Quick Start:** Share `FASTAPI_README.md`
75
-
76
- ---
77
-
78
- ## What You Need
79
-
80
- Make sure these files are in your project:
81
- - ✅ `main.py`
82
- - ✅ `requirements.txt`
83
- - ✅ `Dockerfile`
84
-
85
- ---
86
-
87
- ## Railway Free Tier
88
-
89
- - **$5 free credit per month**
90
- - Perfect for testing and sharing
91
- - Auto-deploys on git push
92
- - No credit card required for free tier
93
-
94
- ---
95
-
96
- ## Troubleshooting
97
-
98
- **Build fails?**
99
- - Check that `requirements.txt` has all dependencies
100
- - Make sure `Dockerfile` is in the root directory
101
-
102
- **API not working?**
103
- - Check Railway logs (click on your service → "View Logs")
104
- - Verify the URL is correct
105
- - Make sure the service is running (not stopped)
106
-
107
- **Need help?**
108
- - Railway has great docs: https://docs.railway.app
109
- - Check `DEPLOYMENT.md` for more details
110
-
111
- ---
112
-
113
- ## That's It!
114
-
115
- Your API is now live and shareable. Developers can access it from anywhere in the world! 🌍
116
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md CHANGED
@@ -10,27 +10,22 @@ A FastAPI-based REST API for predicting lung cancer risk based on patient sympto
10
  - ✅ CORS support for web applications
11
  - ✅ Production-ready with error handling
12
 
13
- ## Quick Start
14
 
15
- 1. **Install dependencies:**
16
- ```bash
17
- pip install -r requirements.txt
18
- ```
19
 
20
- 2. **Run the API:**
21
- ```bash
22
- uvicorn main:app --reload
23
- ```
24
-
25
- 3. **Access API documentation:**
26
- - Swagger UI: http://localhost:8000/docs
27
- - ReDoc: http://localhost:8000/redoc
28
 
29
- ## Deployment
 
 
 
30
 
31
- See `QUICK_DEPLOY.md` for quick deployment instructions (Railway recommended).
32
 
33
- For detailed deployment options, see `DEPLOYMENT.md`.
 
 
34
 
35
  ## API Endpoints
36
 
@@ -71,6 +66,22 @@ For detailed deployment options, see `DEPLOYMENT.md`.
71
  }
72
  ```
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  ## Notes
75
 
76
  - This application is for educational/research purposes only
 
10
  - ✅ CORS support for web applications
11
  - ✅ Production-ready with error handling
12
 
13
+ ## Hugging Face Spaces Deployment
14
 
15
+ This project is configured for deployment on Hugging Face Spaces using the Docker SDK.
 
 
 
16
 
17
+ ### Deploy to Hugging Face
 
 
 
 
 
 
 
18
 
19
+ 1. Create a new Space on [Hugging Face](https://huggingface.co/spaces)
20
+ 2. Select **Docker** as the SDK
21
+ 3. Push this repository to your Space
22
+ 4. The API will be available at your Space URL
23
 
24
+ ### API Endpoints
25
 
26
+ Once deployed, access your API at:
27
+ - **Swagger UI**: `https://your-space.hf.space/docs`
28
+ - **ReDoc**: `https://your-space.hf.space/redoc`
29
 
30
  ## API Endpoints
31
 
 
66
  }
67
  ```
68
 
69
+ ## Local Development
70
+
71
+ 1. **Install dependencies:**
72
+ ```bash
73
+ pip install -r requirements.txt
74
+ ```
75
+
76
+ 2. **Run the API:**
77
+ ```bash
78
+ uvicorn main:app --reload --port 7860
79
+ ```
80
+
81
+ 3. **Access API documentation:**
82
+ - Swagger UI: http://localhost:7860/docs
83
+ - ReDoc: http://localhost:7860/redoc
84
+
85
  ## Notes
86
 
87
  - This application is for educational/research purposes only
fly.toml DELETED
@@ -1,30 +0,0 @@
1
- # Fly.io configuration file
2
- app = "your-app-name"
3
- primary_region = "iad"
4
-
5
- [build]
6
-
7
- [env]
8
- PORT = "8000"
9
-
10
- [http]
11
- internal_port = 8000
12
- force_https = true
13
- auto_stop_machines = true
14
- auto_start_machines = true
15
- min_machines_running = 0
16
-
17
- [[services]]
18
- internal_port = 8000
19
- protocol = "tcp"
20
- processes = ["app"]
21
-
22
- [[services.ports]]
23
- port = 80
24
- handlers = ["http"]
25
- force_https = true
26
-
27
- [[services.ports]]
28
- port = 443
29
- handlers = ["tls", "http"]
30
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
main.py CHANGED
@@ -438,7 +438,7 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
438
 
439
  if __name__ == "__main__":
440
  # Get port from environment variable (for deployment) or default to 8000
441
- port = int(os.environ.get("PORT", 8000))
442
 
443
  # --reload enables auto-reload on code changes (development only)
444
  reload = os.environ.get("ENVIRONMENT", "development") == "development"
 
438
 
439
  if __name__ == "__main__":
440
  # Get port from environment variable (for deployment) or default to 8000
441
+ port = int(os.environ.get("PORT", 7860))
442
 
443
  # --reload enables auto-reload on code changes (development only)
444
  reload = os.environ.get("ENVIRONMENT", "development") == "development"
railway.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "$schema": "https://railway.app/railway.schema.json",
3
- "build": {
4
- "builder": "DOCKERFILE",
5
- "dockerfilePath": "Dockerfile"
6
- },
7
- "deploy": {
8
- "startCommand": "./start.sh",
9
- "restartPolicyType": "ON_FAILURE",
10
- "restartPolicyMaxRetries": 10
11
- }
12
- }
13
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
render.yaml DELETED
@@ -1,10 +0,0 @@
1
- services:
2
- - type: web
3
- name: fastapi-backend
4
- env: python
5
- buildCommand: pip install -r requirements.txt
6
- startCommand: uvicorn main:app --host 0.0.0.0 --port $PORT
7
- envVars:
8
- - key: PYTHON_VERSION
9
- value: 3.11.0
10
-
 
 
 
 
 
 
 
 
 
 
 
start.sh DELETED
@@ -1,7 +0,0 @@
1
- #!/bin/bash
2
- # Startup script for Railway deployment
3
- # Reads PORT from environment variable and starts uvicorn
4
-
5
- PORT=${PORT:-8000}
6
- uvicorn main:app --host 0.0.0.0 --port $PORT
7
-