slothdev commited on
Commit
7988f4b
·
1 Parent(s): b92f97c
Files changed (4) hide show
  1. Dockerfile +33 -0
  2. README copy.md +11 -0
  3. app.py +36 -0
  4. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
22
+
23
+ # Set the working directory to the user's home directory
24
+ WORKDIR $HOME/app
25
+
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"]
README copy.md ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Text Generation
3
+ emoji: 🏆
4
+ colorFrom: red
5
+ colorTo: purple
6
+ sdk: docker
7
+ pinned: false
8
+ license: mit
9
+ ---
10
+
11
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from fastapi.middleware.cors import CORSMiddleware
3
+ from transformers import pipeline
4
+
5
+ # Create a new FastAPI app instance
6
+ app = FastAPI()
7
+
8
+ # Add CORS middleware
9
+ app.add_middleware(
10
+ CORSMiddleware,
11
+ allow_origins=["*"], # Allow all origins
12
+ allow_credentials=True,
13
+ allow_methods=["*"], # Allow all methods
14
+ allow_headers=["*"], # Allow all headers
15
+ )
16
+
17
+ # Initialize the text generation pipeline
18
+ pipe = pipeline("text2text-generation", model="google/flan-t5-small")
19
+
20
+ # Define a root path to verify the server is running
21
+ @app.get("/")
22
+ def read_root():
23
+ return {"message": "Welcome to the FastAPI application. Use /generate to generate text."}
24
+
25
+ # Define a function to handle the GET request at `/generate`
26
+ @app.get("/generate")
27
+ def generate(text: str):
28
+ """
29
+ Using the text2text-generation pipeline from `transformers`, generate text
30
+ from the given input text. The model used is `google/flan-t5-small`.
31
+ """
32
+ # Use the pipeline to generate text from the given input text
33
+ output = pipe(text)
34
+
35
+ # Return the generated text in a JSON response
36
+ return {"output": output[0]["generated_text"]}
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ transformers
4
+ torch