Spaces:
Sleeping
Sleeping
Commit
·
dc68e9c
1
Parent(s):
1be8b6f
app files
Browse files- Dockerfile +25 -13
- app.py +25 -5
- requirements.txt +6 -2
Dockerfile
CHANGED
|
@@ -1,16 +1,28 @@
|
|
| 1 |
-
#
|
| 2 |
-
# you will also find guides on how best to write your Dockerfile
|
| 3 |
-
|
| 4 |
FROM python:3.9
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
RUN useradd -m -u 1000 user
|
|
|
|
| 7 |
USER user
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python 3.9 image
|
|
|
|
|
|
|
| 2 |
FROM python:3.9
|
| 3 |
+
|
| 4 |
+
# Set the working directory to /code
|
| 5 |
+
WORKDIR /code
|
| 6 |
+
|
| 7 |
+
# Copy the current directory contents into the container at /code
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
# Install requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 12 |
+
|
| 13 |
+
# Set up a new user named "user" with user ID 1000
|
| 14 |
RUN useradd -m -u 1000 user
|
| 15 |
+
# Switch to the "user" user
|
| 16 |
USER user
|
| 17 |
+
# Set home to the user's home directory
|
| 18 |
+
ENV HOME=/home/user \\
|
| 19 |
+
PATH=/home/user/.local/bin:$PATH
|
| 20 |
+
|
| 21 |
+
# Set the working directory to the user's home directory
|
| 22 |
+
WORKDIR $HOME/app
|
| 23 |
+
|
| 24 |
+
# Copy the current directory contents into the container at $HOME/app setting the owner to the user
|
| 25 |
+
COPY --chown=user . $HOME/app
|
| 26 |
+
|
| 27 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
| 28 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
CHANGED
|
@@ -1,7 +1,27 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
app = FastAPI()
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Create a new FastAPI app instance
|
| 5 |
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Initialize the text generation pipeline
|
| 8 |
+
# This function will be able to generate text
|
| 9 |
+
# given an input.
|
| 10 |
+
pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
| 11 |
+
|
| 12 |
+
# Define a function to handle the GET request at `/generate`
|
| 13 |
+
# The generate() function is defined as a FastAPI route that takes a
|
| 14 |
+
# string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response
|
| 15 |
+
# containing the generated text under the key "output"
|
| 16 |
+
@app.get("/generate")
|
| 17 |
+
def generate(text: str):
|
| 18 |
+
"""
|
| 19 |
+
Using the text2text-generation pipeline from `transformers`, generate text
|
| 20 |
+
from the given input text. The model used is `google/flan-t5-small`, which
|
| 21 |
+
can be found [here](<https://huggingface.co/google/flan-t5-small>).
|
| 22 |
+
"""
|
| 23 |
+
# Use the pipeline to generate text from the given input text
|
| 24 |
+
output = pipe(text)
|
| 25 |
+
|
| 26 |
+
# Return the generated text in a JSON response
|
| 27 |
+
return {"output": output[0]["generated_text"]}
|
requirements.txt
CHANGED
|
@@ -1,2 +1,6 @@
|
|
| 1 |
-
fastapi
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi==0.74.*
|
| 2 |
+
requests==2.27.*
|
| 3 |
+
uvicorn[standard]==0.17.*
|
| 4 |
+
sentencepiece==0.1.*
|
| 5 |
+
torch==1.11.*
|
| 6 |
+
transformers==4.*
|