Spaces:
Sleeping
Sleeping
Commit
·
69aec40
1
Parent(s):
7e756d5
add error handling and input params
Browse files
app.py
CHANGED
|
@@ -1,30 +1,36 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
from transformers import pipeline
|
| 3 |
-
|
| 4 |
# Create a new FastAPI app instance
|
| 5 |
# NOTE - we configure docs_url to serve the interactive Docs at the root path
|
| 6 |
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
|
| 7 |
app = FastAPI(docs_url="/")
|
| 8 |
-
|
| 9 |
# Initialize the text generation pipeline
|
| 10 |
-
# This
|
| 11 |
-
#
|
| 12 |
-
pipe = pipeline("text2text-generation",
|
| 13 |
model="google/flan-t5-small")
|
| 14 |
-
|
| 15 |
# Define a function to handle the GET request at `/generate`
|
| 16 |
-
#
|
| 17 |
-
#
|
| 18 |
-
# containing the generated text under the key "output"
|
| 19 |
@app.get("/generate")
|
| 20 |
-
def generate(text: str):
|
| 21 |
"""
|
| 22 |
Using the text2text-generation pipeline from `transformers`, generate text
|
| 23 |
from the given input text. The model used is `google/flan-t5-small`, which
|
| 24 |
can be found [here](<https://huggingface.co/google/flan-t5-small>).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
"""
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
from transformers import pipeline
|
| 3 |
+
|
| 4 |
# Create a new FastAPI app instance
|
| 5 |
# NOTE - we configure docs_url to serve the interactive Docs at the root path
|
| 6 |
# of the app. This way, we can use the docs as a landing page for the app on Spaces.
|
| 7 |
app = FastAPI(docs_url="/")
|
| 8 |
+
|
| 9 |
# Initialize the text generation pipeline
|
| 10 |
+
# This pipeline is able to generate text using the
|
| 11 |
+
# google/flan-t5-small model.
|
| 12 |
+
pipe = pipeline("text2text-generation",
|
| 13 |
model="google/flan-t5-small")
|
| 14 |
+
|
| 15 |
# Define a function to handle the GET request at `/generate`
|
| 16 |
+
# This function will use the model to generate text based on the input text
|
| 17 |
+
# It also allows you to specify the maximum length of the generated text
|
|
|
|
| 18 |
@app.get("/generate")
|
| 19 |
+
def generate(text: str, max_length: int = 50):
|
| 20 |
"""
|
| 21 |
Using the text2text-generation pipeline from `transformers`, generate text
|
| 22 |
from the given input text. The model used is `google/flan-t5-small`, which
|
| 23 |
can be found [here](<https://huggingface.co/google/flan-t5-small>).
|
| 24 |
+
Args:
|
| 25 |
+
text: Input text to generate from
|
| 26 |
+
max_length: Maximum length of the generated output
|
| 27 |
+
Returns:
|
| 28 |
+
output: Json response containing the generated text
|
| 29 |
"""
|
| 30 |
+
try:
|
| 31 |
+
# Use the pipeline to generate text from the given input text
|
| 32 |
+
output = pipe(text, max_length=max_length)
|
| 33 |
+
# Return the generated text in a JSON response
|
| 34 |
+
return {"output": output[0]["generated_text"]}
|
| 35 |
+
except Exception as e:
|
| 36 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|