Spaces:
Sleeping
Sleeping
backend model server to serve predictions
Browse files
app.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Create a new FastAPI app instance
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Initialize text generation pipeline - to generate text given an input.
|
| 8 |
+
pipe = pipeline(
|
| 9 |
+
"text2text-generation",
|
| 10 |
+
model = "google/flan-t5-small"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Endpoint route: GET request at `/generate`
|
| 15 |
+
@app.get("/generate")
|
| 16 |
+
def generate(text: str) -> dict:
|
| 17 |
+
"""
|
| 18 |
+
Using the text2text-generation pipeline from `transformers`, generate text
|
| 19 |
+
from the given input text. The model used is `google/flan-t5-small`, which
|
| 20 |
+
can be found [here](<https://huggingface.co/google/flan-t5-small>).
|
| 21 |
+
|
| 22 |
+
generates text based on the # input using the pipeline() object
|
| 23 |
+
returns a JSON response containing the gneerated text under the key 'output'
|
| 24 |
+
"""
|
| 25 |
+
# Use the pipeline to generate text from the given input text
|
| 26 |
+
output = pipe(text)
|
| 27 |
+
|
| 28 |
+
# Return the generated text in a JSON response
|
| 29 |
+
return {"output": output[0]["generated_text"]}
|