Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +12 -8
- requirements.txt +5 -5
app.py
CHANGED
|
@@ -1,27 +1,31 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
|
| 4 |
## Create FastAPI app instance
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
# Initialize a text generation pipeline
|
| 8 |
-
# Load model
|
| 9 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 10 |
-
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 12 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.get("/")
|
| 15 |
def home():
|
| 16 |
return {"message": "Welcome to the Text Generation API!"}
|
| 17 |
|
| 18 |
#Define a function to handle the GET request at /generate
|
| 19 |
@app.get("/generate")
|
| 20 |
-
def generate(text:str):
|
| 21 |
## Generate text using the model
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
##return the text
|
| 25 |
-
return {"output":
|
| 26 |
|
| 27 |
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
## Create FastAPI app instance
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
# Initialize a text generation pipeline
|
| 9 |
+
# Load model and tokenizer
|
|
|
|
|
|
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-small")
|
| 11 |
model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-small")
|
| 12 |
|
| 13 |
+
# Check for GPU availability
|
| 14 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 15 |
+
|
| 16 |
@app.get("/")
|
| 17 |
def home():
|
| 18 |
return {"message": "Welcome to the Text Generation API!"}
|
| 19 |
|
| 20 |
#Define a function to handle the GET request at /generate
|
| 21 |
@app.get("/generate")
|
| 22 |
+
def generate(text: str):
|
| 23 |
## Generate text using the model
|
| 24 |
+
input_ids = tokenizer(text, return_tensors="pt").input_ids
|
| 25 |
+
outputs = model.generate(input_ids, max_length=100)
|
| 26 |
+
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 27 |
|
| 28 |
+
##return the text generated in Json response
|
| 29 |
+
return {"output": output_text}
|
| 30 |
|
| 31 |
|
requirements.txt
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
-
fastapi>=0.
|
| 2 |
-
requests>=2.
|
| 3 |
-
uvicorn[standard]>=0.
|
| 4 |
torch>=2.0
|
| 5 |
-
transformers>=4.
|
| 6 |
-
sentencepiece
|
|
|
|
| 1 |
+
fastapi>=0.100.0
|
| 2 |
+
requests>=2.31.0
|
| 3 |
+
uvicorn[standard]>=0.24.0
|
| 4 |
torch>=2.0
|
| 5 |
+
transformers>=4.30.0
|
| 6 |
+
sentencepiece>=0.2.0
|