Upload 3 files
Browse files- Dockerfile +16 -0
- app.py +42 -0
- requirements.txt +6 -0
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /app
|
4 |
+
|
5 |
+
COPY requirements.txt .
|
6 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
7 |
+
|
8 |
+
RUN useradd -m user
|
9 |
+
USER user
|
10 |
+
|
11 |
+
ENV HOME=/home/user \
|
12 |
+
PATH=/home/user/.local/bin:$PATH
|
13 |
+
|
14 |
+
COPY --chown=user:user . .
|
15 |
+
|
16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
+
import uvicorn
|
5 |
+
from fastapi.middleware.cors import CORSMiddleware
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Add CORS middleware
|
10 |
+
app.add_middleware(
|
11 |
+
CORSMiddleware,
|
12 |
+
allow_origins=["*"], # Allows all origins
|
13 |
+
allow_credentials=True,
|
14 |
+
allow_methods=["*"], # Allows all methods
|
15 |
+
allow_headers=["*"], # Allows all headers
|
16 |
+
)
|
17 |
+
|
18 |
+
# Initialize the model and tokenizer
|
19 |
+
model_name = "bigscience/mt0-base"
|
20 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
21 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
22 |
+
|
23 |
+
class GenerationRequest(BaseModel):
|
24 |
+
prompt: str
|
25 |
+
max_tokens: int = 100
|
26 |
+
|
27 |
+
@app.post("/generate")
|
28 |
+
async def generate(request: GenerationRequest):
|
29 |
+
inputs = tokenizer(request.prompt, return_tensors="pt", padding=True, truncation=True)
|
30 |
+
|
31 |
+
# Move inputs to the same device as the model
|
32 |
+
device = model.device
|
33 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
34 |
+
|
35 |
+
outputs = model.generate(**inputs, max_new_tokens=request.max_tokens)
|
36 |
+
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
37 |
+
return {"generated_text": generated_text}
|
38 |
+
|
39 |
+
@app.get("/")
|
40 |
+
def home():
|
41 |
+
return {"message": "Welcome to the Text Generation API"}
|
42 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
nest_asyncio
|
3 |
+
uvicorn
|
4 |
+
pydantic
|
5 |
+
transformers
|
6 |
+
torch
|