slothdev commited on
Commit
94e8197
·
1 Parent(s): 4a587d2

fixed generate

Browse files
Files changed (2) hide show
  1. Dockerfile +9 -4
  2. app.py +9 -11
Dockerfile CHANGED
@@ -4,16 +4,18 @@ FROM python:3.9
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
@@ -24,5 +26,8 @@ WORKDIR $HOME/app
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"]
 
4
  # Set the working directory to /code
5
  WORKDIR /code
6
 
7
+ # Copy the requirements.txt file into the container
8
+ COPY requirements.txt .
9
 
10
+ # Install the dependencies
11
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
12
 
13
  # Set up a new user named "user" with user ID 1000
14
  RUN useradd -m -u 1000 user
15
+
16
  # Switch to the "user" user
17
  USER user
18
+
19
  # Set home to the user's home directory
20
  ENV HOME=/home/user \
21
  PATH=/home/user/.local/bin:$PATH
 
26
  # Copy the current directory contents into the container at $HOME/app setting the owner to the user
27
  COPY --chown=user . $HOME/app
28
 
29
+ # Expose the port FastAPI will run on
30
+ EXPOSE 7860
31
+
32
  # Start the FastAPI app on port 7860, the default port expected by Spaces
33
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py CHANGED
@@ -5,24 +5,22 @@ from transformers import pipeline
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",
11
- model="google/flan-t5-small")
 
 
12
 
13
  # Define a function to handle the GET request at `/generate`
14
- # The generate() function is defined as a FastAPI route that takes a
15
- # string parameter called text. The function generates text based on the # input using the pipeline() object, and returns a JSON response
16
- # containing the generated text under the key "output"
17
  @app.get("/generate")
18
  def generate(text: str):
19
  """
20
  Using the text2text-generation pipeline from `transformers`, generate text
21
- from the given input text. The model used is `google/flan-t5-small`, which
22
- can be found [here](<https://huggingface.co/google/flan-t5-small>).
23
  """
24
  # Use the pipeline to generate text from the given input text
25
  output = pipe(text)
26
-
27
  # Return the generated text in a JSON response
28
- return {"output": output[0]["generated_text"]}
 
5
  app = FastAPI()
6
 
7
  # Initialize the text generation pipeline
8
+ pipe = pipeline("text2text-generation", model="google/flan-t5-small")
9
+
10
+ # Define a root path to verify the server is running
11
+ @app.get("/")
12
+ def read_root():
13
+ return {"message": "Welcome to the FastAPI application. Use /generate to generate text."}
14
 
15
  # Define a function to handle the GET request at `/generate`
 
 
 
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`.
 
21
  """
22
  # Use the pipeline to generate text from the given input text
23
  output = pipe(text)
24
+
25
  # Return the generated text in a JSON response
26
+ return {"output": output[0]["generated_text"]}