Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- Dockerfile +32 -0
- main.py +22 -0
- requirements.txt +6 -0
Dockerfile
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
FROM python:3.9-slim
|
3 |
+
|
4 |
+
# Define environment variables
|
5 |
+
ENV HOME=/home/user \
|
6 |
+
PATH=/home/user/.local/bin:$PATH \
|
7 |
+
HF_HOME=/home/user/hf_home
|
8 |
+
|
9 |
+
# Set the working directory in the container
|
10 |
+
WORKDIR $HOME/main
|
11 |
+
|
12 |
+
# Create the user, home directory, and HF_HOME directory
|
13 |
+
RUN useradd -m user && mkdir -p $HOME/main $HF_HOME && chown -R user:user $HOME
|
14 |
+
|
15 |
+
# Copy the current directory contents into the container
|
16 |
+
COPY --chown=user:user . $HOME/main
|
17 |
+
|
18 |
+
# Install FastAPI, Uvicorn, Transformers, and PyTorch
|
19 |
+
RUN pip install --no-cache-dir \
|
20 |
+
fastapi \
|
21 |
+
uvicorn \
|
22 |
+
transformers \
|
23 |
+
torch --extra-index-url https://download.pytorch.org/whl/cpu
|
24 |
+
|
25 |
+
# Switch to the non-root user
|
26 |
+
USER user
|
27 |
+
|
28 |
+
# Expose the port that FastAPI will run on
|
29 |
+
EXPOSE 8000
|
30 |
+
|
31 |
+
# Run the application
|
32 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
|
main.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from transformers import pipeline
|
3 |
+
app=FastAPI()
|
4 |
+
pipe = pipeline("text2text-generation", model="google/flan-t5-small")
|
5 |
+
@app.get("/")
|
6 |
+
def welcome():
|
7 |
+
"""
|
8 |
+
Welcoming page.
|
9 |
+
"""
|
10 |
+
return {"message": "Welcome to the Text-to-Text Generation API! Use the /generate endpoint to transform text."}
|
11 |
+
|
12 |
+
@app.post("/generate")
|
13 |
+
def generate_text(data: str):
|
14 |
+
"""
|
15 |
+
Generate text from the input text using the text-to-text generation model.
|
16 |
+
"""
|
17 |
+
try:
|
18 |
+
# Perform text-to-text generation
|
19 |
+
generated = generate_text(data.str, max_length=50, num_return_sequences=1)
|
20 |
+
return {"input": data.str, "generated_text": generated[0]["generated_text"]}
|
21 |
+
except Exception as e:
|
22 |
+
raise HTTPException(status_code=500, detail=str(e))
|
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==2.0.0
|
6 |
+
transformers==4.*
|