Spaces:
Sleeping
Sleeping
basic flask and streamlit with dockerfile
Browse files- .dockerignore +19 -0
- .gitignore +6 -0
- .python-version +1 -0
- Dockerfile +42 -0
- README.md +178 -5
- docker-compose.yml +30 -0
- flask_app.py +13 -0
- main.py +6 -0
- pyproject.toml +11 -0
- requirements.txt +3 -0
- start.sh +10 -0
- streamlit_app.py +38 -0
- testingcommit +0 -0
.dockerignore
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.git
|
| 2 |
+
.gitignore
|
| 3 |
+
.venv
|
| 4 |
+
__pycache__
|
| 5 |
+
*.pyc
|
| 6 |
+
*.pyo
|
| 7 |
+
*.pyd
|
| 8 |
+
.Python
|
| 9 |
+
env/
|
| 10 |
+
venv/
|
| 11 |
+
.idea/
|
| 12 |
+
.vscode/
|
| 13 |
+
*.swp
|
| 14 |
+
*.swo
|
| 15 |
+
.DS_Store
|
| 16 |
+
docker-compose.yml
|
| 17 |
+
DOCKER_INSTRUCTIONS.md
|
| 18 |
+
RUN_INSTRUCTIONS.md
|
| 19 |
+
tmp_rovodev_*
|
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/.venv/
|
| 2 |
+
.venv
|
| 3 |
+
.env
|
| 4 |
+
__pycache__/
|
| 5 |
+
*.pyc
|
| 6 |
+
uv.lock
|
.python-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
3.10
|
Dockerfile
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Install system dependencies
|
| 7 |
+
RUN apt-get update && apt-get install -y \
|
| 8 |
+
build-essential \
|
| 9 |
+
curl \
|
| 10 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 11 |
+
|
| 12 |
+
# Copy requirements first for better caching
|
| 13 |
+
COPY requirements.txt .
|
| 14 |
+
|
| 15 |
+
# Install Python dependencies
|
| 16 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 17 |
+
|
| 18 |
+
# Copy all application files
|
| 19 |
+
COPY . .
|
| 20 |
+
|
| 21 |
+
# Create a non-root user (Hugging Face requirement)
|
| 22 |
+
RUN useradd -m -u 1000 user
|
| 23 |
+
USER user
|
| 24 |
+
|
| 25 |
+
# Set environment variables for Hugging Face
|
| 26 |
+
ENV HOME=/home/user \
|
| 27 |
+
PATH=/home/user/.local/bin:$PATH
|
| 28 |
+
|
| 29 |
+
# Set working directory to user home
|
| 30 |
+
WORKDIR $HOME/app
|
| 31 |
+
|
| 32 |
+
# Copy files to user directory
|
| 33 |
+
COPY --chown=user . $HOME/app
|
| 34 |
+
|
| 35 |
+
# Expose port 7860 (Hugging Face Spaces default)
|
| 36 |
+
EXPOSE 7860
|
| 37 |
+
|
| 38 |
+
# Make start script executable
|
| 39 |
+
RUN chmod +x start.sh
|
| 40 |
+
|
| 41 |
+
# Run both Flask and Streamlit using start script
|
| 42 |
+
CMD ["bash", "start.sh"]
|
README.md
CHANGED
|
@@ -1,12 +1,185 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
| 9 |
-
short_description:
|
|
|
|
| 10 |
---
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Flask Streamlit Demo
|
| 3 |
+
emoji: 🚀
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
| 9 |
+
short_description: Flask + Streamlit integration demo
|
| 10 |
+
app_port: 7860
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Flask + Streamlit Demo
|
| 14 |
+
|
| 15 |
+
This Hugging Face Space demonstrates integration between Flask backend and Streamlit frontend.
|
| 16 |
+
|
| 17 |
+
## Features:
|
| 18 |
+
- Flask API with `/helloworld` endpoint
|
| 19 |
+
- Streamlit app that calls the Flask API and displays the response
|
| 20 |
+
- Runs on Hugging Face Spaces using Docker
|
| 21 |
+
|
| 22 |
+
## How it works:
|
| 23 |
+
1. Flask API runs in the background on port 5000
|
| 24 |
+
2. Streamlit UI runs on port 7860 (Hugging Face default)
|
| 25 |
+
3. Click the button in Streamlit to call the Flask endpoint
|
| 26 |
+
|
| 27 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 28 |
+
|
| 29 |
+
# Flask + Streamlit Project Setup
|
| 30 |
+
|
| 31 |
+
## Files Created:
|
| 32 |
+
- `flask_app.py` - Flask backend with /helloworld endpoint
|
| 33 |
+
- `streamlit_app.py` - Streamlit frontend that calls the Flask API
|
| 34 |
+
|
| 35 |
+
## How to Run:
|
| 36 |
+
|
| 37 |
+
### Step 1: Install Dependencies
|
| 38 |
+
```bash
|
| 39 |
+
pip install -r requirements.txt
|
| 40 |
+
```
|
| 41 |
+
|
| 42 |
+
### Step 2: Start Flask Server (Terminal 1)
|
| 43 |
+
```bash
|
| 44 |
+
python flask_app.py
|
| 45 |
+
```
|
| 46 |
+
The Flask server will start on http://localhost:5000
|
| 47 |
+
|
| 48 |
+
### Step 3: Start Streamlit App (Terminal 2)
|
| 49 |
+
```bash
|
| 50 |
+
streamlit run streamlit_app.py
|
| 51 |
+
```
|
| 52 |
+
The Streamlit app will open in your browser (usually http://localhost:8501)
|
| 53 |
+
|
| 54 |
+
### Step 4: Test the Integration
|
| 55 |
+
1. Click the "Call Flask API" button in the Streamlit interface
|
| 56 |
+
2. The app will call the Flask endpoint and display the response
|
| 57 |
+
|
| 58 |
+
## Endpoints:
|
| 59 |
+
- Flask API: `GET http://localhost:5000/helloworld`
|
| 60 |
+
- Returns: `{"message": "Hello World!", "status": "success"}`
|
| 61 |
+
|
| 62 |
+
## Note:
|
| 63 |
+
Make sure to run Flask first before using the Streamlit app!
|
| 64 |
+
|
| 65 |
+
# Docker Setup Instructions
|
| 66 |
+
|
| 67 |
+
## Option 1: Using Docker Compose (Recommended - Runs both apps together)
|
| 68 |
+
|
| 69 |
+
### Build and Run:
|
| 70 |
+
```bash
|
| 71 |
+
docker-compose up --build
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
This will start:
|
| 75 |
+
- Flask API on http://localhost:5000
|
| 76 |
+
- Streamlit App on http://localhost:8501
|
| 77 |
+
|
| 78 |
+
### Stop:
|
| 79 |
+
```bash
|
| 80 |
+
docker-compose down
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
---
|
| 84 |
+
|
| 85 |
+
## Option 2: Using Individual Docker Commands
|
| 86 |
+
|
| 87 |
+
### Build the image:
|
| 88 |
+
```bash
|
| 89 |
+
docker build -t flask-streamlit-app .
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
### Run Flask only:
|
| 93 |
+
```bash
|
| 94 |
+
docker run -p 5000:5000 flask-streamlit-app python flask_app.py
|
| 95 |
+
```
|
| 96 |
+
|
| 97 |
+
### Run Streamlit only:
|
| 98 |
+
```bash
|
| 99 |
+
docker run -p 8501:8501 flask-streamlit-app streamlit run streamlit_app.py --server.address 0.0.0.0
|
| 100 |
+
```
|
| 101 |
+
|
| 102 |
+
---
|
| 103 |
+
|
| 104 |
+
## Accessing the Apps:
|
| 105 |
+
|
| 106 |
+
- **Flask API**: http://localhost:5000/helloworld
|
| 107 |
+
- **Streamlit App**: http://localhost:8501
|
| 108 |
+
|
| 109 |
+
---
|
| 110 |
+
|
| 111 |
+
## Notes:
|
| 112 |
+
|
| 113 |
+
- The `docker-compose.yml` sets up networking so Streamlit can communicate with Flask
|
| 114 |
+
- Both services are in the same network (`app-network`)
|
| 115 |
+
- Streamlit automatically uses the Flask service URL when running in Docker
|
| 116 |
+
- For local development without Docker, use `python flask_app.py` and `streamlit run streamlit_app.py`
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
# Deploying to Hugging Face Spaces
|
| 120 |
+
|
| 121 |
+
## Prerequisites:
|
| 122 |
+
- Hugging Face account
|
| 123 |
+
- Git installed
|
| 124 |
+
|
| 125 |
+
## Deployment Steps:
|
| 126 |
+
|
| 127 |
+
### 1. Create a new Space on Hugging Face
|
| 128 |
+
- Go to https://huggingface.co/new-space
|
| 129 |
+
- Choose a name for your Space
|
| 130 |
+
- Select **Docker** as the SDK
|
| 131 |
+
- Choose your preferred visibility (public/private)
|
| 132 |
+
|
| 133 |
+
### 2. Clone your Space repository
|
| 134 |
+
```bash
|
| 135 |
+
git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
|
| 136 |
+
cd YOUR_SPACE_NAME
|
| 137 |
+
```
|
| 138 |
+
|
| 139 |
+
### 3. Copy files to the Space repository
|
| 140 |
+
Copy these files to your Space repository:
|
| 141 |
+
- `Dockerfile`
|
| 142 |
+
- `requirements.txt`
|
| 143 |
+
- `flask_app.py`
|
| 144 |
+
- `streamlit_app.py`
|
| 145 |
+
- `start.sh`
|
| 146 |
+
- `README.md`
|
| 147 |
+
- `.dockerignore`
|
| 148 |
+
|
| 149 |
+
### 4. Push to Hugging Face
|
| 150 |
+
```bash
|
| 151 |
+
git add .
|
| 152 |
+
git commit -m "Initial commit: Flask + Streamlit app"
|
| 153 |
+
git push
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
### 5. Wait for build
|
| 157 |
+
- Hugging Face will automatically build your Docker container
|
| 158 |
+
- This may take 5-10 minutes
|
| 159 |
+
- Monitor the build logs in your Space settings
|
| 160 |
+
|
| 161 |
+
### 6. Access your app
|
| 162 |
+
- Once built, your app will be available at:
|
| 163 |
+
`https://YOUR_USERNAME-YOUR_SPACE_NAME.hf.space`
|
| 164 |
+
|
| 165 |
+
## Important Notes:
|
| 166 |
+
|
| 167 |
+
✅ **Port 7860** - Hugging Face Spaces uses port 7860 by default (already configured)
|
| 168 |
+
|
| 169 |
+
✅ **Non-root user** - Dockerfile creates user with UID 1000 (Hugging Face requirement)
|
| 170 |
+
|
| 171 |
+
✅ **Both apps run together** - Flask runs in background, Streamlit in foreground
|
| 172 |
+
|
| 173 |
+
✅ **README.md header** - Contains Hugging Face Space configuration:
|
| 174 |
+
```yaml
|
| 175 |
+
---
|
| 176 |
+
sdk: docker
|
| 177 |
+
app_port: 7860
|
| 178 |
+
---
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
## Troubleshooting:
|
| 182 |
+
|
| 183 |
+
- Check build logs in Space settings if build fails
|
| 184 |
+
- Make sure all files are pushed to the repository
|
| 185 |
+
- Ensure `start.sh` has execute permissions (handled in Dockerfile)
|
docker-compose.yml
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version: '3.8'
|
| 2 |
+
|
| 3 |
+
services:
|
| 4 |
+
flask:
|
| 5 |
+
build: .
|
| 6 |
+
container_name: flask-api
|
| 7 |
+
command: python flask_app.py
|
| 8 |
+
ports:
|
| 9 |
+
- "5000:5000"
|
| 10 |
+
networks:
|
| 11 |
+
- app-network
|
| 12 |
+
environment:
|
| 13 |
+
- FLASK_ENV=development
|
| 14 |
+
|
| 15 |
+
streamlit:
|
| 16 |
+
build: .
|
| 17 |
+
container_name: streamlit-app
|
| 18 |
+
command: streamlit run streamlit_app.py --server.address 0.0.0.0
|
| 19 |
+
ports:
|
| 20 |
+
- "8501:8501"
|
| 21 |
+
networks:
|
| 22 |
+
- app-network
|
| 23 |
+
depends_on:
|
| 24 |
+
- flask
|
| 25 |
+
environment:
|
| 26 |
+
- FLASK_URL=http://flask:5000/helloworld
|
| 27 |
+
|
| 28 |
+
networks:
|
| 29 |
+
app-network:
|
| 30 |
+
driver: bridge
|
flask_app.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, jsonify
|
| 2 |
+
|
| 3 |
+
app = Flask(__name__)
|
| 4 |
+
|
| 5 |
+
@app.route('/helloworld', methods=['GET'])
|
| 6 |
+
def hello_world():
|
| 7 |
+
return jsonify({
|
| 8 |
+
"message": "Hello World!",
|
| 9 |
+
"status": "success"
|
| 10 |
+
})
|
| 11 |
+
|
| 12 |
+
if __name__ == '__main__':
|
| 13 |
+
app.run(debug=True, host='0.0.0.0', port=5000)
|
main.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def main():
|
| 2 |
+
print("Hello from audiosentiment!")
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
if __name__ == "__main__":
|
| 6 |
+
main()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "audiosentiment"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.10"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"flask>=3.1.2",
|
| 9 |
+
"requests>=2.32.5",
|
| 10 |
+
"streamlit>=1.54.0",
|
| 11 |
+
]
|
requirements.txt
CHANGED
|
@@ -1,2 +1,5 @@
|
|
| 1 |
fastapi
|
| 2 |
uvicorn[standard]
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
uvicorn[standard]
|
| 3 |
+
flask
|
| 4 |
+
streamlit
|
| 5 |
+
requests
|
start.sh
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
# Start Flask in background
|
| 4 |
+
python flask_app.py &
|
| 5 |
+
|
| 6 |
+
# Wait a bit for Flask to start
|
| 7 |
+
sleep 2
|
| 8 |
+
|
| 9 |
+
# Start Streamlit on port 7860 (Hugging Face Spaces default)
|
| 10 |
+
streamlit run streamlit_app.py --server.port 7860 --server.address 0.0.0.0 --server.headless true --browser.serverAddress 0.0.0.0 --browser.gatherUsageStats false
|
streamlit_app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
st.title("Flask API Client 🚀")
|
| 5 |
+
st.write("This app calls the Flask HelloWorld endpoint and displays the response")
|
| 6 |
+
|
| 7 |
+
# Flask API URL
|
| 8 |
+
import os
|
| 9 |
+
FLASK_URL = os.getenv("FLASK_URL", "http://localhost:5000/helloworld")
|
| 10 |
+
|
| 11 |
+
st.markdown("---")
|
| 12 |
+
|
| 13 |
+
if st.button("Call Flask API"):
|
| 14 |
+
try:
|
| 15 |
+
with st.spinner("Calling Flask API..."):
|
| 16 |
+
response = requests.get(FLASK_URL)
|
| 17 |
+
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
st.success("API call successful! ✅")
|
| 20 |
+
data = response.json()
|
| 21 |
+
|
| 22 |
+
# Display response in a nice format
|
| 23 |
+
st.subheader("Response:")
|
| 24 |
+
st.json(data)
|
| 25 |
+
|
| 26 |
+
# Also display as text
|
| 27 |
+
st.info(f"Message: {data.get('message', 'N/A')}")
|
| 28 |
+
st.info(f"Status: {data.get('status', 'N/A')}")
|
| 29 |
+
else:
|
| 30 |
+
st.error(f"Error: Received status code {response.status_code}")
|
| 31 |
+
|
| 32 |
+
except requests.exceptions.ConnectionError:
|
| 33 |
+
st.error("❌ Could not connect to Flask server. Make sure it's running on port 5000!")
|
| 34 |
+
except Exception as e:
|
| 35 |
+
st.error(f"An error occurred: {str(e)}")
|
| 36 |
+
|
| 37 |
+
st.markdown("---")
|
| 38 |
+
st.caption("Make sure to run Flask app first: `python flask_app.py`")
|
testingcommit
DELETED
|
File without changes
|