ArxAlfa commited on
Commit ·
83bca60
1
Parent(s): d9bb0ab
Add Dockerfile and FastAPI app with text
Browse filesFiles changed (3)
- DockerfileAdded+27
- app.pyAdded+28
- requirements.txtAdded+6
123456789101112131415161718192021222324252627# Use the official Python 3.9 imageFROM python:3.9
# Set the working directory to /codeWORKDIR /code
# Copy the current directory contents into the container at /codeCOPY ./requirements.txt /code/requirements.txt
# Install requirements.txt RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# Set up a new user named "user" with user ID 1000RUN useradd -m -u 1000 user# Switch to the "user" userUSER user# Set home to the user's home directoryENV HOME=/home/user PATH=/home/user/.local/bin:$PATH
# Set the working directory to the user's home directoryWORKDIR $HOME/app
# Copy the current directory contents into the container at $HOME/app setting the owner to the userCOPY --chown=user . $HOME/app
# Start the FastAPI app on port 7860, the default port expected by SpacesCMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]12345678910111213141516171819202122232425262728from fastapi import FastAPIfrom transformers import pipeline
# Create a new FastAPI app instanceapp = FastAPI()
# Initialize the text generation pipeline# This function will be able to generate text# given an input.pipe = pipeline("text2text-generation", model="google/flan-t5-small")
# Define a function to handle the GET request at `/generate`# The generate() function is defined as a FastAPI route that takes a# string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response# containing the generated text under the key "output"@app.get("/generate")def generate(text: str): """ Using the text2text-generation pipeline from `transformers`, generate text from the given input text. The model used is `google/flan-t5-small`, which can be found [here](<https://huggingface.co/google/flan-t5-small>). """ # Use the pipeline to generate text from the given input text output = pipe(text)
# Return the generated text in a JSON response return {"output": output[0]["generated_text"]}123456fastapi==0.74.*requests==2.27.*uvicorn[standard]==0.17.*sentencepiece==0.1.*torch==1.11.*transformers==4.*DockerfileAdded+27
app.pyAdded+28
requirements.txtAdded+6