Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +35 -0
- app.py +27 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## use the official Python 3.12 imagae
|
| 2 |
+
FROM python:3.12
|
| 3 |
+
|
| 4 |
+
## set the working directory in the container
|
| 5 |
+
WORKDIR /code
|
| 6 |
+
|
| 7 |
+
## copy the dependencies file to the working directory
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
#3 instal the requirements.txt
|
| 11 |
+
|
| 12 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
#setup a new user
|
| 16 |
+
RUN useradd user
|
| 17 |
+
|
| 18 |
+
#switch to the new user
|
| 19 |
+
USER user
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
#set the home to user's home directory
|
| 23 |
+
|
| 24 |
+
ENV HOME /home/user \
|
| 25 |
+
PATH=/home/user/.local/bin:$PATH
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# set the working directory to the user's home directory
|
| 29 |
+
WORKDIR $HOME/app
|
| 30 |
+
|
| 31 |
+
# copy the current directiry cintents into the container at /home/user/app
|
| 32 |
+
COPY --chown=user . $HOME/app
|
| 33 |
+
|
| 34 |
+
## Start the FASTAPI app on 7860
|
| 35 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from trasformers import pipeline
|
| 3 |
+
|
| 4 |
+
## Create FastAPI app instance
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Initialize a text generation pipeline
|
| 8 |
+
# Load model directly
|
| 9 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 10 |
+
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 13 |
+
|
| 14 |
+
@app.get("/")
|
| 15 |
+
def home():
|
| 16 |
+
return {"message": "Welcome to the Text Generation API!"}
|
| 17 |
+
|
| 18 |
+
#Define a function to handle the GET request at /generate
|
| 19 |
+
@app.get("/generate")
|
| 20 |
+
def generate(text:str):
|
| 21 |
+
## Generate text using the model
|
| 22 |
+
output=pipe(text)
|
| 23 |
+
|
| 24 |
+
##return the text generate in Json response
|
| 25 |
+
return {"output":output[0]['generated_text']}
|
| 26 |
+
|
| 27 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi>=0.74,<0.75
|
| 2 |
+
requests>=2.27,<2.28
|
| 3 |
+
uvicorn[standard]>=0.17,<0.18
|
| 4 |
+
torch>=2.0
|
| 5 |
+
transformers>=4.18
|
| 6 |
+
|