Spaces:
Running
Running
Upload project files
Browse files- Dockerfile +48 -0
- api.py +13 -0
- app.py +17 -0
- nginx.conf +35 -0
- requirements.txt +3 -0
- run.sh +7 -0
Dockerfile
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
2 |
+
# you will also find guides on how best to write your Dockerfile
|
3 |
+
FROM python:3
|
4 |
+
|
5 |
+
# Add new user 'user' (non-root)
|
6 |
+
RUN useradd -m -u 1000 user
|
7 |
+
|
8 |
+
# Set working dir to app
|
9 |
+
ENV HOME=/home/user \
|
10 |
+
PATH=/home/user/.local/bin:$PATH
|
11 |
+
RUN mkdir $HOME/app
|
12 |
+
WORKDIR $HOME/app
|
13 |
+
|
14 |
+
# Switch to root
|
15 |
+
USER root
|
16 |
+
|
17 |
+
# Install nginx and packages.txt
|
18 |
+
RUN apt-get -y update && apt-get -y install nginx
|
19 |
+
|
20 |
+
# Give app permissions to 'user' (non-root)
|
21 |
+
RUN chown user:user .
|
22 |
+
|
23 |
+
# Give nginx permissions to 'user' (non-root)
|
24 |
+
# See https://www.rockyourcode.com/run-docker-nginx-as-non-root-user/
|
25 |
+
RUN mkdir -p /var/cache/nginx \
|
26 |
+
/var/log/nginx \
|
27 |
+
/var/lib/nginx
|
28 |
+
RUN touch /var/run/nginx.pid
|
29 |
+
RUN chown -R user:user /var/cache/nginx \
|
30 |
+
/var/log/nginx \
|
31 |
+
/var/lib/nginx \
|
32 |
+
/var/run/nginx.pid
|
33 |
+
|
34 |
+
# Switch to 'user' (non-root)
|
35 |
+
USER user
|
36 |
+
|
37 |
+
# Install requirements.txt
|
38 |
+
COPY --chown=user requirements.txt requirements.txt
|
39 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
40 |
+
|
41 |
+
# Copy nginx configuration
|
42 |
+
COPY --chown=user nginx.conf /etc/nginx/sites-available/default
|
43 |
+
|
44 |
+
# Copy app
|
45 |
+
COPY --chown=user . .
|
46 |
+
|
47 |
+
# Run
|
48 |
+
CMD ["bash", "run.sh"]
|
api.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, Response, json, jsonify, request
|
2 |
+
|
3 |
+
|
4 |
+
api = Flask(__name__)
|
5 |
+
|
6 |
+
@api.route("/", methods=["POST"])
|
7 |
+
def query() -> Response:
|
8 |
+
data = json.loads(request.data)
|
9 |
+
response_data = {"dummy": "response"}
|
10 |
+
response_data.update(data)
|
11 |
+
return jsonify(response_data)
|
12 |
+
|
13 |
+
api.run(host="localhost", port=3000)
|
app.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
|
4 |
+
|
5 |
+
def chatbot_response(user_input: str, history) -> str:
|
6 |
+
r = requests.post(
|
7 |
+
"http://localhost:3000",
|
8 |
+
json={"example": "json"}
|
9 |
+
)
|
10 |
+
response = r.json()
|
11 |
+
return f"Response from the API was {response}"
|
12 |
+
|
13 |
+
demo = gr.ChatInterface(fn=chatbot_response,
|
14 |
+
title="Name of demo goes here",
|
15 |
+
description="Description goes here")
|
16 |
+
|
17 |
+
demo.launch(server_name="localhost", server_port=4000, show_api=False)
|
nginx.conf
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
server {
|
2 |
+
listen 7860 default_server;
|
3 |
+
listen [::]:7860 default_server;
|
4 |
+
|
5 |
+
server_name _;
|
6 |
+
|
7 |
+
location / {
|
8 |
+
# Serve Gradio from port 4000
|
9 |
+
proxy_pass http://localhost:4000;
|
10 |
+
proxy_http_version 1.1;
|
11 |
+
proxy_set_header Upgrade $http_upgrade;
|
12 |
+
proxy_set_header Connection 'upgrade';
|
13 |
+
proxy_set_header Host $host;
|
14 |
+
proxy_set_header X-Real-IP $remote_addr;
|
15 |
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
16 |
+
proxy_cache_bypass $http_upgrade;
|
17 |
+
proxy_read_timeout 86400;
|
18 |
+
proxy_redirect off;
|
19 |
+
}
|
20 |
+
|
21 |
+
location /query {
|
22 |
+
# Serve Flask app API server from port 3000
|
23 |
+
rewrite ^/query/?(.*)$ /$1 break; # strip the /query/
|
24 |
+
proxy_pass http://localhost:3000;
|
25 |
+
proxy_http_version 1.1;
|
26 |
+
proxy_set_header Upgrade $http_upgrade;
|
27 |
+
proxy_set_header Connection 'upgrade';
|
28 |
+
proxy_set_header Host $host;
|
29 |
+
proxy_set_header X-Real-IP $remote_addr;
|
30 |
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
31 |
+
proxy_cache_bypass $http_upgrade;
|
32 |
+
proxy_read_timeout 86400;
|
33 |
+
proxy_redirect off;
|
34 |
+
}
|
35 |
+
}
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
gradio
|
3 |
+
requests
|
run.sh
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# start nginx
|
4 |
+
service nginx start
|
5 |
+
|
6 |
+
# start the processes
|
7 |
+
python api.py & python app.py
|