Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +27 -0
- README (5).md +11 -0
- app.py +52 -0
- 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 (5).md
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: text-generation-GPT2
|
3 |
+
emoji: π
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: red
|
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,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 |
+
|
11 |
+
# Initialize the FastAPI app
|
12 |
+
app = FastAPI(docs_url="/")
|
13 |
+
|
14 |
+
# Define the request model
|
15 |
+
class RequestModel(BaseModel):
|
16 |
+
input: str
|
17 |
+
|
18 |
+
# Define a greeting endpoint
|
19 |
+
@app.get("/")
|
20 |
+
def greet_json():
|
21 |
+
return {"message": "working..."}
|
22 |
+
|
23 |
+
# Define the text generation endpoint
|
24 |
+
@app.post("/generatetext")
|
25 |
+
def get_response(request: RequestModel):
|
26 |
+
# Define the task and model
|
27 |
+
task = "text-generation"
|
28 |
+
model_name = "gpt2"
|
29 |
+
|
30 |
+
# Define the input text, maximum output length, and the number of return sequences
|
31 |
+
input_text = request.input
|
32 |
+
max_output_length = 50
|
33 |
+
num_of_return_sequences = 1
|
34 |
+
|
35 |
+
# Initialize the text generation pipeline
|
36 |
+
text_generator = pipeline(
|
37 |
+
task,
|
38 |
+
model=model_name
|
39 |
+
)
|
40 |
+
|
41 |
+
# Generate text sequences
|
42 |
+
generated_texts = text_generator(
|
43 |
+
input_text,
|
44 |
+
max_length=max_output_length,
|
45 |
+
num_return_sequences=num_of_return_sequences
|
46 |
+
)
|
47 |
+
|
48 |
+
# Extract and return the generated text
|
49 |
+
generated_text = generated_texts[0]['generated_text']
|
50 |
+
return {"generated_text": generated_text}
|
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.*
|