harry85 commited on
Commit
85f8119
·
verified ·
1 Parent(s): 9a78585

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +27 -0
  2. README.md +4 -4
  3. app.py +52 -0
  4. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Text Generation GPT2
3
- emoji: 💻
4
- colorFrom: indigo
5
- colorTo: gray
6
  sdk: docker
7
  pinned: false
8
  license: mit
 
1
  ---
2
+ title: Text Generation
3
+ emoji: 🌍
4
+ colorFrom: green
5
+ colorTo: yellow
6
  sdk: docker
7
  pinned: false
8
  license: mit
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Install the necessary packages
2
+ # pip install accelerate transformers fastapi pydantic torch
3
+
4
+ from transformers import AutoTokenizer, AutoModelForCausalLM
5
+ import torch
6
+ from pydantic import BaseModel
7
+ from fastapi import FastAPI
8
+ # Import the required library
9
+ from transformers import pipeline
10
+ # Load the tokenizer and model
11
+
12
+ # Initialize the FastAPI app
13
+ app = FastAPI(docs_url="/")
14
+
15
+ # Define the request model
16
+ class RequestModel(BaseModel):
17
+ input: str
18
+
19
+ # Define a greeting endpoint
20
+ @app.get("/")
21
+ def greet_json():
22
+ return {"message": "working..."}
23
+
24
+ # Define the text generation endpoint
25
+ @app.post("/generatetext")
26
+ def get_response(request: RequestModel):
27
+ # Define the task and model
28
+ task = "text-generation"
29
+ model_name = "gpt2"
30
+
31
+ # Define the input text, maximum output length, and the number of return sequences
32
+ input_text = "he draw to the town "
33
+ max_output_length = 50
34
+ num_of_return_sequences = 1
35
+
36
+ # Initialize the text generation pipeline
37
+ text_generator = pipeline(
38
+ task,
39
+ model=model_name
40
+ )
41
+
42
+ # Generate text sequences
43
+ generated_texts = text_generator(
44
+ input_text,
45
+ max_length=max_output_length,
46
+ num_return_sequences=num_of_return_sequences
47
+ )
48
+
49
+
50
+ return {"generated_text": generated_texts}
51
+
52
+ # To run the FastAPI app, use the command: uvicorn <filename>:app --reload
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi==0.74.*
2
+ requests==2.27.*
3
+ uvicorn[standard]==0.17.*
4
+ sentencepiece==0.1.*
5
+ torch==1.11.*
6
+ transformers==4.*