Spaces:
Paused
Paused
Upload 6 files
Browse files- Dockerfile +49 -0
- api.py +11 -0
- app.py +17 -0
- nginx.conf +35 -0
- requirements.txt +3 -0
- run.sh +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
COPY --chown=root packages.txt packages.txt
|
| 19 |
+
RUN apt-get -y update && apt-get -y install nginx && xargs apt-get -y install < packages.txt
|
| 20 |
+
|
| 21 |
+
# Give app permissions to 'user' (non-root)
|
| 22 |
+
RUN chown user:user .
|
| 23 |
+
|
| 24 |
+
# Give nginx permissions to 'user' (non-root)
|
| 25 |
+
# See https://www.rockyourcode.com/run-docker-nginx-as-non-root-user/
|
| 26 |
+
RUN mkdir -p /var/cache/nginx \
|
| 27 |
+
/var/log/nginx \
|
| 28 |
+
/var/lib/nginx
|
| 29 |
+
RUN touch /var/run/nginx.pid
|
| 30 |
+
RUN chown -R user:user /var/cache/nginx \
|
| 31 |
+
/var/log/nginx \
|
| 32 |
+
/var/lib/nginx \
|
| 33 |
+
/var/run/nginx.pid
|
| 34 |
+
|
| 35 |
+
# Switch to 'user' (non-root)
|
| 36 |
+
USER user
|
| 37 |
+
|
| 38 |
+
# Install requirements.txt
|
| 39 |
+
COPY --chown=user requirements.txt requirements.txt
|
| 40 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 41 |
+
|
| 42 |
+
# Copy nginx configuration
|
| 43 |
+
COPY --chown=user nginx.conf /etc/nginx/sites-available/default
|
| 44 |
+
|
| 45 |
+
# Copy app
|
| 46 |
+
COPY --chown=user . .
|
| 47 |
+
|
| 48 |
+
# Run
|
| 49 |
+
CMD ["bash", "run.sh"]
|
api.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
return jsonify({"dummy": "response"}.update(data))
|
| 10 |
+
|
| 11 |
+
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 |
+
"https://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=7860, 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
|